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)
168 if ((value == NULL) && ((sindex != 0) || (length != 0)))
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 ((value != NULL) && (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_InternalEquals (MonoString *str1, MonoString *str2)
202 /* Length checking is done in C# */
203 str1len = mono_string_length(str1);
205 str1ptr = mono_string_chars(str1);
206 str2ptr = mono_string_chars(str2);
208 return (0 == memcmp(str1ptr, str2ptr, str1len * sizeof(gunichar2)));
212 ves_icall_System_String_InternalJoin (MonoString *separator, MonoArray * value, gint32 sindex, gint32 count)
226 insert = mono_string_chars(separator);
227 insertlen = mono_string_length(separator);
230 for (pos = sindex; pos != sindex + count; pos++) {
231 length += mono_string_length(mono_array_get(value, MonoString *, pos));
232 if (pos < sindex + count - 1)
236 ret = mono_string_new_size( mono_domain_get (), length);
237 dest = mono_string_chars(ret);
240 for (pos = sindex; pos != sindex + count; pos++) {
241 src = mono_string_chars(mono_array_get(value, MonoString *, pos));
242 srclen = mono_string_length(mono_array_get(value, MonoString *, pos));
244 memcpy(dest + destpos, src, srclen * sizeof(gunichar2));
247 if (pos < sindex + count - 1) {
248 memcpy(dest + destpos, insert, insertlen * sizeof(gunichar2));
249 destpos += insertlen;
257 ves_icall_System_String_InternalInsert (MonoString *me, gint32 sindex, MonoString *value)
261 gunichar2 *insertsrc;
268 src = mono_string_chars(me);
269 srclen = mono_string_length(me);
271 insertsrc = mono_string_chars(value);
272 insertlen = mono_string_length(value);
274 ret = mono_string_new_size( mono_domain_get (), srclen + insertlen);
275 dest = mono_string_chars(ret);
277 memcpy(dest, src, sindex * sizeof(gunichar2));
278 memcpy(dest + sindex, insertsrc, insertlen * sizeof(gunichar2));
279 memcpy(dest + sindex + insertlen, src + sindex, (srclen - sindex) * sizeof(gunichar2));
285 ves_icall_System_String_InternalReplace_Char (MonoString *me, gunichar2 oldChar, gunichar2 newChar)
294 src = mono_string_chars(me);
295 srclen = mono_string_length(me);
297 ret = mono_string_new_size( mono_domain_get (), srclen);
298 dest = mono_string_chars(ret);
300 for (i = 0; i != srclen; i++) {
301 if (src[i] == oldChar)
311 ves_icall_System_String_InternalReplace_Str (MonoString *me, MonoString *oldValue, MonoString *newValue)
330 oldstr = mono_string_chars(oldValue);
331 oldstrlen = mono_string_length(oldValue);
333 if (NULL != newValue) {
334 newstr = mono_string_chars(newValue);
335 newstrlen = mono_string_length(newValue);
339 src = mono_string_chars(me);
340 srclen = mono_string_length(me);
342 if (oldstrlen != newstrlen) {
343 for (i = 0; i <= srclen - oldstrlen; i++)
344 if (0 == memcmp(src + i, oldstr, oldstrlen * sizeof(gunichar2)))
347 newsize = srclen + ((newstrlen - oldstrlen) * occurr);
351 ret = mono_string_new_size( mono_domain_get (), newsize);
352 dest = mono_string_chars(ret);
356 if (0 == memcmp(src + i, oldstr, oldstrlen * sizeof(gunichar2))) {
358 memcpy(dest + destpos, newstr, newstrlen * sizeof(gunichar2));
359 destpos += newstrlen;
363 dest[destpos] = src[i];
373 ves_icall_System_String_InternalRemove (MonoString *me, gint32 sindex, gint32 count)
382 srclen = mono_string_length(me);
383 ret = mono_string_new_size( mono_domain_get (), srclen - count);
385 src = mono_string_chars(me);
386 dest = mono_string_chars(ret);
388 memcpy(dest, src, sindex * sizeof(gunichar2));
389 memcpy(dest + sindex, src + sindex + count, (srclen - count - sindex) * sizeof(gunichar2));
395 ves_icall_System_String_InternalCopyTo (MonoString *me, gint32 sindex, MonoArray *dest, gint32 dindex, gint32 count)
397 gunichar2 *destptr = (gunichar2 *) mono_array_addr(dest, gunichar2, dindex);
398 gunichar2 *src = mono_string_chars(me);
402 memcpy(destptr, src + sindex, sizeof(gunichar2) * count);
406 ves_icall_System_String_InternalSplit (MonoString *me, MonoArray *separator, gint32 count)
411 gint32 arrsize, srcsize, splitsize;
412 gint32 i, lastpos, arrpos;
414 gunichar2 *tmpstrptr;
420 src = mono_string_chars(me);
421 srcsize = mono_string_length(me);
422 arrsize = mono_array_length(separator);
424 cmpchar = mono_array_get(separator, gunichar2, 0);
427 for (i = 0; i != srcsize && splitsize < count; i++) {
428 if (string_icall_is_in_array(separator, arrsize, src[i]))
435 /* if no split chars found return the string */
436 if (splitsize == 0) {
437 retarr = mono_array_new(mono_domain_get(), mono_defaults.string_class, 1);
438 tmpstr = mono_string_new_size( mono_domain_get (), srcsize);
439 tmpstrptr = mono_string_chars(tmpstr);
441 memcpy(tmpstrptr, src, srcsize * sizeof(gunichar2));
442 mono_array_set(retarr, MonoString *, 0, tmpstr);
447 if (splitsize != count)
450 retarr = mono_array_new(mono_domain_get(), mono_defaults.string_class, splitsize);
451 for (i = 0; i != srcsize && arrpos != count; i++) {
452 if (string_icall_is_in_array(separator, arrsize, src[i])) {
453 if (arrpos == count - 1)
454 tmpstrsize = srcsize - lastpos;
456 tmpstrsize = i - lastpos;
458 tmpstr = mono_string_new_size( mono_domain_get (), tmpstrsize);
459 tmpstrptr = mono_string_chars(tmpstr);
461 memcpy(tmpstrptr, src + lastpos, tmpstrsize * sizeof(gunichar2));
462 mono_array_set(retarr, MonoString *, arrpos, tmpstr);
468 if (arrpos < count) {
469 tmpstrsize = srcsize - lastpos;
470 tmpstr = mono_string_new_size( mono_domain_get (), tmpstrsize);
471 tmpstrptr = mono_string_chars(tmpstr);
473 memcpy(tmpstrptr, src + lastpos, tmpstrsize * sizeof(gunichar2));
474 mono_array_set(retarr, MonoString *, arrpos, tmpstr);
481 string_icall_is_in_array (MonoArray *chars, gint32 arraylength, gunichar2 chr)
486 for (arrpos = 0; arrpos != arraylength; arrpos++) {
487 cmpchar = mono_array_get(chars, gunichar2, arrpos);
496 ves_icall_System_String_InternalTrim (MonoString *me, MonoArray *chars, gint32 typ)
499 gunichar2 *src, *dest;
500 gint32 srclen, newlen, arrlen;
501 gint32 i, lenfirst, lenlast;
505 srclen = mono_string_length(me);
506 src = mono_string_chars(me);
507 arrlen = mono_array_length(chars);
512 if (0 == typ || 1 == typ) {
513 for (i = 0; i != srclen; i++) {
514 if (string_icall_is_in_array(chars, arrlen, src[i]))
521 if (0 == typ || 2 == typ) {
522 for (i = srclen - 1; i > lenfirst - 1; i--) {
523 if (string_icall_is_in_array(chars, arrlen, src[i]))
530 newlen = srclen - lenfirst - lenlast;
532 ret = mono_string_new_size( mono_domain_get (), newlen);
533 dest = mono_string_chars(ret);
535 memcpy(dest, src + lenfirst, newlen *sizeof(gunichar2));
541 ves_icall_System_String_InternalIndexOf_Char (MonoString *me, gunichar2 value, gint32 sindex, gint32 count)
548 src = mono_string_chars(me);
549 for (pos = sindex; pos != count + sindex; pos++) {
550 if ( src [pos] == value)
558 ves_icall_System_String_InternalIndexOf_Str (MonoString *me, MonoString *value, gint32 sindex, gint32 count)
567 lencmpstr = mono_string_length(value);
569 src = mono_string_chars(me);
570 cmpstr = mono_string_chars(value);
573 for (pos = sindex; pos <= sindex + count; pos++) {
574 for (i = 0; src [pos + i] == cmpstr [i];) {
575 if (++i == lencmpstr)
584 ves_icall_System_String_InternalIndexOfAny (MonoString *me, MonoArray *arr, gint32 sindex, gint32 count)
593 arraysize = mono_array_length(arr);
594 src = mono_string_chars(me);
596 for (pos = sindex; pos != count + sindex; pos++) {
597 for (loop = 0; loop != arraysize; loop++)
598 if ( src [pos] == mono_array_get(arr, gunichar2, loop) )
606 ves_icall_System_String_InternalLastIndexOf_Char (MonoString *me, gunichar2 value, gint32 sindex, gint32 count)
613 src = mono_string_chars(me);
614 for (pos = sindex; pos > sindex - count; pos--) {
615 if (src [pos] == value)
623 ves_icall_System_String_InternalLastIndexOf_Str (MonoString *me, MonoString *value, gint32 sindex, gint32 count)
632 lencmpstr = mono_string_length(value);
634 src = mono_string_chars(me);
635 cmpstr = mono_string_chars(value);
637 for (pos = sindex - lencmpstr + 1; pos > sindex - count; pos--) {
638 if (0 == memcmp(src + pos, cmpstr, lencmpstr * sizeof(gunichar2)))
646 ves_icall_System_String_InternalLastIndexOfAny (MonoString *me, MonoArray *anyOf, gint32 sindex, gint32 count)
655 arraysize = mono_array_length(anyOf);
656 src = mono_string_chars(me);
658 for (pos = sindex; pos > sindex - count; pos--) {
659 for (loop = 0; loop != arraysize; loop++)
660 if ( src [pos] == mono_array_get(anyOf, gunichar2, loop) )
668 ves_icall_System_String_InternalPad (MonoString *me, gint32 width, gunichar2 chr, MonoBoolean right)
679 srclen = mono_string_length(me);
680 src = mono_string_chars(me);
682 ret = mono_string_new_size( mono_domain_get (), width);
683 dest = mono_string_chars(ret);
684 fillcount = width - srclen;
687 memcpy(dest, src, srclen * sizeof(gunichar2));
688 for (i = srclen; i != width; i++)
695 for (i = 0; i != fillcount; i++)
698 memcpy(dest + fillcount, src, srclen * sizeof(gunichar2));
704 ves_icall_System_String_InternalToLower (MonoString *me)
713 ret = mono_string_new_size(mono_domain_get (), mono_string_length(me));
715 src = mono_string_chars (me);
716 dest = mono_string_chars (ret);
718 for (i = 0; i < mono_string_length (me); ++i)
719 dest[i] = g_unichar_tolower(src[i]);
725 ves_icall_System_String_InternalToUpper (MonoString *me)
734 ret = mono_string_new_size(mono_domain_get (), mono_string_length(me));
736 src = mono_string_chars (me);
737 dest = mono_string_chars (ret);
739 for (i = 0; i < mono_string_length (me); ++i)
740 dest[i] = g_unichar_toupper(src[i]);
746 ves_icall_System_String_InternalAllocateStr (gint32 length)
750 return mono_string_new_size(mono_domain_get (), length);
754 ves_icall_System_String_InternalStrcpy_Str (MonoString *dest, gint32 destPos, MonoString *src)
761 srcptr = mono_string_chars (src);
762 destptr = mono_string_chars (dest);
764 memcpy(destptr + destPos, srcptr, mono_string_length(src) * sizeof(gunichar2));
768 ves_icall_System_String_InternalStrcpy_StrN (MonoString *dest, gint32 destPos, MonoString *src, gint32 startPos, gint32 count)
775 srcptr = mono_string_chars (src);
776 destptr = mono_string_chars (dest);
777 memcpy(destptr + destPos, srcptr + startPos, count * sizeof(gunichar2));
781 ves_icall_System_String_InternalIntern (MonoString *str)
785 return mono_string_intern(str);
789 ves_icall_System_String_InternalIsInterned (MonoString *str)
793 return mono_string_is_interned(str);
797 ves_icall_System_String_InternalCompareStr_N (MonoString *s1, gint32 i1, MonoString *s2, gint32 i2, gint32 length, gint32 mode)
799 /* c translation of C# code from old string.cs.. :) */
810 lenstr1 = mono_string_length(s1);
811 lenstr2 = mono_string_length(s2);
813 str1 = mono_string_chars(s1);
814 str2 = mono_string_chars(s2);
818 for (pos = 0; pos != length; pos++) {
819 if (i1 + pos >= lenstr1 || i2 + pos >= lenstr2)
822 charcmp = string_icall_cmp_char(str1[i1 + pos], str2[i2 + pos], mode);
827 /* the lesser wins, so if we have looped until length we just need to check the last char */
829 return string_icall_cmp_char(str1[i1 + pos - 1], str2[i2 + pos - 1], mode);
832 /* Test if one the strings has been compared to the end */
833 if (i1 + pos >= lenstr1) {
834 if (i2 + pos >= lenstr2)
838 } else if (i2 + pos >= lenstr2)
841 /* if not, check our last char only.. (can this happen?) */
842 return string_icall_cmp_char(str1[i1 + pos], str2[i2 + pos], mode);
846 ves_icall_System_String_GetHashCode (MonoString *me)
849 gunichar2 *data = mono_string_chars (me);
853 for (i = 0; i < mono_string_length (me); ++i)
854 h = (h << 5) - h + data [i];
860 ves_icall_System_String_get_Chars (MonoString *me, gint32 idx)
864 if ((idx < 0) || (idx >= mono_string_length (me)))
865 mono_raise_exception (mono_get_exception_index_out_of_range ());
866 return mono_string_chars(me)[idx];
869 /* @mode : 0 = StringCompareModeDirect
870 1 = StringCompareModeCaseInsensitive
871 2 = StringCompareModeOrdinal
874 string_icall_cmp_char (gunichar2 c1, gunichar2 c2, gint32 mode)
877 GUnicodeType c1type, c2type;
879 c1type = g_unichar_type (c1);
880 c2type = g_unichar_type (c2);
884 /* TODO: compare with culture info */
885 if (c1type == G_UNICODE_UPPERCASE_LETTER && c2type == G_UNICODE_LOWERCASE_LETTER)
888 if (c1type == G_UNICODE_LOWERCASE_LETTER && c2type == G_UNICODE_UPPERCASE_LETTER)
891 result = (gint32) c1 - c2;
894 result = (gint32) (c1type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c1) : c1) -
895 (c2type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c2) : c2);
898 // Rotor/ms return the full value just not -1 and 1
899 return (gint32) c1 - c2; break;
902 return ((result < 0) ? -1 : (result > 0) ? 1 : 0);