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