Welcome to mirror list, hosted at ThFree Co, Russian Federation.

string-icalls.c « metadata « mono - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 428036e4f52b03daf3937beee644a4d4d160612b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
/*
 * string-icalls.c: String internal calls for the corlib
 *
 * Author:
 *   Patrik Torstensson (patrik.torstensson@labs2.com)
 *   Duncan Mak  (duncan@ximian.com)
 *
 * (C) 2001 Ximian, Inc.
 */
#include <config.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <mono/metadata/string-icalls.h>
#include <mono/metadata/appdomain.h>
#include <mono/metadata/tabledefs.h>
#include <mono/metadata/loader.h>
#include <mono/metadata/object.h>
#include <mono/metadata/unicode.h>
#include <mono/metadata/exception.h>

MonoString *
ves_icall_System_String_ctor_charp (gpointer dummy, gunichar2 *value)
{
	gint32 i, length;
	MonoDomain *domain;

	domain = mono_domain_get ();

	if (value == NULL)
		length = 0;
	else {
		for (i = 0; *(value + i) != '\0'; i++);
		length = i;
	}

	return mono_string_new_utf16 (domain, value, length);
}

MonoString *
ves_icall_System_String_ctor_char_int (gpointer dummy, gunichar2 value, gint32 count)
{
	MonoDomain *domain;
	MonoString *res;
	gunichar2 *chars;
	gint32 i;

	domain = mono_domain_get ();
	res = mono_string_new_size (domain, count);

	chars = mono_string_chars (res);
	for (i = 0; i < count; i++)
		chars [i] = value;
	
	return res;
}

MonoString *
ves_icall_System_String_ctor_charp_int_int (gpointer dummy, gunichar2 *value, gint32 sindex, gint32 length)
{
	gunichar2 *begin;
	MonoDomain * domain;
	
	domain = mono_domain_get ();

	if ((value == NULL) && (length != 0))
		mono_raise_exception (mono_get_exception_argument_out_of_range ("Out of range"));

	if ((sindex < 0) || (length < 0))
		mono_raise_exception (mono_get_exception_argument_out_of_range ("Out of range"));
	
	if (length == 0) {	/* fixme: return String.Empty here */
		g_warning ("string doesn't yet support empy strings in char* constructor");
		g_assert_not_reached ();
	}
	
	begin = (gunichar2 *) (value + sindex);

	return mono_string_new_utf16 (domain, begin, length);
}

MonoString *
ves_icall_System_String_ctor_sbytep (gpointer dummy, gint8 *value)
{
	MonoDomain *domain;
	
	domain = mono_domain_get ();

	if (NULL == value) {	/* fixme: return String.Empty here */
		g_warning ("string doesn't yet support empy strings in char* constructor");
		g_assert_not_reached ();
	}

	return mono_string_new (domain, (const char *) value);
}

MonoString *
ves_icall_System_String_ctor_sbytep_int_int (gpointer dummy, gint8 *value, gint32 sindex, gint32 length)
{
	char *begin;
	MonoDomain *domain;
	
	domain = mono_domain_get ();

	if ((value == NULL) && (length != 0))
		mono_raise_exception (mono_get_exception_argument_out_of_range ("Out of range"));

	if ((sindex < 0) || (length < 0))
		mono_raise_exception (mono_get_exception_argument_out_of_range ("Out of range"));

	begin = (char *) (value + sindex);

	return mono_string_new_len (domain, begin, length);
}

MonoString *
ves_icall_System_String_ctor_chara (gpointer dummy, MonoArray *value)
{
	MonoDomain *domain;

	MONO_CHECK_ARG_NULL (value);
	
	domain = mono_domain_get ();
	
	return mono_string_new_utf16 (domain, (gunichar2 *) mono_array_addr(value, gunichar2, 0),  value->max_length);
}

MonoString *
ves_icall_System_String_ctor_chara_int_int (gpointer dummy, MonoArray *value, 
					 gint32 sindex, gint32 length)
{
	MonoDomain *domain;

	MONO_CHECK_ARG_NULL (value);

	domain = mono_domain_get ();
	
	return mono_string_new_utf16 (domain, (gunichar2 *) mono_array_addr(value, gunichar2, sindex), length);
}

MonoString *
ves_icall_System_String_ctor_encoding (gpointer dummy, gint8 *value, gint32 sindex, 
				    gint32 length, MonoObject *enc)
{
	g_warning("string.ctor with encoding obj unimplemented");
	g_assert_not_reached ();
	return NULL;
}

MonoBoolean 
ves_icall_System_String_InternalEquals (MonoString *str1, MonoString *str2)
{
	gunichar2 *str1ptr;
	gunichar2 *str2ptr;
	gint32 str1len;

	/* Length checking is done in C# */
	str1len = mono_string_length(str1);

	str1ptr = mono_string_chars(str1);
	str2ptr = mono_string_chars(str2);

	return (0 == memcmp(str1ptr, str2ptr, str1len * sizeof(gunichar2)));
}

MonoString * 
ves_icall_System_String_InternalJoin (MonoString *separator, MonoArray * value, gint32 sindex, gint32 count)
{
	MonoString * ret;
	gint32 length;
	gint32 pos;
	gint32 insertlen;
	gint32 destpos;
	gint32 srclen;
	gunichar2 *insert;
	gunichar2 *dest;
	gunichar2 *src;

	insert = mono_string_chars(separator);
	insertlen = mono_string_length(separator);

	length = 0;
	for (pos = sindex; pos != sindex + count; pos++) {
		length += mono_string_length(mono_array_get(value, MonoString *, pos));
		if (pos < sindex + count - 1)
			length += insertlen;
	}

	ret = mono_string_new_size( mono_domain_get (), length);
	dest = mono_string_chars(ret);
	destpos = 0;

	for (pos = sindex; pos != sindex + count; pos++) {
		src = mono_string_chars(mono_array_get(value, MonoString *, pos));
		srclen = mono_string_length(mono_array_get(value, MonoString *, pos));

		memcpy(dest + destpos, src, srclen * sizeof(gunichar2));
		destpos += srclen;

		if (pos < sindex + count - 1) {
			memcpy(dest + destpos, insert, insertlen * sizeof(gunichar2));
			destpos += insertlen;
		}
	}

	return ret;
}

MonoString * 
ves_icall_System_String_InternalInsert (MonoString *me, gint32 sindex, MonoString *value)
{
	MonoString * ret;
	gunichar2 *src;
	gunichar2 *insertsrc;
	gunichar2 *dest;
	gint32 srclen;
	gint32 insertlen;

	src = mono_string_chars(me);
	srclen = mono_string_length(me);

	insertsrc = mono_string_chars(value);
	insertlen = mono_string_length(value);

	ret = mono_string_new_size( mono_domain_get (), srclen + insertlen);
	dest = mono_string_chars(ret);

	memcpy(dest, src, sindex * sizeof(gunichar2));
	memcpy(dest + sindex, insertsrc, insertlen * sizeof(gunichar2));
	memcpy(dest + sindex + insertlen, src + sindex, (srclen - sindex) * sizeof(gunichar2));

	return ret;
}

MonoString * 
ves_icall_System_String_InternalReplace_Char (MonoString *me, gunichar2 oldChar, gunichar2 newChar)
{
	MonoString *ret;
	gunichar2 *src;
	gunichar2 *dest;
	gint32 i, srclen;

	src = mono_string_chars(me);
	srclen = mono_string_length(me);

	ret = mono_string_new_size( mono_domain_get (), srclen);
	dest = mono_string_chars(ret);

	for (i = 0; i != srclen; i++) {
		if (src[i] == oldChar)
			dest[i] = newChar;
		else
			dest[i] = src[i];
	}

	return ret;
}

MonoString * 
ves_icall_System_String_InternalReplace_Str (MonoString *me, MonoString *oldValue, MonoString *newValue)
{
	MonoString *ret;
	gunichar2 *src;
	gunichar2 *dest;
	gunichar2 *oldstr;
	gunichar2 *newstr;
	gint32 i, destpos;
	gint32 occurr;
	gint32 newsize;
	gint32 oldstrlen;
	gint32 newstrlen;
	gint32 srclen;

	occurr = 0;
	destpos = 0;

	oldstr = mono_string_chars(oldValue);
	oldstrlen = mono_string_length(oldValue);

	if (NULL != newValue) {
		newstr = mono_string_chars(newValue);
		newstrlen = mono_string_length(newValue);
	} else
		newstrlen = 0;

	src = mono_string_chars(me);
	srclen = mono_string_length(me);

	if (oldstrlen != newstrlen) {
		for (i = 0; i <= srclen - oldstrlen; i++)
			if (0 == memcmp(src + i, oldstr, oldstrlen * sizeof(gunichar2)))
				occurr++;

		newsize = srclen + ((newstrlen - oldstrlen) * occurr);
 	} else
		newsize = srclen;

	ret = mono_string_new_size( mono_domain_get (), newsize);
	dest = mono_string_chars(ret);

	i = 0;
	while (i < srclen) {
		if (0 == memcmp(src + i, oldstr, oldstrlen * sizeof(gunichar2))) {
			if (newstrlen > 0) {
				memcpy(dest + destpos, newstr, newstrlen * sizeof(gunichar2));
				destpos += newstrlen;
			}
			i += oldstrlen;
		} else {
			dest[destpos] = src[i];
			destpos++;
			i++;
		}
	}

	return ret;
}

MonoString * 
ves_icall_System_String_InternalRemove (MonoString *me, gint32 sindex, gint32 count)
{
	MonoString * ret;
	gint32 srclen;
	gunichar2 *dest;
	gunichar2 *src;

	srclen = mono_string_length(me);
	ret = mono_string_new_size( mono_domain_get (), srclen - count);

	src = mono_string_chars(me);
	dest = mono_string_chars(ret);

	memcpy(dest, src, sindex * sizeof(gunichar2));
	memcpy(dest + sindex, src + sindex + count, (srclen - count - sindex) * sizeof(gunichar2));

	return ret;
}

void
ves_icall_System_String_InternalCopyTo (MonoString *me, gint32 sindex, MonoArray *dest, gint32 dindex, gint32 count)
{
	gunichar2 *destptr = (gunichar2 *) mono_array_addr(dest, gunichar2, dindex);
	gunichar2 *src =  mono_string_chars(me);

	memcpy(destptr, src + sindex, sizeof(gunichar2) * count);
}

MonoArray * 
ves_icall_System_String_InternalSplit (MonoString *me, MonoArray *separator, gint32 count)
{
	MonoString * tmpstr;
	MonoArray * retarr;
	gunichar2 *src;
	gint32 arrsize, srcsize, splitsize;
	gint32 i, lastpos, arrpos;
	gint32 tmpstrsize;
	gunichar2 *tmpstrptr;

	gunichar2 cmpchar;

	src = mono_string_chars(me);
	srcsize = mono_string_length(me);
	arrsize = mono_array_length(separator);

	cmpchar = mono_array_get(separator, gunichar2, 0);

	splitsize = 0;
	for (i = 0; i != srcsize && splitsize < count; i++) {
		if (string_icall_is_in_array(separator, arrsize, src[i]))
			splitsize++;
	}

	lastpos = 0;
	arrpos = 0;

	/* if no split chars found return the string */
	if (splitsize == 0) {
		retarr = mono_array_new(mono_domain_get(), mono_defaults.string_class, 1);
		tmpstr = mono_string_new_size( mono_domain_get (), srcsize);
		tmpstrptr = mono_string_chars(tmpstr);

		memcpy(tmpstrptr, src, srcsize * sizeof(gunichar2));
		mono_array_set(retarr, MonoString *, 0, tmpstr);

		return retarr;
	}

	if (splitsize != count)
		splitsize++;

	retarr = mono_array_new(mono_domain_get(), mono_defaults.string_class, splitsize);
	for (i = 0; i != srcsize && arrpos != count; i++) {
		if (string_icall_is_in_array(separator, arrsize, src[i])) {
			if (arrpos == count - 1)
				tmpstrsize = srcsize - lastpos;
			else
				tmpstrsize = i - lastpos;

			tmpstr = mono_string_new_size( mono_domain_get (), tmpstrsize);
			tmpstrptr = mono_string_chars(tmpstr);

			memcpy(tmpstrptr, src + lastpos, tmpstrsize * sizeof(gunichar2));
			mono_array_set(retarr, MonoString *, arrpos, tmpstr);
			arrpos++;
			lastpos = i + 1;
		}
	}

	if (arrpos < count) {
		tmpstrsize = srcsize - lastpos;
		tmpstr = mono_string_new_size( mono_domain_get (), tmpstrsize);
		tmpstrptr = mono_string_chars(tmpstr);

		memcpy(tmpstrptr, src + lastpos, tmpstrsize * sizeof(gunichar2));
		mono_array_set(retarr, MonoString *, arrpos, tmpstr);
	}

	return retarr;
}

gboolean
string_icall_is_in_array (MonoArray *chars, gint32 arraylength, gunichar2 chr)
{
	gunichar2 cmpchar;
	gint32 arrpos;

	for (arrpos = 0; arrpos != arraylength; arrpos++) {
		cmpchar = mono_array_get(chars, gunichar2, arrpos);
		if (cmpchar == chr)
			return TRUE;
	}
	
	return FALSE;
}

MonoString * 
ves_icall_System_String_InternalTrim (MonoString *me, MonoArray *chars, gint32 typ)
{
	MonoString * ret;
	gunichar2 *src, *dest;
	gint32 srclen, newlen, arrlen;
	gint32 i, lenfirst, lenlast;

	srclen = mono_string_length(me);
	src = mono_string_chars(me);
	arrlen = mono_array_length(chars);

	lenfirst = 0;
	lenlast = 0;

	if (0 == typ || 1 == typ) {
		for (i = 0; i != srclen; i++) {
			if (string_icall_is_in_array(chars, arrlen, src[i]))
				lenfirst++;
			else 
				break;
		}
	}

	if (0 == typ || 2 == typ) {
		for (i = srclen - 1; i > lenfirst - 1; i--) {
			if (string_icall_is_in_array(chars, arrlen, src[i]))
				lenlast++;
			else 
				break;
		}
	}

	newlen = srclen - lenfirst - lenlast;

	ret = mono_string_new_size( mono_domain_get (), newlen);
	dest = mono_string_chars(ret);

	memcpy(dest, src + lenfirst, newlen *sizeof(gunichar2));

	return ret;
}

gint32 
ves_icall_System_String_InternalIndexOf_Char (MonoString *me, gunichar2 value, gint32 sindex, gint32 count)
{
	gint32 pos;
	gunichar2 *src;

	src = mono_string_chars(me);
	for (pos = sindex; pos != count + sindex; pos++) {
		if ( src [pos] == value)
			return pos;
	}

	return -1;
}

gint32 
ves_icall_System_String_InternalIndexOf_Str (MonoString *me, MonoString *value, gint32 sindex, gint32 count)
{
	gint32 lencmpstr;
	gint32 pos;
	gunichar2 *src;
	gunichar2 *cmpstr;

	lencmpstr = mono_string_length(value);

	src = mono_string_chars(me);
	cmpstr = mono_string_chars(value);

	for (pos = sindex; pos != count + sindex; pos++) {
		if (pos + lencmpstr > count + sindex)
			return -1;

		if (0 == memcmp(src + pos, cmpstr, lencmpstr * sizeof(gunichar2)))
			return pos;
	}

	return -1;
}

gint32 
ves_icall_System_String_InternalIndexOfAny (MonoString *me, MonoArray *arr, gint32 sindex, gint32 count)
{
	gint32 pos;
	gint32 loop;
	gint32 arraysize;
	gunichar2 *src;

	arraysize = mono_array_length(arr);
	src = mono_string_chars(me);

	for (pos = sindex; pos != count + sindex; pos++) {
		for (loop = 0; loop != arraysize; loop++)
			if ( src [pos] == mono_array_get(arr, gunichar2, loop) )
				return pos;
	}

	return -1;
}

gint32 
ves_icall_System_String_InternalLastIndexOf_Char (MonoString *me, gunichar2 value, gint32 sindex, gint32 count)
{
	gint32 pos;
	gunichar2 *src;

	src = mono_string_chars(me);
	for (pos = sindex; pos > sindex - count; pos--) {
		if (src [pos] == value)
			return pos;
	}

	return -1;
}

gint32 
ves_icall_System_String_InternalLastIndexOf_Str (MonoString *me, MonoString *value, gint32 sindex, gint32 count)
{
	gint32 lencmpstr;
	gint32 pos;
	gunichar2 *src;
	gunichar2 *cmpstr;

	lencmpstr = mono_string_length(value);

	src = mono_string_chars(me);
	cmpstr = mono_string_chars(value);

	for (pos = sindex - lencmpstr + 1; pos > sindex - count; pos--) {
		if (0 == memcmp(src + pos, cmpstr, lencmpstr * sizeof(gunichar2)))
			return pos;
	}

	return -1;
}

gint32 
ves_icall_System_String_InternalLastIndexOfAny (MonoString *me, MonoArray *anyOf, gint32 sindex, gint32 count)
{
	gint32 pos;
	gint32 loop;
	gint32 arraysize;
	gunichar2 *src;

	arraysize = mono_array_length(anyOf);
	src = mono_string_chars(me);

	for (pos = sindex; pos > sindex - count; pos--) {
		for (loop = 0; loop != arraysize; loop++)
			if ( src [pos] == mono_array_get(anyOf, gunichar2, loop) )
				return pos;
	}

	return -1;
}

MonoString *
ves_icall_System_String_InternalPad (MonoString *me, gint32 width, gunichar2 chr, MonoBoolean right)
{
	MonoString * ret;
	gunichar2 *src;
	gunichar2 *dest;
	gint32 fillcount;
	gint32 srclen;
	gint32 i;

	srclen = mono_string_length(me);
	src = mono_string_chars(me);

	ret = mono_string_new_size( mono_domain_get (), width);
	dest = mono_string_chars(ret);
	fillcount = width - srclen;

	if (right) {
		memcpy(dest, src, srclen * sizeof(gunichar2));
		for (i = srclen; i != width; i++)
			dest[i] = chr;

		return ret;
	}

	/* left fill */
	for (i = 0; i != fillcount; i++)
		dest[i] = chr;

	memcpy(dest + fillcount, src, srclen * sizeof(gunichar2));

	return ret;
}

MonoString *
ves_icall_System_String_InternalToLower (MonoString *me)
{
	MonoString * ret;
	gunichar2 *src; 
	gunichar2 *dest;
	gint32 i;

	ret = mono_string_new_size(mono_domain_get (), mono_string_length(me));

	src = mono_string_chars (me);
	dest = mono_string_chars (ret);

	for (i = 0; i < mono_string_length (me); ++i)
		dest[i] = g_unichar_tolower(src[i]);

	return ret;
}

MonoString *
ves_icall_System_String_InternalToUpper (MonoString *me)
{
	int i;
	MonoString * ret;
	gunichar2 *src; 
	gunichar2 *dest;

	ret = mono_string_new_size(mono_domain_get (), mono_string_length(me));

	src = mono_string_chars (me);
	dest = mono_string_chars (ret);

	for (i = 0; i < mono_string_length (me); ++i)
		dest[i] = g_unichar_toupper(src[i]);

	return ret;
}

MonoString *
ves_icall_System_String_InternalAllocateStr (gint32 length)
{
	return mono_string_new_size(mono_domain_get (), length);
}

void 
ves_icall_System_String_InternalStrcpy_Str (MonoString *dest, gint32 destPos, MonoString *src)
{
	gunichar2 *srcptr;
	gunichar2 *destptr;

	srcptr = mono_string_chars (src);
	destptr = mono_string_chars (dest);

	memcpy(destptr + destPos, srcptr, mono_string_length(src) * sizeof(gunichar2));
}

void 
ves_icall_System_String_InternalStrcpy_StrN (MonoString *dest, gint32 destPos, MonoString *src, gint32 startPos, gint32 count)
{
	gunichar2 *srcptr;
	gunichar2 *destptr;

	srcptr = mono_string_chars (src);
	destptr = mono_string_chars (dest);
	memcpy(destptr + destPos, srcptr + startPos, count * sizeof(gunichar2));
}

MonoString  *
ves_icall_System_String_InternalIntern (MonoString *str)
{
	return mono_string_intern(str);
}

MonoString * 
ves_icall_System_String_InternalIsInterned (MonoString *str)
{
	return mono_string_is_interned(str);
}

gint32
ves_icall_System_String_InternalCompareStr_N (MonoString *s1, gint32 i1, MonoString *s2, gint32 i2, gint32 length, MonoBoolean inCase)
{
	/* c translation of C# code from old string.cs.. :) */
	gint32 lenstr1;
	gint32 lenstr2;
	gint32 charcmp;
	gunichar2 *str1;
	gunichar2 *str2;

	gint32 pos;
	gint16 mode;
	
	if (inCase)
		mode = 1;
	else
		mode = 0;

	lenstr1 = mono_string_length(s1);
	lenstr2 = mono_string_length(s2);

	str1 = mono_string_chars(s1);
	str2 = mono_string_chars(s2);

	pos = 0;

	for (pos = 0; pos != length; pos++) {
		if (i1 + pos >= lenstr1 || i2 + pos >= lenstr2)
			break;

		charcmp = string_icall_cmp_char(str1[i1 + pos], str2[i2 + pos], mode);
		if (charcmp != 0)
			return charcmp;
	}

	/* the lesser wins, so if we have looped until length we just need to check the last char */
	if (pos == length) {
		return string_icall_cmp_char(str1[i1 + pos - 1], str2[i2 + pos - 1], mode);
	}

	/* Test if one the strings has been compared to the end */
	if (i1 + pos >= lenstr1) {
		if (i2 + pos >= lenstr2)
			return 0;
		else
			return -1;
	} else if (i2 + pos >= lenstr2)
		return 1;

	/* if not, check our last char only.. (can this happen?) */
	return string_icall_cmp_char(str1[i1 + pos], str2[i2 + pos], mode);
}

gint32
ves_icall_System_String_GetHashCode (MonoString *me)
{
	int i, h = 0;
	gunichar2 *data = mono_string_chars (me);

	for (i = 0; i < mono_string_length (me); ++i)
		h = (h << 5) - h + data [i];

	return h;
}

gunichar2 
ves_icall_System_String_get_Chars (MonoString *me, gint32 idx)
{
	return mono_string_chars(me)[idx];
}

/* @mode :	0 = StringCompareModeDirect
			1 = StringCompareModeCaseInsensitive
			2 = StringCompareModeOrdinal
*/
gint32 
string_icall_cmp_char (gunichar2 c1, gunichar2 c2, gint16 mode)
{
	gint32 result;
	GUnicodeType c1type, c2type;

	c1type = g_unichar_type (c1);
	c2type = g_unichar_type (c2);

	switch (mode) {
	case 0:	
		/* TODO: compare with culture info */
		if (c1type == G_UNICODE_UPPERCASE_LETTER && c2type == G_UNICODE_LOWERCASE_LETTER)
			return 1;
					
		if (c1type == G_UNICODE_LOWERCASE_LETTER && c2type == G_UNICODE_UPPERCASE_LETTER)
			return -1;
	
		result = (gint32) c1 - c2;
		break;
	case 1:	
		result = (gint32) (c1type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c1) : c1) - 
				  (c2type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c2) : c2);
		break;
	case 2:
		result = (gint32) c1 - c2;
		break;
	}

	return ((result < 0) ? -1 : (result > 0) ? 1 : 0);
}