/
string-icalls.c
230 lines (197 loc) · 6.34 KB
/
string-icalls.c
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
/*
* string-icalls.c: String internal calls for the corlib
*
* Author:
* Patrik Torstensson (patrik.torstensson@labs2.com)
* Duncan Mak (duncan@ximian.com)
*
* Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
* Copyright 2004-2009 Novell, Inc (http://www.novell.com)
*/
#include <config.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include "mono/utils/mono-membar.h"
#include <mono/metadata/string-icalls.h>
#include <mono/metadata/class-internals.h>
#include <mono/metadata/appdomain.h>
#include <mono/metadata/tabledefs.h>
#include <mono/metadata/loader.h>
#include <mono/metadata/object.h>
#include <mono/metadata/exception.h>
#include <mono/metadata/debug-helpers.h>
/* Internal helper methods */
static gboolean
string_icall_is_in_array (MonoArray *chars, gint32 arraylength, gunichar2 chr);
/* This function is redirected to String.CreateString ()
by mono_marshal_get_native_wrapper () */
void
ves_icall_System_String_ctor_RedirectToCreateString (void)
{
g_assert_not_reached ();
}
/* System.StringSplitOptions */
typedef enum {
STRINGSPLITOPTIONS_NONE = 0,
STRINGSPLITOPTIONS_REMOVE_EMPTY_ENTRIES = 1
} StringSplitOptions;
MonoArray *
ves_icall_System_String_InternalSplit (MonoString *me, MonoArray *separator, gint32 count, gint32 options)
{
static MonoClass *String_array;
MonoString * tmpstr;
MonoArray * retarr;
gunichar2 *src;
gint32 arrsize, srcsize, splitsize;
gint32 i, lastpos, arrpos;
gint32 tmpstrsize;
gint32 remempty;
gint32 flag;
gunichar2 *tmpstrptr;
remempty = options & STRINGSPLITOPTIONS_REMOVE_EMPTY_ENTRIES;
src = mono_string_chars (me);
srcsize = mono_string_length (me);
arrsize = mono_array_length (separator);
if (!String_array) {
MonoClass *klass = mono_array_class_get (mono_get_string_class (), 1);
mono_memory_barrier ();
String_array = klass;
}
splitsize = 1;
/* Count the number of elements we will return. Note that this operation
* guarantees that we will return exactly splitsize elements, and we will
* have enough data to fill each. This allows us to skip some checks later on.
*/
if (remempty == 0) {
for (i = 0; i != srcsize && splitsize < count; i++) {
if (string_icall_is_in_array (separator, arrsize, src [i]))
splitsize++;
}
} else if (count > 1) {
/* Require pattern "Nondelim + Delim + Nondelim" to increment counter.
* Lastpos != 0 means first nondelim found.
* Flag = 0 means last char was delim.
* Efficient, though perhaps confusing.
*/
lastpos = 0;
flag = 0;
for (i = 0; i != srcsize && splitsize < count; i++) {
if (string_icall_is_in_array (separator, arrsize, src [i])) {
flag = 0;
} else if (flag == 0) {
if (lastpos == 1)
splitsize++;
flag = 1;
lastpos = 1;
}
}
/* Nothing but separators */
if (lastpos == 0) {
retarr = mono_array_new_specific (mono_class_vtable (mono_domain_get (), String_array), 0);
return retarr;
}
}
/* if no split chars found return the string */
if (splitsize == 1) {
if (remempty == 0 || count == 1) {
/* Copy the whole string */
retarr = mono_array_new_specific (mono_class_vtable (mono_domain_get (), String_array), 1);
mono_array_setref (retarr, 0, me);
} else {
/* otherwise we have to filter out leading & trailing delims */
/* find first non-delim char */
for (; srcsize != 0; srcsize--, src++) {
if (!string_icall_is_in_array (separator, arrsize, src [0]))
break;
}
/* find last non-delim char */
for (; srcsize != 0; srcsize--) {
if (!string_icall_is_in_array (separator, arrsize, src [srcsize - 1]))
break;
}
tmpstr = mono_string_new_size (mono_domain_get (), srcsize);
tmpstrptr = mono_string_chars (tmpstr);
memcpy (tmpstrptr, src, srcsize * sizeof (gunichar2));
retarr = mono_array_new_specific (mono_class_vtable (mono_domain_get (), String_array), 1);
mono_array_setref (retarr, 0, tmpstr);
}
return retarr;
}
lastpos = 0;
arrpos = 0;
retarr = mono_array_new_specific (mono_class_vtable (mono_domain_get (), String_array), splitsize);
for (i = 0; i != srcsize && arrpos != splitsize; i++) {
if (string_icall_is_in_array (separator, arrsize, src [i])) {
if (lastpos != i || remempty == 0) {
tmpstrsize = i - lastpos;
tmpstr = mono_string_new_size (mono_domain_get (), tmpstrsize);
tmpstrptr = mono_string_chars (tmpstr);
memcpy (tmpstrptr, src + lastpos, tmpstrsize * sizeof (gunichar2));
mono_array_setref (retarr, arrpos, tmpstr);
arrpos++;
if (arrpos == splitsize - 1) {
/* Shortcut the last array element */
lastpos = i + 1;
if (remempty != 0) {
/* Search for non-delim starting char (guaranteed to find one) Note that loop
* condition is only there for safety. It will never actually terminate the loop. */
for (; lastpos != srcsize ; lastpos++) {
if (!string_icall_is_in_array (separator, arrsize, src [lastpos]))
break;
}
if (count > splitsize) {
/* Since we have fewer results than our limit, we must remove
* trailing delimiters as well.
*/
for (; srcsize != lastpos + 1 ; srcsize--) {
if (!string_icall_is_in_array (separator, arrsize, src [srcsize - 1]))
break;
}
}
}
tmpstrsize = srcsize - lastpos;
tmpstr = mono_string_new_size (mono_domain_get (), tmpstrsize);
tmpstrptr = mono_string_chars (tmpstr);
memcpy (tmpstrptr, src + lastpos, tmpstrsize * sizeof (gunichar2));
mono_array_setref (retarr, arrpos, tmpstr);
/* Loop will ALWAYS end here. Test criteria in the FOR loop is technically unnecessary. */
break;
}
}
lastpos = i + 1;
}
}
return retarr;
}
static gboolean
string_icall_is_in_array (MonoArray *chars, gint32 arraylength, gunichar2 chr)
{
gunichar2 cmpchar;
gint32 arrpos;
for (arrpos = 0; arrpos != arraylength; arrpos++) {
cmpchar = mono_array_get(chars, gunichar2, arrpos);
if (cmpchar == chr)
return TRUE;
}
return FALSE;
}
MonoString *
ves_icall_System_String_InternalAllocateStr (gint32 length)
{
MONO_ARCH_SAVE_REGS;
return mono_string_new_size(mono_domain_get (), length);
}
MonoString *
ves_icall_System_String_InternalIntern (MonoString *str)
{
MONO_ARCH_SAVE_REGS;
return mono_string_intern(str);
}
MonoString *
ves_icall_System_String_InternalIsInterned (MonoString *str)
{
MONO_ARCH_SAVE_REGS;
return mono_string_is_interned(str);
}