2 * locales.c: Culture-sensitive handling
5 * Dick Porter (dick@ximian.com)
6 * Mohammad DAMT (mdamt@cdl2000.com)
8 * (C) 2003 Ximian, Inc.
9 * (C) 2003 PT Cakram Datalingga Duaribu http://www.cdl2000.com
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>
30 static gint32 string_invariant_compare_char (gunichar2 c1, gunichar2 c2,
32 static gint32 string_invariant_compare (MonoString *str1, gint32 off1,
33 gint32 len1, MonoString *str2,
34 gint32 off2, gint32 len2,
36 static MonoString *string_invariant_replace (MonoString *me,
38 MonoString *newValue);
39 static gint32 string_invariant_indexof (MonoString *source, gint32 sindex,
40 gint32 count, MonoString *value,
42 static gint32 string_invariant_indexof_char (MonoString *source, gint32 sindex,
43 gint32 count, gunichar2 value,
46 static const CultureInfoEntry* culture_info_entry_from_lcid (int lcid);
49 culture_lcid_locator (const void *a, const void *b)
51 const CultureInfoEntry *aa = a;
52 const CultureInfoEntry *bb = b;
54 return (aa->lcid - bb->lcid);
58 culture_name_locator (const void *a, const void *b)
60 const CultureInfoNameEntry *aa = a;
61 const CultureInfoNameEntry *bb = b;
64 ret = strcmp (aa->name, bb->name);
70 create_group_sizes_array (const gint *gs, gint ml)
75 for (i = 0; i < ml; i++) {
81 ret = mono_array_new (mono_domain_get (),
82 mono_defaults.int32_class, len);
84 for(i = 0; i < len; i++)
85 mono_array_set (ret, gint32, i, gs [i]);
91 create_names_array (const gchar *const *names, int ml)
100 domain = mono_domain_get ();
102 for (i = 0; i < ml; i++) {
103 if (names [i] == NULL)
108 ret = mono_array_new (mono_domain_get (), mono_defaults.string_class, len);
110 for(i = 0; i < len; i++)
111 mono_array_set (ret, MonoString *, i, mono_string_new (domain, names [i]));
117 ves_icall_System_Globalization_CultureInfo_construct_datetime_format (MonoCultureInfo *this)
120 MonoDateTimeFormatInfo *datetime;
121 const DateTimeFormatEntry *dfe;
125 g_assert (this->datetime_index >= 0);
127 datetime = this->datetime_format;
128 dfe = &datetime_format_entries [this->datetime_index];
130 domain = mono_domain_get ();
132 datetime->AbbreviatedDayNames = create_names_array (dfe->abbreviated_day_names,
134 datetime->AbbreviatedMonthNames = create_names_array (dfe->abbreviated_month_names,
136 datetime->AMDesignator = mono_string_new (domain, dfe->am_designator);
137 datetime->CalendarWeekRule = dfe->calendar_week_rule;
138 datetime->DateSeparator = mono_string_new (domain, dfe->date_separator);
139 datetime->DayNames = create_names_array (dfe->day_names, NUM_DAYS);
140 datetime->FirstDayOfWeek = dfe->first_day_of_week;
141 datetime->FullDateTimePattern = mono_string_new (domain, dfe->full_date_time_pattern);
142 datetime->LongDatePattern = mono_string_new (domain, dfe->long_date_pattern);
143 datetime->LongTimePattern = mono_string_new (domain, dfe->long_time_pattern);
144 datetime->MonthDayPattern = mono_string_new (domain, dfe->month_day_pattern);
145 datetime->MonthNames = create_names_array (dfe->month_names, NUM_MONTHS);
146 datetime->PMDesignator = mono_string_new (domain, dfe->pm_designator);
147 datetime->ShortDatePattern = mono_string_new (domain, dfe->short_date_pattern);
148 datetime->ShortTimePattern = mono_string_new (domain, dfe->short_time_pattern);
149 datetime->TimeSeparator = mono_string_new (domain, dfe->time_separator);
150 datetime->YearMonthPattern = mono_string_new (domain, dfe->year_month_pattern);
151 datetime->ShortDatePatterns = create_names_array (dfe->short_date_patterns,
152 NUM_SHORT_DATE_PATTERNS);
153 datetime->LongDatePatterns = create_names_array (dfe->long_date_patterns,
154 NUM_LONG_DATE_PATTERNS);
155 datetime->ShortTimePatterns = create_names_array (dfe->short_time_patterns,
156 NUM_SHORT_TIME_PATTERNS);
157 datetime->LongTimePatterns = create_names_array (dfe->long_time_patterns,
158 NUM_LONG_TIME_PATTERNS);
163 ves_icall_System_Globalization_CultureInfo_construct_number_format (MonoCultureInfo *this)
166 MonoNumberFormatInfo *number;
167 const NumberFormatEntry *nfe;
171 g_assert (this->number_format != 0);
173 number = this->number_format;
174 nfe = &number_format_entries [this->number_index];
176 domain = mono_domain_get ();
178 number->currencyDecimalDigits = nfe->currency_decimal_digits;
179 number->currencyDecimalSeparator = mono_string_new (domain,
180 nfe->currency_decimal_separator);
181 number->currencyGroupSeparator = mono_string_new (domain,
182 nfe->currency_group_separator);
183 number->currencyGroupSizes = create_group_sizes_array (nfe->currency_group_sizes,
185 number->currencyNegativePattern = nfe->currency_negative_pattern;
186 number->currencyPositivePattern = nfe->currency_positive_pattern;
187 number->currencySymbol = mono_string_new (domain, nfe->currency_symbol);
188 number->naNSymbol = mono_string_new (domain, nfe->nan_symbol);
189 number->negativeInfinitySymbol = mono_string_new (domain,
190 nfe->negative_infinity_symbol);
191 number->negativeSign = mono_string_new (domain, nfe->negative_sign);
192 number->numberDecimalDigits = nfe->number_decimal_digits;
193 number->numberDecimalSeparator = mono_string_new (domain,
194 nfe->number_decimal_separator);
195 number->numberGroupSeparator = mono_string_new (domain, nfe->number_group_separator);
196 number->numberGroupSizes = create_group_sizes_array (nfe->number_group_sizes,
198 number->numberNegativePattern = nfe->number_negative_pattern;
199 number->percentDecimalDigits = nfe->percent_decimal_digits;
200 number->percentDecimalSeparator = mono_string_new (domain,
201 nfe->percent_decimal_separator);
202 number->percentGroupSeparator = mono_string_new (domain,
203 nfe->percent_group_separator);
204 number->percentGroupSizes = create_group_sizes_array (nfe->percent_group_sizes,
206 number->percentNegativePattern = nfe->percent_negative_pattern;
207 number->percentPositivePattern = nfe->percent_positive_pattern;
208 number->percentSymbol = mono_string_new (domain, nfe->percent_symbol);
209 number->perMilleSymbol = mono_string_new (domain, nfe->per_mille_symbol);
210 number->positiveInfinitySymbol = mono_string_new (domain,
211 nfe->positive_infinity_symbol);
212 number->positiveSign = mono_string_new (domain, nfe->positive_sign);
216 construct_culture (MonoCultureInfo *this, const CultureInfoEntry *ci)
218 MonoDomain *domain = mono_domain_get ();
220 this->lcid = ci->lcid;
221 this->name = mono_string_new (domain, ci->name);
222 this->icu_name = mono_string_new (domain, ci->icu_name);
223 this->displayname = mono_string_new (domain, ci->displayname);
224 this->englishname = mono_string_new (domain, ci->englishname);
225 this->nativename = mono_string_new (domain, ci->nativename);
226 this->win3lang = mono_string_new (domain, ci->win3lang);
227 this->iso3lang = mono_string_new (domain, ci->iso3lang);
228 this->iso2lang = mono_string_new (domain, ci->iso2lang);
229 this->parent_lcid = ci->parent_lcid;
230 this->specific_lcid = ci->specific_lcid;
231 this->datetime_index = ci->datetime_format_index;
232 this->number_index = ci->number_format_index;
233 this->calendar_data = ci->calendar_data;
239 construct_culture_from_specific_name (MonoCultureInfo *ci, gchar *name)
241 const CultureInfoEntry *entry;
242 CultureInfoNameEntry key;
243 const CultureInfoNameEntry *ne;
248 ne = bsearch (&key, culture_name_entries, NUM_CULTURE_ENTRIES,
249 sizeof (CultureInfoNameEntry), culture_name_locator);
254 entry = &culture_entries [ne->culture_entry_index];
256 /* try avoiding another lookup, often the culture is its own specific culture */
257 if (entry->lcid != entry->specific_lcid)
258 entry = culture_info_entry_from_lcid (entry->specific_lcid);
260 return construct_culture (ci, entry);
263 static const CultureInfoEntry*
264 culture_info_entry_from_lcid (int lcid)
266 const CultureInfoEntry *ci;
267 CultureInfoEntry key;
270 ci = bsearch (&key, culture_entries, NUM_CULTURE_ENTRIES, sizeof (CultureInfoEntry), culture_lcid_locator);
276 * The following two methods are modified from the ICU source code. (http://oss.software.ibm.com/icu)
277 * Copyright (c) 1995-2003 International Business Machines Corporation and others
278 * All rights reserved.
281 get_posix_locale (void)
283 const gchar* posix_locale = NULL;
285 posix_locale = g_getenv("LC_ALL");
286 if (posix_locale == 0) {
287 posix_locale = g_getenv("LANG");
288 if (posix_locale == 0) {
289 posix_locale = setlocale(LC_ALL, NULL);
293 if (posix_locale == NULL)
296 if ((strcmp ("C", posix_locale) == 0) || (strchr (posix_locale, ' ') != NULL)
297 || (strchr (posix_locale, '/') != NULL)) {
299 * HPUX returns 'C C C C C C C'
300 * Solaris can return /en_US/C/C/C/C/C on the second try.
301 * Maybe we got some garbage.
306 return g_strdup (posix_locale);
310 get_current_locale_name (void)
313 gchar *corrected = NULL;
317 #ifdef PLATFORM_WIN32
318 locale = g_win32_getlocale ();
320 locale = get_posix_locale ();
326 if ((p = strchr (locale, '.')) != NULL) {
327 /* assume new locale can't be larger than old one? */
328 corrected = malloc (strlen (locale));
329 strncpy (corrected, locale, p - locale);
330 corrected [p - locale] = 0;
332 /* do not copy after the @ */
333 if ((p = strchr (corrected, '@')) != NULL)
334 corrected [p - corrected] = 0;
337 /* Note that we scan the *uncorrected* ID. */
338 if ((p = strrchr (locale, '@')) != NULL) {
341 * In Mono we dont handle the '@' modifier because we do
342 * not have any cultures that use it. We just trim it
343 * off of the end of the name.
346 if (corrected == NULL) {
347 corrected = malloc (strlen (locale));
348 strncpy (corrected, locale, p - locale);
349 corrected [p - locale] = 0;
353 if (corrected == NULL)
358 if ((c = strchr (corrected, '_')) != NULL)
361 g_strdown (corrected);
367 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_current_locale (MonoCultureInfo *ci)
374 locale = get_current_locale_name ();
378 ret = construct_culture_from_specific_name (ci, locale);
385 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_lcid (MonoCultureInfo *this,
388 const CultureInfoEntry *ci;
392 ci = culture_info_entry_from_lcid (lcid);
396 return construct_culture (this, ci);
400 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_name (MonoCultureInfo *this,
403 CultureInfoNameEntry key;
404 const CultureInfoNameEntry *ne;
408 key.name = mono_string_to_utf8 (name);
409 ne = bsearch (&key, culture_name_entries, NUM_CULTURE_ENTRIES,
410 sizeof (CultureInfoNameEntry), culture_name_locator);
412 g_free ((gpointer) key.name);
415 g_print ("ne (%s) is null\n", key.name);
419 return construct_culture (this, &culture_entries [ne->culture_entry_index]);
423 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_specific_name (MonoCultureInfo *ci,
431 locale = mono_string_to_utf8 (name);
432 ret = construct_culture_from_specific_name (ci, locale);
439 ves_icall_System_Globalization_CultureInfo_internal_get_cultures (MonoBoolean neutral,
440 MonoBoolean specific, MonoBoolean installed)
444 MonoCultureInfo *culture;
446 const CultureInfoEntry *ci;
452 domain = mono_domain_get ();
455 for (i = 0; i < NUM_CULTURE_ENTRIES; i++) {
456 ci = &culture_entries [i];
457 is_neutral = ((ci->lcid & 0xff00) == 0 || ci->specific_lcid == 0);
458 if ((neutral && is_neutral) || (specific && !is_neutral))
462 class = mono_class_from_name (mono_defaults.corlib,
463 "System.Globalization", "CultureInfo");
464 ret = mono_array_new (domain, class, len);
470 for (i = 0; i < NUM_CULTURE_ENTRIES; i++) {
471 ci = &culture_entries [i];
472 is_neutral = ((ci->lcid & 0xff00) == 0 || ci->specific_lcid == 0);
473 if ((neutral && is_neutral) || (specific && !is_neutral)) {
474 culture = (MonoCultureInfo *) mono_object_new (domain, class);
475 mono_runtime_object_init ((MonoObject *) culture);
476 construct_culture (culture, ci);
477 mono_array_set (ret, MonoCultureInfo *, len++, culture);
485 * Set is_neutral and return TRUE if the culture is found. If it is not found return FALSE.
488 ves_icall_System_Globalization_CultureInfo_internal_is_lcid_neutral (gint lcid, MonoBoolean *is_neutral)
490 const CultureInfoEntry *entry;
494 entry = culture_info_entry_from_lcid (lcid);
499 *is_neutral = (entry->specific_lcid == 0);
506 #include <unicode/utypes.h>
507 #include <unicode/ustring.h>
508 #include <unicode/ures.h>
509 #include <unicode/ucol.h>
510 #include <unicode/usearch.h>
512 static MonoString *monostring_from_resource_index (const UResourceBundle *bundle, int32_t idx)
519 res_str=(gunichar2 *)ures_getStringByIndex (bundle, idx, &res_strlen,
525 return(mono_string_from_utf16 (res_str));
528 static UResourceBundle *open_subbundle (const UResourceBundle *bundle,
529 const char *name, int32_t req_count)
531 UResourceBundle *subbundle;
536 subbundle=ures_getByKey (bundle, name, NULL, &ec);
538 /* Couldn't find the subbundle */
542 count=ures_countArrayItems (bundle, name, &ec);
544 /* Couldn't count the subbundle */
545 ures_close (subbundle);
549 if(count!=req_count) {
551 ures_close (subbundle);
558 static MonoArray *build_array (const UResourceBundle *bundle,
559 const char *resname, int32_t req_count)
562 UResourceBundle *subbundle;
565 subbundle=open_subbundle (bundle, resname, req_count);
566 if(subbundle!=NULL) {
567 arr=mono_array_new(mono_domain_get (),
568 mono_defaults.string_class, req_count);
570 for(i=0; i<req_count; i++) {
571 mono_array_set(arr, MonoString *, i, monostring_from_resource_index (subbundle, i));
574 ures_close (subbundle);
580 static MonoDateTimeFormatInfo *create_DateTimeFormat (const char *locale)
582 MonoDateTimeFormatInfo *new_dtf;
584 UResourceBundle *bundle, *subbundle;
587 class=mono_class_from_name (mono_defaults.corlib,
588 "System.Globalization",
589 "DateTimeFormatInfo");
590 new_dtf=(MonoDateTimeFormatInfo *)mono_object_new (mono_domain_get (),
592 mono_runtime_object_init ((MonoObject *)new_dtf);
596 bundle=ures_open (NULL, locale, &ec);
602 subbundle=open_subbundle (bundle, "AmPmMarkers", 2);
603 if(subbundle!=NULL) {
604 new_dtf->AMDesignator=monostring_from_resource_index (subbundle, 0);
605 new_dtf->PMDesignator=monostring_from_resource_index (subbundle, 1);
607 ures_close (subbundle);
610 /* Date/Time patterns. Don't set FullDateTimePattern. As it
611 * seems to always default to LongDatePattern + " " +
612 * LongTimePattern, let the property accessor deal with it.
614 subbundle=open_subbundle (bundle, "DateTimePatterns", 9);
615 if(subbundle!=NULL) {
616 new_dtf->ShortDatePattern=monostring_from_resource_index (subbundle, 7);
617 new_dtf->LongDatePattern=monostring_from_resource_index (subbundle, 5);
618 new_dtf->ShortTimePattern=monostring_from_resource_index (subbundle, 3);
619 new_dtf->LongTimePattern=monostring_from_resource_index (subbundle, 2);
621 /* RFC1123Pattern, SortableDateTimePattern and
622 * UniversalSortableDateTimePattern all seem to be
623 * constant, and all the same as the invariant default
627 ures_close (subbundle);
631 /* Not sure what to do with these yet, so leave them set to
632 * the invariant default
634 set_field_string (new_dtf, "_DateSeparator", str);
635 set_field_string (new_dtf, "_TimeSeparator", str);
636 set_field_string (new_dtf, "_MonthDayPattern", str);
637 set_field_string (new_dtf, "_YearMonthPattern", str);
640 /* Day names. Luckily both ICU and .net start Sunday at index 0 */
641 new_dtf->DayNames=build_array (bundle, "DayNames", 7);
643 /* Abbreviated day names */
644 new_dtf->AbbreviatedDayNames=build_array (bundle, "DayAbbreviations",
648 new_dtf->MonthNames=build_array (bundle, "MonthNames", 12);
650 /* Abbreviated month names */
651 new_dtf->AbbreviatedMonthNames=build_array (bundle,
652 "MonthAbbreviations", 12);
654 /* TODO: DayOfWeek _FirstDayOfWeek, Calendar _Calendar, CalendarWeekRule _CalendarWeekRule */
661 static MonoNumberFormatInfo *create_NumberFormat (const char *locale)
663 MonoNumberFormatInfo *new_nf;
665 MonoMethodDesc* methodDesc;
667 UResourceBundle *bundle, *subbundle, *table_entries;
670 static char country [7]; //FIXME
671 const UChar *res_str;
674 class=mono_class_from_name (mono_defaults.corlib,
675 "System.Globalization",
677 new_nf=(MonoNumberFormatInfo *)mono_object_new (mono_domain_get (),
679 mono_runtime_object_init ((MonoObject *)new_nf);
683 bundle=ures_open (NULL, locale, &ec);
688 /* Number Elements */
690 subbundle=ures_getByKey (bundle, "NumberElements", NULL, &ec);
692 /* Couldn't find the subbundle */
696 count=ures_countArrayItems (bundle, "NumberElements", &ec);
698 /* Couldn't count the subbundle */
699 ures_close (subbundle);
703 if(subbundle!=NULL) {
704 new_nf->numberDecimalSeparator=monostring_from_resource_index (subbundle, 0);
705 new_nf->numberGroupSeparator=monostring_from_resource_index (subbundle, 1);
706 new_nf->percentDecimalSeparator=monostring_from_resource_index (subbundle, 0);
707 new_nf->percentGroupSeparator=monostring_from_resource_index (subbundle, 1);
708 new_nf->percentSymbol=monostring_from_resource_index (subbundle, 3);
709 new_nf->zeroPattern=monostring_from_resource_index (subbundle, 4);
710 new_nf->digitPattern=monostring_from_resource_index (subbundle, 5);
711 new_nf->negativeSign=monostring_from_resource_index (subbundle, 6);
712 new_nf->perMilleSymbol=monostring_from_resource_index (subbundle, 8);
713 new_nf->positiveInfinitySymbol=monostring_from_resource_index (subbundle, 9);
714 /* we dont have this in CLDR, so copy it from positiveInfinitySymbol */
715 new_nf->negativeInfinitySymbol=monostring_from_resource_index (subbundle, 9);
716 new_nf->naNSymbol=monostring_from_resource_index (subbundle, 10);
717 new_nf->currencyDecimalSeparator=monostring_from_resource_index (subbundle, 0);
718 new_nf->currencyGroupSeparator=monostring_from_resource_index (subbundle, 1);
720 ures_close (subbundle);
723 /* get country name */
725 uloc_getCountry (locale, country, sizeof (country), &ec);
726 if (U_SUCCESS (ec)) {
728 /* find country name in root.CurrencyMap */
729 subbundle = ures_getByKey (bundle, "CurrencyMap", NULL, &ec);
730 if (U_SUCCESS (ec)) {
732 /* get currency id for specified country */
733 table_entries = ures_getByKey (subbundle, country, NULL, &ec);
734 if (U_SUCCESS (ec)) {
735 ures_close (subbundle);
738 res_str = ures_getStringByIndex (
739 table_entries, 0, &res_strlen, &ec);
741 /* now we have currency id string */
742 ures_close (table_entries);
744 u_UCharsToChars (res_str, country,
748 /* find currency string in locale data */
749 subbundle = ures_getByKey (
750 bundle, "Currencies",
753 if (U_SUCCESS (ec)) {
755 /* find currency symbol under specified currency id */
756 table_entries = ures_getByKey (subbundle, country, NULL, &ec);
757 if (U_SUCCESS (ec)) {
758 /* get the first string only,
759 * the second is international currency symbol (not used)*/
760 new_nf->currencySymbol=monostring_from_resource_index (table_entries, 0);
761 ures_close (table_entries);
763 ures_close (subbundle);
771 subbundle=open_subbundle (bundle, "NumberPatterns", 4);
772 if(subbundle!=NULL) {
773 new_nf->decimalFormats=monostring_from_resource_index (subbundle, 0);
774 new_nf->currencyFormats=monostring_from_resource_index (subbundle, 1);
775 new_nf->percentFormats=monostring_from_resource_index (subbundle, 2);
776 ures_close (subbundle);
778 /* calls InitPatterns to parse the patterns
780 methodDesc = mono_method_desc_new (
781 "System.Globalization.NumberFormatInfo:InitPatterns()",
783 method = mono_method_desc_search_in_class (methodDesc, class);
785 mono_runtime_invoke (method, new_nf, NULL, NULL);
787 g_warning (G_GNUC_PRETTY_FUNCTION ": Runtime mismatch with class lib! (Looking for System.Globalization.NumberFormatInfo:InitPatterns())");
796 static char *mono_string_to_icu_locale (MonoString *locale)
799 char *passed_locale, *icu_locale=NULL;
800 int32_t loc_len, ret;
802 passed_locale=mono_string_to_utf8 (locale);
805 ret=uloc_getName (passed_locale, NULL, 0, &ec);
806 if(ec==U_BUFFER_OVERFLOW_ERROR) {
809 icu_locale=(char *)g_malloc0 (sizeof(char)*loc_len);
810 ret=uloc_getName (passed_locale, icu_locale, loc_len, &ec);
812 g_free (passed_locale);
817 void ves_icall_System_Globalization_CultureInfo_construct_internal_locale (MonoCultureInfo *this, MonoString *locale)
823 int32_t str_len, ret;
827 icu_locale=mono_string_to_icu_locale (locale);
828 if(icu_locale==NULL) {
829 /* Something went wrong */
830 mono_raise_exception((MonoException *)mono_exception_from_name(mono_defaults.corlib, "System", "SystemException"));
834 /* Fill in the static fields */
836 /* TODO: Calendar, InstalledUICulture, OptionalCalendars,
840 str_len=256; /* Should be big enough for anything */
841 str=(char *)g_malloc0 (sizeof(char)*str_len);
842 ustr=(UChar *)g_malloc0 (sizeof(UChar)*str_len);
846 ret=uloc_getDisplayName (icu_locale, "en", ustr, str_len, &ec);
847 if(U_SUCCESS (ec) && ret<str_len) {
848 this->englishname=mono_string_from_utf16 ((gunichar2 *)ustr);
851 ret=uloc_getDisplayName (icu_locale, uloc_getDefault (), ustr, str_len,
853 if(U_SUCCESS (ec) && ret<str_len) {
854 this->displayname=mono_string_from_utf16 ((gunichar2 *)ustr);
857 ret=uloc_getDisplayName (icu_locale, icu_locale, ustr, str_len, &ec);
858 if(U_SUCCESS (ec) && ret<str_len) {
859 this->nativename=mono_string_from_utf16 ((gunichar2 *)ustr);
862 this->iso3lang=mono_string_new_wrapper (uloc_getISO3Language (icu_locale));
864 ret=uloc_getLanguage (icu_locale, str, str_len, &ec);
865 if(U_SUCCESS (ec) && ret<str_len) {
866 this->iso2lang=mono_string_new_wrapper (str);
869 this->datetime_format=create_DateTimeFormat (icu_locale);
870 this->number_format=create_NumberFormat (icu_locale);
877 void ves_icall_System_Globalization_CompareInfo_construct_compareinfo (MonoCompareInfo *comp, MonoString *locale)
886 g_message (G_GNUC_PRETTY_FUNCTION ": Constructing collator for locale [%s]", mono_string_to_utf8 (locale));
889 icu_locale=mono_string_to_icu_locale (locale);
890 if(icu_locale==NULL) {
891 /* Something went wrong */
892 mono_raise_exception((MonoException *)mono_exception_from_name(mono_defaults.corlib, "System", "SystemException"));
897 coll=ucol_open (icu_locale, &ec);
899 comp->ICU_collator=coll;
901 comp->ICU_collator=NULL;
907 /* Set up the collator to reflect the options required. Some of these
908 * options clash, as they adjust the collator strength level. Try to
909 * make later checks reduce the strength level, and attempt to take
910 * previous options into account.
912 * Don't bother to check the error returns when setting the
913 * attributes, as a failure here is hardly grounds to error out.
915 static void set_collator_options (UCollator *coll, gint32 options)
917 UErrorCode ec=U_ZERO_ERROR;
919 /* Set up the defaults */
920 ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE,
922 ucol_setAttribute (coll, UCOL_CASE_LEVEL, UCOL_OFF, &ec);
924 /* Do this first so other options will override the quaternary
925 * level strength setting if necessary
927 if(!(options & CompareOptions_IgnoreKanaType)) {
928 ucol_setAttribute (coll, UCOL_HIRAGANA_QUATERNARY_MODE,
930 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_QUATERNARY, &ec);
933 /* Word sort, the default */
934 if(!(options & CompareOptions_StringSort)) {
935 ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING,
937 /* Tertiary strength is the default, but it might have
938 * been set to quaternary above. (We don't want that
939 * here, because that will order all the punctuation
940 * first instead of just ignoring it.)
942 * Unfortunately, tertiary strength with
943 * ALTERNATE_HANDLING==SHIFTED means that '/' and '@'
944 * compare to equal, which has the nasty side effect
945 * of killing mcs :-( (We can't specify a
946 * culture-insensitive compare, because
947 * String.StartsWith doesn't have that option.)
949 * ALTERNATE_HANDLING==SHIFTED is needed to accomplish
950 * the word-sorting-ignoring-punctuation feature. So
951 * we have to live with the slightly mis-ordered
952 * punctuation and a working mcs...
954 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_QUATERNARY, &ec);
957 if(options & CompareOptions_IgnoreCase) {
958 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_SECONDARY, &ec);
959 ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE, &ec);
962 if(options & CompareOptions_IgnoreWidth) {
963 /* Kana width is a tertiary strength difference. This
964 * will totally break the !IgnoreKanaType option
966 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_SECONDARY, &ec);
969 if(options & CompareOptions_IgnoreNonSpace) {
970 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_PRIMARY, &ec);
971 /* We can still compare case even when just checking
974 if(!(options & CompareOptions_IgnoreCase) ||
975 !(options & CompareOptions_IgnoreWidth)) {
976 /* Not sure if CASE_LEVEL handles kana width
978 ucol_setAttribute (coll, UCOL_CASE_LEVEL, UCOL_ON,
983 if(options & CompareOptions_IgnoreSymbols) {
984 /* Don't know what to do here */
987 if(options == CompareOptions_Ordinal) {
988 /* This one is handled elsewhere */
992 gint32 ves_icall_System_Globalization_CompareInfo_internal_compare (MonoCompareInfo *this, MonoString *str1, gint32 off1, gint32 len1, MonoString *str2, gint32 off2, gint32 len2, gint32 options)
995 UCollationResult result;
1000 g_message (G_GNUC_PRETTY_FUNCTION ": Comparing [%s] and [%s]", mono_string_to_utf8 (str1), mono_string_to_utf8 (str2));
1003 coll=this->ICU_collator;
1006 g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", this->lcid);
1009 if(coll==NULL || this->lcid==0x007F ||
1010 options & CompareOptions_Ordinal) {
1012 g_message (G_GNUC_PRETTY_FUNCTION ": No collator or invariant, using shortcut");
1015 return(string_invariant_compare (str1, off1, len1, str2, off2,
1019 if (!mono_monitor_try_enter ((MonoObject *)this, INFINITE))
1022 set_collator_options (coll, options);
1024 result=ucol_strcoll (coll, mono_string_chars (str1)+off1, len1,
1025 mono_string_chars (str2)+off2, len2);
1027 mono_monitor_exit ((MonoObject *)this);
1030 g_message (G_GNUC_PRETTY_FUNCTION ": Comparison of [%s] and [%s] returning %d", mono_string_to_utf8 (str1), mono_string_to_utf8 (str2), result);
1036 void ves_icall_System_Globalization_CompareInfo_free_internal_collator (MonoCompareInfo *this)
1040 MONO_ARCH_SAVE_REGS;
1042 coll=this->ICU_collator;
1048 void ves_icall_System_Globalization_CompareInfo_assign_sortkey (MonoCompareInfo *this, MonoSortKey *key, MonoString *source, gint32 options)
1055 MONO_ARCH_SAVE_REGS;
1057 coll=this->ICU_collator;
1059 mono_raise_exception((MonoException *)mono_exception_from_name(mono_defaults.corlib, "System", "SystemException"));
1063 if (!mono_monitor_try_enter ((MonoObject *)this, INFINITE))
1066 set_collator_options (coll, options);
1068 keylen=ucol_getSortKey (coll, mono_string_chars (source), -1, NULL, 0);
1069 keybuf=g_malloc (sizeof(char)* keylen);
1070 ucol_getSortKey (coll, mono_string_chars (source), -1, keybuf, keylen);
1072 mono_monitor_exit ((MonoObject *)this);
1074 arr=mono_array_new (mono_domain_get (), mono_defaults.byte_class,
1076 for(i=0; i<keylen; i++) {
1077 mono_array_set (arr, guint8, i, keybuf[i]);
1085 int ves_icall_System_Globalization_CompareInfo_internal_index (MonoCompareInfo *this, MonoString *source, gint32 sindex, gint32 count, MonoString *value, gint32 options, MonoBoolean first)
1090 UStringSearch *search;
1093 MONO_ARCH_SAVE_REGS;
1096 g_message (G_GNUC_PRETTY_FUNCTION ": Finding %s [%s] in [%s] (sindex %d,count %d)", first?"first":"last", mono_string_to_utf8 (value), mono_string_to_utf8 (source), sindex, count);
1099 coll=this->ICU_collator;
1102 g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", this->lcid);
1105 if(coll==NULL || this->lcid==0x007F ||
1106 options & CompareOptions_Ordinal) {
1108 g_message (G_GNUC_PRETTY_FUNCTION ": No collator or invariant, using shortcut");
1111 return(string_invariant_indexof (source, sindex, count, value,
1115 usrcstr=g_malloc0 (sizeof(UChar)*(count+1));
1117 memcpy (usrcstr, mono_string_chars (source)+sindex,
1118 sizeof(UChar)*count);
1120 memcpy (usrcstr, mono_string_chars (source)+sindex-count+1,
1121 sizeof(UChar)*count);
1124 if (!mono_monitor_try_enter ((MonoObject *)this, INFINITE))
1129 /* Need to set the collator to a fairly weak level, so that it
1130 * treats characters that can be written differently as
1131 * identical (eg "ß" and "ss", "æ" and "ae" or "ä" etc.) Note
1132 * that this means that the search string and the original
1133 * text might have differing lengths.
1135 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_PRIMARY, &ec);
1137 /* Still notice case differences though (normally a tertiary
1140 ucol_setAttribute (coll, UCOL_CASE_LEVEL, UCOL_ON, &ec);
1142 /* Don't ignore some codepoints */
1143 ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE,
1146 search=usearch_openFromCollator (mono_string_chars (value), -1,
1147 usrcstr, -1, coll, NULL, &ec);
1148 if(U_SUCCESS (ec)) {
1150 pos=usearch_first (search, &ec);
1152 pos=usearch_last (search, &ec);
1155 while (pos!=USEARCH_DONE) {
1158 UCollationResult uret;
1161 g_message (G_GNUC_PRETTY_FUNCTION
1162 ": Got potential match at %d (sindex %d) len %d", pos, sindex, usearch_getMatchedLength (search));
1165 /* ICU usearch currently ignores most of the
1166 * collator attributes :-(
1168 * Check the returned match to see if it
1169 * really does match properly...
1171 match_len = usearch_getMatchedLength (search);
1172 match=(UChar *)g_malloc0 (sizeof(UChar) * (match_len + 1));
1173 usearch_getMatchedText (search, match, match_len, &ec);
1175 uret = ucol_strcoll (coll, match, -1,
1176 mono_string_chars (value), -1);
1179 if (uret == UCOL_EQUAL) {
1180 /* OK, we really did get a match */
1182 g_message (G_GNUC_PRETTY_FUNCTION
1183 ": Got match at %d len %d", pos,
1191 pos+=(sindex-count+1);
1198 /* False alarm, keep looking */
1200 pos=usearch_next (search, &ec);
1202 pos=usearch_previous (search, &ec);
1206 g_message (G_GNUC_PRETTY_FUNCTION ": usearch_open error: %s",
1210 usearch_close (search);
1212 mono_monitor_exit ((MonoObject *)this);
1219 int ves_icall_System_Globalization_CompareInfo_internal_index_char (MonoCompareInfo *this, MonoString *source, gint32 sindex, gint32 count, gunichar2 value, gint32 options, MonoBoolean first)
1222 UChar *usrcstr, uvalstr[2]={0, 0};
1224 UStringSearch *search;
1227 MONO_ARCH_SAVE_REGS;
1230 g_message (G_GNUC_PRETTY_FUNCTION ": Finding %s 0x%0x in [%s] (sindex %d,count %d)", first?"first":"last", value, mono_string_to_utf8 (source), sindex, count);
1233 coll=this->ICU_collator;
1236 g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", this->lcid);
1239 if(coll==NULL || this->lcid==0x007F ||
1240 options & CompareOptions_Ordinal) {
1242 g_message (G_GNUC_PRETTY_FUNCTION ": No collator or invariant, using shortcut");
1245 return(string_invariant_indexof_char (source, sindex, count,
1249 usrcstr=g_malloc0 (sizeof(UChar)*(count+1));
1251 memcpy (usrcstr, mono_string_chars (source)+sindex,
1252 sizeof(UChar)*count);
1254 memcpy (usrcstr, mono_string_chars (source)+sindex-count+1,
1255 sizeof(UChar)*count);
1259 if (!mono_monitor_try_enter ((MonoObject *)this, INFINITE))
1264 /* Need to set the collator to a fairly weak level, so that it
1265 * treats characters that can be written differently as
1266 * identical (eg "ß" and "ss", "æ" and "ae" or "ä" etc.) Note
1267 * that this means that the search string and the original
1268 * text might have differing lengths.
1270 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_PRIMARY, &ec);
1272 /* Still notice case differences though (normally a tertiary
1275 ucol_setAttribute (coll, UCOL_CASE_LEVEL, UCOL_ON, &ec);
1277 /* Don't ignore some codepoints */
1278 ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE,
1281 search=usearch_openFromCollator (uvalstr, -1, usrcstr, -1, coll, NULL,
1283 if(U_SUCCESS (ec)) {
1285 pos=usearch_first (search, &ec);
1287 pos=usearch_last (search, &ec);
1290 while (pos!=USEARCH_DONE) {
1293 UCollationResult uret;
1296 g_message (G_GNUC_PRETTY_FUNCTION
1297 ": Got potential match at %d (sindex %d) len %d", pos, sindex, usearch_getMatchedLength (search));
1300 /* ICU usearch currently ignores most of the
1301 * collator attributes :-(
1303 * Check the returned match to see if it
1304 * really does match properly...
1306 match_len = usearch_getMatchedLength (search);
1307 match=(UChar *)g_malloc0 (sizeof(UChar) * (match_len + 1));
1308 usearch_getMatchedText (search, match, match_len, &ec);
1310 uret = ucol_strcoll (coll, match, -1, uvalstr, -1);
1313 if (uret == UCOL_EQUAL) {
1314 /* OK, we really did get a match */
1316 g_message (G_GNUC_PRETTY_FUNCTION
1317 ": Got match at %d len %d", pos,
1325 pos+=(sindex-count+1);
1332 /* False alarm, keep looking */
1334 pos=usearch_next (search, &ec);
1336 pos=usearch_previous (search, &ec);
1340 g_message (G_GNUC_PRETTY_FUNCTION ": usearch_open error: %s",
1344 usearch_close (search);
1346 mono_monitor_exit ((MonoObject *)this);
1353 int ves_icall_System_Threading_Thread_current_lcid (void)
1355 MONO_ARCH_SAVE_REGS;
1357 return(uloc_getLCID (uloc_getDefault ()));
1360 MonoString *ves_icall_System_String_InternalReplace_Str_Comp (MonoString *this, MonoString *old, MonoString *new, MonoCompareInfo *comp)
1362 MonoString *ret=NULL;
1365 UStringSearch *search;
1367 MONO_ARCH_SAVE_REGS;
1370 g_message (G_GNUC_PRETTY_FUNCTION ": Replacing [%s] with [%s] in [%s]", mono_string_to_utf8 (old), mono_string_to_utf8 (new), mono_string_to_utf8 (this));
1373 coll=comp->ICU_collator;
1376 g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", comp->lcid);
1379 if(coll==NULL || comp->lcid==0x007F) {
1381 g_message (G_GNUC_PRETTY_FUNCTION ": No collator or invariant, using shortcut");
1384 return(string_invariant_replace (this, old, new));
1387 if (!mono_monitor_try_enter ((MonoObject *)comp, INFINITE))
1392 /* Need to set the collator to a fairly weak level, so that it
1393 * treats characters that can be written differently as
1394 * identical (eg "ß" and "ss", "æ" and "ae" or "ä" etc.) Note
1395 * that this means that the search string and the original
1396 * text might have differing lengths.
1398 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_PRIMARY, &ec);
1400 /* Still notice case differences though (normally a tertiary
1403 ucol_setAttribute (coll, UCOL_CASE_LEVEL, UCOL_ON, &ec);
1405 /* Don't ignore some codepoints */
1406 ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE,
1409 search=usearch_openFromCollator (mono_string_chars (old), -1,
1410 mono_string_chars (this), -1, coll,
1412 if(U_SUCCESS (ec)) {
1413 int pos, oldpos, len_delta=0;
1414 int32_t newstr_len=mono_string_length (new), match_len;
1415 UChar *uret, *match;
1417 for(pos=usearch_first (search, &ec);
1419 pos=usearch_next (search, &ec)) {
1420 /* ICU usearch currently ignores most of the collator
1423 * Check the returned match to see if it really
1424 * does match properly...
1426 match_len = usearch_getMatchedLength (search);
1427 match=(UChar *)g_malloc0 (sizeof(UChar) * (match_len + 1));
1428 usearch_getMatchedText (search, match, match_len, &ec);
1430 if (ucol_strcoll (coll, match, -1, mono_string_chars (old), -1) == UCOL_EQUAL) {
1431 /* OK, we really did get a match */
1433 g_message (G_GNUC_PRETTY_FUNCTION
1434 ": Got match at %d len %d", pos,
1438 len_delta += (newstr_len - match_len);
1442 g_message (G_GNUC_PRETTY_FUNCTION
1443 ": Got false match at %d len %d",
1450 g_message (G_GNUC_PRETTY_FUNCTION
1451 ": New string length is %d (delta %d)",
1452 mono_string_length (this)+len_delta, len_delta);
1455 uret=(UChar *)g_malloc0 (sizeof(UChar) * (mono_string_length (this)+len_delta+2));
1457 for(oldpos=0, pos=usearch_first (search, &ec);
1459 pos=usearch_next (search, &ec)) {
1460 match_len = usearch_getMatchedLength (search);
1461 match=(UChar *)g_malloc0 (sizeof(UChar) * (match_len + 1));
1462 usearch_getMatchedText (search, match, match_len, &ec);
1464 /* Add the unmatched text */
1465 u_strncat (uret, mono_string_chars (this)+oldpos,
1467 if (ucol_strcoll (coll, match, -1, mono_string_chars (old), -1) == UCOL_EQUAL) {
1468 /* Then the replacement */
1469 u_strcat (uret, mono_string_chars (new));
1471 /* Then the original, because this is a
1474 u_strncat (uret, mono_string_chars (this)+pos,
1477 oldpos=pos+match_len;
1481 /* Finish off with the trailing unmatched text */
1482 u_strcat (uret, mono_string_chars (this)+oldpos);
1484 ret=mono_string_from_utf16 ((gunichar2 *)uret);
1486 g_message (G_GNUC_PRETTY_FUNCTION ": usearch_open error: %s",
1490 usearch_close (search);
1492 mono_monitor_exit ((MonoObject *)comp);
1495 g_message (G_GNUC_PRETTY_FUNCTION ": Replacing [%s] with [%s] in [%s] returns [%s]", mono_string_to_utf8 (old), mono_string_to_utf8 (new), mono_string_to_utf8 (this), mono_string_to_utf8 (ret));
1501 MonoString *ves_icall_System_String_InternalToLower_Comp (MonoString *this, MonoCultureInfo *cult)
1509 MONO_ARCH_SAVE_REGS;
1512 g_message (G_GNUC_PRETTY_FUNCTION ": [%s]",
1513 mono_string_to_utf8 (this));
1517 g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", cult->lcid);
1520 icu_loc=mono_string_to_icu_locale (cult->icu_name);
1522 mono_raise_exception ((MonoException *)mono_exception_from_name (mono_defaults.corlib, "System", "SystemException"));
1526 udest=(UChar *)g_malloc0 (sizeof(UChar)*(mono_string_length (this)+1));
1528 /* According to the docs, this might result in a longer or
1529 * shorter string than we started with...
1533 len=u_strToLower (udest, mono_string_length (this)+1,
1534 mono_string_chars (this), -1, icu_loc, &ec);
1535 if(ec==U_BUFFER_OVERFLOW_ERROR ||
1536 ec==U_STRING_NOT_TERMINATED_WARNING) {
1538 udest=(UChar *)g_malloc0 (sizeof(UChar)*(len+1));
1539 len=u_strToLower (udest, len+1, mono_string_chars (this), -1,
1543 if(U_SUCCESS (ec)) {
1544 ret=mono_string_from_utf16 ((gunichar2 *)udest);
1546 g_message (G_GNUC_PRETTY_FUNCTION ": u_strToLower error: %s",
1548 /* return something */
1556 g_message (G_GNUC_PRETTY_FUNCTION ": returning [%s]",
1557 mono_string_to_utf8 (ret));
1563 MonoString *ves_icall_System_String_InternalToUpper_Comp (MonoString *this, MonoCultureInfo *cult)
1571 MONO_ARCH_SAVE_REGS;
1574 g_message (G_GNUC_PRETTY_FUNCTION ": [%s]",
1575 mono_string_to_utf8 (this));
1579 g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", cult->lcid);
1582 icu_loc=mono_string_to_icu_locale (cult->icu_name);
1584 mono_raise_exception ((MonoException *)mono_exception_from_name (mono_defaults.corlib, "System", "SystemException"));
1588 udest=(UChar *)g_malloc0 (sizeof(UChar)*(mono_string_length (this)+1));
1590 /* According to the docs, this might result in a longer or
1591 * shorter string than we started with...
1595 len=u_strToUpper (udest, mono_string_length (this)+1,
1596 mono_string_chars (this), -1, icu_loc, &ec);
1597 if(ec==U_BUFFER_OVERFLOW_ERROR ||
1598 ec==U_STRING_NOT_TERMINATED_WARNING) {
1600 udest=(UChar *)g_malloc0 (sizeof(UChar)*(len+1));
1601 len=u_strToUpper (udest, len+1, mono_string_chars (this), -1,
1605 if(U_SUCCESS (ec)) {
1606 ret=mono_string_from_utf16 ((gunichar2 *)udest);
1608 g_message (G_GNUC_PRETTY_FUNCTION ": u_strToUpper error: %s",
1610 /* return something */
1618 g_message (G_GNUC_PRETTY_FUNCTION ": returning [%s]",
1619 mono_string_to_utf8 (ret));
1625 gunichar2 ves_icall_System_Char_InternalToUpper_Comp (gunichar2 c, MonoCultureInfo *cult)
1632 MONO_ARCH_SAVE_REGS;
1634 icu_loc=mono_string_to_icu_locale (cult->icu_name);
1636 mono_raise_exception ((MonoException *)mono_exception_from_name (mono_defaults.corlib, "System", "SystemException"));
1641 len=u_strToUpper (&udest, 1, &c, 1, icu_loc, &ec);
1644 if(U_SUCCESS (ec) && len==1) {
1647 /* return something */
1653 gunichar2 ves_icall_System_Char_InternalToLower_Comp (gunichar2 c, MonoCultureInfo *cult)
1660 MONO_ARCH_SAVE_REGS;
1662 icu_loc=mono_string_to_icu_locale (cult->icu_name);
1664 mono_raise_exception ((MonoException *)mono_exception_from_name (mono_defaults.corlib, "System", "SystemException"));
1669 len=u_strToLower (&udest, 1, &c, 1, icu_loc, &ec);
1672 if(U_SUCCESS (ec) && len==1) {
1675 /* return something */
1680 #else /* HAVE_ICU */
1681 static MonoString *string_invariant_tolower (MonoString *this);
1682 static MonoString *string_invariant_toupper (MonoString *this);
1684 void ves_icall_System_Globalization_CultureInfo_construct_internal_locale (MonoCultureInfo *this, MonoString *locale)
1686 MONO_ARCH_SAVE_REGS;
1688 /* Always claim "unknown locale" if we don't have ICU (only
1689 * called for non-invariant locales)
1691 mono_raise_exception((MonoException *)mono_exception_from_name(mono_defaults.corlib, "System", "ArgumentException"));
1694 void ves_icall_System_Globalization_CompareInfo_construct_compareinfo (MonoCompareInfo *comp, MonoString *locale)
1696 /* Nothing to do here */
1699 int ves_icall_System_Globalization_CompareInfo_internal_compare (MonoCompareInfo *this, MonoString *str1, gint32 off1, gint32 len1, MonoString *str2, gint32 off2, gint32 len2, gint32 options)
1701 MONO_ARCH_SAVE_REGS;
1703 /* Do a normal ascii string compare, as we only know the
1704 * invariant locale if we dont have ICU
1706 return(string_invariant_compare (str1, off1, len1, str2, off2, len2,
1710 void ves_icall_System_Globalization_CompareInfo_free_internal_collator (MonoCompareInfo *this)
1712 /* Nothing to do here */
1715 void ves_icall_System_Globalization_CompareInfo_assign_sortkey (MonoCompareInfo *this, MonoSortKey *key, MonoString *source, gint32 options)
1720 MONO_ARCH_SAVE_REGS;
1722 keylen=mono_string_length (source);
1724 arr=mono_array_new (mono_domain_get (), mono_defaults.byte_class,
1726 for(i=0; i<keylen; i++) {
1727 mono_array_set (arr, guint8, i, mono_string_chars (source)[i]);
1733 int ves_icall_System_Globalization_CompareInfo_internal_index (MonoCompareInfo *this, MonoString *source, gint32 sindex, gint32 count, MonoString *value, gint32 options, MonoBoolean first)
1735 MONO_ARCH_SAVE_REGS;
1737 return(string_invariant_indexof (source, sindex, count, value, first));
1740 int ves_icall_System_Globalization_CompareInfo_internal_index_char (MonoCompareInfo *this, MonoString *source, gint32 sindex, gint32 count, gunichar2 value, gint32 options, MonoBoolean first)
1742 MONO_ARCH_SAVE_REGS;
1744 return(string_invariant_indexof_char (source, sindex, count, value,
1748 int ves_icall_System_Threading_Thread_current_lcid (void)
1750 MONO_ARCH_SAVE_REGS;
1756 MonoString *ves_icall_System_String_InternalReplace_Str_Comp (MonoString *this, MonoString *old, MonoString *new, MonoCompareInfo *comp)
1758 MONO_ARCH_SAVE_REGS;
1760 /* Do a normal ascii string compare and replace, as we only
1761 * know the invariant locale if we dont have ICU
1763 return(string_invariant_replace (this, old, new));
1766 MonoString *ves_icall_System_String_InternalToLower_Comp (MonoString *this, MonoCultureInfo *cult)
1768 MONO_ARCH_SAVE_REGS;
1770 return(string_invariant_tolower (this));
1773 MonoString *ves_icall_System_String_InternalToUpper_Comp (MonoString *this, MonoCultureInfo *cult)
1775 MONO_ARCH_SAVE_REGS;
1777 return(string_invariant_toupper (this));
1780 gunichar2 ves_icall_System_Char_InternalToUpper_Comp (gunichar2 c, MonoCultureInfo *cult)
1782 MONO_ARCH_SAVE_REGS;
1784 return g_unichar_toupper (c);
1788 gunichar2 ves_icall_System_Char_InternalToLower_Comp (gunichar2 c, MonoCultureInfo *cult)
1790 MONO_ARCH_SAVE_REGS;
1792 return g_unichar_tolower (c);
1795 static MonoString *string_invariant_tolower (MonoString *this)
1802 ret = mono_string_new_size(mono_domain_get (),
1803 mono_string_length(this));
1805 src = mono_string_chars (this);
1806 dest = mono_string_chars (ret);
1808 for (i = 0; i < mono_string_length (this); ++i) {
1809 dest[i] = g_unichar_tolower(src[i]);
1815 static MonoString *string_invariant_toupper (MonoString *this)
1822 ret = mono_string_new_size(mono_domain_get (),
1823 mono_string_length(this));
1825 src = mono_string_chars (this);
1826 dest = mono_string_chars (ret);
1828 for (i = 0; i < mono_string_length (this); ++i) {
1829 dest[i] = g_unichar_toupper(src[i]);
1835 #endif /* HAVE_ICU */
1837 static gint32 string_invariant_compare_char (gunichar2 c1, gunichar2 c2,
1841 GUnicodeType c1type, c2type;
1843 c1type = g_unichar_type (c1);
1844 c2type = g_unichar_type (c2);
1846 if (options & CompareOptions_IgnoreCase) {
1847 result = (gint32) (c1type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c1) : c1) - (c2type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c2) : c2);
1848 } else if (options & CompareOptions_Ordinal) {
1849 // Rotor/ms return the full value just not -1 and 1
1850 return (gint32) c1 - c2;
1852 /* No options. Kana, symbol and spacing options don't
1853 * apply to the invariant culture.
1855 if (c1type == G_UNICODE_UPPERCASE_LETTER &&
1856 c2type == G_UNICODE_LOWERCASE_LETTER) {
1860 if (c1type == G_UNICODE_LOWERCASE_LETTER &&
1861 c2type == G_UNICODE_UPPERCASE_LETTER) {
1865 result = (gint32) c1 - c2;
1868 return ((result < 0) ? -1 : (result > 0) ? 1 : 0);
1871 static gint32 string_invariant_compare (MonoString *str1, gint32 off1,
1872 gint32 len1, MonoString *str2,
1873 gint32 off2, gint32 len2,
1876 /* c translation of C# code from old string.cs.. :) */
1889 ustr1 = mono_string_chars(str1)+off1;
1890 ustr2 = mono_string_chars(str2)+off2;
1894 for (pos = 0; pos != length; pos++) {
1895 if (pos >= len1 || pos >= len2)
1898 charcmp = string_invariant_compare_char(ustr1[pos], ustr2[pos],
1905 /* the lesser wins, so if we have looped until length we just
1906 * need to check the last char
1908 if (pos == length) {
1909 return(string_invariant_compare_char(ustr1[pos - 1],
1910 ustr2[pos - 1], options));
1913 /* Test if one of the strings has been compared to the end */
1920 } else if (pos >= len2) {
1924 /* if not, check our last char only.. (can this happen?) */
1925 return(string_invariant_compare_char(ustr1[pos], ustr2[pos], options));
1928 static MonoString *string_invariant_replace (MonoString *me,
1929 MonoString *oldValue,
1930 MonoString *newValue)
1934 gunichar2 *dest=NULL; /* shut gcc up */
1936 gunichar2 *newstr=NULL; /* shut gcc up here too */
1947 oldstr = mono_string_chars(oldValue);
1948 oldstrlen = mono_string_length(oldValue);
1950 if (NULL != newValue) {
1951 newstr = mono_string_chars(newValue);
1952 newstrlen = mono_string_length(newValue);
1956 src = mono_string_chars(me);
1957 srclen = mono_string_length(me);
1959 if (oldstrlen != newstrlen) {
1961 while (i <= srclen - oldstrlen) {
1962 if (0 == memcmp(src + i, oldstr, oldstrlen * sizeof(gunichar2))) {
1971 newsize = srclen + ((newstrlen - oldstrlen) * occurr);
1977 while (i < srclen) {
1978 if (0 == memcmp(src + i, oldstr, oldstrlen * sizeof(gunichar2))) {
1980 ret = mono_string_new_size( mono_domain_get (), newsize);
1981 dest = mono_string_chars(ret);
1982 memcpy (dest, src, i * sizeof(gunichar2));
1984 if (newstrlen > 0) {
1985 memcpy(dest + destpos, newstr, newstrlen * sizeof(gunichar2));
1986 destpos += newstrlen;
1990 } else if (ret != NULL) {
1991 dest[destpos] = src[i];
2003 static gint32 string_invariant_indexof (MonoString *source, gint32 sindex,
2004 gint32 count, MonoString *value,
2012 lencmpstr = mono_string_length(value);
2014 src = mono_string_chars(source);
2015 cmpstr = mono_string_chars(value);
2019 for(pos=sindex;pos <= sindex+count;pos++) {
2020 for(i=0;src[pos+i]==cmpstr[i];) {
2021 if(++i==lencmpstr) {
2029 for(pos=sindex-lencmpstr+1;pos>sindex-count;pos--) {
2030 if(memcmp (src+pos, cmpstr,
2031 lencmpstr*sizeof(gunichar2))==0) {
2040 static gint32 string_invariant_indexof_char (MonoString *source, gint32 sindex,
2041 gint32 count, gunichar2 value,
2047 src = mono_string_chars(source);
2049 for (pos = sindex; pos != count + sindex; pos++) {
2050 if (src [pos] == value) {
2057 for (pos = sindex; pos > sindex - count; pos--) {
2058 if (src [pos] == value)