Sat Oct 11 19:46:19 CEST 2008 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / metadata / locales.c
1 /*
2  * locales.c: Culture-sensitive handling
3  *
4  * Author:
5  *      Dick Porter (dick@ximian.com)
6  *      Mohammad DAMT (mdamt@cdl2000.com)
7  *
8  * (C) 2003 Ximian, Inc.
9  * (C) 2003 PT Cakram Datalingga Duaribu  http://www.cdl2000.com
10  */
11
12 #include <config.h>
13 #include <glib.h>
14 #include <string.h>
15
16 #include <mono/metadata/debug-helpers.h>
17 #include <mono/metadata/object.h>
18 #include <mono/metadata/appdomain.h>
19 #include <mono/metadata/exception.h>
20 #include <mono/metadata/monitor.h>
21 #include <mono/metadata/locales.h>
22 #include <mono/metadata/culture-info.h>
23 #include <mono/metadata/culture-info-tables.h>
24 #include <mono/metadata/normalization-tables.h>
25
26
27 #include <locale.h>
28
29 #undef DEBUG
30
31 static gint32 string_invariant_compare_char (gunichar2 c1, gunichar2 c2,
32                                              gint32 options);
33 static gint32 string_invariant_compare (MonoString *str1, gint32 off1,
34                                         gint32 len1, MonoString *str2,
35                                         gint32 off2, gint32 len2,
36                                         gint32 options);
37 static MonoString *string_invariant_replace (MonoString *me,
38                                              MonoString *oldValue,
39                                              MonoString *newValue);
40 static gint32 string_invariant_indexof (MonoString *source, gint32 sindex,
41                                         gint32 count, MonoString *value,
42                                         MonoBoolean first);
43 static gint32 string_invariant_indexof_char (MonoString *source, gint32 sindex,
44                                              gint32 count, gunichar2 value,
45                                              MonoBoolean first);
46
47 static const CultureInfoEntry* culture_info_entry_from_lcid (int lcid);
48
49 static const RegionInfoEntry* region_info_entry_from_lcid (int lcid);
50
51 static int
52 culture_lcid_locator (const void *a, const void *b)
53 {
54         const CultureInfoEntry *aa = a;
55         const CultureInfoEntry *bb = b;
56
57         return (aa->lcid - bb->lcid);
58 }
59
60 static int
61 region_lcid_locator (const void *a, const void *b)
62 {
63         const int *lcid = a;
64         const CultureInfoEntry *bb = b;
65
66         return *lcid - bb->lcid;
67 }
68
69 static int
70 culture_name_locator (const void *a, const void *b)
71 {
72         const char *aa = a;
73         const CultureInfoNameEntry *bb = b;
74         int ret;
75         
76         ret = strcmp (aa, idx2string (bb->name));
77
78         return ret;
79 }
80
81 static int
82 region_name_locator (const void *a, const void *b)
83 {
84         const char *aa = a;
85         const RegionInfoNameEntry *bb = b;
86         int ret;
87         
88         ret = strcmp (aa, idx2string (bb->name));
89
90         return ret;
91 }
92
93 static MonoArray*
94 create_group_sizes_array (const gint *gs, gint ml)
95 {
96         MonoArray *ret;
97         int i, len = 0;
98
99         for (i = 0; i < ml; i++) {
100                 if (gs [i] == -1)
101                         break;
102                 len++;
103         }
104         
105         ret = mono_array_new (mono_domain_get (),
106                         mono_get_int32_class (), len);
107
108         for(i = 0; i < len; i++)
109                 mono_array_set (ret, gint32, i, gs [i]);
110
111         return ret;
112 }
113
114 static MonoArray*
115 create_names_array_idx (const guint16 *names, int ml)
116 {
117         MonoArray *ret;
118         MonoDomain *domain;
119         int i, len = 0;
120
121         if (names == NULL)
122                 return NULL;
123
124         domain = mono_domain_get ();
125
126         for (i = 0; i < ml; i++) {
127                 if (names [i] == 0)
128                         break;
129                 len++;
130         }
131
132         ret = mono_array_new (mono_domain_get (), mono_get_string_class (), len);
133
134         for(i = 0; i < len; i++)
135                 mono_array_setref (ret, i, mono_string_new (domain, idx2string (names [i])));
136
137         return ret;
138 }
139
140 void
141 ves_icall_System_Globalization_CultureInfo_construct_datetime_format (MonoCultureInfo *this)
142 {
143         MonoDomain *domain;
144         MonoDateTimeFormatInfo *datetime;
145         const DateTimeFormatEntry *dfe;
146
147         MONO_ARCH_SAVE_REGS;
148
149         g_assert (this->datetime_index >= 0);
150
151         datetime = this->datetime_format;
152         dfe = &datetime_format_entries [this->datetime_index];
153
154         domain = mono_domain_get ();
155
156         datetime->readOnly = this->is_read_only;
157         MONO_OBJECT_SETREF (datetime, AbbreviatedDayNames, create_names_array_idx (dfe->abbreviated_day_names,
158                         NUM_DAYS));
159         MONO_OBJECT_SETREF (datetime, AbbreviatedMonthNames, create_names_array_idx (dfe->abbreviated_month_names,
160                         NUM_MONTHS));
161         MONO_OBJECT_SETREF (datetime, AMDesignator, mono_string_new (domain, idx2string (dfe->am_designator)));
162         datetime->CalendarWeekRule = dfe->calendar_week_rule;
163         MONO_OBJECT_SETREF (datetime, DateSeparator, mono_string_new (domain, idx2string (dfe->date_separator)));
164         MONO_OBJECT_SETREF (datetime, DayNames, create_names_array_idx (dfe->day_names, NUM_DAYS));
165         datetime->FirstDayOfWeek = dfe->first_day_of_week;
166         MONO_OBJECT_SETREF (datetime, FullDateTimePattern, mono_string_new (domain, idx2string (dfe->full_date_time_pattern)));
167         MONO_OBJECT_SETREF (datetime, LongDatePattern, mono_string_new (domain, idx2string (dfe->long_date_pattern)));
168         MONO_OBJECT_SETREF (datetime, LongTimePattern, mono_string_new (domain, idx2string (dfe->long_time_pattern)));
169         MONO_OBJECT_SETREF (datetime, MonthDayPattern, mono_string_new (domain, idx2string (dfe->month_day_pattern)));
170         MONO_OBJECT_SETREF (datetime, MonthNames, create_names_array_idx (dfe->month_names, NUM_MONTHS));
171         MONO_OBJECT_SETREF (datetime, PMDesignator, mono_string_new (domain, idx2string (dfe->pm_designator)));
172         MONO_OBJECT_SETREF (datetime, ShortDatePattern, mono_string_new (domain, idx2string (dfe->short_date_pattern)));
173         MONO_OBJECT_SETREF (datetime, ShortTimePattern, mono_string_new (domain, idx2string (dfe->short_time_pattern)));
174         MONO_OBJECT_SETREF (datetime, TimeSeparator, mono_string_new (domain, idx2string (dfe->time_separator)));
175         MONO_OBJECT_SETREF (datetime, YearMonthPattern, mono_string_new (domain, idx2string (dfe->year_month_pattern)));
176         MONO_OBJECT_SETREF (datetime, ShortDatePatterns, create_names_array_idx (dfe->short_date_patterns,
177                         NUM_SHORT_DATE_PATTERNS));
178         MONO_OBJECT_SETREF (datetime, LongDatePatterns, create_names_array_idx (dfe->long_date_patterns,
179                         NUM_LONG_DATE_PATTERNS));
180         MONO_OBJECT_SETREF (datetime, ShortTimePatterns, create_names_array_idx (dfe->short_time_patterns,
181                         NUM_SHORT_TIME_PATTERNS));
182         MONO_OBJECT_SETREF (datetime, LongTimePatterns, create_names_array_idx (dfe->long_time_patterns,
183                         NUM_LONG_TIME_PATTERNS));
184
185 }
186
187 void
188 ves_icall_System_Globalization_CultureInfo_construct_number_format (MonoCultureInfo *this)
189 {
190         MonoDomain *domain;
191         MonoNumberFormatInfo *number;
192         const NumberFormatEntry *nfe;
193
194         MONO_ARCH_SAVE_REGS;
195
196         g_assert (this->number_format != 0);
197
198         number = this->number_format;
199         nfe = &number_format_entries [this->number_index];
200
201         domain = mono_domain_get ();
202
203         number->readOnly = this->is_read_only;
204         number->currencyDecimalDigits = nfe->currency_decimal_digits;
205         MONO_OBJECT_SETREF (number, currencyDecimalSeparator, mono_string_new (domain,
206                         idx2string (nfe->currency_decimal_separator)));
207         MONO_OBJECT_SETREF (number, currencyGroupSeparator, mono_string_new (domain,
208                         idx2string (nfe->currency_group_separator)));
209         MONO_OBJECT_SETREF (number, currencyGroupSizes, create_group_sizes_array (nfe->currency_group_sizes,
210                         GROUP_SIZE));
211         number->currencyNegativePattern = nfe->currency_negative_pattern;
212         number->currencyPositivePattern = nfe->currency_positive_pattern;
213         MONO_OBJECT_SETREF (number, currencySymbol, mono_string_new (domain, idx2string (nfe->currency_symbol)));
214         MONO_OBJECT_SETREF (number, naNSymbol, mono_string_new (domain, idx2string (nfe->nan_symbol)));
215         MONO_OBJECT_SETREF (number, negativeInfinitySymbol, mono_string_new (domain,
216                         idx2string (nfe->negative_infinity_symbol)));
217         MONO_OBJECT_SETREF (number, negativeSign, mono_string_new (domain, idx2string (nfe->negative_sign)));
218         number->numberDecimalDigits = nfe->number_decimal_digits;
219         MONO_OBJECT_SETREF (number, numberDecimalSeparator, mono_string_new (domain,
220                         idx2string (nfe->number_decimal_separator)));
221         MONO_OBJECT_SETREF (number, numberGroupSeparator, mono_string_new (domain, idx2string (nfe->number_group_separator)));
222         MONO_OBJECT_SETREF (number, numberGroupSizes, create_group_sizes_array (nfe->number_group_sizes,
223                         GROUP_SIZE));
224         number->numberNegativePattern = nfe->number_negative_pattern;
225         number->percentDecimalDigits = nfe->percent_decimal_digits;
226         MONO_OBJECT_SETREF (number, percentDecimalSeparator, mono_string_new (domain,
227                         idx2string (nfe->percent_decimal_separator)));
228         MONO_OBJECT_SETREF (number, percentGroupSeparator, mono_string_new (domain,
229                         idx2string (nfe->percent_group_separator)));
230         MONO_OBJECT_SETREF (number, percentGroupSizes, create_group_sizes_array (nfe->percent_group_sizes,
231                         GROUP_SIZE));
232         number->percentNegativePattern = nfe->percent_negative_pattern;
233         number->percentPositivePattern = nfe->percent_positive_pattern;
234         MONO_OBJECT_SETREF (number, percentSymbol, mono_string_new (domain, idx2string (nfe->percent_symbol)));
235         MONO_OBJECT_SETREF (number, perMilleSymbol, mono_string_new (domain, idx2string (nfe->per_mille_symbol)));
236         MONO_OBJECT_SETREF (number, positiveInfinitySymbol, mono_string_new (domain,
237                         idx2string (nfe->positive_infinity_symbol)));
238         MONO_OBJECT_SETREF (number, positiveSign, mono_string_new (domain, idx2string (nfe->positive_sign)));
239 }
240
241 static MonoBoolean
242 construct_culture (MonoCultureInfo *this, const CultureInfoEntry *ci)
243 {
244         MonoDomain *domain = mono_domain_get ();
245
246         this->lcid = ci->lcid;
247         MONO_OBJECT_SETREF (this, name, mono_string_new (domain, idx2string (ci->name)));
248         MONO_OBJECT_SETREF (this, icu_name, mono_string_new (domain, idx2string (ci->icu_name)));
249         MONO_OBJECT_SETREF (this, displayname, mono_string_new (domain, idx2string (ci->displayname)));
250         MONO_OBJECT_SETREF (this, englishname, mono_string_new (domain, idx2string (ci->englishname)));
251         MONO_OBJECT_SETREF (this, nativename, mono_string_new (domain, idx2string (ci->nativename)));
252         MONO_OBJECT_SETREF (this, win3lang, mono_string_new (domain, idx2string (ci->win3lang)));
253         MONO_OBJECT_SETREF (this, iso3lang, mono_string_new (domain, idx2string (ci->iso3lang)));
254         MONO_OBJECT_SETREF (this, iso2lang, mono_string_new (domain, idx2string (ci->iso2lang)));
255         MONO_OBJECT_SETREF (this, territory, mono_string_new (domain, idx2string (ci->territory)));
256         this->parent_lcid = ci->parent_lcid;
257         this->specific_lcid = ci->specific_lcid;
258         this->datetime_index = ci->datetime_format_index;
259         this->number_index = ci->number_format_index;
260         this->calendar_data = ci->calendar_data;
261         this->text_info_data = &ci->text_info;
262         
263         return TRUE;
264 }
265
266 static MonoBoolean
267 construct_region (MonoRegionInfo *this, const RegionInfoEntry *ri)
268 {
269         MonoDomain *domain = mono_domain_get ();
270
271         this->region_id = ri->region_id;
272         MONO_OBJECT_SETREF (this, iso2name, mono_string_new (domain, idx2string (ri->iso2name)));
273         MONO_OBJECT_SETREF (this, iso3name, mono_string_new (domain, idx2string (ri->iso3name)));
274         MONO_OBJECT_SETREF (this, win3name, mono_string_new (domain, idx2string (ri->win3name)));
275         MONO_OBJECT_SETREF (this, english_name, mono_string_new (domain, idx2string (ri->english_name)));
276         MONO_OBJECT_SETREF (this, currency_symbol, mono_string_new (domain, idx2string (ri->currency_symbol)));
277         MONO_OBJECT_SETREF (this, iso_currency_symbol, mono_string_new (domain, idx2string (ri->iso_currency_symbol)));
278         MONO_OBJECT_SETREF (this, currency_english_name, mono_string_new (domain, idx2string (ri->currency_english_name)));
279         
280         return TRUE;
281 }
282
283 static gboolean
284 construct_culture_from_specific_name (MonoCultureInfo *ci, gchar *name)
285 {
286         const CultureInfoEntry *entry;
287         const CultureInfoNameEntry *ne;
288
289         MONO_ARCH_SAVE_REGS;
290
291         ne = bsearch (name, culture_name_entries, NUM_CULTURE_ENTRIES,
292                         sizeof (CultureInfoNameEntry), culture_name_locator);
293
294         if (ne == NULL)
295                 return FALSE;
296
297         entry = &culture_entries [ne->culture_entry_index];
298
299         /* try avoiding another lookup, often the culture is its own specific culture */
300         if (entry->lcid != entry->specific_lcid)
301                 entry = culture_info_entry_from_lcid (entry->specific_lcid);
302
303         return construct_culture (ci, entry);
304 }
305
306 static const CultureInfoEntry*
307 culture_info_entry_from_lcid (int lcid)
308 {
309         const CultureInfoEntry *ci;
310         CultureInfoEntry key;
311
312         key.lcid = lcid;
313         ci = bsearch (&key, culture_entries, NUM_CULTURE_ENTRIES, sizeof (CultureInfoEntry), culture_lcid_locator);
314
315         return ci;
316 }
317
318 static const RegionInfoEntry*
319 region_info_entry_from_lcid (int lcid)
320 {
321         const RegionInfoEntry *entry;
322         const CultureInfoEntry *ne;
323
324         MONO_ARCH_SAVE_REGS;
325
326         ne = bsearch (&lcid, culture_entries, NUM_CULTURE_ENTRIES,
327                         sizeof (CultureInfoEntry), region_lcid_locator);
328
329         if (ne == NULL)
330                 return FALSE;
331
332         entry = &region_entries [ne->region_entry_index];
333
334         return entry;
335 }
336
337 /*
338  * The following two methods are modified from the ICU source code. (http://oss.software.ibm.com/icu)
339  * Copyright (c) 1995-2003 International Business Machines Corporation and others
340  * All rights reserved.
341  */
342 static gchar*
343 get_posix_locale (void)
344 {
345         const gchar* posix_locale = NULL;
346
347         posix_locale = g_getenv("LC_ALL");
348         if (posix_locale == 0) {
349                 posix_locale = g_getenv("LANG");
350                 if (posix_locale == 0) {
351                         posix_locale = setlocale(LC_ALL, NULL);
352                 }
353         }
354
355         if (posix_locale == NULL)
356                 return NULL;
357
358         if ((strcmp ("C", posix_locale) == 0) || (strchr (posix_locale, ' ') != NULL)
359                         || (strchr (posix_locale, '/') != NULL)) {
360                 /*
361                  * HPUX returns 'C C C C C C C'
362                  * Solaris can return /en_US/C/C/C/C/C on the second try.
363                  * Maybe we got some garbage.
364                  */
365                 return NULL;
366         }
367
368         return g_strdup (posix_locale);
369 }
370
371 static gchar*
372 get_current_locale_name (void)
373 {
374         gchar *locale;
375         gchar *corrected = NULL;
376         const gchar *p;
377         gchar *c;
378
379 #ifdef PLATFORM_WIN32
380         locale = g_win32_getlocale ();
381 #else   
382         locale = get_posix_locale ();
383 #endif  
384
385         if (locale == NULL)
386                 return NULL;
387
388         if ((p = strchr (locale, '.')) != NULL) {
389                 /* assume new locale can't be larger than old one? */
390                 corrected = malloc (strlen (locale));
391                 strncpy (corrected, locale, p - locale);
392                 corrected [p - locale] = 0;
393
394                 /* do not copy after the @ */
395                 if ((p = strchr (corrected, '@')) != NULL)
396                         corrected [p - corrected] = 0;
397         }
398
399         /* Note that we scan the *uncorrected* ID. */
400         if ((p = strrchr (locale, '@')) != NULL) {
401
402                 /*
403                  * In Mono we dont handle the '@' modifier because we do
404                  * not have any cultures that use it. We just trim it
405                  * off of the end of the name.
406                  */
407
408                 if (corrected == NULL) {
409                         corrected = malloc (strlen (locale));
410                         strncpy (corrected, locale, p - locale);
411                         corrected [p - locale] = 0;
412                 }
413         }
414
415         if (corrected == NULL)
416                 corrected = locale;
417         else
418                 g_free (locale);
419
420         if ((c = strchr (corrected, '_')) != NULL)
421                 *c = '-';
422
423         c = corrected;
424         corrected = g_ascii_strdown (c, -1);
425         g_free (c);
426
427         return corrected;
428 }        
429
430 MonoBoolean
431 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_current_locale (MonoCultureInfo *ci)
432 {
433         gchar *locale;
434         gboolean ret;
435
436         MONO_ARCH_SAVE_REGS;
437
438         locale = get_current_locale_name ();
439         if (locale == NULL)
440                 return FALSE;
441
442         ret = construct_culture_from_specific_name (ci, locale);
443         g_free (locale);
444         ci->is_read_only = TRUE;
445         ci->use_user_override = TRUE;
446
447         return ret;
448 }
449
450 MonoBoolean
451 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_lcid (MonoCultureInfo *this,
452                 gint lcid)
453 {
454         const CultureInfoEntry *ci;
455         
456         MONO_ARCH_SAVE_REGS;
457
458         ci = culture_info_entry_from_lcid (lcid);
459         if(ci == NULL)
460                 return FALSE;
461
462         return construct_culture (this, ci);
463 }
464
465 MonoBoolean
466 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_name (MonoCultureInfo *this,
467                 MonoString *name)
468 {
469         const CultureInfoNameEntry *ne;
470         char *n;
471         
472         MONO_ARCH_SAVE_REGS;
473
474         n = mono_string_to_utf8 (name);
475         ne = bsearch (n, culture_name_entries, NUM_CULTURE_ENTRIES,
476                         sizeof (CultureInfoNameEntry), culture_name_locator);
477
478         if (ne == NULL) {
479                 /*g_print ("ne (%s) is null\n", n);*/
480                 g_free (n);
481                 return FALSE;
482         }
483         g_free (n);
484
485         return construct_culture (this, &culture_entries [ne->culture_entry_index]);
486 }
487
488 MonoBoolean
489 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_specific_name (MonoCultureInfo *ci,
490                 MonoString *name)
491 {
492         gchar *locale;
493         gboolean ret;
494
495         MONO_ARCH_SAVE_REGS;
496
497         locale = mono_string_to_utf8 (name);
498         ret = construct_culture_from_specific_name (ci, locale);
499         g_free (locale);
500
501         return ret;
502 }
503
504 MonoBoolean
505 ves_icall_System_Globalization_RegionInfo_construct_internal_region_from_lcid (MonoRegionInfo *this,
506                 gint lcid)
507 {
508         const RegionInfoEntry *ri;
509         
510         MONO_ARCH_SAVE_REGS;
511
512         ri = region_info_entry_from_lcid (lcid);
513         if(ri == NULL)
514                 return FALSE;
515
516         return construct_region (this, ri);
517 }
518
519 MonoBoolean
520 ves_icall_System_Globalization_RegionInfo_construct_internal_region_from_name (MonoRegionInfo *this,
521                 MonoString *name)
522 {
523         const RegionInfoNameEntry *ne;
524         char *n;
525         
526         MONO_ARCH_SAVE_REGS;
527
528         n = mono_string_to_utf8 (name);
529         ne = bsearch (n, region_name_entries, NUM_REGION_ENTRIES,
530                 sizeof (RegionInfoNameEntry), region_name_locator);
531
532         if (ne == NULL) {
533                 /*g_print ("ne (%s) is null\n", n);*/
534                 g_free (n);
535                 return FALSE;
536         }
537         g_free (n);
538
539         return construct_region (this, &region_entries [ne->region_entry_index]);
540 }
541
542 MonoArray*
543 ves_icall_System_Globalization_CultureInfo_internal_get_cultures (MonoBoolean neutral,
544                 MonoBoolean specific, MonoBoolean installed)
545 {
546         MonoArray *ret;
547         MonoClass *class;
548         MonoCultureInfo *culture;
549         MonoDomain *domain;
550         const CultureInfoEntry *ci;
551         gint i, len;
552         gboolean is_neutral;
553
554         MONO_ARCH_SAVE_REGS;
555
556         domain = mono_domain_get ();
557
558         len = 0;
559         for (i = 0; i < NUM_CULTURE_ENTRIES; i++) {
560                 ci = &culture_entries [i];
561                 is_neutral = ((ci->lcid & 0xff00) == 0 || ci->specific_lcid == 0);
562                 if ((neutral && is_neutral) || (specific && !is_neutral))
563                         len++;
564         }
565
566         class = mono_class_from_name (mono_get_corlib (),
567                         "System.Globalization", "CultureInfo");
568
569         /* The InvariantCulture is not in culture_entries */
570         /* We reserve the first slot in the array for it */
571         if (neutral)
572                 len++;
573
574         ret = mono_array_new (domain, class, len);
575
576         if (len == 0)
577                 return ret;
578
579         len = 0;
580         if (neutral)
581                 mono_array_setref (ret, len++, NULL);
582
583         for (i = 0; i < NUM_CULTURE_ENTRIES; i++) {
584                 ci = &culture_entries [i];
585                 is_neutral = ((ci->lcid & 0xff00) == 0 || ci->specific_lcid == 0);
586                 if ((neutral && is_neutral) || (specific && !is_neutral)) {
587                         culture = (MonoCultureInfo *) mono_object_new (domain, class);
588                         mono_runtime_object_init ((MonoObject *) culture);
589                         construct_culture (culture, ci);
590                         culture->use_user_override = TRUE;
591                         mono_array_setref (ret, len++, culture);
592                 }
593         }
594
595         return ret;
596 }
597
598 /**
599  * ves_icall_System_Globalization_CultureInfo_internal_is_lcid_neutral:
600  * 
601  * Set is_neutral and return TRUE if the culture is found. If it is not found return FALSE.
602  */
603 MonoBoolean
604 ves_icall_System_Globalization_CultureInfo_internal_is_lcid_neutral (gint lcid, MonoBoolean *is_neutral)
605 {
606         const CultureInfoEntry *entry;
607
608         MONO_ARCH_SAVE_REGS;
609
610         entry = culture_info_entry_from_lcid (lcid);
611
612         if (entry == NULL)
613                 return FALSE;
614
615         *is_neutral = (entry->specific_lcid == 0);
616
617         return TRUE;
618 }
619
620 void ves_icall_System_Globalization_CompareInfo_construct_compareinfo (MonoCompareInfo *comp, MonoString *locale)
621 {
622         /* Nothing to do here */
623 }
624
625 int ves_icall_System_Globalization_CompareInfo_internal_compare (MonoCompareInfo *this, MonoString *str1, gint32 off1, gint32 len1, MonoString *str2, gint32 off2, gint32 len2, gint32 options)
626 {
627         MONO_ARCH_SAVE_REGS;
628         
629         /* Do a normal ascii string compare, as we only know the
630          * invariant locale if we dont have ICU
631          */
632         return(string_invariant_compare (str1, off1, len1, str2, off2, len2,
633                                          options));
634 }
635
636 void ves_icall_System_Globalization_CompareInfo_free_internal_collator (MonoCompareInfo *this)
637 {
638         /* Nothing to do here */
639 }
640
641 void ves_icall_System_Globalization_CompareInfo_assign_sortkey (MonoCompareInfo *this, MonoSortKey *key, MonoString *source, gint32 options)
642 {
643         MonoArray *arr;
644         gint32 keylen, i;
645
646         MONO_ARCH_SAVE_REGS;
647         
648         keylen=mono_string_length (source);
649         
650         arr=mono_array_new (mono_domain_get (), mono_get_byte_class (),
651                             keylen);
652         for(i=0; i<keylen; i++) {
653                 mono_array_set (arr, guint8, i, mono_string_chars (source)[i]);
654         }
655         
656         MONO_OBJECT_SETREF (key, key, arr);
657 }
658
659 int ves_icall_System_Globalization_CompareInfo_internal_index (MonoCompareInfo *this, MonoString *source, gint32 sindex, gint32 count, MonoString *value, gint32 options, MonoBoolean first)
660 {
661         MONO_ARCH_SAVE_REGS;
662         
663         return(string_invariant_indexof (source, sindex, count, value, first));
664 }
665
666 int ves_icall_System_Globalization_CompareInfo_internal_index_char (MonoCompareInfo *this, MonoString *source, gint32 sindex, gint32 count, gunichar2 value, gint32 options, MonoBoolean first)
667 {
668         MONO_ARCH_SAVE_REGS;
669         
670         return(string_invariant_indexof_char (source, sindex, count, value,
671                                               first));
672 }
673
674 int ves_icall_System_Threading_Thread_current_lcid (void)
675 {
676         MONO_ARCH_SAVE_REGS;
677         
678         /* Invariant */
679         return(0x007F);
680 }
681
682 MonoString *ves_icall_System_String_InternalReplace_Str_Comp (MonoString *this, MonoString *old, MonoString *new, MonoCompareInfo *comp)
683 {
684         MONO_ARCH_SAVE_REGS;
685         
686         /* Do a normal ascii string compare and replace, as we only
687          * know the invariant locale if we dont have ICU
688          */
689         return(string_invariant_replace (this, old, new));
690 }
691
692 static gint32 string_invariant_compare_char (gunichar2 c1, gunichar2 c2,
693                                              gint32 options)
694 {
695         gint32 result;
696
697         /* Ordinal can not be mixed with other options, and must return the difference, not only -1, 0, 1 */
698         if (options & CompareOptions_Ordinal) 
699                 return (gint32) c1 - c2;
700         
701         if (options & CompareOptions_IgnoreCase) {
702                 GUnicodeType c1type, c2type;
703
704                 c1type = g_unichar_type (c1);
705                 c2type = g_unichar_type (c2);
706         
707                 result = (gint32) (c1type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c1) : c1) -
708                         (c2type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c2) : c2);
709         } else {
710                 /*
711                  * No options. Kana, symbol and spacing options don't
712                  * apply to the invariant culture.
713                  */
714
715                 /*
716                  * FIXME: here we must use the information from c1type and c2type
717                  * to find out the proper collation, even on the InvariantCulture, the
718                  * sorting is not done by computing the unicode values, but their
719                  * actual sort order.
720                  */
721                 result = (gint32) c1 - c2;
722         }
723
724         return ((result < 0) ? -1 : (result > 0) ? 1 : 0);
725 }
726
727 static gint32 string_invariant_compare (MonoString *str1, gint32 off1,
728                                         gint32 len1, MonoString *str2,
729                                         gint32 off2, gint32 len2,
730                                         gint32 options)
731 {
732         /* c translation of C# code from old string.cs.. :) */
733         gint32 length;
734         gint32 charcmp;
735         gunichar2 *ustr1;
736         gunichar2 *ustr2;
737         gint32 pos;
738
739         if(len1 >= len2) {
740                 length=len1;
741         } else {
742                 length=len2;
743         }
744
745         ustr1 = mono_string_chars(str1)+off1;
746         ustr2 = mono_string_chars(str2)+off2;
747
748         pos = 0;
749
750         for (pos = 0; pos != length; pos++) {
751                 if (pos >= len1 || pos >= len2)
752                         break;
753
754                 charcmp = string_invariant_compare_char(ustr1[pos], ustr2[pos],
755                                                         options);
756                 if (charcmp != 0) {
757                         return(charcmp);
758                 }
759         }
760
761         /* the lesser wins, so if we have looped until length we just
762          * need to check the last char
763          */
764         if (pos == length) {
765                 return(string_invariant_compare_char(ustr1[pos - 1],
766                                                      ustr2[pos - 1], options));
767         }
768
769         /* Test if one of the strings has been compared to the end */
770         if (pos >= len1) {
771                 if (pos >= len2) {
772                         return(0);
773                 } else {
774                         return(-1);
775                 }
776         } else if (pos >= len2) {
777                 return(1);
778         }
779
780         /* if not, check our last char only.. (can this happen?) */
781         return(string_invariant_compare_char(ustr1[pos], ustr2[pos], options));
782 }
783
784 static MonoString *string_invariant_replace (MonoString *me,
785                                              MonoString *oldValue,
786                                              MonoString *newValue)
787 {
788         MonoString *ret;
789         gunichar2 *src;
790         gunichar2 *dest=NULL; /* shut gcc up */
791         gunichar2 *oldstr;
792         gunichar2 *newstr=NULL; /* shut gcc up here too */
793         gint32 i, destpos;
794         gint32 occurr;
795         gint32 newsize;
796         gint32 oldstrlen;
797         gint32 newstrlen;
798         gint32 srclen;
799
800         occurr = 0;
801         destpos = 0;
802
803         oldstr = mono_string_chars(oldValue);
804         oldstrlen = mono_string_length(oldValue);
805
806         if (NULL != newValue) {
807                 newstr = mono_string_chars(newValue);
808                 newstrlen = mono_string_length(newValue);
809         } else
810                 newstrlen = 0;
811
812         src = mono_string_chars(me);
813         srclen = mono_string_length(me);
814
815         if (oldstrlen != newstrlen) {
816                 i = 0;
817                 while (i <= srclen - oldstrlen) {
818                         if (0 == memcmp(src + i, oldstr, oldstrlen * sizeof(gunichar2))) {
819                                 occurr++;
820                                 i += oldstrlen;
821                         }
822                         else
823                                 i ++;
824                 }
825                 if (occurr == 0)
826                         return me;
827                 newsize = srclen + ((newstrlen - oldstrlen) * occurr);
828         } else
829                 newsize = srclen;
830
831         ret = NULL;
832         i = 0;
833         while (i < srclen) {
834                 if (0 == memcmp(src + i, oldstr, oldstrlen * sizeof(gunichar2))) {
835                         if (ret == NULL) {
836                                 ret = mono_string_new_size( mono_domain_get (), newsize);
837                                 dest = mono_string_chars(ret);
838                                 memcpy (dest, src, i * sizeof(gunichar2));
839                         }
840                         if (newstrlen > 0) {
841                                 memcpy(dest + destpos, newstr, newstrlen * sizeof(gunichar2));
842                                 destpos += newstrlen;
843                         }
844                         i += oldstrlen;
845                         continue;
846                 } else if (ret != NULL) {
847                         dest[destpos] = src[i];
848                 }
849                 destpos++;
850                 i++;
851         }
852         
853         if (ret == NULL)
854                 return me;
855
856         return ret;
857 }
858
859 static gint32 string_invariant_indexof (MonoString *source, gint32 sindex,
860                                         gint32 count, MonoString *value,
861                                         MonoBoolean first)
862 {
863         gint32 lencmpstr;
864         gunichar2 *src;
865         gunichar2 *cmpstr;
866         gint32 pos,i;
867         
868         lencmpstr = mono_string_length(value);
869         
870         src = mono_string_chars(source);
871         cmpstr = mono_string_chars(value);
872
873         if(first) {
874                 count -= lencmpstr;
875                 for(pos=sindex;pos <= sindex+count;pos++) {
876                         for(i=0;src[pos+i]==cmpstr[i];) {
877                                 if(++i==lencmpstr) {
878                                         return(pos);
879                                 }
880                         }
881                 }
882                 
883                 return(-1);
884         } else {
885                 for(pos=sindex-lencmpstr+1;pos>sindex-count;pos--) {
886                         if(memcmp (src+pos, cmpstr,
887                                    lencmpstr*sizeof(gunichar2))==0) {
888                                 return(pos);
889                         }
890                 }
891                 
892                 return(-1);
893         }
894 }
895
896 static gint32 string_invariant_indexof_char (MonoString *source, gint32 sindex,
897                                              gint32 count, gunichar2 value,
898                                              MonoBoolean first)
899 {
900         gint32 pos;
901         gunichar2 *src;
902
903         src = mono_string_chars(source);
904         if(first) {
905                 for (pos = sindex; pos != count + sindex; pos++) {
906                         if (src [pos] == value) {
907                                 return(pos);
908                         }
909                 }
910
911                 return(-1);
912         } else {
913                 for (pos = sindex; pos > sindex - count; pos--) {
914                         if (src [pos] == value)
915                                 return(pos);
916                 }
917
918                 return(-1);
919         }
920 }
921
922 void load_normalization_resource (guint8 **argProps,
923                                   guint8 **argMappedChars,
924                                   guint8 **argCharMapIndex,
925                                   guint8 **argHelperIndex,
926                                   guint8 **argMapIdxToComposite,
927                                   guint8 **argCombiningClass)
928 {
929         *argProps = (guint8*)props;
930         *argMappedChars = (guint8*) mappedChars;
931         *argCharMapIndex = (guint8*) charMapIndex;
932         *argHelperIndex = (guint8*) helperIndex;
933         *argMapIdxToComposite = (guint8*) mapIdxToComposite;
934         *argCombiningClass = (guint8*)combiningClass;
935 }
936
937