2 * string-icalls.c: String internal calls for the corlib
5 * Patrik Torstensson (patrik.torstensson@labs2.com)
6 * Duncan Mak (duncan@ximian.com)
8 * (C) 2001 Ximian, Inc.
15 #include "mono/utils/mono-membar.h"
16 #include <mono/metadata/string-icalls.h>
17 #include <mono/metadata/class-internals.h>
18 #include <mono/metadata/appdomain.h>
19 #include <mono/metadata/tabledefs.h>
20 #include <mono/metadata/loader.h>
21 #include <mono/metadata/object.h>
22 #include <mono/metadata/exception.h>
23 #include <mono/metadata/debug-helpers.h>
25 /* Internal helper methods */
28 string_icall_is_in_array (MonoArray *chars, gint32 arraylength, gunichar2 chr);
30 /* This function is redirected to String.CreateString ()
31 by mono_marshal_get_native_wrapper () */
33 ves_icall_System_String_ctor_RedirectToCreateString (void)
35 g_assert_not_reached ();
39 ves_icall_System_String_InternalJoin (MonoString *separator, MonoArray * value, gint32 sindex, gint32 count)
54 insert = mono_string_chars(separator);
55 insertlen = mono_string_length(separator);
58 for (pos = sindex; pos != sindex + count; pos++) {
59 current = mono_array_get (value, MonoString *, pos);
61 length += mono_string_length (current);
63 if (pos < sindex + count - 1)
67 ret = mono_string_new_size( mono_domain_get (), length);
68 dest = mono_string_chars(ret);
71 for (pos = sindex; pos != sindex + count; pos++) {
72 current = mono_array_get (value, MonoString *, pos);
73 if (current != NULL) {
74 src = mono_string_chars (current);
75 srclen = mono_string_length (current);
77 memcpy (dest + destpos, src, srclen * sizeof(gunichar2));
81 if (pos < sindex + count - 1) {
82 memcpy(dest + destpos, insert, insertlen * sizeof(gunichar2));
91 ves_icall_System_String_InternalCopyTo (MonoString *me, gint32 sindex, MonoArray *dest, gint32 dindex, gint32 count)
93 gunichar2 *destptr = (gunichar2 *) mono_array_addr(dest, gunichar2, dindex);
94 gunichar2 *src = mono_string_chars(me);
98 memcpy(destptr, src + sindex, sizeof(gunichar2) * count);
101 /* System.StringSplitOptions */
103 STRINGSPLITOPTIONS_NONE = 0,
104 STRINGSPLITOPTIONS_REMOVE_EMPTY_ENTRIES = 1
105 } StringSplitOptions;
108 ves_icall_System_String_InternalSplit (MonoString *me, MonoArray *separator, gint32 count, gint32 options)
110 static MonoClass *String_array;
114 gint32 arrsize, srcsize, splitsize;
115 gint32 i, lastpos, arrpos;
119 gunichar2 *tmpstrptr;
121 remempty = options & STRINGSPLITOPTIONS_REMOVE_EMPTY_ENTRIES;
122 src = mono_string_chars (me);
123 srcsize = mono_string_length (me);
124 arrsize = mono_array_length (separator);
127 MonoClass *klass = mono_array_class_get (mono_get_string_class (), 1);
128 mono_memory_barrier ();
129 String_array = klass;
133 /* Count the number of elements we will return. Note that this operation
134 * guarantees that we will return exactly splitsize elements, and we will
135 * have enough data to fill each. This allows us to skip some checks later on.
138 for (i = 0; i != srcsize && splitsize < count; i++) {
139 if (string_icall_is_in_array (separator, arrsize, src [i]))
142 } else if (count > 1) {
143 /* Require pattern "Nondelim + Delim + Nondelim" to increment counter.
144 * Lastpos != 0 means first nondelim found.
145 * Flag = 0 means last char was delim.
146 * Efficient, though perhaps confusing.
150 for (i = 0; i != srcsize && splitsize < count; i++) {
151 if (string_icall_is_in_array (separator, arrsize, src [i])) {
153 } else if (flag == 0) {
161 /* Nothing but separators */
163 retarr = mono_array_new_specific (mono_class_vtable (mono_domain_get (), String_array), 0);
168 /* if no split chars found return the string */
169 if (splitsize == 1) {
170 if (remempty == 0 || count == 1) {
171 /* Copy the whole string */
172 retarr = mono_array_new_specific (mono_class_vtable (mono_domain_get (), String_array), 1);
173 mono_array_setref (retarr, 0, me);
175 /* otherwise we have to filter out leading & trailing delims */
177 /* find first non-delim char */
178 for (; srcsize != 0; srcsize--, src++) {
179 if (!string_icall_is_in_array (separator, arrsize, src [0]))
182 /* find last non-delim char */
183 for (; srcsize != 0; srcsize--) {
184 if (!string_icall_is_in_array (separator, arrsize, src [srcsize - 1]))
187 tmpstr = mono_string_new_size (mono_domain_get (), srcsize);
188 tmpstrptr = mono_string_chars (tmpstr);
190 memcpy (tmpstrptr, src, srcsize * sizeof (gunichar2));
191 retarr = mono_array_new_specific (mono_class_vtable (mono_domain_get (), String_array), 1);
192 mono_array_setref (retarr, 0, tmpstr);
200 retarr = mono_array_new_specific (mono_class_vtable (mono_domain_get (), String_array), splitsize);
202 for (i = 0; i != srcsize && arrpos != splitsize; i++) {
203 if (string_icall_is_in_array (separator, arrsize, src [i])) {
205 if (lastpos != i || remempty == 0) {
206 tmpstrsize = i - lastpos;
207 tmpstr = mono_string_new_size (mono_domain_get (), tmpstrsize);
208 tmpstrptr = mono_string_chars (tmpstr);
210 memcpy (tmpstrptr, src + lastpos, tmpstrsize * sizeof (gunichar2));
211 mono_array_setref (retarr, arrpos, tmpstr);
214 if (arrpos == splitsize - 1) {
215 /* Shortcut the last array element */
219 /* Search for non-delim starting char (guaranteed to find one) Note that loop
220 * condition is only there for safety. It will never actually terminate the loop. */
221 for (; lastpos != srcsize ; lastpos++) {
222 if (!string_icall_is_in_array (separator, arrsize, src [lastpos]))
225 if (count > splitsize) {
226 /* Since we have fewer results than our limit, we must remove
227 * trailing delimiters as well.
229 for (; srcsize != lastpos + 1 ; srcsize--) {
230 if (!string_icall_is_in_array (separator, arrsize, src [srcsize - 1]))
236 tmpstrsize = srcsize - lastpos;
237 tmpstr = mono_string_new_size (mono_domain_get (), tmpstrsize);
238 tmpstrptr = mono_string_chars (tmpstr);
240 memcpy (tmpstrptr, src + lastpos, tmpstrsize * sizeof (gunichar2));
241 mono_array_setref (retarr, arrpos, tmpstr);
243 /* Loop will ALWAYS end here. Test criteria in the FOR loop is technically unnecessary. */
255 string_icall_is_in_array (MonoArray *chars, gint32 arraylength, gunichar2 chr)
260 for (arrpos = 0; arrpos != arraylength; arrpos++) {
261 cmpchar = mono_array_get(chars, gunichar2, arrpos);
270 ves_icall_System_String_InternalTrim (MonoString *me, MonoArray *chars, gint32 typ)
273 gunichar2 *src, *dest;
274 gint32 srclen, newlen, arrlen;
275 gint32 i, lenfirst, lenlast;
279 srclen = mono_string_length(me);
280 src = mono_string_chars(me);
281 arrlen = mono_array_length(chars);
286 if (0 == typ || 1 == typ) {
287 for (i = 0; i != srclen; i++) {
288 if (string_icall_is_in_array(chars, arrlen, src[i]))
295 if (0 == typ || 2 == typ) {
296 for (i = srclen - 1; i > lenfirst - 1; i--) {
297 if (string_icall_is_in_array(chars, arrlen, src[i]))
304 newlen = srclen - lenfirst - lenlast;
305 if (newlen == srclen)
308 ret = mono_string_new_size( mono_domain_get (), newlen);
309 dest = mono_string_chars(ret);
311 memcpy(dest, src + lenfirst, newlen *sizeof(gunichar2));
317 ves_icall_System_String_InternalLastIndexOfAny (MonoString *me, MonoArray *anyOf, gint32 sindex, gint32 count)
326 arraysize = mono_array_length(anyOf);
327 src = mono_string_chars(me);
329 for (pos = sindex; pos > sindex - count; pos--) {
330 for (loop = 0; loop != arraysize; loop++)
331 if ( src [pos] == mono_array_get(anyOf, gunichar2, loop) )
339 ves_icall_System_String_InternalPad (MonoString *me, gint32 width, gunichar2 chr, MonoBoolean right)
350 srclen = mono_string_length(me);
351 src = mono_string_chars(me);
353 ret = mono_string_new_size( mono_domain_get (), width);
354 dest = mono_string_chars(ret);
355 fillcount = width - srclen;
358 memcpy(dest, src, srclen * sizeof(gunichar2));
359 for (i = srclen; i != width; i++)
366 for (i = 0; i != fillcount; i++)
369 memcpy(dest + fillcount, src, srclen * sizeof(gunichar2));
375 ves_icall_System_String_InternalAllocateStr (gint32 length)
379 return mono_string_new_size(mono_domain_get (), length);
383 ves_icall_System_String_InternalStrcpy_Str (MonoString *dest, gint32 destPos, MonoString *src)
390 srcptr = mono_string_chars (src);
391 destptr = mono_string_chars (dest);
393 g_memmove (destptr + destPos, srcptr, mono_string_length(src) * sizeof(gunichar2));
397 ves_icall_System_String_InternalStrcpy_StrN (MonoString *dest, gint32 destPos, MonoString *src, gint32 startPos, gint32 count)
404 srcptr = mono_string_chars (src);
405 destptr = mono_string_chars (dest);
406 g_memmove (destptr + destPos, srcptr + startPos, count * sizeof(gunichar2));
410 ves_icall_System_String_InternalStrcpy_Chars (MonoString *dest, gint32 destPos, MonoArray *src)
417 srcptr = mono_array_addr (src, gunichar2, 0);
418 destptr = mono_string_chars (dest);
420 g_memmove (destptr + destPos, srcptr, mono_array_length (src) * sizeof(gunichar2));
424 ves_icall_System_String_InternalStrcpy_CharsN (MonoString *dest, gint32 destPos, MonoArray *src, gint32 startPos, gint32 count)
431 srcptr = mono_array_addr (src, gunichar2, 0);
432 destptr = mono_string_chars (dest);
434 g_memmove (destptr + destPos, srcptr + startPos, count * sizeof(gunichar2));
438 ves_icall_System_String_InternalIntern (MonoString *str)
442 return mono_string_intern(str);
446 ves_icall_System_String_InternalIsInterned (MonoString *str)
450 return mono_string_is_interned(str);
454 ves_icall_System_String_get_Chars (MonoString *me, gint32 idx)
458 if ((idx < 0) || (idx >= mono_string_length (me)))
459 mono_raise_exception (mono_get_exception_index_out_of_range ());
460 return mono_string_chars(me)[idx];