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