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