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);
50 culture_lcid_locator (const void *a, const void *b)
52 const CultureInfoEntry *aa = a;
53 const CultureInfoEntry *bb = b;
55 return (aa->lcid - bb->lcid);
59 culture_name_locator (const void *a, const void *b)
61 const CultureInfoNameEntry *aa = a;
62 const CultureInfoNameEntry *bb = b;
65 ret = strcmp (aa->name, bb->name);
71 create_group_sizes_array (const gint *gs, gint ml)
76 for (i = 0; i < ml; i++) {
82 ret = mono_array_new (mono_domain_get (),
83 mono_defaults.int32_class, len);
85 for(i = 0; i < len; i++)
86 mono_array_set (ret, gint32, i, gs [i]);
92 create_names_array (const gchar **names, int ml)
101 domain = mono_domain_get ();
103 for (i = 0; i < ml; i++) {
104 if (names [i] == NULL)
109 ret = mono_array_new (mono_domain_get (), mono_defaults.string_class, len);
111 for(i = 0; i < len; i++)
112 mono_array_set (ret, MonoString *, i, mono_string_new (domain, names [i]));
118 ves_icall_System_Globalization_CultureInfo_construct_datetime_format (MonoCultureInfo *this)
121 MonoDateTimeFormatInfo *datetime;
122 const DateTimeFormatEntry *dfe;
126 g_assert (this->datetime_index >= 0);
128 datetime = this->datetime_format;
129 dfe = &datetime_format_entries [this->datetime_index];
131 domain = mono_domain_get ();
133 datetime->AbbreviatedDayNames = create_names_array (dfe->abbreviated_day_names,
135 datetime->AbbreviatedMonthNames = create_names_array (dfe->abbreviated_month_names,
137 datetime->AMDesignator = mono_string_new (domain, dfe->am_designator);
138 datetime->CalendarWeekRule = dfe->calendar_week_rule;
139 datetime->DateSeparator = mono_string_new (domain, dfe->date_separator);
140 datetime->DayNames = create_names_array (dfe->day_names, NUM_DAYS);
141 datetime->FirstDayOfWeek = dfe->first_day_of_week;
142 datetime->FullDateTimePattern = mono_string_new (domain, dfe->full_date_time_pattern);
143 datetime->LongDatePattern = mono_string_new (domain, dfe->long_date_pattern);
144 datetime->LongTimePattern = mono_string_new (domain, dfe->long_time_pattern);
145 datetime->MonthDayPattern = mono_string_new (domain, dfe->month_day_pattern);
146 datetime->MonthNames = create_names_array (dfe->month_names, NUM_MONTHS);
147 datetime->PMDesignator = mono_string_new (domain, dfe->pm_designator);
148 datetime->ShortDatePattern = mono_string_new (domain, dfe->short_date_pattern);
149 datetime->ShortTimePattern = mono_string_new (domain, dfe->short_time_pattern);
150 datetime->TimeSeparator = mono_string_new (domain, dfe->time_separator);
151 datetime->YearMonthPattern = mono_string_new (domain, dfe->year_month_pattern);
156 ves_icall_System_Globalization_CultureInfo_construct_number_format (MonoCultureInfo *this)
159 MonoNumberFormatInfo *number;
160 const NumberFormatEntry *nfe;
164 g_assert (this->number_format != 0);
166 number = this->number_format;
167 nfe = &number_format_entries [this->number_index];
169 domain = mono_domain_get ();
171 number->currencyDecimalDigits = nfe->currency_decimal_digits;
172 number->currencyDecimalSeparator = mono_string_new (domain,
173 nfe->currency_decimal_separator);
174 number->currencyGroupSeparator = mono_string_new (domain,
175 nfe->currency_group_separator);
176 number->currencyGroupSizes = create_group_sizes_array (nfe->currency_group_sizes,
178 number->currencyNegativePattern = nfe->currency_negative_pattern;
179 number->currencyPositivePattern = nfe->currency_positive_pattern;
180 number->currencySymbol = mono_string_new (domain, nfe->currency_symbol);
181 number->naNSymbol = mono_string_new (domain, nfe->nan_symbol);
182 number->negativeInfinitySymbol = mono_string_new (domain,
183 nfe->negative_infinity_symbol);
184 number->negativeSign = mono_string_new (domain, nfe->negative_sign);
185 number->numberDecimalDigits = nfe->number_decimal_digits;
186 number->numberDecimalSeparator = mono_string_new (domain,
187 nfe->number_decimal_separator);
188 number->numberGroupSeparator = mono_string_new (domain, nfe->number_group_separator);
189 number->numberGroupSizes = create_group_sizes_array (nfe->number_group_sizes,
191 number->numberNegativePattern = nfe->number_negative_pattern;
192 number->percentDecimalDigits = nfe->percent_decimal_digits;
193 number->percentDecimalSeparator = mono_string_new (domain,
194 nfe->percent_decimal_separator);
195 number->percentGroupSeparator = mono_string_new (domain,
196 nfe->percent_group_separator);
197 number->percentGroupSizes = create_group_sizes_array (nfe->percent_group_sizes,
199 number->percentNegativePattern = nfe->percent_negative_pattern;
200 number->percentPositivePattern = nfe->percent_positive_pattern;
201 number->percentSymbol = mono_string_new (domain, nfe->percent_symbol);
202 number->perMilleSymbol = mono_string_new (domain, nfe->per_mille_symbol);
203 number->positiveInfinitySymbol = mono_string_new (domain,
204 nfe->positive_infinity_symbol);
205 number->positiveSign = mono_string_new (domain, nfe->positive_sign);
209 construct_culture (MonoCultureInfo *this, const CultureInfoEntry *ci)
211 MonoDomain *domain = mono_domain_get ();
213 this->lcid = ci->lcid;
214 this->name = mono_string_new (domain, ci->name);
215 this->icu_name = mono_string_new (domain, ci->icu_name);
216 this->displayname = mono_string_new (domain, ci->displayname);
217 this->englishname = mono_string_new (domain, ci->englishname);
218 this->nativename = mono_string_new (domain, ci->nativename);
219 this->win3lang = mono_string_new (domain, ci->win3lang);
220 this->iso3lang = mono_string_new (domain, ci->iso3lang);
221 this->iso2lang = mono_string_new (domain, ci->iso2lang);
222 this->parent_lcid = ci->parent_lcid;
223 this->specific_lcid = ci->specific_lcid;
224 this->datetime_index = ci->datetime_format_index;
225 this->number_index = ci->number_format_index;
226 this->calendar_data = ci->calendar_data;
231 static const CultureInfoEntry*
232 culture_info_entry_from_lcid (int lcid)
234 const CultureInfoEntry *ci;
235 CultureInfoEntry key;
238 ci = bsearch (&key, culture_entries, NUM_CULTURE_ENTRIES, sizeof (CultureInfoEntry), culture_lcid_locator);
244 * The following two methods are modified from the ICU source code. (http://oss.software.ibm.com/icu)
245 * Copyright (c) 1995-2003 International Business Machines Corporation and others
246 * All rights reserved.
249 get_posix_locale (void)
251 const gchar* posix_locale = NULL;
253 posix_locale = g_getenv("LC_ALL");
254 if (posix_locale == 0) {
255 posix_locale = g_getenv("LANG");
256 if (posix_locale == 0) {
257 posix_locale = setlocale(LC_ALL, NULL);
261 if (posix_locale == NULL)
264 if ((strcmp ("C", posix_locale) == 0) || (strchr (posix_locale, ' ') != NULL)
265 || (strchr (posix_locale, '/') != NULL)) {
267 * HPUX returns 'C C C C C C C'
268 * Solaris can return /en_US/C/C/C/C/C on the second try.
269 * Maybe we got some garbage.
274 return g_strdup (posix_locale);
278 get_current_locale_name (void)
281 gchar *corrected = NULL;
287 #ifdef PLATFORM_WIN32
288 locale = g_win32_getlocale ();
290 locale = get_posix_locale ();
296 if ((p = strchr (locale, '.')) != NULL) {
297 /* assume new locale can't be larger than old one? */
298 corrected = malloc (strlen (locale));
299 strncpy (corrected, locale, p - locale);
300 corrected [p - locale] = 0;
302 /* do not copy after the @ */
303 if ((p = strchr (corrected, '@')) != NULL)
304 corrected [p - corrected] = 0;
307 /* Note that we scan the *uncorrected* ID. */
308 if ((p = strrchr (locale, '@')) != NULL) {
311 * In Mono we dont handle the '@' modifier because we do
312 * not have any cultures that use it. We just trim it
313 * off of the end of the name.
316 if (corrected == NULL) {
317 corrected = malloc (strlen (locale));
318 strncpy (corrected, locale, p - locale);
319 corrected [p - locale] = 0;
323 if (corrected == NULL)
328 if ((c = strchr (corrected, '_')) != NULL)
331 g_strdown (corrected);
337 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_current_locale (MonoCultureInfo *ci)
340 CultureInfoNameEntry key;
341 const CultureInfoNameEntry *ne;
345 locale = get_current_locale_name ();
350 ne = bsearch (&key, culture_name_entries, NUM_CULTURE_ENTRIES,
351 sizeof (CultureInfoNameEntry), culture_name_locator);
358 return construct_culture (ci, &culture_entries [ne->culture_entry_index]);
362 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_lcid (MonoCultureInfo *this,
365 const CultureInfoEntry *ci;
369 ci = culture_info_entry_from_lcid (lcid);
373 return construct_culture (this, ci);
377 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_name (MonoCultureInfo *this,
380 CultureInfoNameEntry key;
381 const CultureInfoNameEntry *ne;
385 key.name = mono_string_to_utf8 (name);
386 ne = bsearch (&key, culture_name_entries, NUM_CULTURE_ENTRIES,
387 sizeof (CultureInfoNameEntry), culture_name_locator);
394 return construct_culture (this, &culture_entries [ne->culture_entry_index]);
398 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_specific_name (MonoCultureInfo *ci,
401 const CultureInfoEntry *entry;
402 CultureInfoNameEntry key;
403 const CultureInfoNameEntry *ne;
407 key.name = mono_string_to_utf8 (name);
408 ne = bsearch (&key, culture_name_entries, NUM_CULTURE_ENTRIES,
409 sizeof (CultureInfoNameEntry), culture_name_locator);
416 entry = &culture_entries [ne->culture_entry_index];
418 /* try avoiding another lookup, often the culture is its own specific culture */
419 if (entry->lcid != entry->specific_lcid)
420 entry = culture_info_entry_from_lcid (entry->specific_lcid);
422 return construct_culture (ci, entry);
426 ves_icall_System_Globalization_CultureInfo_internal_get_cultures (MonoBoolean neutral,
427 MonoBoolean specific, MonoBoolean installed)
431 MonoCultureInfo *culture;
433 const CultureInfoEntry *ci;
439 domain = mono_domain_get ();
442 for (i = 0; i < NUM_CULTURE_ENTRIES; i++) {
443 ci = &culture_entries [i];
444 is_neutral = ((ci->lcid & 0xff00) == 0 || ci->specific_lcid == 0);
445 if ((neutral && is_neutral) || (specific && !is_neutral))
449 class = mono_class_from_name (mono_defaults.corlib,
450 "System.Globalization", "CultureInfo");
451 ret = mono_array_new (domain, class, len);
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)) {
461 culture = (MonoCultureInfo *) mono_object_new (domain, class);
462 mono_runtime_object_init ((MonoObject *) culture);
463 construct_culture (culture, ci);
464 mono_array_set (ret, MonoCultureInfo *, len++, culture);
472 * Set is_neutral and return TRUE if the culture is found. If it is not found return FALSE.
475 ves_icall_System_Globalization_CultureInfo_internal_is_lcid_neutral (gint lcid, MonoBoolean *is_neutral)
477 const CultureInfoEntry *entry;
481 entry = culture_info_entry_from_lcid (lcid);
486 *is_neutral = (entry->specific_lcid == 0);
493 #include <unicode/utypes.h>
494 #include <unicode/ustring.h>
495 #include <unicode/ures.h>
496 #include <unicode/ucol.h>
497 #include <unicode/usearch.h>
499 static MonoString *monostring_from_resource_index (const UResourceBundle *bundle, int32_t idx)
506 res_str=(gunichar2 *)ures_getStringByIndex (bundle, idx, &res_strlen,
512 return(mono_string_from_utf16 (res_str));
515 static UResourceBundle *open_subbundle (const UResourceBundle *bundle,
516 const char *name, int32_t req_count)
518 UResourceBundle *subbundle;
523 subbundle=ures_getByKey (bundle, name, NULL, &ec);
525 /* Couldn't find the subbundle */
529 count=ures_countArrayItems (bundle, name, &ec);
531 /* Couldn't count the subbundle */
532 ures_close (subbundle);
536 if(count!=req_count) {
538 ures_close (subbundle);
545 static MonoArray *build_array (const UResourceBundle *bundle,
546 const char *resname, int32_t req_count)
549 UResourceBundle *subbundle;
552 subbundle=open_subbundle (bundle, resname, req_count);
553 if(subbundle!=NULL) {
554 arr=mono_array_new(mono_domain_get (),
555 mono_defaults.string_class, req_count);
557 for(i=0; i<req_count; i++) {
558 mono_array_set(arr, MonoString *, i, monostring_from_resource_index (subbundle, i));
561 ures_close (subbundle);
567 static MonoDateTimeFormatInfo *create_DateTimeFormat (const char *locale)
569 MonoDateTimeFormatInfo *new_dtf;
571 UResourceBundle *bundle, *subbundle;
574 class=mono_class_from_name (mono_defaults.corlib,
575 "System.Globalization",
576 "DateTimeFormatInfo");
577 new_dtf=(MonoDateTimeFormatInfo *)mono_object_new (mono_domain_get (),
579 mono_runtime_object_init ((MonoObject *)new_dtf);
583 bundle=ures_open (NULL, locale, &ec);
589 subbundle=open_subbundle (bundle, "AmPmMarkers", 2);
590 if(subbundle!=NULL) {
591 new_dtf->AMDesignator=monostring_from_resource_index (subbundle, 0);
592 new_dtf->PMDesignator=monostring_from_resource_index (subbundle, 1);
594 ures_close (subbundle);
597 /* Date/Time patterns. Don't set FullDateTimePattern. As it
598 * seems to always default to LongDatePattern + " " +
599 * LongTimePattern, let the property accessor deal with it.
601 subbundle=open_subbundle (bundle, "DateTimePatterns", 9);
602 if(subbundle!=NULL) {
603 new_dtf->ShortDatePattern=monostring_from_resource_index (subbundle, 7);
604 new_dtf->LongDatePattern=monostring_from_resource_index (subbundle, 5);
605 new_dtf->ShortTimePattern=monostring_from_resource_index (subbundle, 3);
606 new_dtf->LongTimePattern=monostring_from_resource_index (subbundle, 2);
608 /* RFC1123Pattern, SortableDateTimePattern and
609 * UniversalSortableDateTimePattern all seem to be
610 * constant, and all the same as the invariant default
614 ures_close (subbundle);
618 /* Not sure what to do with these yet, so leave them set to
619 * the invariant default
621 set_field_string (new_dtf, "_DateSeparator", str);
622 set_field_string (new_dtf, "_TimeSeparator", str);
623 set_field_string (new_dtf, "_MonthDayPattern", str);
624 set_field_string (new_dtf, "_YearMonthPattern", str);
627 /* Day names. Luckily both ICU and .net start Sunday at index 0 */
628 new_dtf->DayNames=build_array (bundle, "DayNames", 7);
630 /* Abbreviated day names */
631 new_dtf->AbbreviatedDayNames=build_array (bundle, "DayAbbreviations",
635 new_dtf->MonthNames=build_array (bundle, "MonthNames", 12);
637 /* Abbreviated month names */
638 new_dtf->AbbreviatedMonthNames=build_array (bundle,
639 "MonthAbbreviations", 12);
641 /* TODO: DayOfWeek _FirstDayOfWeek, Calendar _Calendar, CalendarWeekRule _CalendarWeekRule */
648 static MonoNumberFormatInfo *create_NumberFormat (const char *locale)
650 MonoNumberFormatInfo *new_nf;
652 MonoMethodDesc* methodDesc;
654 UResourceBundle *bundle, *subbundle, *table_entries;
657 static char country [7]; //FIXME
658 const UChar *res_str;
661 class=mono_class_from_name (mono_defaults.corlib,
662 "System.Globalization",
664 new_nf=(MonoNumberFormatInfo *)mono_object_new (mono_domain_get (),
666 mono_runtime_object_init ((MonoObject *)new_nf);
670 bundle=ures_open (NULL, locale, &ec);
675 /* Number Elements */
677 subbundle=ures_getByKey (bundle, "NumberElements", NULL, &ec);
679 /* Couldn't find the subbundle */
683 count=ures_countArrayItems (bundle, "NumberElements", &ec);
685 /* Couldn't count the subbundle */
686 ures_close (subbundle);
690 if(subbundle!=NULL) {
691 new_nf->numberDecimalSeparator=monostring_from_resource_index (subbundle, 0);
692 new_nf->numberGroupSeparator=monostring_from_resource_index (subbundle, 1);
693 new_nf->percentDecimalSeparator=monostring_from_resource_index (subbundle, 0);
694 new_nf->percentGroupSeparator=monostring_from_resource_index (subbundle, 1);
695 new_nf->percentSymbol=monostring_from_resource_index (subbundle, 3);
696 new_nf->zeroPattern=monostring_from_resource_index (subbundle, 4);
697 new_nf->digitPattern=monostring_from_resource_index (subbundle, 5);
698 new_nf->negativeSign=monostring_from_resource_index (subbundle, 6);
699 new_nf->perMilleSymbol=monostring_from_resource_index (subbundle, 8);
700 new_nf->positiveInfinitySymbol=monostring_from_resource_index (subbundle, 9);
701 /* we dont have this in CLDR, so copy it from positiveInfinitySymbol */
702 new_nf->negativeInfinitySymbol=monostring_from_resource_index (subbundle, 9);
703 new_nf->naNSymbol=monostring_from_resource_index (subbundle, 10);
704 new_nf->currencyDecimalSeparator=monostring_from_resource_index (subbundle, 0);
705 new_nf->currencyGroupSeparator=monostring_from_resource_index (subbundle, 1);
707 ures_close (subbundle);
710 /* get country name */
712 uloc_getCountry (locale, country, sizeof (country), &ec);
713 if (U_SUCCESS (ec)) {
715 /* find country name in root.CurrencyMap */
716 subbundle = ures_getByKey (bundle, "CurrencyMap", NULL, &ec);
717 if (U_SUCCESS (ec)) {
719 /* get currency id for specified country */
720 table_entries = ures_getByKey (subbundle, country, NULL, &ec);
721 if (U_SUCCESS (ec)) {
722 ures_close (subbundle);
725 res_str = ures_getStringByIndex (
726 table_entries, 0, &res_strlen, &ec);
728 /* now we have currency id string */
729 ures_close (table_entries);
731 u_UCharsToChars (res_str, country,
735 /* find currency string in locale data */
736 subbundle = ures_getByKey (
737 bundle, "Currencies",
740 if (U_SUCCESS (ec)) {
742 /* find currency symbol under specified currency id */
743 table_entries = ures_getByKey (subbundle, country, NULL, &ec);
744 if (U_SUCCESS (ec)) {
745 /* get the first string only,
746 * the second is international currency symbol (not used)*/
747 new_nf->currencySymbol=monostring_from_resource_index (table_entries, 0);
748 ures_close (table_entries);
750 ures_close (subbundle);
758 subbundle=open_subbundle (bundle, "NumberPatterns", 4);
759 if(subbundle!=NULL) {
760 new_nf->decimalFormats=monostring_from_resource_index (subbundle, 0);
761 new_nf->currencyFormats=monostring_from_resource_index (subbundle, 1);
762 new_nf->percentFormats=monostring_from_resource_index (subbundle, 2);
763 ures_close (subbundle);
765 /* calls InitPatterns to parse the patterns
767 methodDesc = mono_method_desc_new (
768 "System.Globalization.NumberFormatInfo:InitPatterns()",
770 method = mono_method_desc_search_in_class (methodDesc, class);
772 mono_runtime_invoke (method, new_nf, NULL, NULL);
774 g_warning (G_GNUC_PRETTY_FUNCTION ": Runtime mismatch with class lib! (Looking for System.Globalization.NumberFormatInfo:InitPatterns())");
783 static char *mono_string_to_icu_locale (MonoString *locale)
786 char *passed_locale, *icu_locale=NULL;
787 int32_t loc_len, ret;
789 passed_locale=mono_string_to_utf8 (locale);
792 ret=uloc_getName (passed_locale, NULL, 0, &ec);
793 if(ec==U_BUFFER_OVERFLOW_ERROR) {
796 icu_locale=(char *)g_malloc0 (sizeof(char)*loc_len);
797 ret=uloc_getName (passed_locale, icu_locale, loc_len, &ec);
799 g_free (passed_locale);
804 void ves_icall_System_Globalization_CultureInfo_construct_internal_locale (MonoCultureInfo *this, MonoString *locale)
810 int32_t str_len, ret;
814 icu_locale=mono_string_to_icu_locale (locale);
815 if(icu_locale==NULL) {
816 /* Something went wrong */
817 mono_raise_exception((MonoException *)mono_exception_from_name(mono_defaults.corlib, "System", "SystemException"));
821 /* Fill in the static fields */
823 /* TODO: Calendar, InstalledUICulture, OptionalCalendars,
827 str_len=256; /* Should be big enough for anything */
828 str=(char *)g_malloc0 (sizeof(char)*str_len);
829 ustr=(UChar *)g_malloc0 (sizeof(UChar)*str_len);
833 ret=uloc_getDisplayName (icu_locale, "en", ustr, str_len, &ec);
834 if(U_SUCCESS (ec) && ret<str_len) {
835 this->englishname=mono_string_from_utf16 ((gunichar2 *)ustr);
838 ret=uloc_getDisplayName (icu_locale, uloc_getDefault (), ustr, str_len,
840 if(U_SUCCESS (ec) && ret<str_len) {
841 this->displayname=mono_string_from_utf16 ((gunichar2 *)ustr);
844 ret=uloc_getDisplayName (icu_locale, icu_locale, ustr, str_len, &ec);
845 if(U_SUCCESS (ec) && ret<str_len) {
846 this->nativename=mono_string_from_utf16 ((gunichar2 *)ustr);
849 this->iso3lang=mono_string_new_wrapper (uloc_getISO3Language (icu_locale));
851 ret=uloc_getLanguage (icu_locale, str, str_len, &ec);
852 if(U_SUCCESS (ec) && ret<str_len) {
853 this->iso2lang=mono_string_new_wrapper (str);
856 this->datetime_format=create_DateTimeFormat (icu_locale);
857 this->number_format=create_NumberFormat (icu_locale);
864 void ves_icall_System_Globalization_CompareInfo_construct_compareinfo (MonoCompareInfo *comp, MonoString *locale)
873 g_message (G_GNUC_PRETTY_FUNCTION ": Constructing collator for locale [%s]", mono_string_to_utf8 (locale));
876 icu_locale=mono_string_to_icu_locale (locale);
877 if(icu_locale==NULL) {
878 /* Something went wrong */
879 mono_raise_exception((MonoException *)mono_exception_from_name(mono_defaults.corlib, "System", "SystemException"));
884 coll=ucol_open (icu_locale, &ec);
886 comp->ICU_collator=coll;
888 comp->ICU_collator=NULL;
894 /* Set up the collator to reflect the options required. Some of these
895 * options clash, as they adjust the collator strength level. Try to
896 * make later checks reduce the strength level, and attempt to take
897 * previous options into account.
899 * Don't bother to check the error returns when setting the
900 * attributes, as a failure here is hardly grounds to error out.
902 static void set_collator_options (UCollator *coll, gint32 options)
904 UErrorCode ec=U_ZERO_ERROR;
906 /* Set up the defaults */
907 ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE,
909 ucol_setAttribute (coll, UCOL_CASE_LEVEL, UCOL_OFF, &ec);
911 /* Do this first so other options will override the quaternary
912 * level strength setting if necessary
914 if(!(options & CompareOptions_IgnoreKanaType)) {
915 ucol_setAttribute (coll, UCOL_HIRAGANA_QUATERNARY_MODE,
917 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_QUATERNARY, &ec);
920 /* Word sort, the default */
921 if(!(options & CompareOptions_StringSort)) {
922 ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING,
924 /* Tertiary strength is the default, but it might have
925 * been set to quaternary above. (We don't want that
926 * here, because that will order all the punctuation
927 * first instead of just ignoring it.)
929 * Unfortunately, tertiary strength with
930 * ALTERNATE_HANDLING==SHIFTED means that '/' and '@'
931 * compare to equal, which has the nasty side effect
932 * of killing mcs :-( (We can't specify a
933 * culture-insensitive compare, because
934 * String.StartsWith doesn't have that option.)
936 * ALTERNATE_HANDLING==SHIFTED is needed to accomplish
937 * the word-sorting-ignoring-punctuation feature. So
938 * we have to live with the slightly mis-ordered
939 * punctuation and a working mcs...
941 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_QUATERNARY, &ec);
944 if(options & CompareOptions_IgnoreCase) {
945 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_SECONDARY, &ec);
946 ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE, &ec);
949 if(options & CompareOptions_IgnoreWidth) {
950 /* Kana width is a tertiary strength difference. This
951 * will totally break the !IgnoreKanaType option
953 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_SECONDARY, &ec);
956 if(options & CompareOptions_IgnoreNonSpace) {
957 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_PRIMARY, &ec);
958 /* We can still compare case even when just checking
961 if(!(options & CompareOptions_IgnoreCase) ||
962 !(options & CompareOptions_IgnoreWidth)) {
963 /* Not sure if CASE_LEVEL handles kana width
965 ucol_setAttribute (coll, UCOL_CASE_LEVEL, UCOL_ON,
970 if(options & CompareOptions_IgnoreSymbols) {
971 /* Don't know what to do here */
974 if(options == CompareOptions_Ordinal) {
975 /* This one is handled elsewhere */
979 gint32 ves_icall_System_Globalization_CompareInfo_internal_compare (MonoCompareInfo *this, MonoString *str1, gint32 off1, gint32 len1, MonoString *str2, gint32 off2, gint32 len2, gint32 options)
982 UCollationResult result;
987 g_message (G_GNUC_PRETTY_FUNCTION ": Comparing [%s] and [%s]", mono_string_to_utf8 (str1), mono_string_to_utf8 (str2));
990 coll=this->ICU_collator;
993 g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", this->lcid);
996 if(coll==NULL || this->lcid==0x007F ||
997 options & CompareOptions_Ordinal) {
999 g_message (G_GNUC_PRETTY_FUNCTION ": No collator or invariant, using shortcut");
1002 return(string_invariant_compare (str1, off1, len1, str2, off2,
1006 mono_monitor_try_enter ((MonoObject *)this, INFINITE);
1008 set_collator_options (coll, options);
1010 result=ucol_strcoll (coll, mono_string_chars (str1)+off1, len1,
1011 mono_string_chars (str2)+off2, len2);
1013 mono_monitor_exit ((MonoObject *)this);
1016 g_message (G_GNUC_PRETTY_FUNCTION ": Comparison of [%s] and [%s] returning %d", mono_string_to_utf8 (str1), mono_string_to_utf8 (str2), result);
1022 void ves_icall_System_Globalization_CompareInfo_free_internal_collator (MonoCompareInfo *this)
1026 MONO_ARCH_SAVE_REGS;
1028 coll=this->ICU_collator;
1034 void ves_icall_System_Globalization_CompareInfo_assign_sortkey (MonoCompareInfo *this, MonoSortKey *key, MonoString *source, gint32 options)
1041 MONO_ARCH_SAVE_REGS;
1043 coll=this->ICU_collator;
1045 mono_raise_exception((MonoException *)mono_exception_from_name(mono_defaults.corlib, "System", "SystemException"));
1049 mono_monitor_try_enter ((MonoObject *)this, INFINITE);
1051 set_collator_options (coll, options);
1053 keylen=ucol_getSortKey (coll, mono_string_chars (source), -1, NULL, 0);
1054 keybuf=g_malloc (sizeof(char)* keylen);
1055 ucol_getSortKey (coll, mono_string_chars (source), -1, keybuf, keylen);
1057 mono_monitor_exit ((MonoObject *)this);
1059 arr=mono_array_new (mono_domain_get (), mono_defaults.byte_class,
1061 for(i=0; i<keylen; i++) {
1062 mono_array_set (arr, guint8, i, keybuf[i]);
1070 int ves_icall_System_Globalization_CompareInfo_internal_index (MonoCompareInfo *this, MonoString *source, gint32 sindex, gint32 count, MonoString *value, gint32 options, MonoBoolean first)
1075 UStringSearch *search;
1078 MONO_ARCH_SAVE_REGS;
1081 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);
1084 coll=this->ICU_collator;
1087 g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", this->lcid);
1090 if(coll==NULL || this->lcid==0x007F ||
1091 options & CompareOptions_Ordinal) {
1093 g_message (G_GNUC_PRETTY_FUNCTION ": No collator or invariant, using shortcut");
1096 return(string_invariant_indexof (source, sindex, count, value,
1100 usrcstr=g_malloc0 (sizeof(UChar)*(count+1));
1102 memcpy (usrcstr, mono_string_chars (source)+sindex,
1103 sizeof(UChar)*count);
1105 memcpy (usrcstr, mono_string_chars (source)+sindex-count+1,
1106 sizeof(UChar)*count);
1109 mono_monitor_try_enter ((MonoObject *)this, INFINITE);
1113 /* Need to set the collator to a fairly weak level, so that it
1114 * treats characters that can be written differently as
1115 * identical (eg "ß" and "ss", "æ" and "ae" or "ä" etc.) Note
1116 * that this means that the search string and the original
1117 * text might have differing lengths.
1119 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_PRIMARY, &ec);
1121 /* Still notice case differences though (normally a tertiary
1124 ucol_setAttribute (coll, UCOL_CASE_LEVEL, UCOL_ON, &ec);
1126 /* Don't ignore some codepoints */
1127 ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE,
1130 search=usearch_openFromCollator (mono_string_chars (value), -1, usrcstr, -1, coll, NULL,
1132 if(U_SUCCESS (ec)) {
1134 pos=usearch_first (search, &ec);
1136 pos=usearch_last (search, &ec);
1139 if(pos!=USEARCH_DONE) {
1141 g_message (G_GNUC_PRETTY_FUNCTION
1142 ": Got match at %d (sindex %d) len %d", pos,
1143 sindex, usearch_getMatchedLength (search));
1149 pos+=(sindex-count+1);
1154 g_message (G_GNUC_PRETTY_FUNCTION ": usearch_open error: %s",
1158 usearch_close (search);
1160 mono_monitor_exit ((MonoObject *)this);
1167 int ves_icall_System_Globalization_CompareInfo_internal_index_char (MonoCompareInfo *this, MonoString *source, gint32 sindex, gint32 count, gunichar2 value, gint32 options, MonoBoolean first)
1170 UChar *usrcstr, uvalstr[2]={0, 0};
1172 UStringSearch *search;
1175 MONO_ARCH_SAVE_REGS;
1178 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);
1181 coll=this->ICU_collator;
1184 g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", this->lcid);
1187 if(coll==NULL || this->lcid==0x007F ||
1188 options & CompareOptions_Ordinal) {
1190 g_message (G_GNUC_PRETTY_FUNCTION ": No collator or invariant, using shortcut");
1193 return(string_invariant_indexof_char (source, sindex, count,
1197 usrcstr=g_malloc0 (sizeof(UChar)*(count+1));
1199 memcpy (usrcstr, mono_string_chars (source)+sindex,
1200 sizeof(UChar)*count);
1202 memcpy (usrcstr, mono_string_chars (source)+sindex-count+1,
1203 sizeof(UChar)*count);
1207 mono_monitor_try_enter ((MonoObject *)this, INFINITE);
1211 /* Need to set the collator to a fairly weak level, so that it
1212 * treats characters that can be written differently as
1213 * identical (eg "ß" and "ss", "æ" and "ae" or "ä" etc.) Note
1214 * that this means that the search string and the original
1215 * text might have differing lengths.
1217 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_PRIMARY, &ec);
1219 /* Still notice case differences though (normally a tertiary
1222 ucol_setAttribute (coll, UCOL_CASE_LEVEL, UCOL_ON, &ec);
1224 /* Don't ignore some codepoints */
1225 ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE,
1228 search=usearch_openFromCollator (uvalstr, -1, usrcstr, -1, coll, NULL,
1230 if(U_SUCCESS (ec)) {
1232 pos=usearch_first (search, &ec);
1234 pos=usearch_last (search, &ec);
1237 if(pos!=USEARCH_DONE) {
1239 g_message (G_GNUC_PRETTY_FUNCTION
1240 ": Got match at %d (sindex %d) len %d", pos,
1241 sindex, usearch_getMatchedLength (search));
1247 pos+=(sindex-count+1);
1252 g_message (G_GNUC_PRETTY_FUNCTION ": usearch_open error: %s",
1256 usearch_close (search);
1258 mono_monitor_exit ((MonoObject *)this);
1265 int ves_icall_System_Threading_Thread_current_lcid (void)
1267 MONO_ARCH_SAVE_REGS;
1269 return(uloc_getLCID (uloc_getDefault ()));
1272 MonoString *ves_icall_System_String_InternalReplace_Str_Comp (MonoString *this, MonoString *old, MonoString *new, MonoCompareInfo *comp)
1274 MonoString *ret=NULL;
1277 UStringSearch *search;
1279 MONO_ARCH_SAVE_REGS;
1282 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));
1285 coll=comp->ICU_collator;
1288 g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", comp->lcid);
1291 if(coll==NULL || comp->lcid==0x007F) {
1293 g_message (G_GNUC_PRETTY_FUNCTION ": No collator or invariant, using shortcut");
1296 return(string_invariant_replace (this, old, new));
1299 mono_monitor_try_enter ((MonoObject *)comp, INFINITE);
1303 /* Need to set the collator to a fairly weak level, so that it
1304 * treats characters that can be written differently as
1305 * identical (eg "ß" and "ss", "æ" and "ae" or "ä" etc.) Note
1306 * that this means that the search string and the original
1307 * text might have differing lengths.
1309 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_PRIMARY, &ec);
1311 /* Still notice case differences though (normally a tertiary
1314 ucol_setAttribute (coll, UCOL_CASE_LEVEL, UCOL_ON, &ec);
1316 /* Don't ignore some codepoints */
1317 ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE,
1320 search=usearch_openFromCollator (mono_string_chars (old), -1,
1321 mono_string_chars (this), -1, coll,
1323 if(U_SUCCESS (ec)) {
1324 int pos, oldpos, len_delta=0;
1325 int32_t newstr_len=mono_string_length (new), match_len;
1326 UChar *uret, *match;
1328 for(pos=usearch_first (search, &ec);
1330 pos=usearch_next (search, &ec)) {
1331 /* ICU usearch currently ignores most of the collator
1334 * Check the returned match to see if it really
1335 * does match properly...
1337 match_len = usearch_getMatchedLength (search);
1338 match=(UChar *)g_malloc0 (sizeof(UChar) * (match_len + 1));
1339 usearch_getMatchedText (search, match, match_len, &ec);
1341 if (ucol_strcoll (coll, match, -1, mono_string_chars (old), -1) == UCOL_EQUAL) {
1342 /* OK, we really did get a match */
1344 g_message (G_GNUC_PRETTY_FUNCTION
1345 ": Got match at %d len %d", pos,
1349 len_delta += (newstr_len - match_len);
1353 g_message (G_GNUC_PRETTY_FUNCTION
1354 ": Got false match at %d len %d",
1361 g_message (G_GNUC_PRETTY_FUNCTION
1362 ": New string length is %d (delta %d)",
1363 mono_string_length (this)+len_delta, len_delta);
1366 uret=(UChar *)g_malloc0 (sizeof(UChar) * (mono_string_length (this)+len_delta+2));
1368 for(oldpos=0, pos=usearch_first (search, &ec);
1370 pos=usearch_next (search, &ec)) {
1371 match_len = usearch_getMatchedLength (search);
1372 match=(UChar *)g_malloc0 (sizeof(UChar) * (match_len + 1));
1373 usearch_getMatchedText (search, match, match_len, &ec);
1375 /* Add the unmatched text */
1376 u_strncat (uret, mono_string_chars (this)+oldpos,
1378 if (ucol_strcoll (coll, match, -1, mono_string_chars (old), -1) == UCOL_EQUAL) {
1379 /* Then the replacement */
1380 u_strcat (uret, mono_string_chars (new));
1382 /* Then the original, because this is a
1385 u_strncat (uret, mono_string_chars (this)+pos,
1388 oldpos=pos+match_len;
1392 /* Finish off with the trailing unmatched text */
1393 u_strcat (uret, mono_string_chars (this)+oldpos);
1395 ret=mono_string_from_utf16 ((gunichar2 *)uret);
1397 g_message (G_GNUC_PRETTY_FUNCTION ": usearch_open error: %s",
1401 usearch_close (search);
1403 mono_monitor_exit ((MonoObject *)comp);
1406 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));
1412 MonoString *ves_icall_System_String_InternalToLower_Comp (MonoString *this, MonoCultureInfo *cult)
1420 MONO_ARCH_SAVE_REGS;
1423 g_message (G_GNUC_PRETTY_FUNCTION ": [%s]",
1424 mono_string_to_utf8 (this));
1428 g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", cult->lcid);
1431 icu_loc=mono_string_to_icu_locale (cult->icu_name);
1433 mono_raise_exception ((MonoException *)mono_exception_from_name (mono_defaults.corlib, "System", "SystemException"));
1437 udest=(UChar *)g_malloc0 (sizeof(UChar)*(mono_string_length (this)+1));
1439 /* According to the docs, this might result in a longer or
1440 * shorter string than we started with...
1444 len=u_strToLower (udest, mono_string_length (this)+1,
1445 mono_string_chars (this), -1, icu_loc, &ec);
1446 if(ec==U_BUFFER_OVERFLOW_ERROR ||
1447 ec==U_STRING_NOT_TERMINATED_WARNING) {
1449 udest=(UChar *)g_malloc0 (sizeof(UChar)*(len+1));
1450 len=u_strToLower (udest, len+1, mono_string_chars (this), -1,
1454 if(U_SUCCESS (ec)) {
1455 ret=mono_string_from_utf16 ((gunichar2 *)udest);
1457 g_message (G_GNUC_PRETTY_FUNCTION ": u_strToLower error: %s",
1459 /* return something */
1467 g_message (G_GNUC_PRETTY_FUNCTION ": returning [%s]",
1468 mono_string_to_utf8 (ret));
1474 MonoString *ves_icall_System_String_InternalToUpper_Comp (MonoString *this, MonoCultureInfo *cult)
1482 MONO_ARCH_SAVE_REGS;
1485 g_message (G_GNUC_PRETTY_FUNCTION ": [%s]",
1486 mono_string_to_utf8 (this));
1490 g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", cult->lcid);
1493 icu_loc=mono_string_to_icu_locale (cult->icu_name);
1495 mono_raise_exception ((MonoException *)mono_exception_from_name (mono_defaults.corlib, "System", "SystemException"));
1499 udest=(UChar *)g_malloc0 (sizeof(UChar)*(mono_string_length (this)+1));
1501 /* According to the docs, this might result in a longer or
1502 * shorter string than we started with...
1506 len=u_strToUpper (udest, mono_string_length (this)+1,
1507 mono_string_chars (this), -1, icu_loc, &ec);
1508 if(ec==U_BUFFER_OVERFLOW_ERROR ||
1509 ec==U_STRING_NOT_TERMINATED_WARNING) {
1511 udest=(UChar *)g_malloc0 (sizeof(UChar)*(len+1));
1512 len=u_strToUpper (udest, len+1, mono_string_chars (this), -1,
1516 if(U_SUCCESS (ec)) {
1517 ret=mono_string_from_utf16 ((gunichar2 *)udest);
1519 g_message (G_GNUC_PRETTY_FUNCTION ": u_strToUpper error: %s",
1521 /* return something */
1529 g_message (G_GNUC_PRETTY_FUNCTION ": returning [%s]",
1530 mono_string_to_utf8 (ret));
1536 gunichar2 ves_icall_System_Char_InternalToUpper_Comp (gunichar2 c, MonoCultureInfo *cult)
1543 MONO_ARCH_SAVE_REGS;
1545 icu_loc=mono_string_to_icu_locale (cult->icu_name);
1547 mono_raise_exception ((MonoException *)mono_exception_from_name (mono_defaults.corlib, "System", "SystemException"));
1552 len=u_strToUpper (&udest, 1, &c, 1, icu_loc, &ec);
1554 if(U_SUCCESS (ec) && len==1) {
1557 /* return something */
1563 gunichar2 ves_icall_System_Char_InternalToLower_Comp (gunichar2 c, MonoCultureInfo *cult)
1570 MONO_ARCH_SAVE_REGS;
1572 icu_loc=mono_string_to_icu_locale (cult->icu_name);
1574 mono_raise_exception ((MonoException *)mono_exception_from_name (mono_defaults.corlib, "System", "SystemException"));
1579 len=u_strToLower (&udest, 1, &c, 1, icu_loc, &ec);
1581 if(U_SUCCESS (ec) && len==1) {
1584 /* return something */
1589 #else /* HAVE_ICU */
1590 void ves_icall_System_Globalization_CultureInfo_construct_internal_locale (MonoCultureInfo *this, MonoString *locale)
1592 MONO_ARCH_SAVE_REGS;
1594 /* Always claim "unknown locale" if we don't have ICU (only
1595 * called for non-invariant locales)
1597 mono_raise_exception((MonoException *)mono_exception_from_name(mono_defaults.corlib, "System", "ArgumentException"));
1600 void ves_icall_System_Globalization_CompareInfo_construct_compareinfo (MonoCompareInfo *comp, MonoString *locale)
1602 /* Nothing to do here */
1605 int ves_icall_System_Globalization_CompareInfo_internal_compare (MonoCompareInfo *this, MonoString *str1, gint32 off1, gint32 len1, MonoString *str2, gint32 off2, gint32 len2, gint32 options)
1607 MONO_ARCH_SAVE_REGS;
1609 /* Do a normal ascii string compare, as we only know the
1610 * invariant locale if we dont have ICU
1612 return(string_invariant_compare (str1, off1, len1, str2, off2, len2,
1616 void ves_icall_System_Globalization_CompareInfo_free_internal_collator (MonoCompareInfo *this)
1618 /* Nothing to do here */
1621 void ves_icall_System_Globalization_CompareInfo_assign_sortkey (MonoCompareInfo *this, MonoSortKey *key, MonoString *source, gint32 options)
1626 MONO_ARCH_SAVE_REGS;
1628 keylen=mono_string_length (source);
1630 arr=mono_array_new (mono_domain_get (), mono_defaults.byte_class,
1632 for(i=0; i<keylen; i++) {
1633 mono_array_set (arr, guint8, i, mono_string_chars (source)[i]);
1639 int ves_icall_System_Globalization_CompareInfo_internal_index (MonoCompareInfo *this, MonoString *source, gint32 sindex, gint32 count, MonoString *value, gint32 options, MonoBoolean first)
1641 MONO_ARCH_SAVE_REGS;
1643 return(string_invariant_indexof (source, sindex, count, value, first));
1646 int ves_icall_System_Globalization_CompareInfo_internal_index_char (MonoCompareInfo *this, MonoString *source, gint32 sindex, gint32 count, gunichar2 value, gint32 options, MonoBoolean first)
1648 MONO_ARCH_SAVE_REGS;
1650 return(string_invariant_indexof_char (source, sindex, count, value,
1654 int ves_icall_System_Threading_Thread_current_lcid (void)
1656 MONO_ARCH_SAVE_REGS;
1662 MonoString *ves_icall_System_String_InternalReplace_Str_Comp (MonoString *this, MonoString *old, MonoString *new, MonoCompareInfo *comp)
1664 MONO_ARCH_SAVE_REGS;
1666 /* Do a normal ascii string compare and replace, as we only
1667 * know the invariant locale if we dont have ICU
1669 return(string_invariant_replace (this, old, new));
1672 MonoString *ves_icall_System_String_InternalToLower_Comp (MonoString *this, MonoCultureInfo *cult)
1674 MONO_ARCH_SAVE_REGS;
1676 return(string_invariant_tolower (this));
1679 MonoString *ves_icall_System_String_InternalToUpper_Comp (MonoString *this, MonoCultureInfo *cult)
1681 MONO_ARCH_SAVE_REGS;
1683 return(string_invariant_toupper (this));
1686 gunichar2 ves_icall_System_Char_InternalToUpper_Comp (gunichar2 c, MonoCultureInfo *cult)
1688 MONO_ARCH_SAVE_REGS;
1690 return g_unichar_toupper (c);
1694 gunichar2 ves_icall_System_Char_InternalToLower_Comp (gunichar2 c, MonoCultureInfo *cult)
1696 MONO_ARCH_SAVE_REGS;
1698 return g_unichar_tolower (c);
1701 #endif /* HAVE_ICU */
1703 static gint32 string_invariant_compare_char (gunichar2 c1, gunichar2 c2,
1707 GUnicodeType c1type, c2type;
1709 c1type = g_unichar_type (c1);
1710 c2type = g_unichar_type (c2);
1712 if (options & CompareOptions_IgnoreCase) {
1713 result = (gint32) (c1type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c1) : c1) - (c2type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c2) : c2);
1714 } else if (options & CompareOptions_Ordinal) {
1715 // Rotor/ms return the full value just not -1 and 1
1716 return (gint32) c1 - c2;
1718 /* No options. Kana, symbol and spacing options don't
1719 * apply to the invariant culture.
1721 if (c1type == G_UNICODE_UPPERCASE_LETTER &&
1722 c2type == G_UNICODE_LOWERCASE_LETTER) {
1726 if (c1type == G_UNICODE_LOWERCASE_LETTER &&
1727 c2type == G_UNICODE_UPPERCASE_LETTER) {
1731 result = (gint32) c1 - c2;
1734 return ((result < 0) ? -1 : (result > 0) ? 1 : 0);
1737 static gint32 string_invariant_compare (MonoString *str1, gint32 off1,
1738 gint32 len1, MonoString *str2,
1739 gint32 off2, gint32 len2,
1742 /* c translation of C# code from old string.cs.. :) */
1755 ustr1 = mono_string_chars(str1)+off1;
1756 ustr2 = mono_string_chars(str2)+off2;
1760 for (pos = 0; pos != length; pos++) {
1761 if (pos >= len1 || pos >= len2)
1764 charcmp = string_invariant_compare_char(ustr1[pos], ustr2[pos],
1771 /* the lesser wins, so if we have looped until length we just
1772 * need to check the last char
1774 if (pos == length) {
1775 return(string_invariant_compare_char(ustr1[pos - 1],
1776 ustr2[pos - 1], options));
1779 /* Test if one of the strings has been compared to the end */
1786 } else if (pos >= len2) {
1790 /* if not, check our last char only.. (can this happen?) */
1791 return(string_invariant_compare_char(ustr1[pos], ustr2[pos], options));
1794 static MonoString *string_invariant_replace (MonoString *me,
1795 MonoString *oldValue,
1796 MonoString *newValue)
1800 gunichar2 *dest=NULL; /* shut gcc up */
1802 gunichar2 *newstr=NULL; /* shut gcc up here too */
1813 oldstr = mono_string_chars(oldValue);
1814 oldstrlen = mono_string_length(oldValue);
1816 if (NULL != newValue) {
1817 newstr = mono_string_chars(newValue);
1818 newstrlen = mono_string_length(newValue);
1822 src = mono_string_chars(me);
1823 srclen = mono_string_length(me);
1825 if (oldstrlen != newstrlen) {
1827 while (i <= srclen - oldstrlen) {
1828 if (0 == memcmp(src + i, oldstr, oldstrlen * sizeof(gunichar2))) {
1837 newsize = srclen + ((newstrlen - oldstrlen) * occurr);
1843 while (i < srclen) {
1844 if (0 == memcmp(src + i, oldstr, oldstrlen * sizeof(gunichar2))) {
1846 ret = mono_string_new_size( mono_domain_get (), newsize);
1847 dest = mono_string_chars(ret);
1848 memcpy (dest, src, i * sizeof(gunichar2));
1850 if (newstrlen > 0) {
1851 memcpy(dest + destpos, newstr, newstrlen * sizeof(gunichar2));
1852 destpos += newstrlen;
1856 } else if (ret != NULL) {
1857 dest[destpos] = src[i];
1869 static gint32 string_invariant_indexof (MonoString *source, gint32 sindex,
1870 gint32 count, MonoString *value,
1878 lencmpstr = mono_string_length(value);
1880 src = mono_string_chars(source);
1881 cmpstr = mono_string_chars(value);
1885 for(pos=sindex;pos <= sindex+count;pos++) {
1886 for(i=0;src[pos+i]==cmpstr[i];) {
1887 if(++i==lencmpstr) {
1895 for(pos=sindex-lencmpstr+1;pos>sindex-count;pos--) {
1896 if(memcmp (src+pos, cmpstr,
1897 lencmpstr*sizeof(gunichar2))==0) {
1906 static gint32 string_invariant_indexof_char (MonoString *source, gint32 sindex,
1907 gint32 count, gunichar2 value,
1913 src = mono_string_chars(source);
1915 for (pos = sindex; pos != count + sindex; pos++) {
1916 if (src [pos] == value) {
1923 for (pos = sindex; pos > sindex - count; pos--) {
1924 if (src [pos] == value)
1932 static MonoString *string_invariant_tolower (MonoString *this)
1939 ret = mono_string_new_size(mono_domain_get (),
1940 mono_string_length(this));
1942 src = mono_string_chars (this);
1943 dest = mono_string_chars (ret);
1945 for (i = 0; i < mono_string_length (this); ++i) {
1946 dest[i] = g_unichar_tolower(src[i]);
1952 static MonoString *string_invariant_toupper (MonoString *this)
1959 ret = mono_string_new_size(mono_domain_get (),
1960 mono_string_length(this));
1962 src = mono_string_chars (this);
1963 dest = mono_string_chars (ret);
1965 for (i = 0; i < mono_string_length (this); ++i) {
1966 dest[i] = g_unichar_toupper(src[i]);