Merge pull request #819 from brendanzagaeski/patch-1
[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 const CultureInfoEntry*
310 culture_info_entry_from_lcid (int lcid)
311 {
312         const CultureInfoEntry *ci;
313
314         ci = mono_binary_search (&lcid, culture_entries, NUM_CULTURE_ENTRIES, sizeof (CultureInfoEntry), culture_lcid_locator);
315
316         return ci;
317 }
318
319 static const RegionInfoEntry*
320 region_info_entry_from_lcid (int lcid)
321 {
322         const RegionInfoEntry *entry;
323         const CultureInfoEntry *ne;
324
325         MONO_ARCH_SAVE_REGS;
326
327         ne = mono_binary_search (&lcid, culture_entries, NUM_CULTURE_ENTRIES, sizeof (CultureInfoEntry), culture_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 #if defined (__APPLE__)
338 static gchar*
339 get_darwin_locale (void)
340 {
341         static gchar *darwin_locale = NULL;
342         CFLocaleRef locale = NULL;
343         CFStringRef locale_language = NULL;
344         CFStringRef locale_country = NULL;
345         CFStringRef locale_script = NULL;
346         CFStringRef locale_cfstr = NULL;
347         CFIndex bytes_converted;
348         CFIndex bytes_written;
349         CFIndex len;
350         int i;
351
352         if (darwin_locale != NULL)
353                 return g_strdup (darwin_locale);
354
355         locale = CFLocaleCopyCurrent ();
356
357         if (locale) {
358                 locale_language = CFLocaleGetValue (locale, kCFLocaleLanguageCode);
359                 if (locale_language != NULL && CFStringGetBytes(locale_language, CFRangeMake (0, CFStringGetLength (locale_language)), kCFStringEncodingMacRoman, 0, FALSE, NULL, 0, &bytes_converted) > 0) {
360                         len = bytes_converted + 1;
361
362                         locale_country = CFLocaleGetValue (locale, kCFLocaleCountryCode);
363                         if (locale_country != NULL && CFStringGetBytes (locale_country, CFRangeMake (0, CFStringGetLength (locale_country)), kCFStringEncodingMacRoman, 0, FALSE, NULL, 0, &bytes_converted) > 0) {
364                                 len += bytes_converted + 1;
365
366                                 locale_script = CFLocaleGetValue (locale, kCFLocaleScriptCode);
367                                 if (locale_script != NULL && CFStringGetBytes (locale_script, CFRangeMake (0, CFStringGetLength (locale_script)), kCFStringEncodingMacRoman, 0, FALSE, NULL, 0, &bytes_converted) > 0) {
368                                         len += bytes_converted + 1;
369                                 }
370
371                                 darwin_locale = (char *) malloc (len + 1);
372                                 CFStringGetBytes (locale_language, CFRangeMake (0, CFStringGetLength (locale_language)), kCFStringEncodingMacRoman, 0, FALSE, (UInt8 *) darwin_locale, len, &bytes_converted);
373
374                                 darwin_locale[bytes_converted] = '-';
375                                 bytes_written = bytes_converted + 1;
376                                 if (locale_script != NULL && CFStringGetBytes (locale_script, CFRangeMake (0, CFStringGetLength (locale_script)), kCFStringEncodingMacRoman, 0, FALSE, (UInt8 *) &darwin_locale[bytes_written], len - bytes_written, &bytes_converted) > 0) {
377                                         darwin_locale[bytes_written + bytes_converted] = '-';
378                                         bytes_written += bytes_converted + 1;
379                                 }
380
381                                 CFStringGetBytes (locale_country, CFRangeMake (0, CFStringGetLength (locale_country)), kCFStringEncodingMacRoman, 0, FALSE, (UInt8 *) &darwin_locale[bytes_written], len - bytes_written, &bytes_converted);
382                                 darwin_locale[bytes_written + bytes_converted] = '\0';
383                         }
384                 }
385
386                 if (darwin_locale == NULL) {
387                         locale_cfstr = CFLocaleGetIdentifier (locale);
388
389                         if (locale_cfstr) {
390                                 len = CFStringGetMaximumSizeForEncoding (CFStringGetLength (locale_cfstr), kCFStringEncodingMacRoman) + 1;
391                                 darwin_locale = (char *) malloc (len);
392                                 if (!CFStringGetCString (locale_cfstr, darwin_locale, len, kCFStringEncodingMacRoman)) {
393                                         free (darwin_locale);
394                                         CFRelease (locale);
395                                         darwin_locale = NULL;
396                                         return NULL;
397                                 }
398
399                                 for (i = 0; i < strlen (darwin_locale); i++)
400                                         if (darwin_locale [i] == '_')
401                                                 darwin_locale [i] = '-';
402                         }                       
403                 }
404
405                 CFRelease (locale);
406         }
407
408         return g_strdup (darwin_locale);
409 }
410 #endif
411
412 static char *
413 get_posix_locale (void)
414 {
415         const char *locale;
416
417         locale = g_getenv ("LC_ALL");
418         if (locale == NULL) {
419                 locale = g_getenv ("LANG");
420                 if (locale == NULL)
421                         locale = setlocale (LC_ALL, NULL);
422         }
423         if (locale == NULL)
424                 return NULL;
425
426         /* Skip English-only locale 'C' */
427         if (strcmp (locale, "C") == 0)
428                 return NULL;
429
430         return g_strdup (locale);
431 }
432
433
434 static gchar *
435 get_current_locale_name (void)
436 {
437         char *locale;
438         char *p, *ret;
439                 
440 #ifdef HOST_WIN32
441         locale = g_win32_getlocale ();
442 #elif defined (__APPLE__)       
443         locale = get_darwin_locale ();
444         if (!locale)
445                 locale = get_posix_locale ();
446 #else
447         locale = get_posix_locale ();
448 #endif
449
450         if (locale == NULL)
451                 return NULL;
452
453         p = strchr (locale, '.');
454         if (p != NULL)
455                 *p = 0;
456         p = strchr (locale, '@');
457         if (p != NULL)
458                 *p = 0;
459         p = strchr (locale, '_');
460         if (p != NULL)
461                 *p = '-';
462
463         ret = g_ascii_strdown (locale, -1);
464         g_free (locale);
465
466         return ret;
467 }
468
469 MonoString*
470 ves_icall_System_Globalization_CultureInfo_get_current_locale_name (void)
471 {
472         gchar *locale;
473         MonoString* ret;
474         MonoDomain *domain;
475
476         MONO_ARCH_SAVE_REGS;
477
478         locale = get_current_locale_name ();
479         if (locale == NULL)
480                 return NULL;
481
482         domain = mono_domain_get ();
483         ret = mono_string_new (domain, locale);
484         g_free (locale);
485
486         return ret;
487 }
488
489 MonoBoolean
490 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_lcid (MonoCultureInfo *this,
491                 gint lcid)
492 {
493         const CultureInfoEntry *ci;
494         
495         MONO_ARCH_SAVE_REGS;
496
497         ci = culture_info_entry_from_lcid (lcid);
498         if(ci == NULL)
499                 return FALSE;
500
501         return construct_culture (this, ci);
502 }
503
504 MonoBoolean
505 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_name (MonoCultureInfo *this,
506                 MonoString *name)
507 {
508         const CultureInfoNameEntry *ne;
509         char *n;
510         
511         MONO_ARCH_SAVE_REGS;
512
513         n = mono_string_to_utf8 (name);
514         ne = mono_binary_search (n, culture_name_entries, NUM_CULTURE_ENTRIES,
515                         sizeof (CultureInfoNameEntry), culture_name_locator);
516
517         if (ne == NULL) {
518                 /*g_print ("ne (%s) is null\n", n);*/
519                 g_free (n);
520                 return FALSE;
521         }
522         g_free (n);
523
524         return construct_culture (this, &culture_entries [ne->culture_entry_index]);
525 }
526 /*
527 MonoBoolean
528 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_specific_name (MonoCultureInfo *ci,
529                 MonoString *name)
530 {
531         gchar *locale;
532         gboolean ret;
533
534         MONO_ARCH_SAVE_REGS;
535
536         locale = mono_string_to_utf8 (name);
537         ret = construct_culture_from_specific_name (ci, locale);
538         g_free (locale);
539
540         return ret;
541 }
542 */
543 MonoBoolean
544 ves_icall_System_Globalization_RegionInfo_construct_internal_region_from_lcid (MonoRegionInfo *this,
545                 gint lcid)
546 {
547         const RegionInfoEntry *ri;
548         
549         MONO_ARCH_SAVE_REGS;
550
551         ri = region_info_entry_from_lcid (lcid);
552         if(ri == NULL)
553                 return FALSE;
554
555         return construct_region (this, ri);
556 }
557
558 MonoBoolean
559 ves_icall_System_Globalization_RegionInfo_construct_internal_region_from_name (MonoRegionInfo *this,
560                 MonoString *name)
561 {
562         const RegionInfoNameEntry *ne;
563         char *n;
564         
565         MONO_ARCH_SAVE_REGS;
566
567         n = mono_string_to_utf8 (name);
568         ne = mono_binary_search (n, region_name_entries, NUM_REGION_ENTRIES,
569                 sizeof (RegionInfoNameEntry), region_name_locator);
570
571         if (ne == NULL) {
572                 /*g_print ("ne (%s) is null\n", n);*/
573                 g_free (n);
574                 return FALSE;
575         }
576         g_free (n);
577
578         return construct_region (this, &region_entries [ne->region_entry_index]);
579 }
580
581 MonoArray*
582 ves_icall_System_Globalization_CultureInfo_internal_get_cultures (MonoBoolean neutral,
583                 MonoBoolean specific, MonoBoolean installed)
584 {
585         MonoArray *ret;
586         MonoClass *class;
587         MonoCultureInfo *culture;
588         MonoDomain *domain;
589         const CultureInfoEntry *ci;
590         gint i, len;
591         gboolean is_neutral;
592
593         MONO_ARCH_SAVE_REGS;
594
595         domain = mono_domain_get ();
596
597         len = 0;
598         for (i = 0; i < NUM_CULTURE_ENTRIES; i++) {
599                 ci = &culture_entries [i];
600                 is_neutral = ci->territory == 0;
601                 if ((neutral && is_neutral) || (specific && !is_neutral))
602                         len++;
603         }
604
605         class = mono_class_from_name (mono_get_corlib (),
606                         "System.Globalization", "CultureInfo");
607
608         /* The InvariantCulture is not in culture_entries */
609         /* We reserve the first slot in the array for it */
610         if (neutral)
611                 len++;
612
613         ret = mono_array_new (domain, class, len);
614
615         if (len == 0)
616                 return ret;
617
618         len = 0;
619         if (neutral)
620                 mono_array_setref (ret, len++, NULL);
621
622         for (i = 0; i < NUM_CULTURE_ENTRIES; i++) {
623                 ci = &culture_entries [i];
624                 is_neutral = ci->territory == 0;
625                 if ((neutral && is_neutral) || (specific && !is_neutral)) {
626                         culture = (MonoCultureInfo *) mono_object_new (domain, class);
627                         mono_runtime_object_init ((MonoObject *) culture);
628                         construct_culture (culture, ci);
629                         culture->use_user_override = TRUE;
630                         mono_array_setref (ret, len++, culture);
631                 }
632         }
633
634         return ret;
635 }
636
637 void ves_icall_System_Globalization_CompareInfo_construct_compareinfo (MonoCompareInfo *comp, MonoString *locale)
638 {
639         /* Nothing to do here */
640 }
641
642 int ves_icall_System_Globalization_CompareInfo_internal_compare (MonoCompareInfo *this, MonoString *str1, gint32 off1, gint32 len1, MonoString *str2, gint32 off2, gint32 len2, gint32 options)
643 {
644         MONO_ARCH_SAVE_REGS;
645         
646         /* Do a normal ascii string compare, as we only know the
647          * invariant locale if we dont have ICU
648          */
649         return(string_invariant_compare (str1, off1, len1, str2, off2, len2,
650                                          options));
651 }
652
653 void ves_icall_System_Globalization_CompareInfo_free_internal_collator (MonoCompareInfo *this)
654 {
655         /* Nothing to do here */
656 }
657
658 void ves_icall_System_Globalization_CompareInfo_assign_sortkey (MonoCompareInfo *this, MonoSortKey *key, MonoString *source, gint32 options)
659 {
660         MonoArray *arr;
661         gint32 keylen, i;
662
663         MONO_ARCH_SAVE_REGS;
664         
665         keylen=mono_string_length (source);
666         
667         arr=mono_array_new (mono_domain_get (), mono_get_byte_class (),
668                             keylen);
669         for(i=0; i<keylen; i++) {
670                 mono_array_set (arr, guint8, i, mono_string_chars (source)[i]);
671         }
672         
673         MONO_OBJECT_SETREF (key, key, arr);
674 }
675
676 int ves_icall_System_Globalization_CompareInfo_internal_index (MonoCompareInfo *this, MonoString *source, gint32 sindex, gint32 count, MonoString *value, gint32 options, MonoBoolean first)
677 {
678         MONO_ARCH_SAVE_REGS;
679         
680         return(string_invariant_indexof (source, sindex, count, value, first));
681 }
682
683 int ves_icall_System_Globalization_CompareInfo_internal_index_char (MonoCompareInfo *this, MonoString *source, gint32 sindex, gint32 count, gunichar2 value, gint32 options, MonoBoolean first)
684 {
685         MONO_ARCH_SAVE_REGS;
686         
687         return(string_invariant_indexof_char (source, sindex, count, value,
688                                               first));
689 }
690
691 int ves_icall_System_Threading_Thread_current_lcid (void)
692 {
693         MONO_ARCH_SAVE_REGS;
694         
695         /* Invariant */
696         return(0x007F);
697 }
698
699 MonoString *ves_icall_System_String_InternalReplace_Str_Comp (MonoString *this, MonoString *old, MonoString *new, MonoCompareInfo *comp)
700 {
701         MONO_ARCH_SAVE_REGS;
702         
703         /* Do a normal ascii string compare and replace, as we only
704          * know the invariant locale if we dont have ICU
705          */
706         return(string_invariant_replace (this, old, new));
707 }
708
709 static gint32 string_invariant_compare_char (gunichar2 c1, gunichar2 c2,
710                                              gint32 options)
711 {
712         gint32 result;
713
714         /* Ordinal can not be mixed with other options, and must return the difference, not only -1, 0, 1 */
715         if (options & CompareOptions_Ordinal) 
716                 return (gint32) c1 - c2;
717         
718         if (options & CompareOptions_IgnoreCase) {
719                 GUnicodeType c1type, c2type;
720
721                 c1type = g_unichar_type (c1);
722                 c2type = g_unichar_type (c2);
723         
724                 result = (gint32) (c1type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c1) : c1) -
725                         (c2type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c2) : c2);
726         } else {
727                 /*
728                  * No options. Kana, symbol and spacing options don't
729                  * apply to the invariant culture.
730                  */
731
732                 /*
733                  * FIXME: here we must use the information from c1type and c2type
734                  * to find out the proper collation, even on the InvariantCulture, the
735                  * sorting is not done by computing the unicode values, but their
736                  * actual sort order.
737                  */
738                 result = (gint32) c1 - c2;
739         }
740
741         return ((result < 0) ? -1 : (result > 0) ? 1 : 0);
742 }
743
744 static gint32 string_invariant_compare (MonoString *str1, gint32 off1,
745                                         gint32 len1, MonoString *str2,
746                                         gint32 off2, gint32 len2,
747                                         gint32 options)
748 {
749         /* c translation of C# code from old string.cs.. :) */
750         gint32 length;
751         gint32 charcmp;
752         gunichar2 *ustr1;
753         gunichar2 *ustr2;
754         gint32 pos;
755
756         if(len1 >= len2) {
757                 length=len1;
758         } else {
759                 length=len2;
760         }
761
762         ustr1 = mono_string_chars(str1)+off1;
763         ustr2 = mono_string_chars(str2)+off2;
764
765         pos = 0;
766
767         for (pos = 0; pos != length; pos++) {
768                 if (pos >= len1 || pos >= len2)
769                         break;
770
771                 charcmp = string_invariant_compare_char(ustr1[pos], ustr2[pos],
772                                                         options);
773                 if (charcmp != 0) {
774                         return(charcmp);
775                 }
776         }
777
778         /* the lesser wins, so if we have looped until length we just
779          * need to check the last char
780          */
781         if (pos == length) {
782                 return(string_invariant_compare_char(ustr1[pos - 1],
783                                                      ustr2[pos - 1], options));
784         }
785
786         /* Test if one of the strings has been compared to the end */
787         if (pos >= len1) {
788                 if (pos >= len2) {
789                         return(0);
790                 } else {
791                         return(-1);
792                 }
793         } else if (pos >= len2) {
794                 return(1);
795         }
796
797         /* if not, check our last char only.. (can this happen?) */
798         return(string_invariant_compare_char(ustr1[pos], ustr2[pos], options));
799 }
800
801 static MonoString *string_invariant_replace (MonoString *me,
802                                              MonoString *oldValue,
803                                              MonoString *newValue)
804 {
805         MonoString *ret;
806         gunichar2 *src;
807         gunichar2 *dest=NULL; /* shut gcc up */
808         gunichar2 *oldstr;
809         gunichar2 *newstr=NULL; /* shut gcc up here too */
810         gint32 i, destpos;
811         gint32 occurr;
812         gint32 newsize;
813         gint32 oldstrlen;
814         gint32 newstrlen;
815         gint32 srclen;
816
817         occurr = 0;
818         destpos = 0;
819
820         oldstr = mono_string_chars(oldValue);
821         oldstrlen = mono_string_length(oldValue);
822
823         if (NULL != newValue) {
824                 newstr = mono_string_chars(newValue);
825                 newstrlen = mono_string_length(newValue);
826         } else
827                 newstrlen = 0;
828
829         src = mono_string_chars(me);
830         srclen = mono_string_length(me);
831
832         if (oldstrlen != newstrlen) {
833                 i = 0;
834                 while (i <= srclen - oldstrlen) {
835                         if (0 == memcmp(src + i, oldstr, oldstrlen * sizeof(gunichar2))) {
836                                 occurr++;
837                                 i += oldstrlen;
838                         }
839                         else
840                                 i ++;
841                 }
842                 if (occurr == 0)
843                         return me;
844                 newsize = srclen + ((newstrlen - oldstrlen) * occurr);
845         } else
846                 newsize = srclen;
847
848         ret = NULL;
849         i = 0;
850         while (i < srclen) {
851                 if (0 == memcmp(src + i, oldstr, oldstrlen * sizeof(gunichar2))) {
852                         if (ret == NULL) {
853                                 ret = mono_string_new_size( mono_domain_get (), newsize);
854                                 dest = mono_string_chars(ret);
855                                 memcpy (dest, src, i * sizeof(gunichar2));
856                         }
857                         if (newstrlen > 0) {
858                                 memcpy(dest + destpos, newstr, newstrlen * sizeof(gunichar2));
859                                 destpos += newstrlen;
860                         }
861                         i += oldstrlen;
862                         continue;
863                 } else if (ret != NULL) {
864                         dest[destpos] = src[i];
865                 }
866                 destpos++;
867                 i++;
868         }
869         
870         if (ret == NULL)
871                 return me;
872
873         return ret;
874 }
875
876 static gint32 string_invariant_indexof (MonoString *source, gint32 sindex,
877                                         gint32 count, MonoString *value,
878                                         MonoBoolean first)
879 {
880         gint32 lencmpstr;
881         gunichar2 *src;
882         gunichar2 *cmpstr;
883         gint32 pos,i;
884         
885         lencmpstr = mono_string_length(value);
886         
887         src = mono_string_chars(source);
888         cmpstr = mono_string_chars(value);
889
890         if(first) {
891                 count -= lencmpstr;
892                 for(pos=sindex;pos <= sindex+count;pos++) {
893                         for(i=0;src[pos+i]==cmpstr[i];) {
894                                 if(++i==lencmpstr) {
895                                         return(pos);
896                                 }
897                         }
898                 }
899                 
900                 return(-1);
901         } else {
902                 for(pos=sindex-lencmpstr+1;pos>sindex-count;pos--) {
903                         if(memcmp (src+pos, cmpstr,
904                                    lencmpstr*sizeof(gunichar2))==0) {
905                                 return(pos);
906                         }
907                 }
908                 
909                 return(-1);
910         }
911 }
912
913 static gint32 string_invariant_indexof_char (MonoString *source, gint32 sindex,
914                                              gint32 count, gunichar2 value,
915                                              MonoBoolean first)
916 {
917         gint32 pos;
918         gunichar2 *src;
919
920         src = mono_string_chars(source);
921         if(first) {
922                 for (pos = sindex; pos != count + sindex; pos++) {
923                         if (src [pos] == value) {
924                                 return(pos);
925                         }
926                 }
927
928                 return(-1);
929         } else {
930                 for (pos = sindex; pos > sindex - count; pos--) {
931                         if (src [pos] == value)
932                                 return(pos);
933                 }
934
935                 return(-1);
936         }
937 }
938
939 void load_normalization_resource (guint8 **argProps,
940                                   guint8 **argMappedChars,
941                                   guint8 **argCharMapIndex,
942                                   guint8 **argHelperIndex,
943                                   guint8 **argMapIdxToComposite,
944                                   guint8 **argCombiningClass)
945 {
946 #ifdef DISABLE_NORMALIZATION
947         mono_raise_exception (mono_get_exception_not_supported ("This runtime has been compiled without string normalization support."));
948 #else
949         *argProps = (guint8*)props;
950         *argMappedChars = (guint8*) mappedChars;
951         *argCharMapIndex = (guint8*) charMapIndex;
952         *argHelperIndex = (guint8*) helperIndex;
953         *argMapIdxToComposite = (guint8*) mapIdxToComposite;
954         *argCombiningClass = (guint8*)combiningClass;
955 #endif
956 }
957
958