* RegistryKeyTest.cs: Modified GetValueNamesTest and GetSubKeyNamesTest
[mono.git] / mono / metadata / string-icalls.c
1 /*
2  * string-icalls.c: String internal calls for the corlib
3  *
4  * Author:
5  *   Patrik Torstensson (patrik.torstensson@labs2.com)
6  *   Duncan Mak  (duncan@ximian.com)
7  *
8  * (C) 2001 Ximian, Inc.
9  */
10 #include <config.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <signal.h>
14 #include <string.h>
15 #include <mono/metadata/string-icalls.h>
16 #include <mono/metadata/class-internals.h>
17 #include <mono/metadata/appdomain.h>
18 #include <mono/metadata/tabledefs.h>
19 #include <mono/metadata/loader.h>
20 #include <mono/metadata/object.h>
21 #include <mono/metadata/unicode.h>
22 #include <mono/metadata/exception.h>
23 #include <mono/metadata/debug-helpers.h>
24
25 /* Internal helper methods */
26
27 static gboolean
28 string_icall_is_in_array (MonoArray *chars, gint32 arraylength, gunichar2 chr);
29
30 static MonoString*
31 empty_string (MonoDomain *domain)
32 {
33         MonoVTable *vtable = mono_class_vtable (domain, mono_defaults.string_class);
34         MonoObject *o;
35         static MonoClassField *empty_field = NULL;
36
37         if (!empty_field) {
38                 MonoClassField *field;
39                 gpointer iter;
40
41                 iter = NULL;
42                 while ((field = mono_class_get_fields (mono_defaults.string_class, &iter))) {
43                         if (!strcmp (field->name, "Empty"))
44                                 break;
45                 }
46
47                 g_assert (field);
48                 empty_field = field;
49         }
50
51         mono_field_static_get_value (vtable, empty_field, &o);
52         g_assert (o);
53         return (MonoString*)o;
54 }
55
56 MonoString *
57 ves_icall_System_String_ctor_charp (gpointer dummy, gunichar2 *value)
58 {
59         gint32 i, length;
60         MonoDomain *domain;
61
62         MONO_ARCH_SAVE_REGS;
63
64         domain = mono_domain_get ();
65
66         if (value == NULL)
67                 return empty_string (domain);
68
69         for (i = 0; *(value + i) != '\0'; i++);
70         length = i;
71
72         return mono_string_new_utf16 (domain, value, length);
73 }
74
75 MonoString *
76 ves_icall_System_String_ctor_char_int (gpointer dummy, gunichar2 value, gint32 count)
77 {
78         MonoDomain *domain;
79         MonoString *res;
80         gunichar2 *chars;
81         gint32 i;
82
83         MONO_ARCH_SAVE_REGS;
84
85         if (count < 0)
86                 mono_raise_exception (mono_get_exception_argument_out_of_range ("count"));
87
88         domain = mono_domain_get ();
89         res = mono_string_new_size (domain, count);
90
91         chars = mono_string_chars (res);
92         for (i = 0; i < count; i++)
93                 chars [i] = value;
94         
95         return res;
96 }
97
98 MonoString *
99 ves_icall_System_String_ctor_charp_int_int (gpointer dummy, gunichar2 *value, gint32 sindex, gint32 length)
100 {
101         gunichar2 *begin;
102         MonoDomain * domain;
103         
104         MONO_ARCH_SAVE_REGS;
105
106         domain = mono_domain_get ();
107
108         if ((value == NULL) && (length != 0))
109                 mono_raise_exception (mono_get_exception_argument_out_of_range ("Out of range"));
110
111         if ((sindex < 0) || (length < 0))
112                 mono_raise_exception (mono_get_exception_argument_out_of_range ("Out of range"));
113         
114         if (length == 0)
115                 return empty_string (domain);
116         
117         begin = (gunichar2 *) (value + sindex);
118
119         return mono_string_new_utf16 (domain, begin, length);
120 }
121
122 MonoString *
123 ves_icall_System_String_ctor_sbytep (gpointer dummy, gint8 *value)
124 {
125         MonoDomain *domain;
126         
127         MONO_ARCH_SAVE_REGS;
128
129         domain = mono_domain_get ();
130
131         if (NULL == value)
132                 return empty_string (domain);
133
134         return mono_string_new (domain, (const char *) value);
135 }
136
137 MonoString *
138 ves_icall_System_String_ctor_sbytep_int_int (gpointer dummy, gint8 *value, gint32 sindex, gint32 length)
139 {
140         guchar *begin;
141         MonoDomain *domain;
142         MonoString *res;
143         gunichar2 *chars;
144         int i;
145         
146         MONO_ARCH_SAVE_REGS;
147
148         domain = mono_domain_get ();
149
150         if ((value == NULL) && (length != 0))
151                 mono_raise_exception (mono_get_exception_argument_out_of_range ("Out of range"));
152
153         if ((sindex < 0) || (length < 0))
154                 mono_raise_exception (mono_get_exception_argument_out_of_range ("Out of range"));
155
156         begin = (guchar *) (value + sindex);
157         res = mono_string_new_size (domain, length);
158         chars = mono_string_chars (res);
159         for (i = 0; i < length; ++i)
160                 chars [i] = begin [i];
161
162         return res;
163 }
164
165 MonoString *
166 ves_icall_System_String_ctor_chara (gpointer dummy, MonoArray *value)
167 {
168         MonoDomain *domain;
169
170         MONO_ARCH_SAVE_REGS;
171
172         domain = mono_domain_get ();
173
174         if (value == NULL)
175                 return mono_string_new_utf16 (domain, NULL, 0);
176         else
177                 return mono_string_new_utf16 (domain, (gunichar2 *) mono_array_addr(value, gunichar2, 0),  value->max_length);
178 }
179
180 MonoString *
181 ves_icall_System_String_ctor_chara_int_int (gpointer dummy, MonoArray *value, 
182                                          gint32 sindex, gint32 length)
183 {
184         MonoDomain *domain;
185
186         MONO_ARCH_SAVE_REGS;
187
188         if (value == NULL)
189                 mono_raise_exception (mono_get_exception_argument_null ("value"));
190         if (sindex < 0)
191                 mono_raise_exception (mono_get_exception_argument_out_of_range ("startIndex"));         
192         if (length < 0)
193                 mono_raise_exception (mono_get_exception_argument_out_of_range ("length"));
194         if (sindex + length > mono_array_length (value))
195                 mono_raise_exception (mono_get_exception_argument_out_of_range ("Out of range"));
196
197         domain = mono_domain_get ();
198         
199         return mono_string_new_utf16 (domain, (gunichar2 *) mono_array_addr(value, gunichar2, sindex), length);
200 }
201
202 MonoString *
203 ves_icall_System_String_ctor_encoding (gpointer dummy, gint8 *value, gint32 sindex, 
204                                        gint32 length, MonoObject *enc)
205 {
206         MonoArray *arr;
207         MonoString *s;
208         MonoObject *exc;
209         MonoDomain *domain = mono_domain_get ();
210         MonoMethod *get_string;
211         gpointer args [1];
212         MonoClass *klass;
213
214         MONO_ARCH_SAVE_REGS;
215
216         if ((value == NULL) || (length == 0))
217                 return mono_string_new_size (mono_domain_get (), 0);
218         if (enc == NULL)
219                 mono_raise_exception (mono_get_exception_argument_null ("enc"));
220         if (sindex < 0)
221                 mono_raise_exception (mono_get_exception_argument_out_of_range ("startIndex"));         
222         if (length < 0)
223                 mono_raise_exception (mono_get_exception_argument_out_of_range ("length"));
224
225         arr = mono_array_new (domain, mono_defaults.byte_class, length);
226         memcpy (mono_array_addr (arr, guint8, 0), value + sindex, length);
227
228         /* Find the System.Text.Encoding class */
229         for (klass = enc->vtable->klass; klass->parent->parent != NULL; klass = klass->parent)
230                 ;
231         
232         get_string = mono_class_get_method_from_name (klass, "GetString", 1);
233         args [0] = arr;
234         s = (MonoString*)mono_runtime_invoke (get_string, enc, args, &exc);
235         if (!s || exc)
236                 mono_raise_exception (mono_get_exception_argument ("", "Unable to decode the array into a valid string."));
237
238         return s;
239 }
240
241 /* This function is redirected to String.CreateString ()
242    by mono_marshal_get_native_wrapper () */
243 void
244 ves_icall_System_String_ctor_RedirectToCreateString (void)
245 {
246         g_assert_not_reached ();
247 }
248
249 MonoString * 
250 ves_icall_System_String_InternalJoin (MonoString *separator, MonoArray * value, gint32 sindex, gint32 count)
251 {
252         MonoString * ret;
253         MonoString *current;
254         gint32 length;
255         gint32 pos;
256         gint32 insertlen;
257         gint32 destpos;
258         gint32 srclen;
259         gunichar2 *insert;
260         gunichar2 *dest;
261         gunichar2 *src;
262
263         MONO_ARCH_SAVE_REGS;
264
265         insert = mono_string_chars(separator);
266         insertlen = mono_string_length(separator);
267
268         length = 0;
269         for (pos = sindex; pos != sindex + count; pos++) {
270                 current = mono_array_get (value, MonoString *, pos);
271                 if (current != NULL)
272                         length += mono_string_length (current);
273
274                 if (pos < sindex + count - 1)
275                         length += insertlen;
276         }
277
278         ret = mono_string_new_size( mono_domain_get (), length);
279         dest = mono_string_chars(ret);
280         destpos = 0;
281
282         for (pos = sindex; pos != sindex + count; pos++) {
283                 current = mono_array_get (value, MonoString *, pos);
284                 if (current != NULL) {
285                         src = mono_string_chars (current);
286                         srclen = mono_string_length (current);
287
288                         memcpy (dest + destpos, src, srclen * sizeof(gunichar2));
289                         destpos += srclen;
290                 }
291
292                 if (pos < sindex + count - 1) {
293                         memcpy(dest + destpos, insert, insertlen * sizeof(gunichar2));
294                         destpos += insertlen;
295                 }
296         }
297
298         return ret;
299 }
300
301 MonoString * 
302 ves_icall_System_String_InternalInsert (MonoString *me, gint32 sindex, MonoString *value)
303 {
304         MonoString * ret;
305         gunichar2 *src;
306         gunichar2 *insertsrc;
307         gunichar2 *dest;
308         gint32 srclen;
309         gint32 insertlen;
310
311         MONO_ARCH_SAVE_REGS;
312
313         src = mono_string_chars(me);
314         srclen = mono_string_length(me);
315
316         insertsrc = mono_string_chars(value);
317         insertlen = mono_string_length(value);
318
319         ret = mono_string_new_size( mono_domain_get (), srclen + insertlen);
320         dest = mono_string_chars(ret);
321
322         memcpy(dest, src, sindex * sizeof(gunichar2));
323         memcpy(dest + sindex, insertsrc, insertlen * sizeof(gunichar2));
324         memcpy(dest + sindex + insertlen, src + sindex, (srclen - sindex) * sizeof(gunichar2));
325
326         return ret;
327 }
328
329 MonoString * 
330 ves_icall_System_String_InternalReplace_Char (MonoString *me, gunichar2 oldChar, gunichar2 newChar)
331 {
332         MonoString *ret;
333         gunichar2 *src;
334         gunichar2 *dest;
335         gint32 i, srclen;
336
337         MONO_ARCH_SAVE_REGS;
338
339         src = mono_string_chars(me);
340         srclen = mono_string_length(me);
341
342         ret = mono_string_new_size( mono_domain_get (), srclen);
343         dest = mono_string_chars(ret);
344
345         for (i = 0; i != srclen; i++) {
346                 if (src[i] == oldChar)
347                         dest[i] = newChar;
348                 else
349                         dest[i] = src[i];
350         }
351
352         return ret;
353 }
354
355 MonoString * 
356 ves_icall_System_String_InternalRemove (MonoString *me, gint32 sindex, gint32 count)
357 {
358         MonoString * ret;
359         gint32 srclen;
360         gunichar2 *dest;
361         gunichar2 *src;
362
363         MONO_ARCH_SAVE_REGS;
364
365         srclen = mono_string_length(me);
366         ret = mono_string_new_size( mono_domain_get (), srclen - count);
367
368         src = mono_string_chars(me);
369         dest = mono_string_chars(ret);
370
371         memcpy(dest, src, sindex * sizeof(gunichar2));
372         memcpy(dest + sindex, src + sindex + count, (srclen - count - sindex) * sizeof(gunichar2));
373
374         return ret;
375 }
376
377 void
378 ves_icall_System_String_InternalCopyTo (MonoString *me, gint32 sindex, MonoArray *dest, gint32 dindex, gint32 count)
379 {
380         gunichar2 *destptr = (gunichar2 *) mono_array_addr(dest, gunichar2, dindex);
381         gunichar2 *src =  mono_string_chars(me);
382
383         MONO_ARCH_SAVE_REGS;
384
385         memcpy(destptr, src + sindex, sizeof(gunichar2) * count);
386 }
387
388 MonoArray * 
389 ves_icall_System_String_InternalSplit (MonoString *me, MonoArray *separator, gint32 count)
390 {
391         MonoString * tmpstr;
392         MonoArray * retarr;
393         gunichar2 *src;
394         gint32 arrsize, srcsize, splitsize;
395         gint32 i, lastpos, arrpos;
396         gint32 tmpstrsize;
397         gunichar2 *tmpstrptr;
398
399         gunichar2 cmpchar;
400
401         MONO_ARCH_SAVE_REGS;
402
403         src = mono_string_chars(me);
404         srcsize = mono_string_length(me);
405         arrsize = mono_array_length(separator);
406
407         cmpchar = mono_array_get(separator, gunichar2, 0);
408
409         splitsize = 0;
410         for (i = 0; i != srcsize && splitsize < count; i++) {
411                 if (string_icall_is_in_array(separator, arrsize, src[i]))
412                         splitsize++;
413         }
414
415         lastpos = 0;
416         arrpos = 0;
417
418         /* if no split chars found return the string */
419         if (splitsize == 0) {
420                 retarr = mono_array_new(mono_domain_get(), mono_get_string_class (), 1);
421                 mono_array_setref (retarr, 0, me);
422
423                 return retarr;
424         }
425
426         if (splitsize != count)
427                 splitsize++;
428
429         retarr = mono_array_new(mono_domain_get(), mono_get_string_class (), splitsize);
430         for (i = 0; i != srcsize && arrpos != count; i++) {
431                 if (string_icall_is_in_array(separator, arrsize, src[i])) {
432                         if (arrpos == count - 1)
433                                 tmpstrsize = srcsize - lastpos;
434                         else
435                                 tmpstrsize = i - lastpos;
436
437                         tmpstr = mono_string_new_size( mono_domain_get (), tmpstrsize);
438                         tmpstrptr = mono_string_chars(tmpstr);
439
440                         memcpy(tmpstrptr, src + lastpos, tmpstrsize * sizeof(gunichar2));
441                         mono_array_setref (retarr, arrpos, tmpstr);
442                         arrpos++;
443                         lastpos = i + 1;
444                 }
445         }
446
447         if (arrpos < count) {
448                 tmpstrsize = srcsize - lastpos;
449                 tmpstr = mono_string_new_size( mono_domain_get (), tmpstrsize);
450                 tmpstrptr = mono_string_chars(tmpstr);
451
452                 memcpy(tmpstrptr, src + lastpos, tmpstrsize * sizeof(gunichar2));
453                 mono_array_setref (retarr, arrpos, tmpstr);
454         }
455
456         return retarr;
457 }
458
459 static gboolean
460 string_icall_is_in_array (MonoArray *chars, gint32 arraylength, gunichar2 chr)
461 {
462         gunichar2 cmpchar;
463         gint32 arrpos;
464
465         for (arrpos = 0; arrpos != arraylength; arrpos++) {
466                 cmpchar = mono_array_get(chars, gunichar2, arrpos);
467                 if (cmpchar == chr)
468                         return TRUE;
469         }
470         
471         return FALSE;
472 }
473
474 MonoString * 
475 ves_icall_System_String_InternalTrim (MonoString *me, MonoArray *chars, gint32 typ)
476 {
477         MonoString * ret;
478         gunichar2 *src, *dest;
479         gint32 srclen, newlen, arrlen;
480         gint32 i, lenfirst, lenlast;
481
482         MONO_ARCH_SAVE_REGS;
483
484         srclen = mono_string_length(me);
485         src = mono_string_chars(me);
486         arrlen = mono_array_length(chars);
487
488         lenfirst = 0;
489         lenlast = 0;
490
491         if (0 == typ || 1 == typ) {
492                 for (i = 0; i != srclen; i++) {
493                         if (string_icall_is_in_array(chars, arrlen, src[i]))
494                                 lenfirst++;
495                         else 
496                                 break;
497                 }
498         }
499
500         if (0 == typ || 2 == typ) {
501                 for (i = srclen - 1; i > lenfirst - 1; i--) {
502                         if (string_icall_is_in_array(chars, arrlen, src[i]))
503                                 lenlast++;
504                         else 
505                                 break;
506                 }
507         }
508
509         newlen = srclen - lenfirst - lenlast;
510         if (newlen == srclen)
511                 return me;
512
513         ret = mono_string_new_size( mono_domain_get (), newlen);
514         dest = mono_string_chars(ret);
515
516         memcpy(dest, src + lenfirst, newlen *sizeof(gunichar2));
517
518         return ret;
519 }
520
521 gint32 
522 ves_icall_System_String_InternalIndexOfAny (MonoString *me, MonoArray *arr, gint32 sindex, gint32 count)
523 {
524         gint32 pos;
525         gint32 loop;
526         gint32 arraysize;
527         gunichar2 *src;
528
529         MONO_ARCH_SAVE_REGS;
530
531         arraysize = mono_array_length(arr);
532         src = mono_string_chars(me);
533
534         for (pos = sindex; pos != count + sindex; pos++) {
535                 for (loop = 0; loop != arraysize; loop++)
536                         if ( src [pos] == mono_array_get(arr, gunichar2, loop) )
537                                 return pos;
538         }
539
540         return -1;
541 }
542
543 gint32 
544 ves_icall_System_String_InternalLastIndexOf_Char (MonoString *me, gunichar2 value, gint32 sindex, gint32 count)
545 {
546         gint32 pos;
547         gunichar2 *src;
548
549         MONO_ARCH_SAVE_REGS;
550
551         src = mono_string_chars(me);
552         for (pos = sindex; pos > sindex - count; pos--) {
553                 if (src [pos] == value)
554                         return pos;
555         }
556
557         return -1;
558 }
559
560 gint32 
561 ves_icall_System_String_InternalLastIndexOf_Str (MonoString *me, MonoString *value, gint32 sindex, gint32 count)
562 {
563         gint32 lencmpstr;
564         gint32 pos;
565         gunichar2 *src;
566         gunichar2 *cmpstr;
567
568         MONO_ARCH_SAVE_REGS;
569
570         lencmpstr = mono_string_length(value);
571
572         src = mono_string_chars(me);
573         cmpstr = mono_string_chars(value);
574
575         for (pos = sindex - lencmpstr + 1; pos > sindex - count; pos--) {
576                 if (0 == memcmp(src + pos, cmpstr, lencmpstr * sizeof(gunichar2)))
577                         return pos;
578         }
579
580         return -1;
581 }
582
583 gint32 
584 ves_icall_System_String_InternalLastIndexOfAny (MonoString *me, MonoArray *anyOf, gint32 sindex, gint32 count)
585 {
586         gint32 pos;
587         gint32 loop;
588         gint32 arraysize;
589         gunichar2 *src;
590
591         MONO_ARCH_SAVE_REGS;
592
593         arraysize = mono_array_length(anyOf);
594         src = mono_string_chars(me);
595
596         for (pos = sindex; pos > sindex - count; pos--) {
597                 for (loop = 0; loop != arraysize; loop++)
598                         if ( src [pos] == mono_array_get(anyOf, gunichar2, loop) )
599                                 return pos;
600         }
601
602         return -1;
603 }
604
605 MonoString *
606 ves_icall_System_String_InternalPad (MonoString *me, gint32 width, gunichar2 chr, MonoBoolean right)
607 {
608         MonoString * ret;
609         gunichar2 *src;
610         gunichar2 *dest;
611         gint32 fillcount;
612         gint32 srclen;
613         gint32 i;
614
615         MONO_ARCH_SAVE_REGS;
616
617         srclen = mono_string_length(me);
618         src = mono_string_chars(me);
619
620         ret = mono_string_new_size( mono_domain_get (), width);
621         dest = mono_string_chars(ret);
622         fillcount = width - srclen;
623
624         if (right) {
625                 memcpy(dest, src, srclen * sizeof(gunichar2));
626                 for (i = srclen; i != width; i++)
627                         dest[i] = chr;
628
629                 return ret;
630         }
631
632         /* left fill */
633         for (i = 0; i != fillcount; i++)
634                 dest[i] = chr;
635
636         memcpy(dest + fillcount, src, srclen * sizeof(gunichar2));
637
638         return ret;
639 }
640
641 MonoString *
642 ves_icall_System_String_InternalAllocateStr (gint32 length)
643 {
644         MONO_ARCH_SAVE_REGS;
645
646         return mono_string_new_size(mono_domain_get (), length);
647 }
648
649 void 
650 ves_icall_System_String_InternalStrcpy_Str (MonoString *dest, gint32 destPos, MonoString *src)
651 {
652         gunichar2 *srcptr;
653         gunichar2 *destptr;
654
655         MONO_ARCH_SAVE_REGS;
656
657         srcptr = mono_string_chars (src);
658         destptr = mono_string_chars (dest);
659
660         g_memmove (destptr + destPos, srcptr, mono_string_length(src) * sizeof(gunichar2));
661 }
662
663 void 
664 ves_icall_System_String_InternalStrcpy_StrN (MonoString *dest, gint32 destPos, MonoString *src, gint32 startPos, gint32 count)
665 {
666         gunichar2 *srcptr;
667         gunichar2 *destptr;
668
669         MONO_ARCH_SAVE_REGS;
670
671         srcptr = mono_string_chars (src);
672         destptr = mono_string_chars (dest);
673         g_memmove (destptr + destPos, srcptr + startPos, count * sizeof(gunichar2));
674 }
675
676 void 
677 ves_icall_System_String_InternalStrcpy_Chars (MonoString *dest, gint32 destPos, MonoArray *src)
678 {
679         gunichar2 *srcptr;
680         gunichar2 *destptr;
681
682         MONO_ARCH_SAVE_REGS;
683
684         srcptr = mono_array_addr (src, gunichar2, 0);
685         destptr = mono_string_chars (dest);
686
687         g_memmove (destptr + destPos, srcptr, mono_array_length (src) * sizeof(gunichar2));
688 }
689
690 void 
691 ves_icall_System_String_InternalStrcpy_CharsN (MonoString *dest, gint32 destPos, MonoArray *src, gint32 startPos, gint32 count)
692 {
693         gunichar2 *srcptr;
694         gunichar2 *destptr;
695
696         MONO_ARCH_SAVE_REGS;
697
698         srcptr = mono_array_addr (src, gunichar2, 0);
699         destptr = mono_string_chars (dest);
700
701         g_memmove (destptr + destPos, srcptr + startPos, count * sizeof(gunichar2));
702 }
703
704 MonoString  *
705 ves_icall_System_String_InternalIntern (MonoString *str)
706 {
707         MONO_ARCH_SAVE_REGS;
708
709         return mono_string_intern(str);
710 }
711
712 MonoString * 
713 ves_icall_System_String_InternalIsInterned (MonoString *str)
714 {
715         MONO_ARCH_SAVE_REGS;
716
717         return mono_string_is_interned(str);
718 }
719
720 gunichar2 
721 ves_icall_System_String_get_Chars (MonoString *me, gint32 idx)
722 {
723         MONO_ARCH_SAVE_REGS;
724
725         if ((idx < 0) || (idx >= mono_string_length (me)))
726                 mono_raise_exception (mono_get_exception_index_out_of_range ());
727         return mono_string_chars(me)[idx];
728 }
729
730 void
731 ves_icall_System_String_InternalCharCopy (gunichar2 *src, gunichar2 *dest, gint32 count)
732 {
733         MONO_ARCH_SAVE_REGS;
734
735         memcpy (dest, src, sizeof (gunichar2) * count);
736 }