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 MonoString *string_invariant_tolower (MonoString *this);
47 static MonoString *string_invariant_toupper (MonoString *this);
48 static const CultureInfoEntry* culture_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 culture_name_locator (const void *a, const void *b)
62 const CultureInfoNameEntry *aa = a;
63 const CultureInfoNameEntry *bb = b;
66 ret = strcmp (aa->name, bb->name);
72 create_group_sizes_array (const gint *gs, gint ml)
77 for (i = 0; i < ml; i++) {
83 ret = mono_array_new (mono_domain_get (),
84 mono_defaults.int32_class, len);
86 for(i = 0; i < len; i++)
87 mono_array_set (ret, gint32, i, gs [i]);
93 create_names_array (const gchar **names, int ml)
102 domain = mono_domain_get ();
104 for (i = 0; i < ml; i++) {
105 if (names [i] == NULL)
110 ret = mono_array_new (mono_domain_get (), mono_defaults.string_class, len);
112 for(i = 0; i < len; i++)
113 mono_array_set (ret, MonoString *, i, mono_string_new (domain, names [i]));
119 ves_icall_System_Globalization_CultureInfo_construct_datetime_format (MonoCultureInfo *this)
122 MonoDateTimeFormatInfo *datetime;
123 const DateTimeFormatEntry *dfe;
127 g_assert (this->datetime_index >= 0);
129 datetime = this->datetime_format;
130 dfe = &datetime_format_entries [this->datetime_index];
132 domain = mono_domain_get ();
134 datetime->AbbreviatedDayNames = create_names_array (dfe->abbreviated_day_names,
136 datetime->AbbreviatedMonthNames = create_names_array (dfe->abbreviated_month_names,
138 datetime->AMDesignator = mono_string_new (domain, dfe->am_designator);
139 datetime->CalendarWeekRule = dfe->calendar_week_rule;
140 datetime->DateSeparator = mono_string_new (domain, dfe->date_separator);
141 datetime->DayNames = create_names_array (dfe->day_names, NUM_DAYS);
142 datetime->FirstDayOfWeek = dfe->first_day_of_week;
143 datetime->FullDateTimePattern = mono_string_new (domain, dfe->full_date_time_pattern);
144 datetime->LongDatePattern = mono_string_new (domain, dfe->long_date_pattern);
145 datetime->LongTimePattern = mono_string_new (domain, dfe->long_time_pattern);
146 datetime->MonthDayPattern = mono_string_new (domain, dfe->month_day_pattern);
147 datetime->MonthNames = create_names_array (dfe->month_names, NUM_MONTHS);
148 datetime->PMDesignator = mono_string_new (domain, dfe->pm_designator);
149 datetime->ShortDatePattern = mono_string_new (domain, dfe->short_date_pattern);
150 datetime->ShortTimePattern = mono_string_new (domain, dfe->short_time_pattern);
151 datetime->TimeSeparator = mono_string_new (domain, dfe->time_separator);
152 datetime->YearMonthPattern = mono_string_new (domain, dfe->year_month_pattern);
153 datetime->ShortDatePatterns = create_names_array (dfe->short_date_patterns,
154 NUM_SHORT_DATE_PATTERNS);
155 datetime->LongDatePatterns = create_names_array (dfe->long_date_patterns,
156 NUM_LONG_DATE_PATTERNS);
157 datetime->ShortTimePatterns = create_names_array (dfe->short_time_patterns,
158 NUM_SHORT_TIME_PATTERNS);
159 datetime->LongTimePatterns = create_names_array (dfe->long_time_patterns,
160 NUM_LONG_TIME_PATTERNS);
165 ves_icall_System_Globalization_CultureInfo_construct_number_format (MonoCultureInfo *this)
168 MonoNumberFormatInfo *number;
169 const NumberFormatEntry *nfe;
173 g_assert (this->number_format != 0);
175 number = this->number_format;
176 nfe = &number_format_entries [this->number_index];
178 domain = mono_domain_get ();
180 number->currencyDecimalDigits = nfe->currency_decimal_digits;
181 number->currencyDecimalSeparator = mono_string_new (domain,
182 nfe->currency_decimal_separator);
183 number->currencyGroupSeparator = mono_string_new (domain,
184 nfe->currency_group_separator);
185 number->currencyGroupSizes = create_group_sizes_array (nfe->currency_group_sizes,
187 number->currencyNegativePattern = nfe->currency_negative_pattern;
188 number->currencyPositivePattern = nfe->currency_positive_pattern;
189 number->currencySymbol = mono_string_new (domain, nfe->currency_symbol);
190 number->naNSymbol = mono_string_new (domain, nfe->nan_symbol);
191 number->negativeInfinitySymbol = mono_string_new (domain,
192 nfe->negative_infinity_symbol);
193 number->negativeSign = mono_string_new (domain, nfe->negative_sign);
194 number->numberDecimalDigits = nfe->number_decimal_digits;
195 number->numberDecimalSeparator = mono_string_new (domain,
196 nfe->number_decimal_separator);
197 number->numberGroupSeparator = mono_string_new (domain, nfe->number_group_separator);
198 number->numberGroupSizes = create_group_sizes_array (nfe->number_group_sizes,
200 number->numberNegativePattern = nfe->number_negative_pattern;
201 number->percentDecimalDigits = nfe->percent_decimal_digits;
202 number->percentDecimalSeparator = mono_string_new (domain,
203 nfe->percent_decimal_separator);
204 number->percentGroupSeparator = mono_string_new (domain,
205 nfe->percent_group_separator);
206 number->percentGroupSizes = create_group_sizes_array (nfe->percent_group_sizes,
208 number->percentNegativePattern = nfe->percent_negative_pattern;
209 number->percentPositivePattern = nfe->percent_positive_pattern;
210 number->percentSymbol = mono_string_new (domain, nfe->percent_symbol);
211 number->perMilleSymbol = mono_string_new (domain, nfe->per_mille_symbol);
212 number->positiveInfinitySymbol = mono_string_new (domain,
213 nfe->positive_infinity_symbol);
214 number->positiveSign = mono_string_new (domain, nfe->positive_sign);
218 construct_culture (MonoCultureInfo *this, const CultureInfoEntry *ci)
220 MonoDomain *domain = mono_domain_get ();
222 this->lcid = ci->lcid;
223 this->name = mono_string_new (domain, ci->name);
224 this->icu_name = mono_string_new (domain, ci->icu_name);
225 this->displayname = mono_string_new (domain, ci->displayname);
226 this->englishname = mono_string_new (domain, ci->englishname);
227 this->nativename = mono_string_new (domain, ci->nativename);
228 this->win3lang = mono_string_new (domain, ci->win3lang);
229 this->iso3lang = mono_string_new (domain, ci->iso3lang);
230 this->iso2lang = mono_string_new (domain, ci->iso2lang);
231 this->parent_lcid = ci->parent_lcid;
232 this->specific_lcid = ci->specific_lcid;
233 this->datetime_index = ci->datetime_format_index;
234 this->number_index = ci->number_format_index;
235 this->calendar_data = ci->calendar_data;
241 construct_culture_from_specific_name (MonoCultureInfo *ci, gchar *name)
243 const CultureInfoEntry *entry;
244 CultureInfoNameEntry key;
245 const CultureInfoNameEntry *ne;
250 ne = bsearch (&key, culture_name_entries, NUM_CULTURE_ENTRIES,
251 sizeof (CultureInfoNameEntry), culture_name_locator);
256 entry = &culture_entries [ne->culture_entry_index];
258 /* try avoiding another lookup, often the culture is its own specific culture */
259 if (entry->lcid != entry->specific_lcid)
260 entry = culture_info_entry_from_lcid (entry->specific_lcid);
262 return construct_culture (ci, entry);
265 static const CultureInfoEntry*
266 culture_info_entry_from_lcid (int lcid)
268 const CultureInfoEntry *ci;
269 CultureInfoEntry key;
272 ci = bsearch (&key, culture_entries, NUM_CULTURE_ENTRIES, sizeof (CultureInfoEntry), culture_lcid_locator);
278 * The following two methods are modified from the ICU source code. (http://oss.software.ibm.com/icu)
279 * Copyright (c) 1995-2003 International Business Machines Corporation and others
280 * All rights reserved.
283 get_posix_locale (void)
285 const gchar* posix_locale = NULL;
287 posix_locale = g_getenv("LC_ALL");
288 if (posix_locale == 0) {
289 posix_locale = g_getenv("LANG");
290 if (posix_locale == 0) {
291 posix_locale = setlocale(LC_ALL, NULL);
295 if (posix_locale == NULL)
298 if ((strcmp ("C", posix_locale) == 0) || (strchr (posix_locale, ' ') != NULL)
299 || (strchr (posix_locale, '/') != NULL)) {
301 * HPUX returns 'C C C C C C C'
302 * Solaris can return /en_US/C/C/C/C/C on the second try.
303 * Maybe we got some garbage.
308 return g_strdup (posix_locale);
312 get_current_locale_name (void)
315 gchar *corrected = NULL;
319 #ifdef PLATFORM_WIN32
320 locale = g_win32_getlocale ();
322 locale = get_posix_locale ();
328 if ((p = strchr (locale, '.')) != NULL) {
329 /* assume new locale can't be larger than old one? */
330 corrected = malloc (strlen (locale));
331 strncpy (corrected, locale, p - locale);
332 corrected [p - locale] = 0;
334 /* do not copy after the @ */
335 if ((p = strchr (corrected, '@')) != NULL)
336 corrected [p - corrected] = 0;
339 /* Note that we scan the *uncorrected* ID. */
340 if ((p = strrchr (locale, '@')) != NULL) {
343 * In Mono we dont handle the '@' modifier because we do
344 * not have any cultures that use it. We just trim it
345 * off of the end of the name.
348 if (corrected == NULL) {
349 corrected = malloc (strlen (locale));
350 strncpy (corrected, locale, p - locale);
351 corrected [p - locale] = 0;
355 if (corrected == NULL)
360 if ((c = strchr (corrected, '_')) != NULL)
363 g_strdown (corrected);
369 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_current_locale (MonoCultureInfo *ci)
376 locale = get_current_locale_name ();
380 ret = construct_culture_from_specific_name (ci, locale);
387 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_lcid (MonoCultureInfo *this,
390 const CultureInfoEntry *ci;
394 ci = culture_info_entry_from_lcid (lcid);
398 return construct_culture (this, ci);
402 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_name (MonoCultureInfo *this,
405 CultureInfoNameEntry key;
406 const CultureInfoNameEntry *ne;
410 key.name = mono_string_to_utf8 (name);
411 ne = bsearch (&key, culture_name_entries, NUM_CULTURE_ENTRIES,
412 sizeof (CultureInfoNameEntry), culture_name_locator);
414 g_free ((gpointer) key.name);
417 g_print ("ne (%s) is null\n", key.name);
421 return construct_culture (this, &culture_entries [ne->culture_entry_index]);
425 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_specific_name (MonoCultureInfo *ci,
433 locale = mono_string_to_utf8 (name);
434 ret = construct_culture_from_specific_name (ci, locale);
441 ves_icall_System_Globalization_CultureInfo_internal_get_cultures (MonoBoolean neutral,
442 MonoBoolean specific, MonoBoolean installed)
446 MonoCultureInfo *culture;
448 const CultureInfoEntry *ci;
454 domain = mono_domain_get ();
457 for (i = 0; i < NUM_CULTURE_ENTRIES; i++) {
458 ci = &culture_entries [i];
459 is_neutral = ((ci->lcid & 0xff00) == 0 || ci->specific_lcid == 0);
460 if ((neutral && is_neutral) || (specific && !is_neutral))
464 class = mono_class_from_name (mono_defaults.corlib,
465 "System.Globalization", "CultureInfo");
466 ret = mono_array_new (domain, class, len);
472 for (i = 0; i < NUM_CULTURE_ENTRIES; i++) {
473 ci = &culture_entries [i];
474 is_neutral = ((ci->lcid & 0xff00) == 0 || ci->specific_lcid == 0);
475 if ((neutral && is_neutral) || (specific && !is_neutral)) {
476 culture = (MonoCultureInfo *) mono_object_new (domain, class);
477 mono_runtime_object_init ((MonoObject *) culture);
478 construct_culture (culture, ci);
479 mono_array_set (ret, MonoCultureInfo *, len++, culture);
487 * Set is_neutral and return TRUE if the culture is found. If it is not found return FALSE.
490 ves_icall_System_Globalization_CultureInfo_internal_is_lcid_neutral (gint lcid, MonoBoolean *is_neutral)
492 const CultureInfoEntry *entry;
496 entry = culture_info_entry_from_lcid (lcid);
501 *is_neutral = (entry->specific_lcid == 0);
508 #include <unicode/utypes.h>
509 #include <unicode/ustring.h>
510 #include <unicode/ures.h>
511 #include <unicode/ucol.h>
512 #include <unicode/usearch.h>
514 static MonoString *monostring_from_resource_index (const UResourceBundle *bundle, int32_t idx)
521 res_str=(gunichar2 *)ures_getStringByIndex (bundle, idx, &res_strlen,
527 return(mono_string_from_utf16 (res_str));
530 static UResourceBundle *open_subbundle (const UResourceBundle *bundle,
531 const char *name, int32_t req_count)
533 UResourceBundle *subbundle;
538 subbundle=ures_getByKey (bundle, name, NULL, &ec);
540 /* Couldn't find the subbundle */
544 count=ures_countArrayItems (bundle, name, &ec);
546 /* Couldn't count the subbundle */
547 ures_close (subbundle);
551 if(count!=req_count) {
553 ures_close (subbundle);
560 static MonoArray *build_array (const UResourceBundle *bundle,
561 const char *resname, int32_t req_count)
564 UResourceBundle *subbundle;
567 subbundle=open_subbundle (bundle, resname, req_count);
568 if(subbundle!=NULL) {
569 arr=mono_array_new(mono_domain_get (),
570 mono_defaults.string_class, req_count);
572 for(i=0; i<req_count; i++) {
573 mono_array_set(arr, MonoString *, i, monostring_from_resource_index (subbundle, i));
576 ures_close (subbundle);
582 static MonoDateTimeFormatInfo *create_DateTimeFormat (const char *locale)
584 MonoDateTimeFormatInfo *new_dtf;
586 UResourceBundle *bundle, *subbundle;
589 class=mono_class_from_name (mono_defaults.corlib,
590 "System.Globalization",
591 "DateTimeFormatInfo");
592 new_dtf=(MonoDateTimeFormatInfo *)mono_object_new (mono_domain_get (),
594 mono_runtime_object_init ((MonoObject *)new_dtf);
598 bundle=ures_open (NULL, locale, &ec);
604 subbundle=open_subbundle (bundle, "AmPmMarkers", 2);
605 if(subbundle!=NULL) {
606 new_dtf->AMDesignator=monostring_from_resource_index (subbundle, 0);
607 new_dtf->PMDesignator=monostring_from_resource_index (subbundle, 1);
609 ures_close (subbundle);
612 /* Date/Time patterns. Don't set FullDateTimePattern. As it
613 * seems to always default to LongDatePattern + " " +
614 * LongTimePattern, let the property accessor deal with it.
616 subbundle=open_subbundle (bundle, "DateTimePatterns", 9);
617 if(subbundle!=NULL) {
618 new_dtf->ShortDatePattern=monostring_from_resource_index (subbundle, 7);
619 new_dtf->LongDatePattern=monostring_from_resource_index (subbundle, 5);
620 new_dtf->ShortTimePattern=monostring_from_resource_index (subbundle, 3);
621 new_dtf->LongTimePattern=monostring_from_resource_index (subbundle, 2);
623 /* RFC1123Pattern, SortableDateTimePattern and
624 * UniversalSortableDateTimePattern all seem to be
625 * constant, and all the same as the invariant default
629 ures_close (subbundle);
633 /* Not sure what to do with these yet, so leave them set to
634 * the invariant default
636 set_field_string (new_dtf, "_DateSeparator", str);
637 set_field_string (new_dtf, "_TimeSeparator", str);
638 set_field_string (new_dtf, "_MonthDayPattern", str);
639 set_field_string (new_dtf, "_YearMonthPattern", str);
642 /* Day names. Luckily both ICU and .net start Sunday at index 0 */
643 new_dtf->DayNames=build_array (bundle, "DayNames", 7);
645 /* Abbreviated day names */
646 new_dtf->AbbreviatedDayNames=build_array (bundle, "DayAbbreviations",
650 new_dtf->MonthNames=build_array (bundle, "MonthNames", 12);
652 /* Abbreviated month names */
653 new_dtf->AbbreviatedMonthNames=build_array (bundle,
654 "MonthAbbreviations", 12);
656 /* TODO: DayOfWeek _FirstDayOfWeek, Calendar _Calendar, CalendarWeekRule _CalendarWeekRule */
663 static MonoNumberFormatInfo *create_NumberFormat (const char *locale)
665 MonoNumberFormatInfo *new_nf;
667 MonoMethodDesc* methodDesc;
669 UResourceBundle *bundle, *subbundle, *table_entries;
672 static char country [7]; //FIXME
673 const UChar *res_str;
676 class=mono_class_from_name (mono_defaults.corlib,
677 "System.Globalization",
679 new_nf=(MonoNumberFormatInfo *)mono_object_new (mono_domain_get (),
681 mono_runtime_object_init ((MonoObject *)new_nf);
685 bundle=ures_open (NULL, locale, &ec);
690 /* Number Elements */
692 subbundle=ures_getByKey (bundle, "NumberElements", NULL, &ec);
694 /* Couldn't find the subbundle */
698 count=ures_countArrayItems (bundle, "NumberElements", &ec);
700 /* Couldn't count the subbundle */
701 ures_close (subbundle);
705 if(subbundle!=NULL) {
706 new_nf->numberDecimalSeparator=monostring_from_resource_index (subbundle, 0);
707 new_nf->numberGroupSeparator=monostring_from_resource_index (subbundle, 1);
708 new_nf->percentDecimalSeparator=monostring_from_resource_index (subbundle, 0);
709 new_nf->percentGroupSeparator=monostring_from_resource_index (subbundle, 1);
710 new_nf->percentSymbol=monostring_from_resource_index (subbundle, 3);
711 new_nf->zeroPattern=monostring_from_resource_index (subbundle, 4);
712 new_nf->digitPattern=monostring_from_resource_index (subbundle, 5);
713 new_nf->negativeSign=monostring_from_resource_index (subbundle, 6);
714 new_nf->perMilleSymbol=monostring_from_resource_index (subbundle, 8);
715 new_nf->positiveInfinitySymbol=monostring_from_resource_index (subbundle, 9);
716 /* we dont have this in CLDR, so copy it from positiveInfinitySymbol */
717 new_nf->negativeInfinitySymbol=monostring_from_resource_index (subbundle, 9);
718 new_nf->naNSymbol=monostring_from_resource_index (subbundle, 10);
719 new_nf->currencyDecimalSeparator=monostring_from_resource_index (subbundle, 0);
720 new_nf->currencyGroupSeparator=monostring_from_resource_index (subbundle, 1);
722 ures_close (subbundle);
725 /* get country name */
727 uloc_getCountry (locale, country, sizeof (country), &ec);
728 if (U_SUCCESS (ec)) {
730 /* find country name in root.CurrencyMap */
731 subbundle = ures_getByKey (bundle, "CurrencyMap", NULL, &ec);
732 if (U_SUCCESS (ec)) {
734 /* get currency id for specified country */
735 table_entries = ures_getByKey (subbundle, country, NULL, &ec);
736 if (U_SUCCESS (ec)) {
737 ures_close (subbundle);
740 res_str = ures_getStringByIndex (
741 table_entries, 0, &res_strlen, &ec);
743 /* now we have currency id string */
744 ures_close (table_entries);
746 u_UCharsToChars (res_str, country,
750 /* find currency string in locale data */
751 subbundle = ures_getByKey (
752 bundle, "Currencies",
755 if (U_SUCCESS (ec)) {
757 /* find currency symbol under specified currency id */
758 table_entries = ures_getByKey (subbundle, country, NULL, &ec);
759 if (U_SUCCESS (ec)) {
760 /* get the first string only,
761 * the second is international currency symbol (not used)*/
762 new_nf->currencySymbol=monostring_from_resource_index (table_entries, 0);
763 ures_close (table_entries);
765 ures_close (subbundle);
773 subbundle=open_subbundle (bundle, "NumberPatterns", 4);
774 if(subbundle!=NULL) {
775 new_nf->decimalFormats=monostring_from_resource_index (subbundle, 0);
776 new_nf->currencyFormats=monostring_from_resource_index (subbundle, 1);
777 new_nf->percentFormats=monostring_from_resource_index (subbundle, 2);
778 ures_close (subbundle);
780 /* calls InitPatterns to parse the patterns
782 methodDesc = mono_method_desc_new (
783 "System.Globalization.NumberFormatInfo:InitPatterns()",
785 method = mono_method_desc_search_in_class (methodDesc, class);
787 mono_runtime_invoke (method, new_nf, NULL, NULL);
789 g_warning (G_GNUC_PRETTY_FUNCTION ": Runtime mismatch with class lib! (Looking for System.Globalization.NumberFormatInfo:InitPatterns())");
798 static char *mono_string_to_icu_locale (MonoString *locale)
801 char *passed_locale, *icu_locale=NULL;
802 int32_t loc_len, ret;
804 passed_locale=mono_string_to_utf8 (locale);
807 ret=uloc_getName (passed_locale, NULL, 0, &ec);
808 if(ec==U_BUFFER_OVERFLOW_ERROR) {
811 icu_locale=(char *)g_malloc0 (sizeof(char)*loc_len);
812 ret=uloc_getName (passed_locale, icu_locale, loc_len, &ec);
814 g_free (passed_locale);
819 void ves_icall_System_Globalization_CultureInfo_construct_internal_locale (MonoCultureInfo *this, MonoString *locale)
825 int32_t str_len, ret;
829 icu_locale=mono_string_to_icu_locale (locale);
830 if(icu_locale==NULL) {
831 /* Something went wrong */
832 mono_raise_exception((MonoException *)mono_exception_from_name(mono_defaults.corlib, "System", "SystemException"));
836 /* Fill in the static fields */
838 /* TODO: Calendar, InstalledUICulture, OptionalCalendars,
842 str_len=256; /* Should be big enough for anything */
843 str=(char *)g_malloc0 (sizeof(char)*str_len);
844 ustr=(UChar *)g_malloc0 (sizeof(UChar)*str_len);
848 ret=uloc_getDisplayName (icu_locale, "en", ustr, str_len, &ec);
849 if(U_SUCCESS (ec) && ret<str_len) {
850 this->englishname=mono_string_from_utf16 ((gunichar2 *)ustr);
853 ret=uloc_getDisplayName (icu_locale, uloc_getDefault (), ustr, str_len,
855 if(U_SUCCESS (ec) && ret<str_len) {
856 this->displayname=mono_string_from_utf16 ((gunichar2 *)ustr);
859 ret=uloc_getDisplayName (icu_locale, icu_locale, ustr, str_len, &ec);
860 if(U_SUCCESS (ec) && ret<str_len) {
861 this->nativename=mono_string_from_utf16 ((gunichar2 *)ustr);
864 this->iso3lang=mono_string_new_wrapper (uloc_getISO3Language (icu_locale));
866 ret=uloc_getLanguage (icu_locale, str, str_len, &ec);
867 if(U_SUCCESS (ec) && ret<str_len) {
868 this->iso2lang=mono_string_new_wrapper (str);
871 this->datetime_format=create_DateTimeFormat (icu_locale);
872 this->number_format=create_NumberFormat (icu_locale);
879 void ves_icall_System_Globalization_CompareInfo_construct_compareinfo (MonoCompareInfo *comp, MonoString *locale)
888 g_message (G_GNUC_PRETTY_FUNCTION ": Constructing collator for locale [%s]", mono_string_to_utf8 (locale));
891 icu_locale=mono_string_to_icu_locale (locale);
892 if(icu_locale==NULL) {
893 /* Something went wrong */
894 mono_raise_exception((MonoException *)mono_exception_from_name(mono_defaults.corlib, "System", "SystemException"));
899 coll=ucol_open (icu_locale, &ec);
901 comp->ICU_collator=coll;
903 comp->ICU_collator=NULL;
909 /* Set up the collator to reflect the options required. Some of these
910 * options clash, as they adjust the collator strength level. Try to
911 * make later checks reduce the strength level, and attempt to take
912 * previous options into account.
914 * Don't bother to check the error returns when setting the
915 * attributes, as a failure here is hardly grounds to error out.
917 static void set_collator_options (UCollator *coll, gint32 options)
919 UErrorCode ec=U_ZERO_ERROR;
921 /* Set up the defaults */
922 ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE,
924 ucol_setAttribute (coll, UCOL_CASE_LEVEL, UCOL_OFF, &ec);
926 /* Do this first so other options will override the quaternary
927 * level strength setting if necessary
929 if(!(options & CompareOptions_IgnoreKanaType)) {
930 ucol_setAttribute (coll, UCOL_HIRAGANA_QUATERNARY_MODE,
932 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_QUATERNARY, &ec);
935 /* Word sort, the default */
936 if(!(options & CompareOptions_StringSort)) {
937 ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING,
939 /* Tertiary strength is the default, but it might have
940 * been set to quaternary above. (We don't want that
941 * here, because that will order all the punctuation
942 * first instead of just ignoring it.)
944 * Unfortunately, tertiary strength with
945 * ALTERNATE_HANDLING==SHIFTED means that '/' and '@'
946 * compare to equal, which has the nasty side effect
947 * of killing mcs :-( (We can't specify a
948 * culture-insensitive compare, because
949 * String.StartsWith doesn't have that option.)
951 * ALTERNATE_HANDLING==SHIFTED is needed to accomplish
952 * the word-sorting-ignoring-punctuation feature. So
953 * we have to live with the slightly mis-ordered
954 * punctuation and a working mcs...
956 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_QUATERNARY, &ec);
959 if(options & CompareOptions_IgnoreCase) {
960 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_SECONDARY, &ec);
961 ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE, &ec);
964 if(options & CompareOptions_IgnoreWidth) {
965 /* Kana width is a tertiary strength difference. This
966 * will totally break the !IgnoreKanaType option
968 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_SECONDARY, &ec);
971 if(options & CompareOptions_IgnoreNonSpace) {
972 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_PRIMARY, &ec);
973 /* We can still compare case even when just checking
976 if(!(options & CompareOptions_IgnoreCase) ||
977 !(options & CompareOptions_IgnoreWidth)) {
978 /* Not sure if CASE_LEVEL handles kana width
980 ucol_setAttribute (coll, UCOL_CASE_LEVEL, UCOL_ON,
985 if(options & CompareOptions_IgnoreSymbols) {
986 /* Don't know what to do here */
989 if(options == CompareOptions_Ordinal) {
990 /* This one is handled elsewhere */
994 gint32 ves_icall_System_Globalization_CompareInfo_internal_compare (MonoCompareInfo *this, MonoString *str1, gint32 off1, gint32 len1, MonoString *str2, gint32 off2, gint32 len2, gint32 options)
997 UCollationResult result;
1002 g_message (G_GNUC_PRETTY_FUNCTION ": Comparing [%s] and [%s]", mono_string_to_utf8 (str1), mono_string_to_utf8 (str2));
1005 coll=this->ICU_collator;
1008 g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", this->lcid);
1011 if(coll==NULL || this->lcid==0x007F ||
1012 options & CompareOptions_Ordinal) {
1014 g_message (G_GNUC_PRETTY_FUNCTION ": No collator or invariant, using shortcut");
1017 return(string_invariant_compare (str1, off1, len1, str2, off2,
1021 if (!mono_monitor_try_enter ((MonoObject *)this, INFINITE))
1024 set_collator_options (coll, options);
1026 result=ucol_strcoll (coll, mono_string_chars (str1)+off1, len1,
1027 mono_string_chars (str2)+off2, len2);
1029 mono_monitor_exit ((MonoObject *)this);
1032 g_message (G_GNUC_PRETTY_FUNCTION ": Comparison of [%s] and [%s] returning %d", mono_string_to_utf8 (str1), mono_string_to_utf8 (str2), result);
1038 void ves_icall_System_Globalization_CompareInfo_free_internal_collator (MonoCompareInfo *this)
1042 MONO_ARCH_SAVE_REGS;
1044 coll=this->ICU_collator;
1050 void ves_icall_System_Globalization_CompareInfo_assign_sortkey (MonoCompareInfo *this, MonoSortKey *key, MonoString *source, gint32 options)
1057 MONO_ARCH_SAVE_REGS;
1059 coll=this->ICU_collator;
1061 mono_raise_exception((MonoException *)mono_exception_from_name(mono_defaults.corlib, "System", "SystemException"));
1065 if (!mono_monitor_try_enter ((MonoObject *)this, INFINITE))
1068 set_collator_options (coll, options);
1070 keylen=ucol_getSortKey (coll, mono_string_chars (source), -1, NULL, 0);
1071 keybuf=g_malloc (sizeof(char)* keylen);
1072 ucol_getSortKey (coll, mono_string_chars (source), -1, keybuf, keylen);
1074 mono_monitor_exit ((MonoObject *)this);
1076 arr=mono_array_new (mono_domain_get (), mono_defaults.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_try_enter ((MonoObject *)this, INFINITE))
1131 set_collator_options (coll, options);
1133 /* Need to set the collator to a fairly weak level, so that it
1134 * treats characters that can be written differently as
1135 * identical (eg "
\8eß" and "ss", "æ" and "ae" or "ä" etc.) Note
1136 * that this means that the search string and the original
1137 * text might have differing lengths.
1139 // ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_PRIMARY, &ec);
1141 /* Still notice case differences though (normally a tertiary
1144 ucol_setAttribute (coll, UCOL_CASE_LEVEL, UCOL_ON, &ec);
1146 /* Don't ignore some codepoints */
1147 ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE,
1150 search=usearch_openFromCollator (mono_string_chars (value), -1, usrcstr, -1, coll, NULL,
1152 if(U_SUCCESS (ec)) {
1154 pos=usearch_first (search, &ec);
1156 pos=usearch_last (search, &ec);
1159 if(pos!=USEARCH_DONE) {
1161 g_message (G_GNUC_PRETTY_FUNCTION
1162 ": Got match at %d (sindex %d) len %d", pos,
1163 sindex, usearch_getMatchedLength (search));
1169 pos+=(sindex-count+1);
1174 g_message (G_GNUC_PRETTY_FUNCTION ": usearch_open error: %s",
1178 usearch_close (search);
1180 mono_monitor_exit ((MonoObject *)this);
1187 int ves_icall_System_Globalization_CompareInfo_internal_index_char (MonoCompareInfo *this, MonoString *source, gint32 sindex, gint32 count, gunichar2 value, gint32 options, MonoBoolean first)
1190 UChar *usrcstr, uvalstr[2]={0, 0};
1192 UStringSearch *search;
1195 MONO_ARCH_SAVE_REGS;
1198 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);
1201 coll=this->ICU_collator;
1204 g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", this->lcid);
1207 if(coll==NULL || this->lcid==0x007F ||
1208 options & CompareOptions_Ordinal) {
1210 g_message (G_GNUC_PRETTY_FUNCTION ": No collator or invariant, using shortcut");
1213 return(string_invariant_indexof_char (source, sindex, count,
1217 usrcstr=g_malloc0 (sizeof(UChar)*(count+1));
1219 memcpy (usrcstr, mono_string_chars (source)+sindex,
1220 sizeof(UChar)*count);
1222 memcpy (usrcstr, mono_string_chars (source)+sindex-count+1,
1223 sizeof(UChar)*count);
1227 if (!mono_monitor_try_enter ((MonoObject *)this, INFINITE))
1232 /* Need to set the collator to a fairly weak level, so that it
1233 * treats characters that can be written differently as
1234 * identical (eg "
\8eß" and "ss", "æ" and "ae" or "ä" etc.) Note
1235 * that this means that the search string and the original
1236 * text might have differing lengths.
1238 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_PRIMARY, &ec);
1240 /* Still notice case differences though (normally a tertiary
1243 ucol_setAttribute (coll, UCOL_CASE_LEVEL, UCOL_ON, &ec);
1245 /* Don't ignore some codepoints */
1246 ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE,
1249 search=usearch_openFromCollator (uvalstr, -1, usrcstr, -1, coll, NULL,
1251 if(U_SUCCESS (ec)) {
1253 pos=usearch_first (search, &ec);
1255 pos=usearch_last (search, &ec);
1258 if(pos!=USEARCH_DONE) {
1260 g_message (G_GNUC_PRETTY_FUNCTION
1261 ": Got match at %d (sindex %d) len %d", pos,
1262 sindex, usearch_getMatchedLength (search));
1268 pos+=(sindex-count+1);
1273 g_message (G_GNUC_PRETTY_FUNCTION ": usearch_open error: %s",
1277 usearch_close (search);
1279 mono_monitor_exit ((MonoObject *)this);
1286 int ves_icall_System_Threading_Thread_current_lcid (void)
1288 MONO_ARCH_SAVE_REGS;
1290 return(uloc_getLCID (uloc_getDefault ()));
1293 MonoString *ves_icall_System_String_InternalReplace_Str_Comp (MonoString *this, MonoString *old, MonoString *new, MonoCompareInfo *comp)
1295 MonoString *ret=NULL;
1298 UStringSearch *search;
1300 MONO_ARCH_SAVE_REGS;
1303 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));
1306 coll=comp->ICU_collator;
1309 g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", comp->lcid);
1312 if(coll==NULL || comp->lcid==0x007F) {
1314 g_message (G_GNUC_PRETTY_FUNCTION ": No collator or invariant, using shortcut");
1317 return(string_invariant_replace (this, old, new));
1320 if (!mono_monitor_try_enter ((MonoObject *)comp, INFINITE))
1325 /* Need to set the collator to a fairly weak level, so that it
1326 * treats characters that can be written differently as
1327 * identical (eg "
\8eß" and "ss", "æ" and "ae" or "ä" etc.) Note
1328 * that this means that the search string and the original
1329 * text might have differing lengths.
1331 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_PRIMARY, &ec);
1333 /* Still notice case differences though (normally a tertiary
1336 ucol_setAttribute (coll, UCOL_CASE_LEVEL, UCOL_ON, &ec);
1338 /* Don't ignore some codepoints */
1339 ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE,
1342 search=usearch_openFromCollator (mono_string_chars (old), -1,
1343 mono_string_chars (this), -1, coll,
1345 if(U_SUCCESS (ec)) {
1346 int pos, oldpos, len_delta=0;
1347 int32_t newstr_len=mono_string_length (new), match_len;
1348 UChar *uret, *match;
1350 for(pos=usearch_first (search, &ec);
1352 pos=usearch_next (search, &ec)) {
1353 /* ICU usearch currently ignores most of the collator
1356 * Check the returned match to see if it really
1357 * does match properly...
1359 match_len = usearch_getMatchedLength (search);
1360 match=(UChar *)g_malloc0 (sizeof(UChar) * (match_len + 1));
1361 usearch_getMatchedText (search, match, match_len, &ec);
1363 if (ucol_strcoll (coll, match, -1, mono_string_chars (old), -1) == UCOL_EQUAL) {
1364 /* OK, we really did get a match */
1366 g_message (G_GNUC_PRETTY_FUNCTION
1367 ": Got match at %d len %d", pos,
1371 len_delta += (newstr_len - match_len);
1375 g_message (G_GNUC_PRETTY_FUNCTION
1376 ": Got false match at %d len %d",
1383 g_message (G_GNUC_PRETTY_FUNCTION
1384 ": New string length is %d (delta %d)",
1385 mono_string_length (this)+len_delta, len_delta);
1388 uret=(UChar *)g_malloc0 (sizeof(UChar) * (mono_string_length (this)+len_delta+2));
1390 for(oldpos=0, pos=usearch_first (search, &ec);
1392 pos=usearch_next (search, &ec)) {
1393 match_len = usearch_getMatchedLength (search);
1394 match=(UChar *)g_malloc0 (sizeof(UChar) * (match_len + 1));
1395 usearch_getMatchedText (search, match, match_len, &ec);
1397 /* Add the unmatched text */
1398 u_strncat (uret, mono_string_chars (this)+oldpos,
1400 if (ucol_strcoll (coll, match, -1, mono_string_chars (old), -1) == UCOL_EQUAL) {
1401 /* Then the replacement */
1402 u_strcat (uret, mono_string_chars (new));
1404 /* Then the original, because this is a
1407 u_strncat (uret, mono_string_chars (this)+pos,
1410 oldpos=pos+match_len;
1414 /* Finish off with the trailing unmatched text */
1415 u_strcat (uret, mono_string_chars (this)+oldpos);
1417 ret=mono_string_from_utf16 ((gunichar2 *)uret);
1419 g_message (G_GNUC_PRETTY_FUNCTION ": usearch_open error: %s",
1423 usearch_close (search);
1425 mono_monitor_exit ((MonoObject *)comp);
1428 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));
1434 MonoString *ves_icall_System_String_InternalToLower_Comp (MonoString *this, MonoCultureInfo *cult)
1442 MONO_ARCH_SAVE_REGS;
1445 g_message (G_GNUC_PRETTY_FUNCTION ": [%s]",
1446 mono_string_to_utf8 (this));
1450 g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", cult->lcid);
1453 icu_loc=mono_string_to_icu_locale (cult->icu_name);
1455 mono_raise_exception ((MonoException *)mono_exception_from_name (mono_defaults.corlib, "System", "SystemException"));
1459 udest=(UChar *)g_malloc0 (sizeof(UChar)*(mono_string_length (this)+1));
1461 /* According to the docs, this might result in a longer or
1462 * shorter string than we started with...
1466 len=u_strToLower (udest, mono_string_length (this)+1,
1467 mono_string_chars (this), -1, icu_loc, &ec);
1468 if(ec==U_BUFFER_OVERFLOW_ERROR ||
1469 ec==U_STRING_NOT_TERMINATED_WARNING) {
1471 udest=(UChar *)g_malloc0 (sizeof(UChar)*(len+1));
1472 len=u_strToLower (udest, len+1, mono_string_chars (this), -1,
1476 if(U_SUCCESS (ec)) {
1477 ret=mono_string_from_utf16 ((gunichar2 *)udest);
1479 g_message (G_GNUC_PRETTY_FUNCTION ": u_strToLower error: %s",
1481 /* return something */
1489 g_message (G_GNUC_PRETTY_FUNCTION ": returning [%s]",
1490 mono_string_to_utf8 (ret));
1496 MonoString *ves_icall_System_String_InternalToUpper_Comp (MonoString *this, MonoCultureInfo *cult)
1504 MONO_ARCH_SAVE_REGS;
1507 g_message (G_GNUC_PRETTY_FUNCTION ": [%s]",
1508 mono_string_to_utf8 (this));
1512 g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", cult->lcid);
1515 icu_loc=mono_string_to_icu_locale (cult->icu_name);
1517 mono_raise_exception ((MonoException *)mono_exception_from_name (mono_defaults.corlib, "System", "SystemException"));
1521 udest=(UChar *)g_malloc0 (sizeof(UChar)*(mono_string_length (this)+1));
1523 /* According to the docs, this might result in a longer or
1524 * shorter string than we started with...
1528 len=u_strToUpper (udest, mono_string_length (this)+1,
1529 mono_string_chars (this), -1, icu_loc, &ec);
1530 if(ec==U_BUFFER_OVERFLOW_ERROR ||
1531 ec==U_STRING_NOT_TERMINATED_WARNING) {
1533 udest=(UChar *)g_malloc0 (sizeof(UChar)*(len+1));
1534 len=u_strToUpper (udest, len+1, mono_string_chars (this), -1,
1538 if(U_SUCCESS (ec)) {
1539 ret=mono_string_from_utf16 ((gunichar2 *)udest);
1541 g_message (G_GNUC_PRETTY_FUNCTION ": u_strToUpper error: %s",
1543 /* return something */
1551 g_message (G_GNUC_PRETTY_FUNCTION ": returning [%s]",
1552 mono_string_to_utf8 (ret));
1558 gunichar2 ves_icall_System_Char_InternalToUpper_Comp (gunichar2 c, MonoCultureInfo *cult)
1565 MONO_ARCH_SAVE_REGS;
1567 icu_loc=mono_string_to_icu_locale (cult->icu_name);
1569 mono_raise_exception ((MonoException *)mono_exception_from_name (mono_defaults.corlib, "System", "SystemException"));
1574 len=u_strToUpper (&udest, 1, &c, 1, icu_loc, &ec);
1576 if(U_SUCCESS (ec) && len==1) {
1579 /* return something */
1585 gunichar2 ves_icall_System_Char_InternalToLower_Comp (gunichar2 c, MonoCultureInfo *cult)
1592 MONO_ARCH_SAVE_REGS;
1594 icu_loc=mono_string_to_icu_locale (cult->icu_name);
1596 mono_raise_exception ((MonoException *)mono_exception_from_name (mono_defaults.corlib, "System", "SystemException"));
1601 len=u_strToLower (&udest, 1, &c, 1, icu_loc, &ec);
1603 if(U_SUCCESS (ec) && len==1) {
1606 /* return something */
1611 #else /* HAVE_ICU */
1612 void ves_icall_System_Globalization_CultureInfo_construct_internal_locale (MonoCultureInfo *this, MonoString *locale)
1614 MONO_ARCH_SAVE_REGS;
1616 /* Always claim "unknown locale" if we don't have ICU (only
1617 * called for non-invariant locales)
1619 mono_raise_exception((MonoException *)mono_exception_from_name(mono_defaults.corlib, "System", "ArgumentException"));
1622 void ves_icall_System_Globalization_CompareInfo_construct_compareinfo (MonoCompareInfo *comp, MonoString *locale)
1624 /* Nothing to do here */
1627 int ves_icall_System_Globalization_CompareInfo_internal_compare (MonoCompareInfo *this, MonoString *str1, gint32 off1, gint32 len1, MonoString *str2, gint32 off2, gint32 len2, gint32 options)
1629 MONO_ARCH_SAVE_REGS;
1631 /* Do a normal ascii string compare, as we only know the
1632 * invariant locale if we dont have ICU
1634 return(string_invariant_compare (str1, off1, len1, str2, off2, len2,
1638 void ves_icall_System_Globalization_CompareInfo_free_internal_collator (MonoCompareInfo *this)
1640 /* Nothing to do here */
1643 void ves_icall_System_Globalization_CompareInfo_assign_sortkey (MonoCompareInfo *this, MonoSortKey *key, MonoString *source, gint32 options)
1648 MONO_ARCH_SAVE_REGS;
1650 keylen=mono_string_length (source);
1652 arr=mono_array_new (mono_domain_get (), mono_defaults.byte_class,
1654 for(i=0; i<keylen; i++) {
1655 mono_array_set (arr, guint8, i, mono_string_chars (source)[i]);
1661 int ves_icall_System_Globalization_CompareInfo_internal_index (MonoCompareInfo *this, MonoString *source, gint32 sindex, gint32 count, MonoString *value, gint32 options, MonoBoolean first)
1663 MONO_ARCH_SAVE_REGS;
1665 return(string_invariant_indexof (source, sindex, count, value, first));
1668 int ves_icall_System_Globalization_CompareInfo_internal_index_char (MonoCompareInfo *this, MonoString *source, gint32 sindex, gint32 count, gunichar2 value, gint32 options, MonoBoolean first)
1670 MONO_ARCH_SAVE_REGS;
1672 return(string_invariant_indexof_char (source, sindex, count, value,
1676 int ves_icall_System_Threading_Thread_current_lcid (void)
1678 MONO_ARCH_SAVE_REGS;
1684 MonoString *ves_icall_System_String_InternalReplace_Str_Comp (MonoString *this, MonoString *old, MonoString *new, MonoCompareInfo *comp)
1686 MONO_ARCH_SAVE_REGS;
1688 /* Do a normal ascii string compare and replace, as we only
1689 * know the invariant locale if we dont have ICU
1691 return(string_invariant_replace (this, old, new));
1694 MonoString *ves_icall_System_String_InternalToLower_Comp (MonoString *this, MonoCultureInfo *cult)
1696 MONO_ARCH_SAVE_REGS;
1698 return(string_invariant_tolower (this));
1701 MonoString *ves_icall_System_String_InternalToUpper_Comp (MonoString *this, MonoCultureInfo *cult)
1703 MONO_ARCH_SAVE_REGS;
1705 return(string_invariant_toupper (this));
1708 gunichar2 ves_icall_System_Char_InternalToUpper_Comp (gunichar2 c, MonoCultureInfo *cult)
1710 MONO_ARCH_SAVE_REGS;
1712 return g_unichar_toupper (c);
1716 gunichar2 ves_icall_System_Char_InternalToLower_Comp (gunichar2 c, MonoCultureInfo *cult)
1718 MONO_ARCH_SAVE_REGS;
1720 return g_unichar_tolower (c);
1723 #endif /* HAVE_ICU */
1725 static gint32 string_invariant_compare_char (gunichar2 c1, gunichar2 c2,
1729 GUnicodeType c1type, c2type;
1731 c1type = g_unichar_type (c1);
1732 c2type = g_unichar_type (c2);
1734 if (options & CompareOptions_IgnoreCase) {
1735 result = (gint32) (c1type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c1) : c1) - (c2type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c2) : c2);
1736 } else if (options & CompareOptions_Ordinal) {
1737 // Rotor/ms return the full value just not -1 and 1
1738 return (gint32) c1 - c2;
1740 /* No options. Kana, symbol and spacing options don't
1741 * apply to the invariant culture.
1743 if (c1type == G_UNICODE_UPPERCASE_LETTER &&
1744 c2type == G_UNICODE_LOWERCASE_LETTER) {
1748 if (c1type == G_UNICODE_LOWERCASE_LETTER &&
1749 c2type == G_UNICODE_UPPERCASE_LETTER) {
1753 result = (gint32) c1 - c2;
1756 return ((result < 0) ? -1 : (result > 0) ? 1 : 0);
1759 static gint32 string_invariant_compare (MonoString *str1, gint32 off1,
1760 gint32 len1, MonoString *str2,
1761 gint32 off2, gint32 len2,
1764 /* c translation of C# code from old string.cs.. :) */
1777 ustr1 = mono_string_chars(str1)+off1;
1778 ustr2 = mono_string_chars(str2)+off2;
1782 for (pos = 0; pos != length; pos++) {
1783 if (pos >= len1 || pos >= len2)
1786 charcmp = string_invariant_compare_char(ustr1[pos], ustr2[pos],
1793 /* the lesser wins, so if we have looped until length we just
1794 * need to check the last char
1796 if (pos == length) {
1797 return(string_invariant_compare_char(ustr1[pos - 1],
1798 ustr2[pos - 1], options));
1801 /* Test if one of the strings has been compared to the end */
1808 } else if (pos >= len2) {
1812 /* if not, check our last char only.. (can this happen?) */
1813 return(string_invariant_compare_char(ustr1[pos], ustr2[pos], options));
1816 static MonoString *string_invariant_replace (MonoString *me,
1817 MonoString *oldValue,
1818 MonoString *newValue)
1822 gunichar2 *dest=NULL; /* shut gcc up */
1824 gunichar2 *newstr=NULL; /* shut gcc up here too */
1835 oldstr = mono_string_chars(oldValue);
1836 oldstrlen = mono_string_length(oldValue);
1838 if (NULL != newValue) {
1839 newstr = mono_string_chars(newValue);
1840 newstrlen = mono_string_length(newValue);
1844 src = mono_string_chars(me);
1845 srclen = mono_string_length(me);
1847 if (oldstrlen != newstrlen) {
1849 while (i <= srclen - oldstrlen) {
1850 if (0 == memcmp(src + i, oldstr, oldstrlen * sizeof(gunichar2))) {
1859 newsize = srclen + ((newstrlen - oldstrlen) * occurr);
1865 while (i < srclen) {
1866 if (0 == memcmp(src + i, oldstr, oldstrlen * sizeof(gunichar2))) {
1868 ret = mono_string_new_size( mono_domain_get (), newsize);
1869 dest = mono_string_chars(ret);
1870 memcpy (dest, src, i * sizeof(gunichar2));
1872 if (newstrlen > 0) {
1873 memcpy(dest + destpos, newstr, newstrlen * sizeof(gunichar2));
1874 destpos += newstrlen;
1878 } else if (ret != NULL) {
1879 dest[destpos] = src[i];
1891 static gint32 string_invariant_indexof (MonoString *source, gint32 sindex,
1892 gint32 count, MonoString *value,
1900 lencmpstr = mono_string_length(value);
1902 src = mono_string_chars(source);
1903 cmpstr = mono_string_chars(value);
1907 for(pos=sindex;pos <= sindex+count;pos++) {
1908 for(i=0;src[pos+i]==cmpstr[i];) {
1909 if(++i==lencmpstr) {
1917 for(pos=sindex-lencmpstr+1;pos>sindex-count;pos--) {
1918 if(memcmp (src+pos, cmpstr,
1919 lencmpstr*sizeof(gunichar2))==0) {
1928 static gint32 string_invariant_indexof_char (MonoString *source, gint32 sindex,
1929 gint32 count, gunichar2 value,
1935 src = mono_string_chars(source);
1937 for (pos = sindex; pos != count + sindex; pos++) {
1938 if (src [pos] == value) {
1945 for (pos = sindex; pos > sindex - count; pos--) {
1946 if (src [pos] == value)
1954 static MonoString *string_invariant_tolower (MonoString *this)
1961 ret = mono_string_new_size(mono_domain_get (),
1962 mono_string_length(this));
1964 src = mono_string_chars (this);
1965 dest = mono_string_chars (ret);
1967 for (i = 0; i < mono_string_length (this); ++i) {
1968 dest[i] = g_unichar_tolower(src[i]);
1974 static MonoString *string_invariant_toupper (MonoString *this)
1981 ret = mono_string_new_size(mono_domain_get (),
1982 mono_string_length(this));
1984 src = mono_string_chars (this);
1985 dest = mono_string_chars (ret);
1987 for (i = 0; i < mono_string_length (this); ++i) {
1988 dest[i] = g_unichar_toupper(src[i]);