forked from focus-creative-games/il2cpp_plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString.cpp
More file actions
191 lines (154 loc) · 5.36 KB
/
String.cpp
File metadata and controls
191 lines (154 loc) · 5.36 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
#include "il2cpp-config.h"
#include "gc/Allocator.h"
#include "gc/GarbageCollector.h"
#include "gc/GCHandle.h"
#include "os/Atomic.h"
#include "os/Mutex.h"
#include "vm/Exception.h"
#include "vm/String.h"
#include "vm/Object.h"
#include "vm/Profiler.h"
#include "gc/AppendOnlyGCHashMap.h"
#include "utils/StringUtils.h"
#include <string>
#include <memory.h>
#include "il2cpp-class-internals.h"
#include "il2cpp-object-internals.h"
#include "Baselib.h"
#include "Cpp/ReentrantLock.h"
namespace il2cpp
{
namespace vm
{
static Il2CppString* s_EmptyString;
void String::InitializeEmptyString(Il2CppClass* stringClass)
{
IL2CPP_ASSERT(s_EmptyString == NULL && "Empty string was already initialized");
// size for string and null terminator
s_EmptyString = static_cast<Il2CppString*>(gc::GarbageCollector::AllocateFixed(sizeof(Il2CppString) + 2, 0));
s_EmptyString->object.klass = stringClass;
s_EmptyString->length = 0;
s_EmptyString->chars[0] = 0;
}
void String::CleanupEmptyString()
{
IL2CPP_ASSERT(s_EmptyString && "Empty string was not yet initialized");
gc::GarbageCollector::FreeFixed(s_EmptyString);
s_EmptyString = NULL;
}
Il2CppString* String::Empty()
{
IL2CPP_ASSERT(s_EmptyString && "Empty string was not yet initialized");
return s_EmptyString;
}
int32_t String::GetHash(Il2CppString* str)
{
const Il2CppChar* p = utils::StringUtils::GetChars(str);
int i, len = utils::StringUtils::GetLength(str);
uint32_t h = 0;
for (i = 0; i < len; i++)
{
h = (h << 5) - h + *p;
p++;
}
return h;
}
Il2CppString* String::New(const char* str)
{
return NewLen(str, (uint32_t)strlen(str));
}
Il2CppString* String::NewWrapper(const char* str)
{
return New(str);
}
Il2CppString* String::NewLen(const char* str, uint32_t length)
{
UTF16String utf16Chars = il2cpp::utils::StringUtils::Utf8ToUtf16(str, length);
return NewUtf16(utf16Chars.c_str(), (uint32_t)utf16Chars.length());
}
Il2CppString* String::NewUtf16(const Il2CppChar* text, int32_t len)
{
Il2CppString *s;
s = NewSize(len);
IL2CPP_ASSERT(s != NULL);
memcpy(utils::StringUtils::GetChars(s), text, (size_t)len * 2);
return s;
}
Il2CppString* String::NewUtf16(const utils::StringView<Il2CppChar>& text)
{
IL2CPP_ASSERT(text.Length() < static_cast<uint32_t>(std::numeric_limits<int32_t>::max()));
return NewUtf16(text.Str(), static_cast<int32_t>(text.Length()));
}
Il2CppString* String::NewSize(int32_t len)
{
if (len == 0)
return Empty();
Il2CppString *s;
IL2CPP_ASSERT(len >= 0);
size_t size = (sizeof(Il2CppString) + (((size_t)len + 1) * 2));
/* overflow ? can't fit it, can't allocate it! */
if (static_cast<uint32_t>(len) > size)
Exception::RaiseOutOfMemoryException();
s = reinterpret_cast<Il2CppString*>(Object::AllocatePtrFree(size, il2cpp_defaults.string_class));
s->length = len;
#if NEED_TO_ZERO_PTRFREE
s->chars[len] = 0;
#endif
#if IL2CPP_ENABLE_PROFILER
if (Profiler::ProfileAllocations())
Profiler::Allocation((Il2CppObject*)s, il2cpp_defaults.string_class);
#endif
return s;
}
struct InternedString
{
int32_t length;
const Il2CppChar* chars;
};
class InternedStringHash
{
public:
size_t operator()(const InternedString& ea) const
{
return utils::StringUtils::Hash(ea.chars, ea.length);
}
};
class InternedStringCompare
{
public:
bool operator()(const InternedString& ea, const InternedString& eb) const
{
return (ea.length == eb.length) && (0 == memcmp(ea.chars, eb.chars, sizeof(Il2CppChar) * ea.length));
}
};
typedef il2cpp::gc::AppendOnlyGCHashMap<InternedString, Il2CppString*, InternedStringHash, InternedStringCompare> InternedStringMap;
static InternedStringMap* s_InternedStringMap;
Il2CppString* String::Intern(Il2CppString* str)
{
// allocate this at runtime since it uses GC allocator to keep managed strings alive and needs GC initialized
if (s_InternedStringMap == NULL)
{
InternedStringMap* newMap = new InternedStringMap();
if (os::Atomic::CompareExchangePointer<InternedStringMap>(&s_InternedStringMap, newMap, NULL) != NULL)
delete newMap;
}
InternedString internedString = { str->length, str->chars };
Il2CppString* value = NULL;
if (s_InternedStringMap->TryGetValue(internedString, &value))
return value;
internedString.chars = utils::StringUtils::GetChars(str);
return s_InternedStringMap->GetOrAdd(internedString, str);
}
Il2CppString* String::IsInterned(Il2CppString* str)
{
// if this is NULL, it means we have no interned strings
if (s_InternedStringMap == NULL)
return NULL;
InternedString internedString = { str->length, str->chars };
Il2CppString* value = NULL;
if (s_InternedStringMap->TryGetValue(internedString, &value))
return value;
return NULL;
}
} /* namespace vm */
} /* namespace il2cpp */