StringUtils.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // The MIT License (MIT)
  2. //
  3. // Copyright(c) Unity Technologies, Microsoft Corporation
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files(the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions :
  11. //
  12. // The above copyright notice and this permission notice shall be included in all
  13. // copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. // SOFTWARE.
  22. #pragma once
  23. #include "il2cpp-string-types.h"
  24. #include <string>
  25. #include <vector>
  26. #include <limits>
  27. #include <stdint.h>
  28. #include "il2cpp-config.h"
  29. #include "StringView.h"
  30. #include "StringViewUtils.h"
  31. #include "Baselib.h"
  32. namespace il2cpp
  33. {
  34. namespace utils
  35. {
  36. class LIBIL2CPP_CODEGEN_API StringUtils
  37. {
  38. public:
  39. static std::string Printf(const char* format, ...);
  40. static std::string NPrintf(const char* format, size_t max_n, ...);
  41. static std::string Utf16ToUtf8(const Il2CppChar* utf16String);
  42. static std::string Utf16ToUtf8(const Il2CppChar* utf16String, int maximumSize);
  43. static std::string Utf16ToUtf8(const UTF16String& utf16String);
  44. static UTF16String Utf8ToUtf16(const char* utf8String);
  45. static UTF16String Utf8ToUtf16(const char* utf8String, size_t length);
  46. static UTF16String Utf8ToUtf16(const std::string& utf8String);
  47. static char* StringDuplicate(const char *strSource);
  48. static Il2CppChar* StringDuplicate(const Il2CppChar* strSource, size_t length);
  49. static bool EndsWith(const std::string& string, const std::string& suffix);
  50. static Il2CppChar* GetChars(Il2CppString* str);
  51. static int32_t GetLength(Il2CppString* str);
  52. #if IL2CPP_TARGET_WINDOWS
  53. static inline std::string NativeStringToUtf8(const Il2CppNativeString& nativeStr)
  54. {
  55. IL2CPP_ASSERT(nativeStr.length() < static_cast<size_t>(std::numeric_limits<int>::max()));
  56. return Utf16ToUtf8(nativeStr.c_str(), static_cast<int>(nativeStr.length()));
  57. }
  58. static inline std::string NativeStringToUtf8(const Il2CppNativeChar* nativeStr)
  59. {
  60. return Utf16ToUtf8(nativeStr);
  61. }
  62. static inline std::string NativeStringToUtf8(const Il2CppNativeChar* nativeStr, uint32_t length)
  63. {
  64. IL2CPP_ASSERT(length < static_cast<uint32_t>(std::numeric_limits<int>::max()));
  65. return Utf16ToUtf8(nativeStr, static_cast<int>(length));
  66. }
  67. static inline Il2CppNativeString Utf8ToNativeString(const std::string& str)
  68. {
  69. IL2CPP_ASSERT(str.length() < static_cast<size_t>(std::numeric_limits<int>::max()));
  70. return Utf8ToUtf16(str.c_str(), str.length());
  71. }
  72. static inline Il2CppNativeString Utf8ToNativeString(const char* str)
  73. {
  74. return Utf8ToUtf16(str);
  75. }
  76. #else
  77. static inline std::string NativeStringToUtf8(Il2CppNativeString& nativeStr)
  78. {
  79. return nativeStr;
  80. }
  81. static inline std::string NativeStringToUtf8(const Il2CppNativeChar* nativeStr)
  82. {
  83. return nativeStr;
  84. }
  85. static inline std::string NativeStringToUtf8(const Il2CppNativeChar* nativeStr, uint32_t length)
  86. {
  87. return std::string(nativeStr, length);
  88. }
  89. static inline Il2CppNativeString Utf8ToNativeString(const std::string& str)
  90. {
  91. return str;
  92. }
  93. static inline Il2CppNativeString Utf8ToNativeString(const char* str)
  94. {
  95. return str;
  96. }
  97. #endif
  98. template<typename CharType, size_t N>
  99. static inline size_t LiteralLength(const CharType(&str)[N])
  100. {
  101. return N - 1;
  102. }
  103. template<typename CharType>
  104. static size_t StrLen(const CharType* str)
  105. {
  106. size_t length = 0;
  107. while (*str)
  108. {
  109. str++;
  110. length++;
  111. }
  112. return length;
  113. }
  114. template <typename CharType>
  115. static inline bool Equals(const StringView<CharType>& left, const StringView<CharType>& right)
  116. {
  117. if (left.Length() != right.Length())
  118. return false;
  119. return memcmp(left.Str(), right.Str(), left.Length() * sizeof(CharType)) == 0;
  120. }
  121. template <typename CharType, size_t rightLength>
  122. static inline bool Equals(const StringView<CharType>& left, const CharType (&right)[rightLength])
  123. {
  124. if (left.Length() != rightLength - 1)
  125. return false;
  126. return memcmp(left.Str(), right, (rightLength - 1) * sizeof(CharType)) == 0;
  127. }
  128. template <typename CharType>
  129. static inline bool StartsWith(const StringView<CharType>& left, const StringView<CharType>& right)
  130. {
  131. if (left.Length() < right.Length())
  132. return false;
  133. return memcmp(left.Str(), right.Str(), right.Length() * sizeof(CharType)) == 0;
  134. }
  135. template <typename CharType, size_t rightLength>
  136. static inline bool StartsWith(const StringView<CharType>& left, const CharType(&right)[rightLength])
  137. {
  138. if (left.Length() < rightLength - 1)
  139. return false;
  140. return memcmp(left.Str(), right, (rightLength - 1) * sizeof(CharType)) == 0;
  141. }
  142. // Taken from github.com/Microsoft/referencesource/blob/master/mscorlib/system/string.cs
  143. template<typename CharType>
  144. static inline size_t Hash(const CharType *str, size_t length)
  145. {
  146. IL2CPP_ASSERT(length <= static_cast<size_t>(std::numeric_limits<int>::max()));
  147. size_t hash1 = 5381;
  148. size_t hash2 = hash1;
  149. size_t i = 0;
  150. CharType c;
  151. const CharType* s = str;
  152. while (true)
  153. {
  154. if (i++ >= length)
  155. break;
  156. c = s[0];
  157. hash1 = ((hash1 << 5) + hash1) ^ c;
  158. if (i++ >= length)
  159. break;
  160. c = s[1];
  161. hash2 = ((hash2 << 5) + hash2) ^ c;
  162. s += 2;
  163. }
  164. return hash1 + (hash2 * 1566083941);
  165. }
  166. template<typename CharType>
  167. static inline size_t Hash(const CharType *str)
  168. {
  169. size_t hash1 = 5381;
  170. size_t hash2 = hash1;
  171. CharType c;
  172. const CharType* s = str;
  173. while ((c = s[0]) != 0)
  174. {
  175. hash1 = ((hash1 << 5) + hash1) ^ c;
  176. c = s[1];
  177. if (c == 0)
  178. break;
  179. hash2 = ((hash2 << 5) + hash2) ^ c;
  180. s += 2;
  181. }
  182. return hash1 + (hash2 * 1566083941);
  183. }
  184. template<typename StringType>
  185. struct StringHasher
  186. {
  187. typedef typename StringType::value_type CharType;
  188. size_t operator()(const StringType& value) const
  189. {
  190. return Hash(value.c_str(), value.length());
  191. }
  192. };
  193. template<typename CharType>
  194. struct StringHasher<const CharType*>
  195. {
  196. size_t operator()(const CharType* value) const
  197. {
  198. return Hash(value);
  199. }
  200. };
  201. #if defined(_MSC_VER)
  202. static inline const baselib_char16_t* NativeStringToBaselib(const Il2CppNativeChar* str)
  203. {
  204. static_assert(sizeof(Il2CppNativeChar) == sizeof(baselib_char16_t), "type sizes should match");
  205. return reinterpret_cast<const baselib_char16_t*>(str);
  206. }
  207. #else
  208. static inline const char* NativeStringToBaselib(const Il2CppNativeChar* str)
  209. {
  210. static_assert(sizeof(Il2CppNativeChar) == sizeof(char), "type sizes should match");
  211. return str;
  212. }
  213. #endif
  214. };
  215. } /* utils */
  216. } /* il2cpp */
  217. // Assumes str is not NULL
  218. #if defined(_MSC_VER)
  219. #define DECLARE_IL2CPP_STRING_AS_STRING_VIEW_OF_NATIVE_CHARS(variableName, str) \
  220. il2cpp::utils::StringView<Il2CppNativeChar> variableName((str)->chars, (str)->length);
  221. #define DECLARE_IL2CPP_CHAR_PTR_AS_STRING_VIEW_OF_NATIVE_CHARS(variableName, str) \
  222. il2cpp::utils::StringView<Il2CppNativeChar> variableName(str, il2cpp::utils::StringUtils::StrLen (str));
  223. #define DECLARE_NATIVE_C_STRING_AS_STRING_VIEW_OF_IL2CPP_CHARS(variableName, str) \
  224. il2cpp::utils::StringView<Il2CppChar> variableName(str, wcslen(str));
  225. #define DECLARE_NATIVE_STRING_AS_STRING_VIEW_OF_IL2CPP_CHARS(variableName, str) \
  226. il2cpp::utils::StringView<Il2CppChar> variableName = STRING_TO_STRINGVIEW(str);
  227. #else
  228. #define DECLARE_IL2CPP_STRING_AS_STRING_VIEW_OF_NATIVE_CHARS(variableName, str) \
  229. Il2CppNativeString variableName##_native_string_storage = il2cpp::utils::StringUtils::Utf16ToUtf8((str)->chars, (str)->length); \
  230. il2cpp::utils::StringView<Il2CppNativeChar> variableName(variableName##_native_string_storage.c_str(), variableName##_native_string_storage.length());
  231. #define DECLARE_IL2CPP_CHAR_PTR_AS_STRING_VIEW_OF_NATIVE_CHARS(variableName, str) \
  232. Il2CppNativeString variableName##_native_string_storage = il2cpp::utils::StringUtils::Utf16ToUtf8(str, il2cpp::utils::StringUtils::StrLen (str)); \
  233. il2cpp::utils::StringView<Il2CppNativeChar> variableName(variableName##_native_string_storage.c_str(), variableName##_native_string_storage.length());
  234. #define DECLARE_NATIVE_C_STRING_AS_STRING_VIEW_OF_IL2CPP_CHARS(variableName, str) \
  235. UTF16String variableName##_utf16String = il2cpp::utils::StringUtils::Utf8ToUtf16(str); \
  236. il2cpp::utils::StringView<Il2CppChar> variableName = STRING_TO_STRINGVIEW(variableName##_utf16String);
  237. #define DECLARE_NATIVE_STRING_AS_STRING_VIEW_OF_IL2CPP_CHARS DECLARE_NATIVE_C_STRING_AS_STRING_VIEW_OF_IL2CPP_CHARS
  238. #endif