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