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/metadata/string-icalls.h>
16 #include <mono/metadata/appdomain.h>
17 #include <mono/metadata/tabledefs.h>
18 #include <mono/metadata/loader.h>
19 #include <mono/metadata/object.h>
20 #include <mono/metadata/unicode.h>
21 #include <mono/metadata/exception.h>
23 /* Internal helper methods */
26 string_icall_is_in_array (MonoArray *chars, gint32 arraylength, gunichar2 chr);
29 string_icall_cmp_char (gunichar2 c1, gunichar2 c2, gint32 mode);
32 ves_icall_System_String_ctor_charp (gpointer dummy, gunichar2 *value)
39 domain = mono_domain_get ();
44 for (i = 0; *(value + i) != '\0'; i++);
48 return mono_string_new_utf16 (domain, value, length);
52 ves_icall_System_String_ctor_char_int (gpointer dummy, gunichar2 value, gint32 count)
62 mono_raise_exception (mono_get_exception_argument_out_of_range ("count"));
64 domain = mono_domain_get ();
65 res = mono_string_new_size (domain, count);
67 chars = mono_string_chars (res);
68 for (i = 0; i < count; i++)
75 ves_icall_System_String_ctor_charp_int_int (gpointer dummy, gunichar2 *value, gint32 sindex, gint32 length)
82 domain = mono_domain_get ();
84 if ((value == NULL) && (length != 0))
85 mono_raise_exception (mono_get_exception_argument_out_of_range ("Out of range"));
87 if ((sindex < 0) || (length < 0))
88 mono_raise_exception (mono_get_exception_argument_out_of_range ("Out of range"));
90 if (length == 0) { /* fixme: return String.Empty here */
91 g_warning ("string doesn't yet support empy strings in char* constructor");
92 g_assert_not_reached ();
95 begin = (gunichar2 *) (value + sindex);
97 return mono_string_new_utf16 (domain, begin, length);
101 ves_icall_System_String_ctor_sbytep (gpointer dummy, gint8 *value)
107 domain = mono_domain_get ();
109 if (NULL == value) { /* fixme: return String.Empty here */
110 g_warning ("string doesn't yet support empy strings in char* constructor");
111 g_assert_not_reached ();
114 return mono_string_new (domain, (const char *) value);
118 ves_icall_System_String_ctor_sbytep_int_int (gpointer dummy, gint8 *value, gint32 sindex, gint32 length)
128 domain = mono_domain_get ();
130 if ((value == NULL) && (length != 0))
131 mono_raise_exception (mono_get_exception_argument_out_of_range ("Out of range"));
133 if ((sindex < 0) || (length < 0))
134 mono_raise_exception (mono_get_exception_argument_out_of_range ("Out of range"));
136 begin = (guchar *) (value + sindex);
137 res = mono_string_new_size (domain, length);
138 chars = mono_string_chars (res);
139 for (i = 0; i < length; ++i)
140 chars [i] = begin [i];
146 ves_icall_System_String_ctor_chara (gpointer dummy, MonoArray *value)
152 domain = mono_domain_get ();
155 return mono_string_new_utf16 (domain, NULL, 0);
157 return mono_string_new_utf16 (domain, (gunichar2 *) mono_array_addr(value, gunichar2, 0), value->max_length);
161 ves_icall_System_String_ctor_chara_int_int (gpointer dummy, MonoArray *value,
162 gint32 sindex, gint32 length)
169 mono_raise_exception (mono_get_exception_argument_null ("value"));
171 mono_raise_exception (mono_get_exception_argument_out_of_range ("startIndex"));
173 mono_raise_exception (mono_get_exception_argument_out_of_range ("length"));
174 if (sindex + length > mono_array_length (value))
175 mono_raise_exception (mono_get_exception_argument_out_of_range ("Out of range"));
177 domain = mono_domain_get ();
179 return mono_string_new_utf16 (domain, (gunichar2 *) mono_array_addr(value, gunichar2, sindex), length);
183 ves_icall_System_String_ctor_encoding (gpointer dummy, gint8 *value, gint32 sindex,
184 gint32 length, MonoObject *enc)
188 g_warning("string.ctor with encoding obj unimplemented");
189 g_assert_not_reached ();
194 ves_icall_System_String_InternalJoin (MonoString *separator, MonoArray * value, gint32 sindex, gint32 count)
209 insert = mono_string_chars(separator);
210 insertlen = mono_string_length(separator);
213 for (pos = sindex; pos != sindex + count; pos++) {
214 current = mono_array_get (value, MonoString *, pos);
216 length += mono_string_length (current);
218 if (pos < sindex + count - 1)
222 ret = mono_string_new_size( mono_domain_get (), length);
223 dest = mono_string_chars(ret);
226 for (pos = sindex; pos != sindex + count; pos++) {
227 current = mono_array_get (value, MonoString *, pos);
228 if (current != NULL) {
229 src = mono_string_chars (current);
230 srclen = mono_string_length (current);
232 memcpy (dest + destpos, src, srclen * sizeof(gunichar2));
236 if (pos < sindex + count - 1) {
237 memcpy(dest + destpos, insert, insertlen * sizeof(gunichar2));
238 destpos += insertlen;
246 ves_icall_System_String_InternalInsert (MonoString *me, gint32 sindex, MonoString *value)
250 gunichar2 *insertsrc;
257 src = mono_string_chars(me);
258 srclen = mono_string_length(me);
260 insertsrc = mono_string_chars(value);
261 insertlen = mono_string_length(value);
263 ret = mono_string_new_size( mono_domain_get (), srclen + insertlen);
264 dest = mono_string_chars(ret);
266 memcpy(dest, src, sindex * sizeof(gunichar2));
267 memcpy(dest + sindex, insertsrc, insertlen * sizeof(gunichar2));
268 memcpy(dest + sindex + insertlen, src + sindex, (srclen - sindex) * sizeof(gunichar2));
274 ves_icall_System_String_InternalReplace_Char (MonoString *me, gunichar2 oldChar, gunichar2 newChar)
283 src = mono_string_chars(me);
284 srclen = mono_string_length(me);
286 ret = mono_string_new_size( mono_domain_get (), srclen);
287 dest = mono_string_chars(ret);
289 for (i = 0; i != srclen; i++) {
290 if (src[i] == oldChar)
300 ves_icall_System_String_InternalRemove (MonoString *me, gint32 sindex, gint32 count)
309 srclen = mono_string_length(me);
310 ret = mono_string_new_size( mono_domain_get (), srclen - count);
312 src = mono_string_chars(me);
313 dest = mono_string_chars(ret);
315 memcpy(dest, src, sindex * sizeof(gunichar2));
316 memcpy(dest + sindex, src + sindex + count, (srclen - count - sindex) * sizeof(gunichar2));
322 ves_icall_System_String_InternalCopyTo (MonoString *me, gint32 sindex, MonoArray *dest, gint32 dindex, gint32 count)
324 gunichar2 *destptr = (gunichar2 *) mono_array_addr(dest, gunichar2, dindex);
325 gunichar2 *src = mono_string_chars(me);
329 memcpy(destptr, src + sindex, sizeof(gunichar2) * count);
333 ves_icall_System_String_InternalSplit (MonoString *me, MonoArray *separator, gint32 count)
338 gint32 arrsize, srcsize, splitsize;
339 gint32 i, lastpos, arrpos;
341 gunichar2 *tmpstrptr;
347 src = mono_string_chars(me);
348 srcsize = mono_string_length(me);
349 arrsize = mono_array_length(separator);
351 cmpchar = mono_array_get(separator, gunichar2, 0);
354 for (i = 0; i != srcsize && splitsize < count; i++) {
355 if (string_icall_is_in_array(separator, arrsize, src[i]))
362 /* if no split chars found return the string */
363 if (splitsize == 0) {
364 retarr = mono_array_new(mono_domain_get(), mono_get_string_class (), 1);
365 mono_array_set(retarr, MonoString *, 0, me);
370 if (splitsize != count)
373 retarr = mono_array_new(mono_domain_get(), mono_get_string_class (), splitsize);
374 for (i = 0; i != srcsize && arrpos != count; i++) {
375 if (string_icall_is_in_array(separator, arrsize, src[i])) {
376 if (arrpos == count - 1)
377 tmpstrsize = srcsize - lastpos;
379 tmpstrsize = i - lastpos;
381 tmpstr = mono_string_new_size( mono_domain_get (), tmpstrsize);
382 tmpstrptr = mono_string_chars(tmpstr);
384 memcpy(tmpstrptr, src + lastpos, tmpstrsize * sizeof(gunichar2));
385 mono_array_set(retarr, MonoString *, arrpos, tmpstr);
391 if (arrpos < count) {
392 tmpstrsize = srcsize - lastpos;
393 tmpstr = mono_string_new_size( mono_domain_get (), tmpstrsize);
394 tmpstrptr = mono_string_chars(tmpstr);
396 memcpy(tmpstrptr, src + lastpos, tmpstrsize * sizeof(gunichar2));
397 mono_array_set(retarr, MonoString *, arrpos, tmpstr);
404 string_icall_is_in_array (MonoArray *chars, gint32 arraylength, gunichar2 chr)
409 for (arrpos = 0; arrpos != arraylength; arrpos++) {
410 cmpchar = mono_array_get(chars, gunichar2, arrpos);
419 ves_icall_System_String_InternalTrim (MonoString *me, MonoArray *chars, gint32 typ)
422 gunichar2 *src, *dest;
423 gint32 srclen, newlen, arrlen;
424 gint32 i, lenfirst, lenlast;
428 srclen = mono_string_length(me);
429 src = mono_string_chars(me);
430 arrlen = mono_array_length(chars);
435 if (0 == typ || 1 == typ) {
436 for (i = 0; i != srclen; i++) {
437 if (string_icall_is_in_array(chars, arrlen, src[i]))
444 if (0 == typ || 2 == typ) {
445 for (i = srclen - 1; i > lenfirst - 1; i--) {
446 if (string_icall_is_in_array(chars, arrlen, src[i]))
453 newlen = srclen - lenfirst - lenlast;
454 if (newlen == srclen)
457 ret = mono_string_new_size( mono_domain_get (), newlen);
458 dest = mono_string_chars(ret);
460 memcpy(dest, src + lenfirst, newlen *sizeof(gunichar2));
466 ves_icall_System_String_InternalIndexOfAny (MonoString *me, MonoArray *arr, gint32 sindex, gint32 count)
475 arraysize = mono_array_length(arr);
476 src = mono_string_chars(me);
478 for (pos = sindex; pos != count + sindex; pos++) {
479 for (loop = 0; loop != arraysize; loop++)
480 if ( src [pos] == mono_array_get(arr, gunichar2, loop) )
488 ves_icall_System_String_InternalLastIndexOf_Char (MonoString *me, gunichar2 value, gint32 sindex, gint32 count)
495 src = mono_string_chars(me);
496 for (pos = sindex; pos > sindex - count; pos--) {
497 if (src [pos] == value)
505 ves_icall_System_String_InternalLastIndexOf_Str (MonoString *me, MonoString *value, gint32 sindex, gint32 count)
514 lencmpstr = mono_string_length(value);
516 src = mono_string_chars(me);
517 cmpstr = mono_string_chars(value);
519 for (pos = sindex - lencmpstr + 1; pos > sindex - count; pos--) {
520 if (0 == memcmp(src + pos, cmpstr, lencmpstr * sizeof(gunichar2)))
528 ves_icall_System_String_InternalLastIndexOfAny (MonoString *me, MonoArray *anyOf, gint32 sindex, gint32 count)
537 arraysize = mono_array_length(anyOf);
538 src = mono_string_chars(me);
540 for (pos = sindex; pos > sindex - count; pos--) {
541 for (loop = 0; loop != arraysize; loop++)
542 if ( src [pos] == mono_array_get(anyOf, gunichar2, loop) )
550 ves_icall_System_String_InternalPad (MonoString *me, gint32 width, gunichar2 chr, MonoBoolean right)
561 srclen = mono_string_length(me);
562 src = mono_string_chars(me);
564 ret = mono_string_new_size( mono_domain_get (), width);
565 dest = mono_string_chars(ret);
566 fillcount = width - srclen;
569 memcpy(dest, src, srclen * sizeof(gunichar2));
570 for (i = srclen; i != width; i++)
577 for (i = 0; i != fillcount; i++)
580 memcpy(dest + fillcount, src, srclen * sizeof(gunichar2));
586 ves_icall_System_String_InternalAllocateStr (gint32 length)
590 return mono_string_new_size(mono_domain_get (), length);
594 ves_icall_System_String_InternalStrcpy_Str (MonoString *dest, gint32 destPos, MonoString *src)
601 srcptr = mono_string_chars (src);
602 destptr = mono_string_chars (dest);
604 g_memmove (destptr + destPos, srcptr, mono_string_length(src) * sizeof(gunichar2));
608 ves_icall_System_String_InternalStrcpy_StrN (MonoString *dest, gint32 destPos, MonoString *src, gint32 startPos, gint32 count)
615 srcptr = mono_string_chars (src);
616 destptr = mono_string_chars (dest);
617 g_memmove (destptr + destPos, srcptr + startPos, count * sizeof(gunichar2));
621 ves_icall_System_String_InternalStrcpy_Chars (MonoString *dest, gint32 destPos, MonoArray *src)
628 srcptr = mono_array_addr (src, gunichar2, 0);
629 destptr = mono_string_chars (dest);
631 g_memmove (destptr + destPos, srcptr, mono_array_length (src) * sizeof(gunichar2));
635 ves_icall_System_String_InternalStrcpy_CharsN (MonoString *dest, gint32 destPos, MonoArray *src, gint32 startPos, gint32 count)
642 srcptr = mono_array_addr (src, gunichar2, 0);
643 destptr = mono_string_chars (dest);
645 g_memmove (destptr + destPos, srcptr + startPos, count * sizeof(gunichar2));
649 ves_icall_System_String_InternalIntern (MonoString *str)
653 return mono_string_intern(str);
657 ves_icall_System_String_InternalIsInterned (MonoString *str)
661 return mono_string_is_interned(str);
665 ves_icall_System_String_GetHashCode (MonoString *me)
668 gunichar2 *data = mono_string_chars (me);
672 for (i = 0; i < mono_string_length (me); ++i)
673 h = (h << 5) - h + data [i];
679 ves_icall_System_String_get_Chars (MonoString *me, gint32 idx)
683 if ((idx < 0) || (idx >= mono_string_length (me)))
684 mono_raise_exception (mono_get_exception_index_out_of_range ());
685 return mono_string_chars(me)[idx];
689 ves_icall_System_String_InternalCharCopy (gunichar2 *src, gunichar2 *dest, gint32 count)
693 memcpy (dest, src, sizeof (gunichar2) * count);
698 * 0 = StringCompareModeDirect
699 * 1 = StringCompareModeCaseInsensitive
700 * 2 = StringCompareModeOrdinal
703 string_icall_cmp_char (gunichar2 c1, gunichar2 c2, gint32 mode)
706 GUnicodeType c1type, c2type;
708 c1type = g_unichar_type (c1);
709 c2type = g_unichar_type (c2);
713 /* TODO: compare with culture info */
714 if (c1type == G_UNICODE_UPPERCASE_LETTER && c2type == G_UNICODE_LOWERCASE_LETTER)
717 if (c1type == G_UNICODE_LOWERCASE_LETTER && c2type == G_UNICODE_UPPERCASE_LETTER)
720 result = (gint32) c1 - c2;
723 result = (gint32) (c1type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c1) : c1) -
724 (c2type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c2) : c2);
727 /* Rotor/ms return the full value just not -1 and 1 */
728 return (gint32) c1 - c2; break;
731 return ((result < 0) ? -1 : (result > 0) ? 1 : 0);