Diligent Engine API Reference
StringTools.h
1 /* Copyright 2015-2018 Egor Yusov
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
10  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS.
12  *
13  * In no event and under no legal theory, whether in tort (including negligence),
14  * contract, or otherwise, unless required by applicable law (such as deliberate
15  * and grossly negligent acts) or agreed to in writing, shall any Contributor be
16  * liable for any damages, including any direct, indirect, special, incidental,
17  * or consequential damages of any character arising as a result of this License or
18  * out of the use or inability to use the software (including but not limited to damages
19  * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
20  * all other commercial damages or losses), even if such Contributor has been advised
21  * of the possibility of such damages.
22  */
23 
24 #pragma once
25 
26 #include <string>
27 #include <sstream>
28 #include <locale>
29 #include <algorithm>
30 #include <cctype>
31 #include "DebugUtilities.h"
32 
33 namespace Diligent
34 {
35 
36 inline std::string NarrowString(const std::wstring &WideStr)
37 {
38  std::string NarrowStr;
39  const std::ctype<wchar_t>& ctfacet = std::use_facet< std::ctype<wchar_t> >( std::wstringstream().getloc() ) ;
40  for( std::wstring::const_iterator CurrWChar = WideStr.begin();
41  CurrWChar != WideStr.end();
42  CurrWChar++ )
43  NarrowStr.push_back( ctfacet.narrow( *CurrWChar, 0 ) );
44 
45  return NarrowStr;
46 }
47 
48 inline std::wstring WidenString(const char *Str)
49 {
50  std::wstring WideStr;
51  const std::ctype<wchar_t>& ctfacet = std::use_facet< std::ctype<wchar_t> >( std::wstringstream().getloc() ) ;
52  for( auto CurrChar = Str; *CurrChar != 0; ++CurrChar )
53  WideStr.push_back( ctfacet.widen( *CurrChar ) );
54 
55  return WideStr;
56 }
57 
58 inline std::wstring WidenString(const std::string &Str)
59 {
60  std::wstring WideStr;
61  const std::ctype<wchar_t>& ctfacet = std::use_facet< std::ctype<wchar_t> >( std::wstringstream().getloc() ) ;
62  for( std::string::const_iterator CurrChar = Str.begin();
63  CurrChar != Str.end();
64  CurrChar++ )
65  WideStr.push_back( ctfacet.widen( *CurrChar ) );
66 
67  return WideStr;
68 }
69 
70 inline int StrCmpNoCase(const char* Str1, const char* Str2, size_t NumChars)
71 {
72 #if defined(PLATFORM_ANDROID) || defined(PLATFORM_LINUX)
73 # define _strnicmp strncasecmp
74 #endif
75 
76  return _strnicmp( Str1, Str2, NumChars );
77 }
78 
79 inline int StrCmpNoCase(const char* Str1, const char* Str2)
80 {
81 #if defined(PLATFORM_ANDROID) || defined(PLATFORM_LINUX)
82 # define _stricmp strcasecmp
83 #endif
84 
85  return _stricmp( Str1, Str2 );
86 }
87 
88 // Returns true if RefStr == Str + Suff
89 inline bool StrCmpSuff(const char *RefStr, const char *Str, const char *Suff)
90 {
91  VERIFY_EXPR(RefStr != nullptr && Str!= nullptr && Suff != nullptr);
92  if(RefStr==nullptr)
93  return false;
94 
95  const auto *r = RefStr;
96  const auto *s = Str;
97  for(; *r!=0 && *s!=0; ++r, ++s)
98  {
99  if (*r != *s)
100  return false;
101  }
102 
103  if( *s != 0 )
104  {
105  VERIFY_EXPR(*r == 0);
106  return false;
107  }
108 
109  return strcmp(r, Suff) == 0;
110 }
111 
112 inline void StrToLowerInPlace(std::string &str)
113 {
114  std::transform(str.begin(), str.end(), str.begin(),
115  // http://en.cppreference.com/w/cpp/string/byte/tolower
116  [](unsigned char c) { return static_cast<char>(std::tolower(c)); }
117  );
118 }
119 
120 inline std::string StrToLower(std::string str)
121 {
122  StrToLowerInPlace(str);
123  return str;
124 }
125 
126 }
Namespace for the OpenGL implementation of the graphics engine.
Definition: BufferD3D11Impl.h:34