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_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)
227 insert = mono_string_chars(separator);
228 insertlen = mono_string_length(separator);
231 for (pos = sindex; pos != sindex + count; pos++) {
232 current = mono_array_get (value, MonoString *, pos);
234 length += mono_string_length (current);
236 if (pos < sindex + count - 1)
240 ret = mono_string_new_size( mono_domain_get (), length);
241 dest = mono_string_chars(ret);
244 for (pos = sindex; pos != sindex + count; pos++) {
245 current = mono_array_get (value, MonoString *, pos);
246 if (current != NULL) {
247 src = mono_string_chars (current);
248 srclen = mono_string_length (current);
250 memcpy (dest + destpos, src, srclen * sizeof(gunichar2));
254 if (pos < sindex + count - 1) {
255 memcpy(dest + destpos, insert, insertlen * sizeof(gunichar2));
256 destpos += insertlen;
264 ves_icall_System_String_InternalInsert (MonoString *me, gint32 sindex, MonoString *value)
268 gunichar2 *insertsrc;
275 src = mono_string_chars(me);
276 srclen = mono_string_length(me);
278 insertsrc = mono_string_chars(value);
279 insertlen = mono_string_length(value);
281 ret = mono_string_new_size( mono_domain_get (), srclen + insertlen);
282 dest = mono_string_chars(ret);
284 memcpy(dest, src, sindex * sizeof(gunichar2));
285 memcpy(dest + sindex, insertsrc, insertlen * sizeof(gunichar2));
286 memcpy(dest + sindex + insertlen, src + sindex, (srclen - sindex) * sizeof(gunichar2));
292 ves_icall_System_String_InternalReplace_Char (MonoString *me, gunichar2 oldChar, gunichar2 newChar)
301 src = mono_string_chars(me);
302 srclen = mono_string_length(me);
304 ret = mono_string_new_size( mono_domain_get (), srclen);
305 dest = mono_string_chars(ret);
307 for (i = 0; i != srclen; i++) {
308 if (src[i] == oldChar)
318 ves_icall_System_String_InternalReplace_Str (MonoString *me, MonoString *oldValue, MonoString *newValue)
337 oldstr = mono_string_chars(oldValue);
338 oldstrlen = mono_string_length(oldValue);
340 if (NULL != newValue) {
341 newstr = mono_string_chars(newValue);
342 newstrlen = mono_string_length(newValue);
346 src = mono_string_chars(me);
347 srclen = mono_string_length(me);
349 if (oldstrlen != newstrlen) {
350 for (i = 0; i <= srclen - oldstrlen; i++)
351 if (0 == memcmp(src + i, oldstr, oldstrlen * sizeof(gunichar2)))
355 newsize = srclen + ((newstrlen - oldstrlen) * occurr);
362 if (0 == memcmp(src + i, oldstr, oldstrlen * sizeof(gunichar2))) {
364 ret = mono_string_new_size( mono_domain_get (), newsize);
365 dest = mono_string_chars(ret);
366 memcpy (dest, src, i * sizeof(gunichar2));
369 memcpy(dest + destpos, newstr, newstrlen * sizeof(gunichar2));
370 destpos += newstrlen;
374 } else if (ret != NULL) {
375 dest[destpos] = src[i];
388 ves_icall_System_String_InternalRemove (MonoString *me, gint32 sindex, gint32 count)
397 srclen = mono_string_length(me);
398 ret = mono_string_new_size( mono_domain_get (), srclen - count);
400 src = mono_string_chars(me);
401 dest = mono_string_chars(ret);
403 memcpy(dest, src, sindex * sizeof(gunichar2));
404 memcpy(dest + sindex, src + sindex + count, (srclen - count - sindex) * sizeof(gunichar2));
410 ves_icall_System_String_InternalCopyTo (MonoString *me, gint32 sindex, MonoArray *dest, gint32 dindex, gint32 count)
412 gunichar2 *destptr = (gunichar2 *) mono_array_addr(dest, gunichar2, dindex);
413 gunichar2 *src = mono_string_chars(me);
417 memcpy(destptr, src + sindex, sizeof(gunichar2) * count);
421 ves_icall_System_String_InternalSplit (MonoString *me, MonoArray *separator, gint32 count)
426 gint32 arrsize, srcsize, splitsize;
427 gint32 i, lastpos, arrpos;
429 gunichar2 *tmpstrptr;
435 src = mono_string_chars(me);
436 srcsize = mono_string_length(me);
437 arrsize = mono_array_length(separator);
439 cmpchar = mono_array_get(separator, gunichar2, 0);
442 for (i = 0; i != srcsize && splitsize < count; i++) {
443 if (string_icall_is_in_array(separator, arrsize, src[i]))
450 /* if no split chars found return the string */
451 if (splitsize == 0) {
452 retarr = mono_array_new(mono_domain_get(), mono_defaults.string_class, 1);
453 mono_array_set(retarr, MonoString *, 0, me);
458 if (splitsize != count)
461 retarr = mono_array_new(mono_domain_get(), mono_defaults.string_class, splitsize);
462 for (i = 0; i != srcsize && arrpos != count; i++) {
463 if (string_icall_is_in_array(separator, arrsize, src[i])) {
464 if (arrpos == count - 1)
465 tmpstrsize = srcsize - lastpos;
467 tmpstrsize = i - lastpos;
469 tmpstr = mono_string_new_size( mono_domain_get (), tmpstrsize);
470 tmpstrptr = mono_string_chars(tmpstr);
472 memcpy(tmpstrptr, src + lastpos, tmpstrsize * sizeof(gunichar2));
473 mono_array_set(retarr, MonoString *, arrpos, tmpstr);
479 if (arrpos < count) {
480 tmpstrsize = srcsize - lastpos;
481 tmpstr = mono_string_new_size( mono_domain_get (), tmpstrsize);
482 tmpstrptr = mono_string_chars(tmpstr);
484 memcpy(tmpstrptr, src + lastpos, tmpstrsize * sizeof(gunichar2));
485 mono_array_set(retarr, MonoString *, arrpos, tmpstr);
492 string_icall_is_in_array (MonoArray *chars, gint32 arraylength, gunichar2 chr)
497 for (arrpos = 0; arrpos != arraylength; arrpos++) {
498 cmpchar = mono_array_get(chars, gunichar2, arrpos);
507 ves_icall_System_String_InternalTrim (MonoString *me, MonoArray *chars, gint32 typ)
510 gunichar2 *src, *dest;
511 gint32 srclen, newlen, arrlen;
512 gint32 i, lenfirst, lenlast;
516 srclen = mono_string_length(me);
517 src = mono_string_chars(me);
518 arrlen = mono_array_length(chars);
523 if (0 == typ || 1 == typ) {
524 for (i = 0; i != srclen; i++) {
525 if (string_icall_is_in_array(chars, arrlen, src[i]))
532 if (0 == typ || 2 == typ) {
533 for (i = srclen - 1; i > lenfirst - 1; i--) {
534 if (string_icall_is_in_array(chars, arrlen, src[i]))
541 newlen = srclen - lenfirst - lenlast;
542 if (newlen == srclen)
545 ret = mono_string_new_size( mono_domain_get (), newlen);
546 dest = mono_string_chars(ret);
548 memcpy(dest, src + lenfirst, newlen *sizeof(gunichar2));
554 ves_icall_System_String_InternalIndexOf_Char (MonoString *me, gunichar2 value, gint32 sindex, gint32 count)
561 src = mono_string_chars(me);
562 for (pos = sindex; pos != count + sindex; pos++) {
563 if ( src [pos] == value)
571 ves_icall_System_String_InternalIndexOf_Str (MonoString *me, MonoString *value, gint32 sindex, gint32 count)
580 lencmpstr = mono_string_length(value);
582 src = mono_string_chars(me);
583 cmpstr = mono_string_chars(value);
586 for (pos = sindex; pos <= sindex + count; pos++) {
587 for (i = 0; src [pos + i] == cmpstr [i];) {
588 if (++i == lencmpstr)
597 ves_icall_System_String_InternalIndexOfAny (MonoString *me, MonoArray *arr, gint32 sindex, gint32 count)
606 arraysize = mono_array_length(arr);
607 src = mono_string_chars(me);
609 for (pos = sindex; pos != count + sindex; pos++) {
610 for (loop = 0; loop != arraysize; loop++)
611 if ( src [pos] == mono_array_get(arr, gunichar2, loop) )
619 ves_icall_System_String_InternalLastIndexOf_Char (MonoString *me, gunichar2 value, gint32 sindex, gint32 count)
626 src = mono_string_chars(me);
627 for (pos = sindex; pos > sindex - count; pos--) {
628 if (src [pos] == value)
636 ves_icall_System_String_InternalLastIndexOf_Str (MonoString *me, MonoString *value, gint32 sindex, gint32 count)
645 lencmpstr = mono_string_length(value);
647 src = mono_string_chars(me);
648 cmpstr = mono_string_chars(value);
650 for (pos = sindex - lencmpstr + 1; pos > sindex - count; pos--) {
651 if (0 == memcmp(src + pos, cmpstr, lencmpstr * sizeof(gunichar2)))
659 ves_icall_System_String_InternalLastIndexOfAny (MonoString *me, MonoArray *anyOf, gint32 sindex, gint32 count)
668 arraysize = mono_array_length(anyOf);
669 src = mono_string_chars(me);
671 for (pos = sindex; pos > sindex - count; pos--) {
672 for (loop = 0; loop != arraysize; loop++)
673 if ( src [pos] == mono_array_get(anyOf, gunichar2, loop) )
681 ves_icall_System_String_InternalPad (MonoString *me, gint32 width, gunichar2 chr, MonoBoolean right)
692 srclen = mono_string_length(me);
693 src = mono_string_chars(me);
695 ret = mono_string_new_size( mono_domain_get (), width);
696 dest = mono_string_chars(ret);
697 fillcount = width - srclen;
700 memcpy(dest, src, srclen * sizeof(gunichar2));
701 for (i = srclen; i != width; i++)
708 for (i = 0; i != fillcount; i++)
711 memcpy(dest + fillcount, src, srclen * sizeof(gunichar2));
717 ves_icall_System_String_InternalToLower (MonoString *me)
726 ret = mono_string_new_size(mono_domain_get (), mono_string_length(me));
728 src = mono_string_chars (me);
729 dest = mono_string_chars (ret);
731 for (i = 0; i < mono_string_length (me); ++i)
732 dest[i] = g_unichar_tolower(src[i]);
738 ves_icall_System_String_InternalToUpper (MonoString *me)
747 ret = mono_string_new_size(mono_domain_get (), mono_string_length(me));
749 src = mono_string_chars (me);
750 dest = mono_string_chars (ret);
752 for (i = 0; i < mono_string_length (me); ++i)
753 dest[i] = g_unichar_toupper(src[i]);
759 ves_icall_System_String_InternalAllocateStr (gint32 length)
763 return mono_string_new_size(mono_domain_get (), length);
767 ves_icall_System_String_InternalStrcpy_Str (MonoString *dest, gint32 destPos, MonoString *src)
774 srcptr = mono_string_chars (src);
775 destptr = mono_string_chars (dest);
777 memcpy(destptr + destPos, srcptr, mono_string_length(src) * sizeof(gunichar2));
781 ves_icall_System_String_InternalStrcpy_StrN (MonoString *dest, gint32 destPos, MonoString *src, gint32 startPos, gint32 count)
788 srcptr = mono_string_chars (src);
789 destptr = mono_string_chars (dest);
790 memcpy(destptr + destPos, srcptr + startPos, count * sizeof(gunichar2));
794 ves_icall_System_String_InternalIntern (MonoString *str)
798 return mono_string_intern(str);
802 ves_icall_System_String_InternalIsInterned (MonoString *str)
806 return mono_string_is_interned(str);
810 ves_icall_System_String_InternalCompareStr_N (MonoString *s1, gint32 i1, MonoString *s2, gint32 i2, gint32 length, gint32 mode)
812 /* c translation of C# code from old string.cs.. :) */
823 lenstr1 = mono_string_length(s1);
824 lenstr2 = mono_string_length(s2);
826 str1 = mono_string_chars(s1);
827 str2 = mono_string_chars(s2);
831 for (pos = 0; pos != length; pos++) {
832 if (i1 + pos >= lenstr1 || i2 + pos >= lenstr2)
835 charcmp = string_icall_cmp_char(str1[i1 + pos], str2[i2 + pos], mode);
840 /* the lesser wins, so if we have looped until length we just need to check the last char */
842 return string_icall_cmp_char(str1[i1 + pos - 1], str2[i2 + pos - 1], mode);
845 /* Test if one the strings has been compared to the end */
846 if (i1 + pos >= lenstr1) {
847 if (i2 + pos >= lenstr2)
851 } else if (i2 + pos >= lenstr2)
854 /* if not, check our last char only.. (can this happen?) */
855 return string_icall_cmp_char(str1[i1 + pos], str2[i2 + pos], mode);
859 ves_icall_System_String_GetHashCode (MonoString *me)
862 gunichar2 *data = mono_string_chars (me);
866 for (i = 0; i < mono_string_length (me); ++i)
867 h = (h << 5) - h + data [i];
873 ves_icall_System_String_get_Chars (MonoString *me, gint32 idx)
877 if ((idx < 0) || (idx >= mono_string_length (me)))
878 mono_raise_exception (mono_get_exception_index_out_of_range ());
879 return mono_string_chars(me)[idx];
884 * 0 = StringCompareModeDirect
885 * 1 = StringCompareModeCaseInsensitive
886 * 2 = StringCompareModeOrdinal
889 string_icall_cmp_char (gunichar2 c1, gunichar2 c2, gint32 mode)
892 GUnicodeType c1type, c2type;
894 c1type = g_unichar_type (c1);
895 c2type = g_unichar_type (c2);
899 /* TODO: compare with culture info */
900 if (c1type == G_UNICODE_UPPERCASE_LETTER && c2type == G_UNICODE_LOWERCASE_LETTER)
903 if (c1type == G_UNICODE_LOWERCASE_LETTER && c2type == G_UNICODE_UPPERCASE_LETTER)
906 result = (gint32) c1 - c2;
909 result = (gint32) (c1type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c1) : c1) -
910 (c2type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c2) : c2);
913 // Rotor/ms return the full value just not -1 and 1
914 return (gint32) c1 - c2; break;
917 return ((result < 0) ? -1 : (result > 0) ? 1 : 0);