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