StringUtils.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. namespace il2cpp
  30. {
  31. namespace utils
  32. {
  33. class LIBIL2CPP_CODEGEN_API StringUtils
  34. {
  35. public:
  36. static std::string Printf(const char* format, ...);
  37. static std::string NPrintf(const char* format, size_t max_n, ...);
  38. static std::string Utf16ToUtf8(const Il2CppChar* utf16String);
  39. static std::string Utf16ToUtf8(const Il2CppChar* utf16String, int maximumSize);
  40. static std::string Utf16ToUtf8(const UTF16String& utf16String);
  41. static UTF16String Utf8ToUtf16(const char* utf8String);
  42. static UTF16String Utf8ToUtf16(const char* utf8String, size_t length);
  43. static UTF16String Utf8ToUtf16(const std::string& utf8String);
  44. static char* StringDuplicate(const char *strSource);
  45. static Il2CppChar* StringDuplicate(const Il2CppChar* strSource, size_t length);
  46. static bool EndsWith(const std::string& string, const std::string& suffix);
  47. static Il2CppChar* GetChars(Il2CppString* str);
  48. static int32_t GetLength(Il2CppString* str);
  49. #if IL2CPP_TARGET_WINDOWS
  50. static inline std::string NativeStringToUtf8(const Il2CppNativeString& nativeStr)
  51. {
  52. IL2CPP_ASSERT(nativeStr.length() < static_cast<size_t>(std::numeric_limits<int>::max()));
  53. return Utf16ToUtf8(nativeStr.c_str(), static_cast<int>(nativeStr.length()));
  54. }
  55. static inline Il2CppNativeString Utf8ToNativeString(const std::string str)
  56. {
  57. IL2CPP_ASSERT(str.length() < static_cast<size_t>(std::numeric_limits<int>::max()));
  58. return Utf8ToUtf16(str.c_str(), str.length());
  59. }
  60. #else
  61. static inline std::string NativeStringToUtf8(Il2CppNativeString nativeStr)
  62. {
  63. return nativeStr;
  64. }
  65. static inline Il2CppNativeString Utf8ToNativeString(const std::string str)
  66. {
  67. return str;
  68. }
  69. #endif
  70. template<typename CharType, size_t N>
  71. static inline size_t LiteralLength(const CharType(&str)[N])
  72. {
  73. return N - 1;
  74. }
  75. template<typename CharType>
  76. static size_t StrLen(const CharType* str)
  77. {
  78. size_t length = 0;
  79. while (*str)
  80. {
  81. str++;
  82. length++;
  83. }
  84. return length;
  85. }
  86. // Taken from github.com/Microsoft/referencesource/blob/master/mscorlib/system/string.cs
  87. template<typename CharType>
  88. static inline size_t Hash(const CharType *str, size_t length)
  89. {
  90. IL2CPP_ASSERT(length <= static_cast<size_t>(std::numeric_limits<int>::max()));
  91. size_t hash1 = 5381;
  92. size_t hash2 = hash1;
  93. size_t i = 0;
  94. CharType c;
  95. const CharType* s = str;
  96. while (true)
  97. {
  98. if (i++ >= length)
  99. break;
  100. c = s[0];
  101. hash1 = ((hash1 << 5) + hash1) ^ c;
  102. if (i++ >= length)
  103. break;
  104. c = s[1];
  105. hash2 = ((hash2 << 5) + hash2) ^ c;
  106. s += 2;
  107. }
  108. return hash1 + (hash2 * 1566083941);
  109. }
  110. template<typename CharType>
  111. static inline size_t Hash(const CharType *str)
  112. {
  113. size_t hash1 = 5381;
  114. size_t hash2 = hash1;
  115. CharType c;
  116. const CharType* s = str;
  117. while ((c = s[0]) != 0)
  118. {
  119. hash1 = ((hash1 << 5) + hash1) ^ c;
  120. c = s[1];
  121. if (c == 0)
  122. break;
  123. hash2 = ((hash2 << 5) + hash2) ^ c;
  124. s += 2;
  125. }
  126. return hash1 + (hash2 * 1566083941);
  127. }
  128. template<typename StringType>
  129. struct StringHasher
  130. {
  131. typedef typename StringType::value_type CharType;
  132. size_t operator()(const StringType& value) const
  133. {
  134. return Hash(value.c_str(), value.length());
  135. }
  136. };
  137. template<typename CharType>
  138. struct StringHasher<const CharType*>
  139. {
  140. size_t operator()(const CharType* value) const
  141. {
  142. return Hash(value);
  143. }
  144. };
  145. };
  146. } /* utils */
  147. } /* il2cpp */
  148. // Assumes str is not NULL
  149. #if defined(_MSC_VER)
  150. #define DECLARE_IL2CPP_STRING_AS_STRING_VIEW_OF_NATIVE_CHARS(variableName, str) \
  151. il2cpp::utils::StringView<Il2CppNativeChar> variableName(reinterpret_cast<Il2CppString*>(str)->chars, reinterpret_cast<Il2CppString*>(str)->length);
  152. #define DECLARE_NATIVE_C_STRING_AS_STRING_VIEW_OF_IL2CPP_CHARS(variableName, str) \
  153. il2cpp::utils::StringView<Il2CppChar> variableName(str, wcslen(str));
  154. #define DECLARE_NATIVE_STRING_AS_STRING_VIEW_OF_IL2CPP_CHARS(variableName, str) \
  155. il2cpp::utils::StringView<Il2CppChar> variableName(str);
  156. #else
  157. #define DECLARE_IL2CPP_STRING_AS_STRING_VIEW_OF_NATIVE_CHARS(variableName, str) \
  158. Il2CppNativeString variableName##_native_string_storage = il2cpp::utils::StringUtils::Utf16ToUtf8(reinterpret_cast<Il2CppString*>(str)->chars, reinterpret_cast<Il2CppString*>(str)->length); \
  159. il2cpp::utils::StringView<Il2CppNativeChar> variableName(variableName##_native_string_storage.c_str(), variableName##_native_string_storage.length());
  160. #define DECLARE_NATIVE_C_STRING_AS_STRING_VIEW_OF_IL2CPP_CHARS(variableName, str) \
  161. UTF16String variableName##_utf16String = il2cpp::utils::StringUtils::Utf8ToUtf16(str); \
  162. il2cpp::utils::StringView<Il2CppChar> variableName(variableName##_utf16String);
  163. #define DECLARE_NATIVE_STRING_AS_STRING_VIEW_OF_IL2CPP_CHARS DECLARE_NATIVE_C_STRING_AS_STRING_VIEW_OF_IL2CPP_CHARS
  164. #endif