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_get_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_get_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_get_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_get_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_get_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_get_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_get_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_get_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_enter ((MonoObject *)this))
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_get_corlib (), "System", "SystemException"));
1063 if (!mono_monitor_enter ((MonoObject *)this))
1066 set_collator_options (coll, options);
1068 keylen=ucol_getSortKey (coll, mono_string_chars (source),
1069 mono_string_length (source), NULL, 0);
1070 keybuf=g_malloc (sizeof(char)* keylen);
1071 ucol_getSortKey (coll, mono_string_chars (source),
1072 mono_string_length (source), keybuf, keylen);
1074 mono_monitor_exit ((MonoObject *)this);
1076 arr=mono_array_new (mono_domain_get (), mono_get_byte_class (),
1078 for(i=0; i<keylen; i++) {
1079 mono_array_set (arr, guint8, i, keybuf[i]);
1087 int ves_icall_System_Globalization_CompareInfo_internal_index (MonoCompareInfo *this, MonoString *source, gint32 sindex, gint32 count, MonoString *value, gint32 options, MonoBoolean first)
1092 UStringSearch *search;
1095 MONO_ARCH_SAVE_REGS;
1098 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);
1101 coll=this->ICU_collator;
1104 g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", this->lcid);
1107 if(coll==NULL || this->lcid==0x007F ||
1108 options & CompareOptions_Ordinal) {
1110 g_message (G_GNUC_PRETTY_FUNCTION ": No collator or invariant, using shortcut");
1113 return(string_invariant_indexof (source, sindex, count, value,
1117 usrcstr=g_malloc0 (sizeof(UChar)*(count+1));
1119 memcpy (usrcstr, mono_string_chars (source)+sindex,
1120 sizeof(UChar)*count);
1122 memcpy (usrcstr, mono_string_chars (source)+sindex-count+1,
1123 sizeof(UChar)*count);
1126 if (!mono_monitor_enter ((MonoObject *)this))
1131 /* Need to set the collator to a fairly weak level, so that it
1132 * treats characters that can be written differently as
1133 * identical (eg "ß" and "ss", "æ" and "ae" or "ä" etc.) Note
1134 * that this means that the search string and the original
1135 * text might have differing lengths.
1137 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_PRIMARY, &ec);
1139 /* Still notice case differences though (normally a tertiary
1142 ucol_setAttribute (coll, UCOL_CASE_LEVEL, UCOL_ON, &ec);
1144 /* Don't ignore some codepoints */
1145 ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE,
1148 search=usearch_openFromCollator (mono_string_chars (value),
1149 mono_string_length (value),
1150 usrcstr, count, coll, NULL, &ec);
1151 if(U_SUCCESS (ec)) {
1153 pos=usearch_first (search, &ec);
1155 pos=usearch_last (search, &ec);
1158 while (pos!=USEARCH_DONE) {
1161 UCollationResult uret;
1164 g_message (G_GNUC_PRETTY_FUNCTION
1165 ": Got potential match at %d (sindex %d) len %d", pos, sindex, usearch_getMatchedLength (search));
1168 /* ICU usearch currently ignores most of the
1169 * collator attributes :-(
1171 * Check the returned match to see if it
1172 * really does match properly...
1174 match_len = usearch_getMatchedLength (search);
1175 match=(UChar *)g_malloc0 (sizeof(UChar) * (match_len + 1));
1176 usearch_getMatchedText (search, match, match_len, &ec);
1178 uret = ucol_strcoll (coll, match, match_len,
1179 mono_string_chars (value),
1180 mono_string_length (value));
1183 if (uret == UCOL_EQUAL) {
1184 /* OK, we really did get a match */
1186 g_message (G_GNUC_PRETTY_FUNCTION
1187 ": Got match at %d len %d", pos,
1195 pos+=(sindex-count+1);
1202 /* False alarm, keep looking */
1204 pos=usearch_next (search, &ec);
1206 pos=usearch_previous (search, &ec);
1210 g_message (G_GNUC_PRETTY_FUNCTION ": usearch_open error: %s",
1214 usearch_close (search);
1216 mono_monitor_exit ((MonoObject *)this);
1223 int ves_icall_System_Globalization_CompareInfo_internal_index_char (MonoCompareInfo *this, MonoString *source, gint32 sindex, gint32 count, gunichar2 value, gint32 options, MonoBoolean first)
1226 UChar *usrcstr, uvalstr[2]={0, 0};
1228 UStringSearch *search;
1231 MONO_ARCH_SAVE_REGS;
1234 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);
1237 coll=this->ICU_collator;
1240 g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", this->lcid);
1243 if(coll==NULL || this->lcid==0x007F ||
1244 options & CompareOptions_Ordinal) {
1246 g_message (G_GNUC_PRETTY_FUNCTION ": No collator or invariant, using shortcut");
1249 return(string_invariant_indexof_char (source, sindex, count,
1253 usrcstr=g_malloc0 (sizeof(UChar)*(count+1));
1255 memcpy (usrcstr, mono_string_chars (source)+sindex,
1256 sizeof(UChar)*count);
1258 memcpy (usrcstr, mono_string_chars (source)+sindex-count+1,
1259 sizeof(UChar)*count);
1263 if (!mono_monitor_enter ((MonoObject *)this))
1268 /* Need to set the collator to a fairly weak level, so that it
1269 * treats characters that can be written differently as
1270 * identical (eg "ß" and "ss", "æ" and "ae" or "ä" etc.) Note
1271 * that this means that the search string and the original
1272 * text might have differing lengths.
1274 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_PRIMARY, &ec);
1276 /* Still notice case differences though (normally a tertiary
1279 ucol_setAttribute (coll, UCOL_CASE_LEVEL, UCOL_ON, &ec);
1281 /* Don't ignore some codepoints */
1282 ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE,
1285 search=usearch_openFromCollator (uvalstr, 1, usrcstr, count, coll,
1287 if(U_SUCCESS (ec)) {
1289 pos=usearch_first (search, &ec);
1291 pos=usearch_last (search, &ec);
1294 while (pos!=USEARCH_DONE) {
1297 UCollationResult uret;
1300 g_message (G_GNUC_PRETTY_FUNCTION
1301 ": Got potential match at %d (sindex %d) len %d", pos, sindex, usearch_getMatchedLength (search));
1304 /* ICU usearch currently ignores most of the
1305 * collator attributes :-(
1307 * Check the returned match to see if it
1308 * really does match properly...
1310 match_len = usearch_getMatchedLength (search);
1311 match=(UChar *)g_malloc0 (sizeof(UChar) * (match_len + 1));
1312 usearch_getMatchedText (search, match, match_len, &ec);
1314 uret = ucol_strcoll (coll, match, match_len, uvalstr,
1318 if (uret == UCOL_EQUAL) {
1319 /* OK, we really did get a match */
1321 g_message (G_GNUC_PRETTY_FUNCTION
1322 ": Got match at %d len %d", pos,
1330 pos+=(sindex-count+1);
1337 /* False alarm, keep looking */
1339 pos=usearch_next (search, &ec);
1341 pos=usearch_previous (search, &ec);
1345 g_message (G_GNUC_PRETTY_FUNCTION ": usearch_open error: %s",
1349 usearch_close (search);
1351 mono_monitor_exit ((MonoObject *)this);
1358 int ves_icall_System_Threading_Thread_current_lcid (void)
1360 MONO_ARCH_SAVE_REGS;
1362 return(uloc_getLCID (uloc_getDefault ()));
1365 MonoString *ves_icall_System_String_InternalReplace_Str_Comp (MonoString *this, MonoString *old, MonoString *new, MonoCompareInfo *comp)
1367 MonoString *ret=NULL;
1370 UStringSearch *search;
1372 MONO_ARCH_SAVE_REGS;
1375 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));
1378 coll=comp->ICU_collator;
1381 g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", comp->lcid);
1384 if(coll==NULL || comp->lcid==0x007F) {
1386 g_message (G_GNUC_PRETTY_FUNCTION ": No collator or invariant, using shortcut");
1389 return(string_invariant_replace (this, old, new));
1392 if (!mono_monitor_enter ((MonoObject *)comp))
1397 /* Need to set the collator to a fairly weak level, so that it
1398 * treats characters that can be written differently as
1399 * identical (eg "ß" and "ss", "æ" and "ae" or "ä" etc.) Note
1400 * that this means that the search string and the original
1401 * text might have differing lengths.
1403 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_PRIMARY, &ec);
1405 /* Still notice case differences though (normally a tertiary
1408 ucol_setAttribute (coll, UCOL_CASE_LEVEL, UCOL_ON, &ec);
1410 /* Don't ignore some codepoints */
1411 ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE,
1414 search=usearch_openFromCollator (mono_string_chars (old),
1415 mono_string_length (old),
1416 mono_string_chars (this),
1417 mono_string_length (this),
1419 if(U_SUCCESS (ec)) {
1420 int pos, oldpos, len_delta=0;
1421 int32_t newstr_len=mono_string_length (new), match_len;
1422 UChar *uret, *match;
1424 for(pos=usearch_first (search, &ec);
1426 pos=usearch_next (search, &ec)) {
1427 /* ICU usearch currently ignores most of the collator
1430 * Check the returned match to see if it really
1431 * does match properly...
1433 match_len = usearch_getMatchedLength (search);
1435 if(match_len == 0) {
1439 match=(UChar *)g_malloc0 (sizeof(UChar) * (match_len + 1));
1440 usearch_getMatchedText (search, match, match_len, &ec);
1442 if (ucol_strcoll (coll, match, match_len,
1443 mono_string_chars (old),
1444 mono_string_length (old)) == UCOL_EQUAL) {
1445 /* OK, we really did get a match */
1447 g_message (G_GNUC_PRETTY_FUNCTION
1448 ": Got match at %d len %d", pos,
1452 len_delta += (newstr_len - match_len);
1456 g_message (G_GNUC_PRETTY_FUNCTION
1457 ": Got false match at %d len %d",
1464 g_message (G_GNUC_PRETTY_FUNCTION
1465 ": New string length is %d (delta %d)",
1466 mono_string_length (this)+len_delta, len_delta);
1469 uret=(UChar *)g_malloc0 (sizeof(UChar) * (mono_string_length (this)+len_delta+2));
1471 for(oldpos=0, pos=usearch_first (search, &ec);
1473 pos=usearch_next (search, &ec)) {
1474 match_len = usearch_getMatchedLength (search);
1476 if (match_len == 0) {
1480 match=(UChar *)g_malloc0 (sizeof(UChar) * (match_len + 1));
1481 usearch_getMatchedText (search, match, match_len, &ec);
1483 /* Add the unmatched text */
1484 u_strncat (uret, mono_string_chars (this)+oldpos,
1486 if (ucol_strcoll (coll, match, match_len,
1487 mono_string_chars (old),
1488 mono_string_length (old)) == UCOL_EQUAL) {
1489 /* Then the replacement */
1490 u_strcat (uret, mono_string_chars (new));
1492 /* Then the original, because this is a
1495 u_strncat (uret, mono_string_chars (this)+pos,
1498 oldpos=pos+match_len;
1502 /* Finish off with the trailing unmatched text */
1503 u_strcat (uret, mono_string_chars (this)+oldpos);
1505 ret=mono_string_from_utf16 ((gunichar2 *)uret);
1507 g_message (G_GNUC_PRETTY_FUNCTION ": usearch_open error: %s",
1511 usearch_close (search);
1513 mono_monitor_exit ((MonoObject *)comp);
1516 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));
1522 MonoString *ves_icall_System_String_InternalToLower_Comp (MonoString *this, MonoCultureInfo *cult)
1530 MONO_ARCH_SAVE_REGS;
1533 g_message (G_GNUC_PRETTY_FUNCTION ": [%s]",
1534 mono_string_to_utf8 (this));
1538 g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", cult->lcid);
1541 icu_loc=mono_string_to_icu_locale (cult->icu_name);
1543 mono_raise_exception ((MonoException *)mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1547 udest=(UChar *)g_malloc0 (sizeof(UChar)*(mono_string_length (this)+1));
1549 /* According to the docs, this might result in a longer or
1550 * shorter string than we started with...
1554 len=u_strToLower (udest, mono_string_length (this)+1,
1555 mono_string_chars (this), mono_string_length (this),
1557 if(ec==U_BUFFER_OVERFLOW_ERROR ||
1558 ec==U_STRING_NOT_TERMINATED_WARNING) {
1560 udest=(UChar *)g_malloc0 (sizeof(UChar)*(len+1));
1561 len=u_strToLower (udest, len+1, mono_string_chars (this),
1562 mono_string_length (this), icu_loc, &ec);
1565 if(U_SUCCESS (ec)) {
1566 ret=mono_string_from_utf16 ((gunichar2 *)udest);
1568 g_message (G_GNUC_PRETTY_FUNCTION ": u_strToLower error: %s",
1570 /* return something */
1578 g_message (G_GNUC_PRETTY_FUNCTION ": returning [%s]",
1579 mono_string_to_utf8 (ret));
1585 MonoString *ves_icall_System_String_InternalToUpper_Comp (MonoString *this, MonoCultureInfo *cult)
1593 MONO_ARCH_SAVE_REGS;
1596 g_message (G_GNUC_PRETTY_FUNCTION ": [%s]",
1597 mono_string_to_utf8 (this));
1601 g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", cult->lcid);
1604 icu_loc=mono_string_to_icu_locale (cult->icu_name);
1606 mono_raise_exception ((MonoException *)mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1610 udest=(UChar *)g_malloc0 (sizeof(UChar)*(mono_string_length (this)+1));
1612 /* According to the docs, this might result in a longer or
1613 * shorter string than we started with...
1617 len=u_strToUpper (udest, mono_string_length (this)+1,
1618 mono_string_chars (this), mono_string_length (this),
1620 if(ec==U_BUFFER_OVERFLOW_ERROR ||
1621 ec==U_STRING_NOT_TERMINATED_WARNING) {
1623 udest=(UChar *)g_malloc0 (sizeof(UChar)*(len+1));
1624 len=u_strToUpper (udest, len+1, mono_string_chars (this),
1625 mono_string_length (this), icu_loc, &ec);
1628 if(U_SUCCESS (ec)) {
1629 ret=mono_string_from_utf16 ((gunichar2 *)udest);
1631 g_message (G_GNUC_PRETTY_FUNCTION ": u_strToUpper error: %s",
1633 /* return something */
1641 g_message (G_GNUC_PRETTY_FUNCTION ": returning [%s]",
1642 mono_string_to_utf8 (ret));
1648 gunichar2 ves_icall_System_Char_InternalToUpper_Comp (gunichar2 c, MonoCultureInfo *cult)
1655 MONO_ARCH_SAVE_REGS;
1657 icu_loc=mono_string_to_icu_locale (cult->icu_name);
1659 mono_raise_exception ((MonoException *)mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1664 len=u_strToUpper (&udest, 1, &c, 1, icu_loc, &ec);
1667 if(U_SUCCESS (ec) && len==1) {
1670 /* return something */
1676 gunichar2 ves_icall_System_Char_InternalToLower_Comp (gunichar2 c, MonoCultureInfo *cult)
1683 MONO_ARCH_SAVE_REGS;
1685 icu_loc=mono_string_to_icu_locale (cult->icu_name);
1687 mono_raise_exception ((MonoException *)mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1692 len=u_strToLower (&udest, 1, &c, 1, icu_loc, &ec);
1695 if(U_SUCCESS (ec) && len==1) {
1698 /* return something */
1703 #else /* HAVE_ICU */
1704 static MonoString *string_invariant_tolower (MonoString *this);
1705 static MonoString *string_invariant_toupper (MonoString *this);
1707 void ves_icall_System_Globalization_CultureInfo_construct_internal_locale (MonoCultureInfo *this, MonoString *locale)
1709 MONO_ARCH_SAVE_REGS;
1711 /* Always claim "unknown locale" if we don't have ICU (only
1712 * called for non-invariant locales)
1714 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "ArgumentException"));
1717 void ves_icall_System_Globalization_CompareInfo_construct_compareinfo (MonoCompareInfo *comp, MonoString *locale)
1719 /* Nothing to do here */
1722 int ves_icall_System_Globalization_CompareInfo_internal_compare (MonoCompareInfo *this, MonoString *str1, gint32 off1, gint32 len1, MonoString *str2, gint32 off2, gint32 len2, gint32 options)
1724 MONO_ARCH_SAVE_REGS;
1726 /* Do a normal ascii string compare, as we only know the
1727 * invariant locale if we dont have ICU
1729 return(string_invariant_compare (str1, off1, len1, str2, off2, len2,
1733 void ves_icall_System_Globalization_CompareInfo_free_internal_collator (MonoCompareInfo *this)
1735 /* Nothing to do here */
1738 void ves_icall_System_Globalization_CompareInfo_assign_sortkey (MonoCompareInfo *this, MonoSortKey *key, MonoString *source, gint32 options)
1743 MONO_ARCH_SAVE_REGS;
1745 keylen=mono_string_length (source);
1747 arr=mono_array_new (mono_domain_get (), mono_get_byte_class (),
1749 for(i=0; i<keylen; i++) {
1750 mono_array_set (arr, guint8, i, mono_string_chars (source)[i]);
1756 int ves_icall_System_Globalization_CompareInfo_internal_index (MonoCompareInfo *this, MonoString *source, gint32 sindex, gint32 count, MonoString *value, gint32 options, MonoBoolean first)
1758 MONO_ARCH_SAVE_REGS;
1760 return(string_invariant_indexof (source, sindex, count, value, first));
1763 int ves_icall_System_Globalization_CompareInfo_internal_index_char (MonoCompareInfo *this, MonoString *source, gint32 sindex, gint32 count, gunichar2 value, gint32 options, MonoBoolean first)
1765 MONO_ARCH_SAVE_REGS;
1767 return(string_invariant_indexof_char (source, sindex, count, value,
1771 int ves_icall_System_Threading_Thread_current_lcid (void)
1773 MONO_ARCH_SAVE_REGS;
1779 MonoString *ves_icall_System_String_InternalReplace_Str_Comp (MonoString *this, MonoString *old, MonoString *new, MonoCompareInfo *comp)
1781 MONO_ARCH_SAVE_REGS;
1783 /* Do a normal ascii string compare and replace, as we only
1784 * know the invariant locale if we dont have ICU
1786 return(string_invariant_replace (this, old, new));
1789 MonoString *ves_icall_System_String_InternalToLower_Comp (MonoString *this, MonoCultureInfo *cult)
1791 MONO_ARCH_SAVE_REGS;
1793 return(string_invariant_tolower (this));
1796 MonoString *ves_icall_System_String_InternalToUpper_Comp (MonoString *this, MonoCultureInfo *cult)
1798 MONO_ARCH_SAVE_REGS;
1800 return(string_invariant_toupper (this));
1803 gunichar2 ves_icall_System_Char_InternalToUpper_Comp (gunichar2 c, MonoCultureInfo *cult)
1805 MONO_ARCH_SAVE_REGS;
1807 return g_unichar_toupper (c);
1811 gunichar2 ves_icall_System_Char_InternalToLower_Comp (gunichar2 c, MonoCultureInfo *cult)
1813 MONO_ARCH_SAVE_REGS;
1815 return g_unichar_tolower (c);
1818 static MonoString *string_invariant_tolower (MonoString *this)
1825 ret = mono_string_new_size(mono_domain_get (),
1826 mono_string_length(this));
1828 src = mono_string_chars (this);
1829 dest = mono_string_chars (ret);
1831 for (i = 0; i < mono_string_length (this); ++i) {
1832 dest[i] = g_unichar_tolower(src[i]);
1838 static MonoString *string_invariant_toupper (MonoString *this)
1845 ret = mono_string_new_size(mono_domain_get (),
1846 mono_string_length(this));
1848 src = mono_string_chars (this);
1849 dest = mono_string_chars (ret);
1851 for (i = 0; i < mono_string_length (this); ++i) {
1852 dest[i] = g_unichar_toupper(src[i]);
1858 #endif /* HAVE_ICU */
1860 static gint32 string_invariant_compare_char (gunichar2 c1, gunichar2 c2,
1864 GUnicodeType c1type, c2type;
1866 c1type = g_unichar_type (c1);
1867 c2type = g_unichar_type (c2);
1869 if (options & CompareOptions_IgnoreCase) {
1870 result = (gint32) (c1type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c1) : c1) - (c2type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c2) : c2);
1871 } else if (options & CompareOptions_Ordinal) {
1872 /* Rotor/ms return the full value just not -1 and 1 */
1873 return (gint32) c1 - c2;
1875 /* No options. Kana, symbol and spacing options don't
1876 * apply to the invariant culture.
1879 result = (gint32) c1 - c2;
1882 return ((result < 0) ? -1 : (result > 0) ? 1 : 0);
1885 static gint32 string_invariant_compare (MonoString *str1, gint32 off1,
1886 gint32 len1, MonoString *str2,
1887 gint32 off2, gint32 len2,
1890 /* c translation of C# code from old string.cs.. :) */
1903 ustr1 = mono_string_chars(str1)+off1;
1904 ustr2 = mono_string_chars(str2)+off2;
1908 for (pos = 0; pos != length; pos++) {
1909 if (pos >= len1 || pos >= len2)
1912 charcmp = string_invariant_compare_char(ustr1[pos], ustr2[pos],
1919 /* the lesser wins, so if we have looped until length we just
1920 * need to check the last char
1922 if (pos == length) {
1923 return(string_invariant_compare_char(ustr1[pos - 1],
1924 ustr2[pos - 1], options));
1927 /* Test if one of the strings has been compared to the end */
1934 } else if (pos >= len2) {
1938 /* if not, check our last char only.. (can this happen?) */
1939 return(string_invariant_compare_char(ustr1[pos], ustr2[pos], options));
1942 static MonoString *string_invariant_replace (MonoString *me,
1943 MonoString *oldValue,
1944 MonoString *newValue)
1948 gunichar2 *dest=NULL; /* shut gcc up */
1950 gunichar2 *newstr=NULL; /* shut gcc up here too */
1961 oldstr = mono_string_chars(oldValue);
1962 oldstrlen = mono_string_length(oldValue);
1964 if (NULL != newValue) {
1965 newstr = mono_string_chars(newValue);
1966 newstrlen = mono_string_length(newValue);
1970 src = mono_string_chars(me);
1971 srclen = mono_string_length(me);
1973 if (oldstrlen != newstrlen) {
1975 while (i <= srclen - oldstrlen) {
1976 if (0 == memcmp(src + i, oldstr, oldstrlen * sizeof(gunichar2))) {
1985 newsize = srclen + ((newstrlen - oldstrlen) * occurr);
1991 while (i < srclen) {
1992 if (0 == memcmp(src + i, oldstr, oldstrlen * sizeof(gunichar2))) {
1994 ret = mono_string_new_size( mono_domain_get (), newsize);
1995 dest = mono_string_chars(ret);
1996 memcpy (dest, src, i * sizeof(gunichar2));
1998 if (newstrlen > 0) {
1999 memcpy(dest + destpos, newstr, newstrlen * sizeof(gunichar2));
2000 destpos += newstrlen;
2004 } else if (ret != NULL) {
2005 dest[destpos] = src[i];
2017 static gint32 string_invariant_indexof (MonoString *source, gint32 sindex,
2018 gint32 count, MonoString *value,
2026 lencmpstr = mono_string_length(value);
2028 src = mono_string_chars(source);
2029 cmpstr = mono_string_chars(value);
2033 for(pos=sindex;pos <= sindex+count;pos++) {
2034 for(i=0;src[pos+i]==cmpstr[i];) {
2035 if(++i==lencmpstr) {
2043 for(pos=sindex-lencmpstr+1;pos>sindex-count;pos--) {
2044 if(memcmp (src+pos, cmpstr,
2045 lencmpstr*sizeof(gunichar2))==0) {
2054 static gint32 string_invariant_indexof_char (MonoString *source, gint32 sindex,
2055 gint32 count, gunichar2 value,
2061 src = mono_string_chars(source);
2063 for (pos = sindex; pos != count + sindex; pos++) {
2064 if (src [pos] == value) {
2071 for (pos = sindex; pos > sindex - count; pos--) {
2072 if (src [pos] == value)