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);
48 static const RegionInfoEntry* region_info_entry_from_lcid (int lcid);
51 culture_lcid_locator (const void *a, const void *b)
53 const CultureInfoEntry *aa = a;
54 const CultureInfoEntry *bb = b;
56 return (aa->lcid - bb->lcid);
60 region_lcid_locator (const void *a, const void *b)
63 const RegionLCIDMap *bb = b;
65 return *lcid - bb->lcid;
69 culture_name_locator (const void *a, const void *b)
72 const CultureInfoNameEntry *bb = b;
75 ret = strcmp (aa, idx2string (bb->name));
81 region_name_locator (const void *a, const void *b)
84 const RegionInfoNameEntry *bb = b;
87 ret = strcmp (aa, idx2string (bb->name));
93 create_group_sizes_array (const gint *gs, gint ml)
98 for (i = 0; i < ml; i++) {
104 ret = mono_array_new (mono_domain_get (),
105 mono_get_int32_class (), len);
107 for(i = 0; i < len; i++)
108 mono_array_set (ret, gint32, i, gs [i]);
114 create_names_array_idx (const guint16 *names, int ml)
123 domain = mono_domain_get ();
125 for (i = 0; i < ml; i++) {
131 ret = mono_array_new (mono_domain_get (), mono_get_string_class (), len);
133 for(i = 0; i < len; i++)
134 mono_array_set (ret, MonoString *, i, mono_string_new (domain, idx2string (names [i])));
140 ves_icall_System_Globalization_CultureInfo_construct_datetime_format (MonoCultureInfo *this)
143 MonoDateTimeFormatInfo *datetime;
144 const DateTimeFormatEntry *dfe;
148 g_assert (this->datetime_index >= 0);
150 datetime = this->datetime_format;
151 dfe = &datetime_format_entries [this->datetime_index];
153 domain = mono_domain_get ();
155 datetime->AbbreviatedDayNames = create_names_array_idx (dfe->abbreviated_day_names,
157 datetime->AbbreviatedMonthNames = create_names_array_idx (dfe->abbreviated_month_names,
159 datetime->AMDesignator = mono_string_new (domain, idx2string (dfe->am_designator));
160 datetime->CalendarWeekRule = dfe->calendar_week_rule;
161 datetime->DateSeparator = mono_string_new (domain, idx2string (dfe->date_separator));
162 datetime->DayNames = create_names_array_idx (dfe->day_names, NUM_DAYS);
163 datetime->FirstDayOfWeek = dfe->first_day_of_week;
164 datetime->FullDateTimePattern = mono_string_new (domain, idx2string (dfe->full_date_time_pattern));
165 datetime->LongDatePattern = mono_string_new (domain, idx2string (dfe->long_date_pattern));
166 datetime->LongTimePattern = mono_string_new (domain, idx2string (dfe->long_time_pattern));
167 datetime->MonthDayPattern = mono_string_new (domain, idx2string (dfe->month_day_pattern));
168 datetime->MonthNames = create_names_array_idx (dfe->month_names, NUM_MONTHS);
169 datetime->PMDesignator = mono_string_new (domain, idx2string (dfe->pm_designator));
170 datetime->ShortDatePattern = mono_string_new (domain, idx2string (dfe->short_date_pattern));
171 datetime->ShortTimePattern = mono_string_new (domain, idx2string (dfe->short_time_pattern));
172 datetime->TimeSeparator = mono_string_new (domain, idx2string (dfe->time_separator));
173 datetime->YearMonthPattern = mono_string_new (domain, idx2string (dfe->year_month_pattern));
174 datetime->ShortDatePatterns = create_names_array_idx (dfe->short_date_patterns,
175 NUM_SHORT_DATE_PATTERNS);
176 datetime->LongDatePatterns = create_names_array_idx (dfe->long_date_patterns,
177 NUM_LONG_DATE_PATTERNS);
178 datetime->ShortTimePatterns = create_names_array_idx (dfe->short_time_patterns,
179 NUM_SHORT_TIME_PATTERNS);
180 datetime->LongTimePatterns = create_names_array_idx (dfe->long_time_patterns,
181 NUM_LONG_TIME_PATTERNS);
186 ves_icall_System_Globalization_CultureInfo_construct_number_format (MonoCultureInfo *this)
189 MonoNumberFormatInfo *number;
190 const NumberFormatEntry *nfe;
194 g_assert (this->number_format != 0);
196 number = this->number_format;
197 nfe = &number_format_entries [this->number_index];
199 domain = mono_domain_get ();
201 number->currencyDecimalDigits = nfe->currency_decimal_digits;
202 number->currencyDecimalSeparator = mono_string_new (domain,
203 idx2string (nfe->currency_decimal_separator));
204 number->currencyGroupSeparator = mono_string_new (domain,
205 idx2string (nfe->currency_group_separator));
206 number->currencyGroupSizes = create_group_sizes_array (nfe->currency_group_sizes,
208 number->currencyNegativePattern = nfe->currency_negative_pattern;
209 number->currencyPositivePattern = nfe->currency_positive_pattern;
210 number->currencySymbol = mono_string_new (domain, idx2string (nfe->currency_symbol));
211 number->naNSymbol = mono_string_new (domain, idx2string (nfe->nan_symbol));
212 number->negativeInfinitySymbol = mono_string_new (domain,
213 idx2string (nfe->negative_infinity_symbol));
214 number->negativeSign = mono_string_new (domain, idx2string (nfe->negative_sign));
215 number->numberDecimalDigits = nfe->number_decimal_digits;
216 number->numberDecimalSeparator = mono_string_new (domain,
217 idx2string (nfe->number_decimal_separator));
218 number->numberGroupSeparator = mono_string_new (domain, idx2string (nfe->number_group_separator));
219 number->numberGroupSizes = create_group_sizes_array (nfe->number_group_sizes,
221 number->numberNegativePattern = nfe->number_negative_pattern;
222 number->percentDecimalDigits = nfe->percent_decimal_digits;
223 number->percentDecimalSeparator = mono_string_new (domain,
224 idx2string (nfe->percent_decimal_separator));
225 number->percentGroupSeparator = mono_string_new (domain,
226 idx2string (nfe->percent_group_separator));
227 number->percentGroupSizes = create_group_sizes_array (nfe->percent_group_sizes,
229 number->percentNegativePattern = nfe->percent_negative_pattern;
230 number->percentPositivePattern = nfe->percent_positive_pattern;
231 number->percentSymbol = mono_string_new (domain, idx2string (nfe->percent_symbol));
232 number->perMilleSymbol = mono_string_new (domain, idx2string (nfe->per_mille_symbol));
233 number->positiveInfinitySymbol = mono_string_new (domain,
234 idx2string (nfe->positive_infinity_symbol));
235 number->positiveSign = mono_string_new (domain, idx2string (nfe->positive_sign));
239 construct_culture (MonoCultureInfo *this, const CultureInfoEntry *ci)
241 MonoDomain *domain = mono_domain_get ();
243 this->lcid = ci->lcid;
244 this->name = mono_string_new (domain, idx2string (ci->name));
245 this->icu_name = mono_string_new (domain, idx2string (ci->icu_name));
246 this->displayname = mono_string_new (domain, idx2string (ci->displayname));
247 this->englishname = mono_string_new (domain, idx2string (ci->englishname));
248 this->nativename = mono_string_new (domain, idx2string (ci->nativename));
249 this->win3lang = mono_string_new (domain, idx2string (ci->win3lang));
250 this->iso3lang = mono_string_new (domain, idx2string (ci->iso3lang));
251 this->iso2lang = mono_string_new (domain, idx2string (ci->iso2lang));
252 this->parent_lcid = ci->parent_lcid;
253 this->specific_lcid = ci->specific_lcid;
254 this->datetime_index = ci->datetime_format_index;
255 this->number_index = ci->number_format_index;
256 this->calendar_data = ci->calendar_data;
257 this->text_info_data = &ci->text_info;
263 construct_region (MonoRegionInfo *this, const RegionInfoEntry *ri)
265 MonoDomain *domain = mono_domain_get ();
267 this->region_id = ri->region_id;
268 this->iso2name = mono_string_new (domain, idx2string (ri->iso2name));
269 this->iso3name = mono_string_new (domain, idx2string (ri->iso3name));
270 this->win3name = mono_string_new (domain, idx2string (ri->win3name));
271 this->english_name = mono_string_new (domain, idx2string (ri->english_name));
272 this->currency_symbol = mono_string_new (domain, idx2string (ri->currency_symbol));
273 this->iso_currency_symbol = mono_string_new (domain, idx2string (ri->iso_currency_symbol));
274 this->currency_english_name = mono_string_new (domain, idx2string (ri->currency_english_name));
280 construct_culture_from_specific_name (MonoCultureInfo *ci, gchar *name)
282 const CultureInfoEntry *entry;
283 const CultureInfoNameEntry *ne;
287 ne = bsearch (name, culture_name_entries, NUM_CULTURE_ENTRIES,
288 sizeof (CultureInfoNameEntry), culture_name_locator);
293 entry = &culture_entries [ne->culture_entry_index];
295 /* try avoiding another lookup, often the culture is its own specific culture */
296 if (entry->lcid != entry->specific_lcid)
297 entry = culture_info_entry_from_lcid (entry->specific_lcid);
299 return construct_culture (ci, entry);
303 construct_region_from_specific_name (MonoRegionInfo *ri, gchar *name)
305 const RegionInfoEntry *entry;
306 const RegionInfoNameEntry *ne;
310 ne = bsearch (name, region_name_entries, NUM_REGION_ENTRIES,
311 sizeof (RegionInfoNameEntry), region_name_locator);
316 entry = ®ion_entries [ne->region_entry_index];
318 return construct_region (ri, entry);
321 static const CultureInfoEntry*
322 culture_info_entry_from_lcid (int lcid)
324 const CultureInfoEntry *ci;
325 CultureInfoEntry key;
328 ci = bsearch (&key, culture_entries, NUM_CULTURE_ENTRIES, sizeof (CultureInfoEntry), culture_lcid_locator);
333 static const RegionInfoEntry*
334 region_info_entry_from_lcid (int lcid)
336 const RegionInfoEntry *entry;
337 const RegionLCIDMap *ne;
341 ne = bsearch (&lcid, region_lcid_map, NUM_REGION_LCID_MAP,
342 sizeof (RegionLCIDMap), region_lcid_locator);
347 entry = ®ion_entries [ne->region_entry_index];
353 * The following two methods are modified from the ICU source code. (http://oss.software.ibm.com/icu)
354 * Copyright (c) 1995-2003 International Business Machines Corporation and others
355 * All rights reserved.
358 get_posix_locale (void)
360 const gchar* posix_locale = NULL;
362 posix_locale = g_getenv("LC_ALL");
363 if (posix_locale == 0) {
364 posix_locale = g_getenv("LANG");
365 if (posix_locale == 0) {
366 posix_locale = setlocale(LC_ALL, NULL);
370 if (posix_locale == NULL)
373 if ((strcmp ("C", posix_locale) == 0) || (strchr (posix_locale, ' ') != NULL)
374 || (strchr (posix_locale, '/') != NULL)) {
376 * HPUX returns 'C C C C C C C'
377 * Solaris can return /en_US/C/C/C/C/C on the second try.
378 * Maybe we got some garbage.
383 return g_strdup (posix_locale);
387 get_current_locale_name (void)
390 gchar *corrected = NULL;
394 #ifdef PLATFORM_WIN32
395 locale = g_win32_getlocale ();
397 locale = get_posix_locale ();
403 if ((p = strchr (locale, '.')) != NULL) {
404 /* assume new locale can't be larger than old one? */
405 corrected = malloc (strlen (locale));
406 strncpy (corrected, locale, p - locale);
407 corrected [p - locale] = 0;
409 /* do not copy after the @ */
410 if ((p = strchr (corrected, '@')) != NULL)
411 corrected [p - corrected] = 0;
414 /* Note that we scan the *uncorrected* ID. */
415 if ((p = strrchr (locale, '@')) != NULL) {
418 * In Mono we dont handle the '@' modifier because we do
419 * not have any cultures that use it. We just trim it
420 * off of the end of the name.
423 if (corrected == NULL) {
424 corrected = malloc (strlen (locale));
425 strncpy (corrected, locale, p - locale);
426 corrected [p - locale] = 0;
430 if (corrected == NULL)
435 if ((c = strchr (corrected, '_')) != NULL)
438 g_strdown (corrected);
444 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_current_locale (MonoCultureInfo *ci)
451 locale = get_current_locale_name ();
455 ret = construct_culture_from_specific_name (ci, locale);
462 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_lcid (MonoCultureInfo *this,
465 const CultureInfoEntry *ci;
469 ci = culture_info_entry_from_lcid (lcid);
473 return construct_culture (this, ci);
477 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_name (MonoCultureInfo *this,
480 const CultureInfoNameEntry *ne;
485 n = mono_string_to_utf8 (name);
486 ne = bsearch (n, culture_name_entries, NUM_CULTURE_ENTRIES,
487 sizeof (CultureInfoNameEntry), culture_name_locator);
490 /*g_print ("ne (%s) is null\n", n);*/
496 return construct_culture (this, &culture_entries [ne->culture_entry_index]);
500 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_specific_name (MonoCultureInfo *ci,
508 locale = mono_string_to_utf8 (name);
509 ret = construct_culture_from_specific_name (ci, locale);
516 ves_icall_System_Globalization_RegionInfo_construct_internal_region_from_lcid (MonoRegionInfo *this,
519 const RegionInfoEntry *ri;
523 ri = region_info_entry_from_lcid (lcid);
527 return construct_region (this, ri);
531 ves_icall_System_Globalization_RegionInfo_construct_internal_region_from_name (MonoRegionInfo *this,
534 const RegionInfoNameEntry *ne;
539 n = mono_string_to_utf8 (name);
540 ne = bsearch (n, region_name_entries, NUM_REGION_ENTRIES,
541 sizeof (RegionInfoNameEntry), region_name_locator);
544 /*g_print ("ne (%s) is null\n", n);*/
550 return construct_region (this, ®ion_entries [ne->region_entry_index]);
554 ves_icall_System_Globalization_CultureInfo_internal_get_cultures (MonoBoolean neutral,
555 MonoBoolean specific, MonoBoolean installed)
559 MonoCultureInfo *culture;
561 const CultureInfoEntry *ci;
567 domain = mono_domain_get ();
570 for (i = 0; i < NUM_CULTURE_ENTRIES; i++) {
571 ci = &culture_entries [i];
572 is_neutral = ((ci->lcid & 0xff00) == 0 || ci->specific_lcid == 0);
573 if ((neutral && is_neutral) || (specific && !is_neutral))
577 class = mono_class_from_name (mono_get_corlib (),
578 "System.Globalization", "CultureInfo");
580 /* The InvariantCulture is not in culture_entries */
581 /* We reserve the first slot in the array for it */
585 ret = mono_array_new (domain, class, len);
592 mono_array_set (ret, MonoCultureInfo *, len++, NULL);
594 for (i = 0; i < NUM_CULTURE_ENTRIES; i++) {
595 ci = &culture_entries [i];
596 is_neutral = ((ci->lcid & 0xff00) == 0 || ci->specific_lcid == 0);
597 if ((neutral && is_neutral) || (specific && !is_neutral)) {
598 culture = (MonoCultureInfo *) mono_object_new (domain, class);
599 mono_runtime_object_init ((MonoObject *) culture);
600 construct_culture (culture, ci);
601 mono_array_set (ret, MonoCultureInfo *, len++, culture);
609 * ves_icall_System_Globalization_CultureInfo_internal_is_lcid_neutral:
611 * Set is_neutral and return TRUE if the culture is found. If it is not found return FALSE.
614 ves_icall_System_Globalization_CultureInfo_internal_is_lcid_neutral (gint lcid, MonoBoolean *is_neutral)
616 const CultureInfoEntry *entry;
620 entry = culture_info_entry_from_lcid (lcid);
625 *is_neutral = (entry->specific_lcid == 0);
633 #include <unicode/utypes.h>
634 #include <unicode/ustring.h>
635 #include <unicode/ures.h>
636 #include <unicode/ucol.h>
637 #include <unicode/usearch.h>
639 static MonoString *monostring_from_resource_index (const UResourceBundle *bundle, int32_t idx)
646 res_str=(gunichar2 *)ures_getStringByIndex (bundle, idx, &res_strlen,
652 return(mono_string_from_utf16 (res_str));
655 static UResourceBundle *open_subbundle (const UResourceBundle *bundle,
656 const char *name, int32_t req_count)
658 UResourceBundle *subbundle;
663 subbundle=ures_getByKey (bundle, name, NULL, &ec);
665 /* Couldn't find the subbundle */
669 count=ures_countArrayItems (bundle, name, &ec);
671 /* Couldn't count the subbundle */
672 ures_close (subbundle);
676 if(count!=req_count) {
678 ures_close (subbundle);
685 static MonoArray *build_array (const UResourceBundle *bundle,
686 const char *resname, int32_t req_count)
689 UResourceBundle *subbundle;
692 subbundle=open_subbundle (bundle, resname, req_count);
693 if(subbundle!=NULL) {
694 arr=mono_array_new(mono_domain_get (),
695 mono_get_string_class (), req_count);
697 for(i=0; i<req_count; i++) {
698 mono_array_set(arr, MonoString *, i, monostring_from_resource_index (subbundle, i));
701 ures_close (subbundle);
707 static MonoDateTimeFormatInfo *create_DateTimeFormat (const char *locale)
709 MonoDateTimeFormatInfo *new_dtf;
711 UResourceBundle *bundle, *subbundle;
714 class=mono_class_from_name (mono_get_corlib (),
715 "System.Globalization",
716 "DateTimeFormatInfo");
717 new_dtf=(MonoDateTimeFormatInfo *)mono_object_new (mono_domain_get (),
719 mono_runtime_object_init ((MonoObject *)new_dtf);
723 bundle=ures_open (NULL, locale, &ec);
729 subbundle=open_subbundle (bundle, "AmPmMarkers", 2);
730 if(subbundle!=NULL) {
731 new_dtf->AMDesignator=monostring_from_resource_index (subbundle, 0);
732 new_dtf->PMDesignator=monostring_from_resource_index (subbundle, 1);
734 ures_close (subbundle);
737 /* Date/Time patterns. Don't set FullDateTimePattern. As it
738 * seems to always default to LongDatePattern + " " +
739 * LongTimePattern, let the property accessor deal with it.
741 subbundle=open_subbundle (bundle, "DateTimePatterns", 9);
742 if(subbundle!=NULL) {
743 new_dtf->ShortDatePattern=monostring_from_resource_index (subbundle, 7);
744 new_dtf->LongDatePattern=monostring_from_resource_index (subbundle, 5);
745 new_dtf->ShortTimePattern=monostring_from_resource_index (subbundle, 3);
746 new_dtf->LongTimePattern=monostring_from_resource_index (subbundle, 2);
748 /* RFC1123Pattern, SortableDateTimePattern and
749 * UniversalSortableDateTimePattern all seem to be
750 * constant, and all the same as the invariant default
754 ures_close (subbundle);
758 /* Not sure what to do with these yet, so leave them set to
759 * the invariant default
761 set_field_string (new_dtf, "_DateSeparator", str);
762 set_field_string (new_dtf, "_TimeSeparator", str);
763 set_field_string (new_dtf, "_MonthDayPattern", str);
764 set_field_string (new_dtf, "_YearMonthPattern", str);
767 /* Day names. Luckily both ICU and .net start Sunday at index 0 */
768 new_dtf->DayNames=build_array (bundle, "DayNames", 7);
770 /* Abbreviated day names */
771 new_dtf->AbbreviatedDayNames=build_array (bundle, "DayAbbreviations",
775 new_dtf->MonthNames=build_array (bundle, "MonthNames", 12);
777 /* Abbreviated month names */
778 new_dtf->AbbreviatedMonthNames=build_array (bundle,
779 "MonthAbbreviations", 12);
781 /* TODO: DayOfWeek _FirstDayOfWeek, Calendar _Calendar, CalendarWeekRule _CalendarWeekRule */
788 static MonoNumberFormatInfo *create_NumberFormat (const char *locale)
790 MonoNumberFormatInfo *new_nf;
792 MonoMethodDesc* methodDesc;
794 UResourceBundle *bundle, *subbundle, *table_entries;
797 static char country [7]; /* FIXME */
798 const UChar *res_str;
801 class=mono_class_from_name (mono_get_corlib (),
802 "System.Globalization",
804 new_nf=(MonoNumberFormatInfo *)mono_object_new (mono_domain_get (),
806 mono_runtime_object_init ((MonoObject *)new_nf);
810 bundle=ures_open (NULL, locale, &ec);
815 /* Number Elements */
817 subbundle=ures_getByKey (bundle, "NumberElements", NULL, &ec);
819 /* Couldn't find the subbundle */
823 count=ures_countArrayItems (bundle, "NumberElements", &ec);
825 /* Couldn't count the subbundle */
826 ures_close (subbundle);
830 if(subbundle!=NULL) {
831 new_nf->numberDecimalSeparator=monostring_from_resource_index (subbundle, 0);
832 new_nf->numberGroupSeparator=monostring_from_resource_index (subbundle, 1);
833 new_nf->percentDecimalSeparator=monostring_from_resource_index (subbundle, 0);
834 new_nf->percentGroupSeparator=monostring_from_resource_index (subbundle, 1);
835 new_nf->percentSymbol=monostring_from_resource_index (subbundle, 3);
836 new_nf->zeroPattern=monostring_from_resource_index (subbundle, 4);
837 new_nf->digitPattern=monostring_from_resource_index (subbundle, 5);
838 new_nf->negativeSign=monostring_from_resource_index (subbundle, 6);
839 new_nf->perMilleSymbol=monostring_from_resource_index (subbundle, 8);
840 new_nf->positiveInfinitySymbol=monostring_from_resource_index (subbundle, 9);
841 /* we dont have this in CLDR, so copy it from positiveInfinitySymbol */
842 new_nf->negativeInfinitySymbol=monostring_from_resource_index (subbundle, 9);
843 new_nf->naNSymbol=monostring_from_resource_index (subbundle, 10);
844 new_nf->currencyDecimalSeparator=monostring_from_resource_index (subbundle, 0);
845 new_nf->currencyGroupSeparator=monostring_from_resource_index (subbundle, 1);
847 ures_close (subbundle);
850 /* get country name */
852 uloc_getCountry (locale, country, sizeof (country), &ec);
853 if (U_SUCCESS (ec)) {
855 /* find country name in root.CurrencyMap */
856 subbundle = ures_getByKey (bundle, "CurrencyMap", NULL, &ec);
857 if (U_SUCCESS (ec)) {
859 /* get currency id for specified country */
860 table_entries = ures_getByKey (subbundle, country, NULL, &ec);
861 if (U_SUCCESS (ec)) {
862 ures_close (subbundle);
865 res_str = ures_getStringByIndex (
866 table_entries, 0, &res_strlen, &ec);
868 /* now we have currency id string */
869 ures_close (table_entries);
871 u_UCharsToChars (res_str, country,
875 /* find currency string in locale data */
876 subbundle = ures_getByKey (
877 bundle, "Currencies",
880 if (U_SUCCESS (ec)) {
882 /* find currency symbol under specified currency id */
883 table_entries = ures_getByKey (subbundle, country, NULL, &ec);
884 if (U_SUCCESS (ec)) {
885 /* get the first string only,
886 * the second is international currency symbol (not used)*/
887 new_nf->currencySymbol=monostring_from_resource_index (table_entries, 0);
888 ures_close (table_entries);
890 ures_close (subbundle);
898 subbundle=open_subbundle (bundle, "NumberPatterns", 4);
899 if(subbundle!=NULL) {
900 new_nf->decimalFormats=monostring_from_resource_index (subbundle, 0);
901 new_nf->currencyFormats=monostring_from_resource_index (subbundle, 1);
902 new_nf->percentFormats=monostring_from_resource_index (subbundle, 2);
903 ures_close (subbundle);
905 /* calls InitPatterns to parse the patterns
907 methodDesc = mono_method_desc_new (
908 "System.Globalization.NumberFormatInfo:InitPatterns()",
910 method = mono_method_desc_search_in_class (methodDesc, class);
912 mono_runtime_invoke (method, new_nf, NULL, NULL);
914 g_warning (G_GNUC_PRETTY_FUNCTION ": Runtime mismatch with class lib! (Looking for System.Globalization.NumberFormatInfo:InitPatterns())");
923 static char *mono_string_to_icu_locale (MonoString *locale)
926 char *passed_locale, *icu_locale=NULL;
927 int32_t loc_len, ret;
929 passed_locale=mono_string_to_utf8 (locale);
932 ret=uloc_getName (passed_locale, NULL, 0, &ec);
933 if(ec==U_BUFFER_OVERFLOW_ERROR) {
936 icu_locale=(char *)g_malloc0 (sizeof(char)*loc_len);
937 ret=uloc_getName (passed_locale, icu_locale, loc_len, &ec);
939 g_free (passed_locale);
944 void ves_icall_System_Globalization_CultureInfo_construct_internal_locale (MonoCultureInfo *this, MonoString *locale)
950 int32_t str_len, ret;
954 icu_locale=mono_string_to_icu_locale (locale);
955 if(icu_locale==NULL) {
956 /* Something went wrong */
957 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
961 /* Fill in the static fields */
963 /* TODO: Calendar, InstalledUICulture, OptionalCalendars,
967 str_len=256; /* Should be big enough for anything */
968 str=(char *)g_malloc0 (sizeof(char)*str_len);
969 ustr=(UChar *)g_malloc0 (sizeof(UChar)*str_len);
973 ret=uloc_getDisplayName (icu_locale, "en", ustr, str_len, &ec);
974 if(U_SUCCESS (ec) && ret<str_len) {
975 this->englishname=mono_string_from_utf16 ((gunichar2 *)ustr);
978 ret=uloc_getDisplayName (icu_locale, uloc_getDefault (), ustr, str_len,
980 if(U_SUCCESS (ec) && ret<str_len) {
981 this->displayname=mono_string_from_utf16 ((gunichar2 *)ustr);
984 ret=uloc_getDisplayName (icu_locale, icu_locale, ustr, str_len, &ec);
985 if(U_SUCCESS (ec) && ret<str_len) {
986 this->nativename=mono_string_from_utf16 ((gunichar2 *)ustr);
989 this->iso3lang=mono_string_new_wrapper (uloc_getISO3Language (icu_locale));
991 ret=uloc_getLanguage (icu_locale, str, str_len, &ec);
992 if(U_SUCCESS (ec) && ret<str_len) {
993 this->iso2lang=mono_string_new_wrapper (str);
996 this->datetime_format=create_DateTimeFormat (icu_locale);
997 this->number_format=create_NumberFormat (icu_locale);
1001 g_free (icu_locale);
1004 void ves_icall_System_Globalization_CompareInfo_construct_compareinfo (MonoCompareInfo *comp, MonoString *locale)
1010 MONO_ARCH_SAVE_REGS;
1013 g_message (G_GNUC_PRETTY_FUNCTION ": Constructing collator for locale [%s]", mono_string_to_utf8 (locale));
1016 icu_locale=mono_string_to_icu_locale (locale);
1017 if(icu_locale==NULL) {
1018 /* Something went wrong */
1019 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
1024 coll=ucol_open (icu_locale, &ec);
1025 if(U_SUCCESS (ec)) {
1026 comp->ICU_collator=coll;
1028 comp->ICU_collator=NULL;
1031 g_free (icu_locale);
1034 /* Set up the collator to reflect the options required. Some of these
1035 * options clash, as they adjust the collator strength level. Try to
1036 * make later checks reduce the strength level, and attempt to take
1037 * previous options into account.
1039 * Don't bother to check the error returns when setting the
1040 * attributes, as a failure here is hardly grounds to error out.
1042 static void set_collator_options (UCollator *coll, gint32 options)
1044 UErrorCode ec=U_ZERO_ERROR;
1046 /* Set up the defaults */
1047 ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE,
1049 ucol_setAttribute (coll, UCOL_CASE_LEVEL, UCOL_OFF, &ec);
1051 /* Do this first so other options will override the quaternary
1052 * level strength setting if necessary
1054 if(!(options & CompareOptions_IgnoreKanaType)) {
1055 ucol_setAttribute (coll, UCOL_HIRAGANA_QUATERNARY_MODE,
1057 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_QUATERNARY, &ec);
1060 /* Word sort, the default */
1061 if(!(options & CompareOptions_StringSort)) {
1062 ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING,
1064 /* Tertiary strength is the default, but it might have
1065 * been set to quaternary above. (We don't want that
1066 * here, because that will order all the punctuation
1067 * first instead of just ignoring it.)
1069 * Unfortunately, tertiary strength with
1070 * ALTERNATE_HANDLING==SHIFTED means that '/' and '@'
1071 * compare to equal, which has the nasty side effect
1072 * of killing mcs :-( (We can't specify a
1073 * culture-insensitive compare, because
1074 * String.StartsWith doesn't have that option.)
1076 * ALTERNATE_HANDLING==SHIFTED is needed to accomplish
1077 * the word-sorting-ignoring-punctuation feature. So
1078 * we have to live with the slightly mis-ordered
1079 * punctuation and a working mcs...
1081 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_QUATERNARY, &ec);
1084 if(options & CompareOptions_IgnoreCase) {
1085 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_SECONDARY, &ec);
1086 ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE, &ec);
1089 if(options & CompareOptions_IgnoreWidth) {
1090 /* Kana width is a tertiary strength difference. This
1091 * will totally break the !IgnoreKanaType option
1093 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_SECONDARY, &ec);
1096 if(options & CompareOptions_IgnoreNonSpace) {
1097 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_PRIMARY, &ec);
1098 /* We can still compare case even when just checking
1101 if(!(options & CompareOptions_IgnoreCase) ||
1102 !(options & CompareOptions_IgnoreWidth)) {
1103 /* Not sure if CASE_LEVEL handles kana width
1105 ucol_setAttribute (coll, UCOL_CASE_LEVEL, UCOL_ON,
1110 if(options & CompareOptions_IgnoreSymbols) {
1111 /* Don't know what to do here */
1114 if(options == CompareOptions_Ordinal) {
1115 /* This one is handled elsewhere */
1119 gint32 ves_icall_System_Globalization_CompareInfo_internal_compare (MonoCompareInfo *this, MonoString *str1, gint32 off1, gint32 len1, MonoString *str2, gint32 off2, gint32 len2, gint32 options)
1122 UCollationResult result;
1124 MONO_ARCH_SAVE_REGS;
1127 g_message (G_GNUC_PRETTY_FUNCTION ": Comparing [%s] and [%s]", mono_string_to_utf8 (str1), mono_string_to_utf8 (str2));
1130 coll=this->ICU_collator;
1133 g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", this->lcid);
1136 if(coll==NULL || this->lcid==0x007F ||
1137 options & CompareOptions_Ordinal) {
1139 g_message (G_GNUC_PRETTY_FUNCTION ": No collator or invariant, using shortcut");
1142 return(string_invariant_compare (str1, off1, len1, str2, off2,
1146 if (!mono_monitor_enter ((MonoObject *)this))
1149 set_collator_options (coll, options);
1151 result=ucol_strcoll (coll, mono_string_chars (str1)+off1, len1,
1152 mono_string_chars (str2)+off2, len2);
1154 mono_monitor_exit ((MonoObject *)this);
1157 g_message (G_GNUC_PRETTY_FUNCTION ": Comparison of [%s] and [%s] returning %d", mono_string_to_utf8 (str1), mono_string_to_utf8 (str2), result);
1163 void ves_icall_System_Globalization_CompareInfo_free_internal_collator (MonoCompareInfo *this)
1167 MONO_ARCH_SAVE_REGS;
1169 coll=this->ICU_collator;
1171 this->ICU_collator = NULL;
1176 void ves_icall_System_Globalization_CompareInfo_assign_sortkey (MonoCompareInfo *this, MonoSortKey *key, MonoString *source, gint32 options)
1183 MONO_ARCH_SAVE_REGS;
1185 coll=this->ICU_collator;
1187 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
1191 if (!mono_monitor_enter ((MonoObject *)this))
1194 set_collator_options (coll, options);
1196 keylen=ucol_getSortKey (coll, mono_string_chars (source),
1197 mono_string_length (source), NULL, 0);
1198 keybuf=g_malloc (sizeof(char)* keylen);
1199 ucol_getSortKey (coll, mono_string_chars (source),
1200 mono_string_length (source), keybuf, keylen);
1202 mono_monitor_exit ((MonoObject *)this);
1204 arr=mono_array_new (mono_domain_get (), mono_get_byte_class (),
1206 for(i=0; i<keylen; i++) {
1207 mono_array_set (arr, guint8, i, keybuf[i]);
1215 int ves_icall_System_Globalization_CompareInfo_internal_index (MonoCompareInfo *this, MonoString *source, gint32 sindex, gint32 count, MonoString *value, gint32 options, MonoBoolean first)
1220 UStringSearch *search;
1223 MONO_ARCH_SAVE_REGS;
1226 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);
1229 coll=this->ICU_collator;
1232 g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", this->lcid);
1235 if(coll==NULL || this->lcid==0x007F ||
1236 options & CompareOptions_Ordinal) {
1238 g_message (G_GNUC_PRETTY_FUNCTION ": No collator or invariant, using shortcut");
1241 return(string_invariant_indexof (source, sindex, count, value,
1245 usrcstr=g_malloc0 (sizeof(UChar)*(count+1));
1247 memcpy (usrcstr, mono_string_chars (source)+sindex,
1248 sizeof(UChar)*count);
1250 memcpy (usrcstr, mono_string_chars (source)+sindex-count+1,
1251 sizeof(UChar)*count);
1254 if (!mono_monitor_enter ((MonoObject *)this))
1259 /* Need to set the collator to a fairly weak level, so that it
1260 * treats characters that can be written differently as
1261 * identical (eg "ß" and "ss", "æ" and "ae" or "ä" etc.) Note
1262 * that this means that the search string and the original
1263 * text might have differing lengths.
1265 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_PRIMARY, &ec);
1267 /* Still notice case differences though (normally a tertiary
1270 ucol_setAttribute (coll, UCOL_CASE_LEVEL, UCOL_ON, &ec);
1272 /* Don't ignore some codepoints */
1273 ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE,
1276 search=usearch_openFromCollator (mono_string_chars (value),
1277 mono_string_length (value),
1278 usrcstr, count, coll, NULL, &ec);
1279 if(U_SUCCESS (ec)) {
1281 pos=usearch_first (search, &ec);
1283 pos=usearch_last (search, &ec);
1286 while (pos!=USEARCH_DONE) {
1289 UCollationResult uret;
1292 g_message (G_GNUC_PRETTY_FUNCTION
1293 ": Got potential match at %d (sindex %d) len %d", pos, sindex, usearch_getMatchedLength (search));
1296 /* ICU usearch currently ignores most of the
1297 * collator attributes :-(
1299 * Check the returned match to see if it
1300 * really does match properly...
1302 match_len = usearch_getMatchedLength (search);
1303 match=(UChar *)g_malloc0 (sizeof(UChar) * (match_len + 1));
1304 usearch_getMatchedText (search, match, match_len, &ec);
1306 uret = ucol_strcoll (coll, match, match_len,
1307 mono_string_chars (value),
1308 mono_string_length (value));
1311 if (uret == UCOL_EQUAL) {
1312 /* OK, we really did get a match */
1314 g_message (G_GNUC_PRETTY_FUNCTION
1315 ": Got match at %d len %d", pos,
1323 pos+=(sindex-count+1);
1330 /* False alarm, keep looking */
1332 pos=usearch_next (search, &ec);
1334 pos=usearch_previous (search, &ec);
1338 g_message (G_GNUC_PRETTY_FUNCTION ": usearch_open error: %s",
1342 usearch_close (search);
1344 mono_monitor_exit ((MonoObject *)this);
1351 int ves_icall_System_Globalization_CompareInfo_internal_index_char (MonoCompareInfo *this, MonoString *source, gint32 sindex, gint32 count, gunichar2 value, gint32 options, MonoBoolean first)
1354 UChar *usrcstr, uvalstr[2]={0, 0};
1356 UStringSearch *search;
1359 MONO_ARCH_SAVE_REGS;
1362 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);
1365 coll=this->ICU_collator;
1368 g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", this->lcid);
1371 if(coll==NULL || this->lcid==0x007F ||
1372 options & CompareOptions_Ordinal) {
1374 g_message (G_GNUC_PRETTY_FUNCTION ": No collator or invariant, using shortcut");
1377 return(string_invariant_indexof_char (source, sindex, count,
1381 usrcstr=g_malloc0 (sizeof(UChar)*(count+1));
1383 memcpy (usrcstr, mono_string_chars (source)+sindex,
1384 sizeof(UChar)*count);
1386 memcpy (usrcstr, mono_string_chars (source)+sindex-count+1,
1387 sizeof(UChar)*count);
1391 if (!mono_monitor_enter ((MonoObject *)this))
1396 /* Need to set the collator to a fairly weak level, so that it
1397 * treats characters that can be written differently as
1398 * identical (eg "ß" and "ss", "æ" and "ae" or "ä" etc.) Note
1399 * that this means that the search string and the original
1400 * text might have differing lengths.
1402 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_PRIMARY, &ec);
1404 /* Still notice case differences though (normally a tertiary
1407 ucol_setAttribute (coll, UCOL_CASE_LEVEL, UCOL_ON, &ec);
1409 /* Don't ignore some codepoints */
1410 ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE,
1413 search=usearch_openFromCollator (uvalstr, 1, usrcstr, count, coll,
1415 if(U_SUCCESS (ec)) {
1417 pos=usearch_first (search, &ec);
1419 pos=usearch_last (search, &ec);
1422 while (pos!=USEARCH_DONE) {
1425 UCollationResult uret;
1428 g_message (G_GNUC_PRETTY_FUNCTION
1429 ": Got potential match at %d (sindex %d) len %d", pos, sindex, usearch_getMatchedLength (search));
1432 /* ICU usearch currently ignores most of the
1433 * collator attributes :-(
1435 * Check the returned match to see if it
1436 * really does match properly...
1438 match_len = usearch_getMatchedLength (search);
1439 match=(UChar *)g_malloc0 (sizeof(UChar) * (match_len + 1));
1440 usearch_getMatchedText (search, match, match_len, &ec);
1442 uret = ucol_strcoll (coll, match, match_len, uvalstr,
1446 if (uret == UCOL_EQUAL) {
1447 /* OK, we really did get a match */
1449 g_message (G_GNUC_PRETTY_FUNCTION
1450 ": Got match at %d len %d", pos,
1458 pos+=(sindex-count+1);
1465 /* False alarm, keep looking */
1467 pos=usearch_next (search, &ec);
1469 pos=usearch_previous (search, &ec);
1473 g_message (G_GNUC_PRETTY_FUNCTION ": usearch_open error: %s",
1477 usearch_close (search);
1479 mono_monitor_exit ((MonoObject *)this);
1486 int ves_icall_System_Threading_Thread_current_lcid (void)
1488 MONO_ARCH_SAVE_REGS;
1490 return(uloc_getLCID (uloc_getDefault ()));
1493 MonoString *ves_icall_System_String_InternalReplace_Str_Comp (MonoString *this, MonoString *old, MonoString *new, MonoCompareInfo *comp)
1495 MonoString *ret=NULL;
1498 UStringSearch *search;
1500 MONO_ARCH_SAVE_REGS;
1503 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));
1506 coll=comp->ICU_collator;
1509 g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", comp->lcid);
1512 if(coll==NULL || comp->lcid==0x007F) {
1514 g_message (G_GNUC_PRETTY_FUNCTION ": No collator or invariant, using shortcut");
1517 return(string_invariant_replace (this, old, new));
1520 if (!mono_monitor_enter ((MonoObject *)comp))
1525 /* Need to set the collator to a fairly weak level, so that it
1526 * treats characters that can be written differently as
1527 * identical (eg "ß" and "ss", "æ" and "ae" or "ä" etc.) Note
1528 * that this means that the search string and the original
1529 * text might have differing lengths.
1531 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_PRIMARY, &ec);
1533 /* Still notice case differences though (normally a tertiary
1536 ucol_setAttribute (coll, UCOL_CASE_LEVEL, UCOL_ON, &ec);
1538 /* Don't ignore some codepoints */
1539 ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE,
1542 search=usearch_openFromCollator (mono_string_chars (old),
1543 mono_string_length (old),
1544 mono_string_chars (this),
1545 mono_string_length (this),
1547 if(U_SUCCESS (ec)) {
1548 int pos, oldpos, len_delta=0;
1549 int32_t newstr_len=mono_string_length (new), match_len;
1550 UChar *uret, *match;
1552 for(pos=usearch_first (search, &ec);
1554 pos=usearch_next (search, &ec)) {
1555 /* ICU usearch currently ignores most of the collator
1558 * Check the returned match to see if it really
1559 * does match properly...
1561 match_len = usearch_getMatchedLength (search);
1563 if(match_len == 0) {
1567 match=(UChar *)g_malloc0 (sizeof(UChar) * (match_len + 1));
1568 usearch_getMatchedText (search, match, match_len, &ec);
1570 if (ucol_strcoll (coll, match, match_len,
1571 mono_string_chars (old),
1572 mono_string_length (old)) == UCOL_EQUAL) {
1573 /* OK, we really did get a match */
1575 g_message (G_GNUC_PRETTY_FUNCTION
1576 ": Got match at %d len %d", pos,
1580 len_delta += (newstr_len - match_len);
1584 g_message (G_GNUC_PRETTY_FUNCTION
1585 ": Got false match at %d len %d",
1592 g_message (G_GNUC_PRETTY_FUNCTION
1593 ": New string length is %d (delta %d)",
1594 mono_string_length (this)+len_delta, len_delta);
1597 uret=(UChar *)g_malloc0 (sizeof(UChar) * (mono_string_length (this)+len_delta+2));
1599 for(oldpos=0, pos=usearch_first (search, &ec);
1601 pos=usearch_next (search, &ec)) {
1602 match_len = usearch_getMatchedLength (search);
1604 if (match_len == 0) {
1608 match=(UChar *)g_malloc0 (sizeof(UChar) * (match_len + 1));
1609 usearch_getMatchedText (search, match, match_len, &ec);
1611 /* Add the unmatched text */
1612 u_strncat (uret, mono_string_chars (this)+oldpos,
1614 if (ucol_strcoll (coll, match, match_len,
1615 mono_string_chars (old),
1616 mono_string_length (old)) == UCOL_EQUAL) {
1617 /* Then the replacement */
1618 u_strcat (uret, mono_string_chars (new));
1620 /* Then the original, because this is a
1623 u_strncat (uret, mono_string_chars (this)+pos,
1626 oldpos=pos+match_len;
1630 /* Finish off with the trailing unmatched text */
1631 u_strcat (uret, mono_string_chars (this)+oldpos);
1633 ret=mono_string_from_utf16 ((gunichar2 *)uret);
1635 g_message (G_GNUC_PRETTY_FUNCTION ": usearch_open error: %s",
1639 usearch_close (search);
1641 mono_monitor_exit ((MonoObject *)comp);
1644 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));
1650 #else /* HAVE_ICU */
1652 void ves_icall_System_Globalization_CultureInfo_construct_internal_locale (MonoCultureInfo *this, MonoString *locale)
1654 MONO_ARCH_SAVE_REGS;
1656 /* Always claim "unknown locale" if we don't have ICU (only
1657 * called for non-invariant locales)
1659 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "ArgumentException"));
1662 void ves_icall_System_Globalization_CompareInfo_construct_compareinfo (MonoCompareInfo *comp, MonoString *locale)
1664 /* Nothing to do here */
1667 int ves_icall_System_Globalization_CompareInfo_internal_compare (MonoCompareInfo *this, MonoString *str1, gint32 off1, gint32 len1, MonoString *str2, gint32 off2, gint32 len2, gint32 options)
1669 MONO_ARCH_SAVE_REGS;
1671 /* Do a normal ascii string compare, as we only know the
1672 * invariant locale if we dont have ICU
1674 return(string_invariant_compare (str1, off1, len1, str2, off2, len2,
1678 void ves_icall_System_Globalization_CompareInfo_free_internal_collator (MonoCompareInfo *this)
1680 /* Nothing to do here */
1683 void ves_icall_System_Globalization_CompareInfo_assign_sortkey (MonoCompareInfo *this, MonoSortKey *key, MonoString *source, gint32 options)
1688 MONO_ARCH_SAVE_REGS;
1690 keylen=mono_string_length (source);
1692 arr=mono_array_new (mono_domain_get (), mono_get_byte_class (),
1694 for(i=0; i<keylen; i++) {
1695 mono_array_set (arr, guint8, i, mono_string_chars (source)[i]);
1701 int ves_icall_System_Globalization_CompareInfo_internal_index (MonoCompareInfo *this, MonoString *source, gint32 sindex, gint32 count, MonoString *value, gint32 options, MonoBoolean first)
1703 MONO_ARCH_SAVE_REGS;
1705 return(string_invariant_indexof (source, sindex, count, value, first));
1708 int ves_icall_System_Globalization_CompareInfo_internal_index_char (MonoCompareInfo *this, MonoString *source, gint32 sindex, gint32 count, gunichar2 value, gint32 options, MonoBoolean first)
1710 MONO_ARCH_SAVE_REGS;
1712 return(string_invariant_indexof_char (source, sindex, count, value,
1716 int ves_icall_System_Threading_Thread_current_lcid (void)
1718 MONO_ARCH_SAVE_REGS;
1724 MonoString *ves_icall_System_String_InternalReplace_Str_Comp (MonoString *this, MonoString *old, MonoString *new, MonoCompareInfo *comp)
1726 MONO_ARCH_SAVE_REGS;
1728 /* Do a normal ascii string compare and replace, as we only
1729 * know the invariant locale if we dont have ICU
1731 return(string_invariant_replace (this, old, new));
1734 #endif /* HAVE_ICU */
1736 static gint32 string_invariant_compare_char (gunichar2 c1, gunichar2 c2,
1740 GUnicodeType c1type, c2type;
1742 /* Ordinal can not be mixed with other options, and must return the difference, not only -1, 0, 1 */
1743 if (options & CompareOptions_Ordinal)
1744 return (gint32) c1 - c2;
1746 c1type = g_unichar_type (c1);
1747 c2type = g_unichar_type (c2);
1749 if (options & CompareOptions_IgnoreCase) {
1750 result = (gint32) (c1type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c1) : c1) -
1751 (c2type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c2) : c2);
1754 * No options. Kana, symbol and spacing options don't
1755 * apply to the invariant culture.
1759 * FIXME: here we must use the information from c1type and c2type
1760 * to find out the proper collation, even on the InvariantCulture, the
1761 * sorting is not done by computing the unicode values, but their
1762 * actual sort order.
1764 result = (gint32) c1 - c2;
1767 return ((result < 0) ? -1 : (result > 0) ? 1 : 0);
1770 static gint32 string_invariant_compare (MonoString *str1, gint32 off1,
1771 gint32 len1, MonoString *str2,
1772 gint32 off2, gint32 len2,
1775 /* c translation of C# code from old string.cs.. :) */
1788 ustr1 = mono_string_chars(str1)+off1;
1789 ustr2 = mono_string_chars(str2)+off2;
1793 for (pos = 0; pos != length; pos++) {
1794 if (pos >= len1 || pos >= len2)
1797 charcmp = string_invariant_compare_char(ustr1[pos], ustr2[pos],
1804 /* the lesser wins, so if we have looped until length we just
1805 * need to check the last char
1807 if (pos == length) {
1808 return(string_invariant_compare_char(ustr1[pos - 1],
1809 ustr2[pos - 1], options));
1812 /* Test if one of the strings has been compared to the end */
1819 } else if (pos >= len2) {
1823 /* if not, check our last char only.. (can this happen?) */
1824 return(string_invariant_compare_char(ustr1[pos], ustr2[pos], options));
1827 static MonoString *string_invariant_replace (MonoString *me,
1828 MonoString *oldValue,
1829 MonoString *newValue)
1833 gunichar2 *dest=NULL; /* shut gcc up */
1835 gunichar2 *newstr=NULL; /* shut gcc up here too */
1846 oldstr = mono_string_chars(oldValue);
1847 oldstrlen = mono_string_length(oldValue);
1849 if (NULL != newValue) {
1850 newstr = mono_string_chars(newValue);
1851 newstrlen = mono_string_length(newValue);
1855 src = mono_string_chars(me);
1856 srclen = mono_string_length(me);
1858 if (oldstrlen != newstrlen) {
1860 while (i <= srclen - oldstrlen) {
1861 if (0 == memcmp(src + i, oldstr, oldstrlen * sizeof(gunichar2))) {
1870 newsize = srclen + ((newstrlen - oldstrlen) * occurr);
1876 while (i < srclen) {
1877 if (0 == memcmp(src + i, oldstr, oldstrlen * sizeof(gunichar2))) {
1879 ret = mono_string_new_size( mono_domain_get (), newsize);
1880 dest = mono_string_chars(ret);
1881 memcpy (dest, src, i * sizeof(gunichar2));
1883 if (newstrlen > 0) {
1884 memcpy(dest + destpos, newstr, newstrlen * sizeof(gunichar2));
1885 destpos += newstrlen;
1889 } else if (ret != NULL) {
1890 dest[destpos] = src[i];
1902 static gint32 string_invariant_indexof (MonoString *source, gint32 sindex,
1903 gint32 count, MonoString *value,
1911 lencmpstr = mono_string_length(value);
1913 src = mono_string_chars(source);
1914 cmpstr = mono_string_chars(value);
1918 for(pos=sindex;pos <= sindex+count;pos++) {
1919 for(i=0;src[pos+i]==cmpstr[i];) {
1920 if(++i==lencmpstr) {
1928 for(pos=sindex-lencmpstr+1;pos>sindex-count;pos--) {
1929 if(memcmp (src+pos, cmpstr,
1930 lencmpstr*sizeof(gunichar2))==0) {
1939 static gint32 string_invariant_indexof_char (MonoString *source, gint32 sindex,
1940 gint32 count, gunichar2 value,
1946 src = mono_string_chars(source);
1948 for (pos = sindex; pos != count + sindex; pos++) {
1949 if (src [pos] == value) {
1956 for (pos = sindex; pos > sindex - count; pos--) {
1957 if (src [pos] == value)