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)
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)))
348 newsize = srclen + ((newstrlen - oldstrlen) * occurr);
355 if (0 == memcmp(src + i, oldstr, oldstrlen * sizeof(gunichar2))) {
357 ret = mono_string_new_size( mono_domain_get (), newsize);
358 dest = mono_string_chars(ret);
359 memcpy (dest, src, i * sizeof(gunichar2));
362 memcpy(dest + destpos, newstr, newstrlen * sizeof(gunichar2));
363 destpos += newstrlen;
367 } else if (ret != NULL) {
368 dest[destpos] = src[i];
381 ves_icall_System_String_InternalRemove (MonoString *me, gint32 sindex, gint32 count)
390 srclen = mono_string_length(me);
391 ret = mono_string_new_size( mono_domain_get (), srclen - count);
393 src = mono_string_chars(me);
394 dest = mono_string_chars(ret);
396 memcpy(dest, src, sindex * sizeof(gunichar2));
397 memcpy(dest + sindex, src + sindex + count, (srclen - count - sindex) * sizeof(gunichar2));
403 ves_icall_System_String_InternalCopyTo (MonoString *me, gint32 sindex, MonoArray *dest, gint32 dindex, gint32 count)
405 gunichar2 *destptr = (gunichar2 *) mono_array_addr(dest, gunichar2, dindex);
406 gunichar2 *src = mono_string_chars(me);
410 memcpy(destptr, src + sindex, sizeof(gunichar2) * count);
414 ves_icall_System_String_InternalSplit (MonoString *me, MonoArray *separator, gint32 count)
419 gint32 arrsize, srcsize, splitsize;
420 gint32 i, lastpos, arrpos;
422 gunichar2 *tmpstrptr;
428 src = mono_string_chars(me);
429 srcsize = mono_string_length(me);
430 arrsize = mono_array_length(separator);
432 cmpchar = mono_array_get(separator, gunichar2, 0);
435 for (i = 0; i != srcsize && splitsize < count; i++) {
436 if (string_icall_is_in_array(separator, arrsize, src[i]))
443 /* if no split chars found return the string */
444 if (splitsize == 0) {
445 retarr = mono_array_new(mono_domain_get(), mono_defaults.string_class, 1);
446 tmpstr = mono_string_new_size( mono_domain_get (), srcsize);
447 tmpstrptr = mono_string_chars(tmpstr);
449 memcpy(tmpstrptr, src, srcsize * sizeof(gunichar2));
450 mono_array_set(retarr, MonoString *, 0, tmpstr);
455 if (splitsize != count)
458 retarr = mono_array_new(mono_domain_get(), mono_defaults.string_class, splitsize);
459 for (i = 0; i != srcsize && arrpos != count; i++) {
460 if (string_icall_is_in_array(separator, arrsize, src[i])) {
461 if (arrpos == count - 1)
462 tmpstrsize = srcsize - lastpos;
464 tmpstrsize = i - lastpos;
466 tmpstr = mono_string_new_size( mono_domain_get (), tmpstrsize);
467 tmpstrptr = mono_string_chars(tmpstr);
469 memcpy(tmpstrptr, src + lastpos, tmpstrsize * sizeof(gunichar2));
470 mono_array_set(retarr, MonoString *, arrpos, tmpstr);
476 if (arrpos < count) {
477 tmpstrsize = srcsize - lastpos;
478 tmpstr = mono_string_new_size( mono_domain_get (), tmpstrsize);
479 tmpstrptr = mono_string_chars(tmpstr);
481 memcpy(tmpstrptr, src + lastpos, tmpstrsize * sizeof(gunichar2));
482 mono_array_set(retarr, MonoString *, arrpos, tmpstr);
489 string_icall_is_in_array (MonoArray *chars, gint32 arraylength, gunichar2 chr)
494 for (arrpos = 0; arrpos != arraylength; arrpos++) {
495 cmpchar = mono_array_get(chars, gunichar2, arrpos);
504 ves_icall_System_String_InternalTrim (MonoString *me, MonoArray *chars, gint32 typ)
507 gunichar2 *src, *dest;
508 gint32 srclen, newlen, arrlen;
509 gint32 i, lenfirst, lenlast;
513 srclen = mono_string_length(me);
514 src = mono_string_chars(me);
515 arrlen = mono_array_length(chars);
520 if (0 == typ || 1 == typ) {
521 for (i = 0; i != srclen; i++) {
522 if (string_icall_is_in_array(chars, arrlen, src[i]))
529 if (0 == typ || 2 == typ) {
530 for (i = srclen - 1; i > lenfirst - 1; i--) {
531 if (string_icall_is_in_array(chars, arrlen, src[i]))
538 newlen = srclen - lenfirst - lenlast;
540 ret = mono_string_new_size( mono_domain_get (), newlen);
541 dest = mono_string_chars(ret);
543 memcpy(dest, src + lenfirst, newlen *sizeof(gunichar2));
549 ves_icall_System_String_InternalIndexOf_Char (MonoString *me, gunichar2 value, gint32 sindex, gint32 count)
556 src = mono_string_chars(me);
557 for (pos = sindex; pos != count + sindex; pos++) {
558 if ( src [pos] == value)
566 ves_icall_System_String_InternalIndexOf_Str (MonoString *me, MonoString *value, gint32 sindex, gint32 count)
575 lencmpstr = mono_string_length(value);
577 src = mono_string_chars(me);
578 cmpstr = mono_string_chars(value);
581 for (pos = sindex; pos <= sindex + count; pos++) {
582 for (i = 0; src [pos + i] == cmpstr [i];) {
583 if (++i == lencmpstr)
592 ves_icall_System_String_InternalIndexOfAny (MonoString *me, MonoArray *arr, gint32 sindex, gint32 count)
601 arraysize = mono_array_length(arr);
602 src = mono_string_chars(me);
604 for (pos = sindex; pos != count + sindex; pos++) {
605 for (loop = 0; loop != arraysize; loop++)
606 if ( src [pos] == mono_array_get(arr, gunichar2, loop) )
614 ves_icall_System_String_InternalLastIndexOf_Char (MonoString *me, gunichar2 value, gint32 sindex, gint32 count)
621 src = mono_string_chars(me);
622 for (pos = sindex; pos > sindex - count; pos--) {
623 if (src [pos] == value)
631 ves_icall_System_String_InternalLastIndexOf_Str (MonoString *me, MonoString *value, gint32 sindex, gint32 count)
640 lencmpstr = mono_string_length(value);
642 src = mono_string_chars(me);
643 cmpstr = mono_string_chars(value);
645 for (pos = sindex - lencmpstr + 1; pos > sindex - count; pos--) {
646 if (0 == memcmp(src + pos, cmpstr, lencmpstr * sizeof(gunichar2)))
654 ves_icall_System_String_InternalLastIndexOfAny (MonoString *me, MonoArray *anyOf, gint32 sindex, gint32 count)
663 arraysize = mono_array_length(anyOf);
664 src = mono_string_chars(me);
666 for (pos = sindex; pos > sindex - count; pos--) {
667 for (loop = 0; loop != arraysize; loop++)
668 if ( src [pos] == mono_array_get(anyOf, gunichar2, loop) )
676 ves_icall_System_String_InternalPad (MonoString *me, gint32 width, gunichar2 chr, MonoBoolean right)
687 srclen = mono_string_length(me);
688 src = mono_string_chars(me);
690 ret = mono_string_new_size( mono_domain_get (), width);
691 dest = mono_string_chars(ret);
692 fillcount = width - srclen;
695 memcpy(dest, src, srclen * sizeof(gunichar2));
696 for (i = srclen; i != width; i++)
703 for (i = 0; i != fillcount; i++)
706 memcpy(dest + fillcount, src, srclen * sizeof(gunichar2));
712 ves_icall_System_String_InternalToLower (MonoString *me)
721 ret = mono_string_new_size(mono_domain_get (), mono_string_length(me));
723 src = mono_string_chars (me);
724 dest = mono_string_chars (ret);
726 for (i = 0; i < mono_string_length (me); ++i)
727 dest[i] = g_unichar_tolower(src[i]);
733 ves_icall_System_String_InternalToUpper (MonoString *me)
742 ret = mono_string_new_size(mono_domain_get (), mono_string_length(me));
744 src = mono_string_chars (me);
745 dest = mono_string_chars (ret);
747 for (i = 0; i < mono_string_length (me); ++i)
748 dest[i] = g_unichar_toupper(src[i]);
754 ves_icall_System_String_InternalAllocateStr (gint32 length)
758 return mono_string_new_size(mono_domain_get (), length);
762 ves_icall_System_String_InternalStrcpy_Str (MonoString *dest, gint32 destPos, MonoString *src)
769 srcptr = mono_string_chars (src);
770 destptr = mono_string_chars (dest);
772 memcpy(destptr + destPos, srcptr, mono_string_length(src) * sizeof(gunichar2));
776 ves_icall_System_String_InternalStrcpy_StrN (MonoString *dest, gint32 destPos, MonoString *src, gint32 startPos, gint32 count)
783 srcptr = mono_string_chars (src);
784 destptr = mono_string_chars (dest);
785 memcpy(destptr + destPos, srcptr + startPos, count * sizeof(gunichar2));
789 ves_icall_System_String_InternalIntern (MonoString *str)
793 return mono_string_intern(str);
797 ves_icall_System_String_InternalIsInterned (MonoString *str)
801 return mono_string_is_interned(str);
805 ves_icall_System_String_InternalCompareStr_N (MonoString *s1, gint32 i1, MonoString *s2, gint32 i2, gint32 length, gint32 mode)
807 /* c translation of C# code from old string.cs.. :) */
818 lenstr1 = mono_string_length(s1);
819 lenstr2 = mono_string_length(s2);
821 str1 = mono_string_chars(s1);
822 str2 = mono_string_chars(s2);
826 for (pos = 0; pos != length; pos++) {
827 if (i1 + pos >= lenstr1 || i2 + pos >= lenstr2)
830 charcmp = string_icall_cmp_char(str1[i1 + pos], str2[i2 + pos], mode);
835 /* the lesser wins, so if we have looped until length we just need to check the last char */
837 return string_icall_cmp_char(str1[i1 + pos - 1], str2[i2 + pos - 1], mode);
840 /* Test if one the strings has been compared to the end */
841 if (i1 + pos >= lenstr1) {
842 if (i2 + pos >= lenstr2)
846 } else if (i2 + pos >= lenstr2)
849 /* if not, check our last char only.. (can this happen?) */
850 return string_icall_cmp_char(str1[i1 + pos], str2[i2 + pos], mode);
854 ves_icall_System_String_GetHashCode (MonoString *me)
857 gunichar2 *data = mono_string_chars (me);
861 for (i = 0; i < mono_string_length (me); ++i)
862 h = (h << 5) - h + data [i];
868 ves_icall_System_String_get_Chars (MonoString *me, gint32 idx)
872 if ((idx < 0) || (idx >= mono_string_length (me)))
873 mono_raise_exception (mono_get_exception_index_out_of_range ());
874 return mono_string_chars(me)[idx];
879 * 0 = StringCompareModeDirect
880 * 1 = StringCompareModeCaseInsensitive
881 * 2 = StringCompareModeOrdinal
884 string_icall_cmp_char (gunichar2 c1, gunichar2 c2, gint32 mode)
887 GUnicodeType c1type, c2type;
889 c1type = g_unichar_type (c1);
890 c2type = g_unichar_type (c2);
894 /* TODO: compare with culture info */
895 if (c1type == G_UNICODE_UPPERCASE_LETTER && c2type == G_UNICODE_LOWERCASE_LETTER)
898 if (c1type == G_UNICODE_LOWERCASE_LETTER && c2type == G_UNICODE_UPPERCASE_LETTER)
901 result = (gint32) c1 - c2;
904 result = (gint32) (c1type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c1) : c1) -
905 (c2type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c2) : c2);
908 // Rotor/ms return the full value just not -1 and 1
909 return (gint32) c1 - c2; break;
912 return ((result < 0) ? -1 : (result > 0) ? 1 : 0);