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)
61 domain = mono_domain_get ();
62 res = mono_string_new_size (domain, count);
64 chars = mono_string_chars (res);
65 for (i = 0; i < count; i++)
72 ves_icall_System_String_ctor_charp_int_int (gpointer dummy, gunichar2 *value, gint32 sindex, gint32 length)
79 domain = mono_domain_get ();
81 if ((value == NULL) && (length != 0))
82 mono_raise_exception (mono_get_exception_argument_out_of_range ("Out of range"));
84 if ((sindex < 0) || (length < 0))
85 mono_raise_exception (mono_get_exception_argument_out_of_range ("Out of range"));
87 if (length == 0) { /* fixme: return String.Empty here */
88 g_warning ("string doesn't yet support empy strings in char* constructor");
89 g_assert_not_reached ();
92 begin = (gunichar2 *) (value + sindex);
94 return mono_string_new_utf16 (domain, begin, length);
98 ves_icall_System_String_ctor_sbytep (gpointer dummy, gint8 *value)
104 domain = mono_domain_get ();
106 if (NULL == value) { /* fixme: return String.Empty here */
107 g_warning ("string doesn't yet support empy strings in char* constructor");
108 g_assert_not_reached ();
111 return mono_string_new (domain, (const char *) value);
115 ves_icall_System_String_ctor_sbytep_int_int (gpointer dummy, gint8 *value, gint32 sindex, gint32 length)
125 domain = mono_domain_get ();
127 if ((value == NULL) && (length != 0))
128 mono_raise_exception (mono_get_exception_argument_out_of_range ("Out of range"));
130 if ((sindex < 0) || (length < 0))
131 mono_raise_exception (mono_get_exception_argument_out_of_range ("Out of range"));
133 begin = (guchar *) (value + sindex);
134 res = mono_string_new_size (domain, length);
135 chars = mono_string_chars (res);
136 for (i = 0; i < length; ++i)
137 chars [i] = begin [i];
143 ves_icall_System_String_ctor_chara (gpointer dummy, MonoArray *value)
149 MONO_CHECK_ARG_NULL (value);
151 domain = mono_domain_get ();
153 return mono_string_new_utf16 (domain, (gunichar2 *) mono_array_addr(value, gunichar2, 0), value->max_length);
157 ves_icall_System_String_ctor_chara_int_int (gpointer dummy, MonoArray *value,
158 gint32 sindex, gint32 length)
164 MONO_CHECK_ARG_NULL (value);
166 domain = mono_domain_get ();
168 return mono_string_new_utf16 (domain, (gunichar2 *) mono_array_addr(value, gunichar2, sindex), length);
172 ves_icall_System_String_ctor_encoding (gpointer dummy, gint8 *value, gint32 sindex,
173 gint32 length, MonoObject *enc)
177 g_warning("string.ctor with encoding obj unimplemented");
178 g_assert_not_reached ();
183 ves_icall_System_String_InternalEquals (MonoString *str1, MonoString *str2)
191 /* Length checking is done in C# */
192 str1len = mono_string_length(str1);
194 str1ptr = mono_string_chars(str1);
195 str2ptr = mono_string_chars(str2);
197 return (0 == memcmp(str1ptr, str2ptr, str1len * sizeof(gunichar2)));
201 ves_icall_System_String_InternalJoin (MonoString *separator, MonoArray * value, gint32 sindex, gint32 count)
215 insert = mono_string_chars(separator);
216 insertlen = mono_string_length(separator);
219 for (pos = sindex; pos != sindex + count; pos++) {
220 length += mono_string_length(mono_array_get(value, MonoString *, pos));
221 if (pos < sindex + count - 1)
225 ret = mono_string_new_size( mono_domain_get (), length);
226 dest = mono_string_chars(ret);
229 for (pos = sindex; pos != sindex + count; pos++) {
230 src = mono_string_chars(mono_array_get(value, MonoString *, pos));
231 srclen = mono_string_length(mono_array_get(value, MonoString *, pos));
233 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_InternalReplace_Str (MonoString *me, MonoString *oldValue, MonoString *newValue)
319 oldstr = mono_string_chars(oldValue);
320 oldstrlen = mono_string_length(oldValue);
322 if (NULL != newValue) {
323 newstr = mono_string_chars(newValue);
324 newstrlen = mono_string_length(newValue);
328 src = mono_string_chars(me);
329 srclen = mono_string_length(me);
331 if (oldstrlen != newstrlen) {
332 for (i = 0; i <= srclen - oldstrlen; i++)
333 if (0 == memcmp(src + i, oldstr, oldstrlen * sizeof(gunichar2)))
336 newsize = srclen + ((newstrlen - oldstrlen) * occurr);
340 ret = mono_string_new_size( mono_domain_get (), newsize);
341 dest = mono_string_chars(ret);
345 if (0 == memcmp(src + i, oldstr, oldstrlen * sizeof(gunichar2))) {
347 memcpy(dest + destpos, newstr, newstrlen * sizeof(gunichar2));
348 destpos += newstrlen;
352 dest[destpos] = src[i];
362 ves_icall_System_String_InternalRemove (MonoString *me, gint32 sindex, gint32 count)
371 srclen = mono_string_length(me);
372 ret = mono_string_new_size( mono_domain_get (), srclen - count);
374 src = mono_string_chars(me);
375 dest = mono_string_chars(ret);
377 memcpy(dest, src, sindex * sizeof(gunichar2));
378 memcpy(dest + sindex, src + sindex + count, (srclen - count - sindex) * sizeof(gunichar2));
384 ves_icall_System_String_InternalCopyTo (MonoString *me, gint32 sindex, MonoArray *dest, gint32 dindex, gint32 count)
386 gunichar2 *destptr = (gunichar2 *) mono_array_addr(dest, gunichar2, dindex);
387 gunichar2 *src = mono_string_chars(me);
391 memcpy(destptr, src + sindex, sizeof(gunichar2) * count);
395 ves_icall_System_String_InternalSplit (MonoString *me, MonoArray *separator, gint32 count)
400 gint32 arrsize, srcsize, splitsize;
401 gint32 i, lastpos, arrpos;
403 gunichar2 *tmpstrptr;
409 src = mono_string_chars(me);
410 srcsize = mono_string_length(me);
411 arrsize = mono_array_length(separator);
413 cmpchar = mono_array_get(separator, gunichar2, 0);
416 for (i = 0; i != srcsize && splitsize < count; i++) {
417 if (string_icall_is_in_array(separator, arrsize, src[i]))
424 /* if no split chars found return the string */
425 if (splitsize == 0) {
426 retarr = mono_array_new(mono_domain_get(), mono_defaults.string_class, 1);
427 tmpstr = mono_string_new_size( mono_domain_get (), srcsize);
428 tmpstrptr = mono_string_chars(tmpstr);
430 memcpy(tmpstrptr, src, srcsize * sizeof(gunichar2));
431 mono_array_set(retarr, MonoString *, 0, tmpstr);
436 if (splitsize != count)
439 retarr = mono_array_new(mono_domain_get(), mono_defaults.string_class, splitsize);
440 for (i = 0; i != srcsize && arrpos != count; i++) {
441 if (string_icall_is_in_array(separator, arrsize, src[i])) {
442 if (arrpos == count - 1)
443 tmpstrsize = srcsize - lastpos;
445 tmpstrsize = i - lastpos;
447 tmpstr = mono_string_new_size( mono_domain_get (), tmpstrsize);
448 tmpstrptr = mono_string_chars(tmpstr);
450 memcpy(tmpstrptr, src + lastpos, tmpstrsize * sizeof(gunichar2));
451 mono_array_set(retarr, MonoString *, arrpos, tmpstr);
457 if (arrpos < count) {
458 tmpstrsize = srcsize - lastpos;
459 tmpstr = mono_string_new_size( mono_domain_get (), tmpstrsize);
460 tmpstrptr = mono_string_chars(tmpstr);
462 memcpy(tmpstrptr, src + lastpos, tmpstrsize * sizeof(gunichar2));
463 mono_array_set(retarr, MonoString *, arrpos, tmpstr);
470 string_icall_is_in_array (MonoArray *chars, gint32 arraylength, gunichar2 chr)
475 for (arrpos = 0; arrpos != arraylength; arrpos++) {
476 cmpchar = mono_array_get(chars, gunichar2, arrpos);
485 ves_icall_System_String_InternalTrim (MonoString *me, MonoArray *chars, gint32 typ)
488 gunichar2 *src, *dest;
489 gint32 srclen, newlen, arrlen;
490 gint32 i, lenfirst, lenlast;
494 srclen = mono_string_length(me);
495 src = mono_string_chars(me);
496 arrlen = mono_array_length(chars);
501 if (0 == typ || 1 == typ) {
502 for (i = 0; i != srclen; i++) {
503 if (string_icall_is_in_array(chars, arrlen, src[i]))
510 if (0 == typ || 2 == typ) {
511 for (i = srclen - 1; i > lenfirst - 1; i--) {
512 if (string_icall_is_in_array(chars, arrlen, src[i]))
519 newlen = srclen - lenfirst - lenlast;
521 ret = mono_string_new_size( mono_domain_get (), newlen);
522 dest = mono_string_chars(ret);
524 memcpy(dest, src + lenfirst, newlen *sizeof(gunichar2));
530 ves_icall_System_String_InternalIndexOf_Char (MonoString *me, gunichar2 value, gint32 sindex, gint32 count)
537 src = mono_string_chars(me);
538 for (pos = sindex; pos != count + sindex; pos++) {
539 if ( src [pos] == value)
547 ves_icall_System_String_InternalIndexOf_Str (MonoString *me, MonoString *value, gint32 sindex, gint32 count)
556 lencmpstr = mono_string_length(value);
558 src = mono_string_chars(me);
559 cmpstr = mono_string_chars(value);
562 for (pos = sindex; pos <= sindex + count; pos++) {
563 for (i = 0; src [pos + i] == cmpstr [i];) {
564 if (++i == lencmpstr)
573 ves_icall_System_String_InternalIndexOfAny (MonoString *me, MonoArray *arr, gint32 sindex, gint32 count)
582 arraysize = mono_array_length(arr);
583 src = mono_string_chars(me);
585 for (pos = sindex; pos != count + sindex; pos++) {
586 for (loop = 0; loop != arraysize; loop++)
587 if ( src [pos] == mono_array_get(arr, gunichar2, loop) )
595 ves_icall_System_String_InternalLastIndexOf_Char (MonoString *me, gunichar2 value, gint32 sindex, gint32 count)
602 src = mono_string_chars(me);
603 for (pos = sindex; pos > sindex - count; pos--) {
604 if (src [pos] == value)
612 ves_icall_System_String_InternalLastIndexOf_Str (MonoString *me, MonoString *value, gint32 sindex, gint32 count)
621 lencmpstr = mono_string_length(value);
623 src = mono_string_chars(me);
624 cmpstr = mono_string_chars(value);
626 for (pos = sindex - lencmpstr + 1; pos > sindex - count; pos--) {
627 if (0 == memcmp(src + pos, cmpstr, lencmpstr * sizeof(gunichar2)))
635 ves_icall_System_String_InternalLastIndexOfAny (MonoString *me, MonoArray *anyOf, gint32 sindex, gint32 count)
644 arraysize = mono_array_length(anyOf);
645 src = mono_string_chars(me);
647 for (pos = sindex; pos > sindex - count; pos--) {
648 for (loop = 0; loop != arraysize; loop++)
649 if ( src [pos] == mono_array_get(anyOf, gunichar2, loop) )
657 ves_icall_System_String_InternalPad (MonoString *me, gint32 width, gunichar2 chr, MonoBoolean right)
668 srclen = mono_string_length(me);
669 src = mono_string_chars(me);
671 ret = mono_string_new_size( mono_domain_get (), width);
672 dest = mono_string_chars(ret);
673 fillcount = width - srclen;
676 memcpy(dest, src, srclen * sizeof(gunichar2));
677 for (i = srclen; i != width; i++)
684 for (i = 0; i != fillcount; i++)
687 memcpy(dest + fillcount, src, srclen * sizeof(gunichar2));
693 ves_icall_System_String_InternalToLower (MonoString *me)
702 ret = mono_string_new_size(mono_domain_get (), mono_string_length(me));
704 src = mono_string_chars (me);
705 dest = mono_string_chars (ret);
707 for (i = 0; i < mono_string_length (me); ++i)
708 dest[i] = g_unichar_tolower(src[i]);
714 ves_icall_System_String_InternalToUpper (MonoString *me)
723 ret = mono_string_new_size(mono_domain_get (), mono_string_length(me));
725 src = mono_string_chars (me);
726 dest = mono_string_chars (ret);
728 for (i = 0; i < mono_string_length (me); ++i)
729 dest[i] = g_unichar_toupper(src[i]);
735 ves_icall_System_String_InternalAllocateStr (gint32 length)
739 return mono_string_new_size(mono_domain_get (), length);
743 ves_icall_System_String_InternalStrcpy_Str (MonoString *dest, gint32 destPos, MonoString *src)
750 srcptr = mono_string_chars (src);
751 destptr = mono_string_chars (dest);
753 memcpy(destptr + destPos, srcptr, mono_string_length(src) * sizeof(gunichar2));
757 ves_icall_System_String_InternalStrcpy_StrN (MonoString *dest, gint32 destPos, MonoString *src, gint32 startPos, gint32 count)
764 srcptr = mono_string_chars (src);
765 destptr = mono_string_chars (dest);
766 memcpy(destptr + destPos, srcptr + startPos, count * sizeof(gunichar2));
770 ves_icall_System_String_InternalIntern (MonoString *str)
774 return mono_string_intern(str);
778 ves_icall_System_String_InternalIsInterned (MonoString *str)
782 return mono_string_is_interned(str);
786 ves_icall_System_String_InternalCompareStr_N (MonoString *s1, gint32 i1, MonoString *s2, gint32 i2, gint32 length, gint32 mode)
788 /* c translation of C# code from old string.cs.. :) */
799 lenstr1 = mono_string_length(s1);
800 lenstr2 = mono_string_length(s2);
802 str1 = mono_string_chars(s1);
803 str2 = mono_string_chars(s2);
807 for (pos = 0; pos != length; pos++) {
808 if (i1 + pos >= lenstr1 || i2 + pos >= lenstr2)
811 charcmp = string_icall_cmp_char(str1[i1 + pos], str2[i2 + pos], mode);
816 /* the lesser wins, so if we have looped until length we just need to check the last char */
818 return string_icall_cmp_char(str1[i1 + pos - 1], str2[i2 + pos - 1], mode);
821 /* Test if one the strings has been compared to the end */
822 if (i1 + pos >= lenstr1) {
823 if (i2 + pos >= lenstr2)
827 } else if (i2 + pos >= lenstr2)
830 /* if not, check our last char only.. (can this happen?) */
831 return string_icall_cmp_char(str1[i1 + pos], str2[i2 + pos], mode);
835 ves_icall_System_String_GetHashCode (MonoString *me)
838 gunichar2 *data = mono_string_chars (me);
842 for (i = 0; i < mono_string_length (me); ++i)
843 h = (h << 5) - h + data [i];
849 ves_icall_System_String_get_Chars (MonoString *me, gint32 idx)
853 if ((idx < 0) || (idx >= mono_string_length (me)))
854 mono_raise_exception (mono_get_exception_index_out_of_range ());
855 return mono_string_chars(me)[idx];
858 /* @mode : 0 = StringCompareModeDirect
859 1 = StringCompareModeCaseInsensitive
860 2 = StringCompareModeOrdinal
863 string_icall_cmp_char (gunichar2 c1, gunichar2 c2, gint32 mode)
866 GUnicodeType c1type, c2type;
868 c1type = g_unichar_type (c1);
869 c2type = g_unichar_type (c2);
873 /* TODO: compare with culture info */
874 if (c1type == G_UNICODE_UPPERCASE_LETTER && c2type == G_UNICODE_LOWERCASE_LETTER)
877 if (c1type == G_UNICODE_LOWERCASE_LETTER && c2type == G_UNICODE_UPPERCASE_LETTER)
880 result = (gint32) c1 - c2;
883 result = (gint32) (c1type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c1) : c1) -
884 (c2type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c2) : c2);
887 // Rotor/ms return the full value just not -1 and 1
888 return (gint32) c1 - c2; break;
891 return ((result < 0) ? -1 : (result > 0) ? 1 : 0);