2 * locales.c: Culture-sensitive handling
5 * Dick Porter (dick@ximian.com)
6 * Mohammad DAMT (mdamt@cdl2000.com)
8 * (C) 2003 Ximian, Inc.
9 * (C) 2003 PT Cakram Datalingga Duaribu http://www.cdl2000.com
16 #include <mono/metadata/debug-helpers.h>
17 #include <mono/metadata/object.h>
18 #include <mono/metadata/appdomain.h>
19 #include <mono/metadata/exception.h>
20 #include <mono/metadata/monitor.h>
21 #include <mono/metadata/locales.h>
22 #include <mono/metadata/culture-info.h>
23 #include <mono/metadata/culture-info-tables.h>
30 static gint32 string_invariant_compare_char (gunichar2 c1, gunichar2 c2,
32 static gint32 string_invariant_compare (MonoString *str1, gint32 off1,
33 gint32 len1, MonoString *str2,
34 gint32 off2, gint32 len2,
36 static MonoString *string_invariant_replace (MonoString *me,
38 MonoString *newValue);
39 static gint32 string_invariant_indexof (MonoString *source, gint32 sindex,
40 gint32 count, MonoString *value,
42 static gint32 string_invariant_indexof_char (MonoString *source, gint32 sindex,
43 gint32 count, gunichar2 value,
46 static const CultureInfoEntry* culture_info_entry_from_lcid (int lcid);
49 culture_lcid_locator (const void *a, const void *b)
51 const CultureInfoEntry *aa = a;
52 const CultureInfoEntry *bb = b;
54 return (aa->lcid - bb->lcid);
58 culture_name_locator (const void *a, const void *b)
61 const CultureInfoNameEntry *bb = b;
64 ret = strcmp (aa, idx2string (bb->name));
70 create_group_sizes_array (const gint *gs, gint ml)
75 for (i = 0; i < ml; i++) {
81 ret = mono_array_new (mono_domain_get (),
82 mono_get_int32_class (), len);
84 for(i = 0; i < len; i++)
85 mono_array_set (ret, gint32, i, gs [i]);
91 create_names_array_idx (const guint16 *names, int ml)
100 domain = mono_domain_get ();
102 for (i = 0; i < ml; i++) {
108 ret = mono_array_new (mono_domain_get (), mono_get_string_class (), len);
110 for(i = 0; i < len; i++)
111 mono_array_set (ret, MonoString *, i, mono_string_new (domain, idx2string (names [i])));
117 ves_icall_System_Globalization_CultureInfo_construct_datetime_format (MonoCultureInfo *this)
120 MonoDateTimeFormatInfo *datetime;
121 const DateTimeFormatEntry *dfe;
125 g_assert (this->datetime_index >= 0);
127 datetime = this->datetime_format;
128 dfe = &datetime_format_entries [this->datetime_index];
130 domain = mono_domain_get ();
132 datetime->AbbreviatedDayNames = create_names_array_idx (dfe->abbreviated_day_names,
134 datetime->AbbreviatedMonthNames = create_names_array_idx (dfe->abbreviated_month_names,
136 datetime->AMDesignator = mono_string_new (domain, idx2string (dfe->am_designator));
137 datetime->CalendarWeekRule = dfe->calendar_week_rule;
138 datetime->DateSeparator = mono_string_new (domain, idx2string (dfe->date_separator));
139 datetime->DayNames = create_names_array_idx (dfe->day_names, NUM_DAYS);
140 datetime->FirstDayOfWeek = dfe->first_day_of_week;
141 datetime->FullDateTimePattern = mono_string_new (domain, idx2string (dfe->full_date_time_pattern));
142 datetime->LongDatePattern = mono_string_new (domain, idx2string (dfe->long_date_pattern));
143 datetime->LongTimePattern = mono_string_new (domain, idx2string (dfe->long_time_pattern));
144 datetime->MonthDayPattern = mono_string_new (domain, idx2string (dfe->month_day_pattern));
145 datetime->MonthNames = create_names_array_idx (dfe->month_names, NUM_MONTHS);
146 datetime->PMDesignator = mono_string_new (domain, idx2string (dfe->pm_designator));
147 datetime->ShortDatePattern = mono_string_new (domain, idx2string (dfe->short_date_pattern));
148 datetime->ShortTimePattern = mono_string_new (domain, idx2string (dfe->short_time_pattern));
149 datetime->TimeSeparator = mono_string_new (domain, idx2string (dfe->time_separator));
150 datetime->YearMonthPattern = mono_string_new (domain, idx2string (dfe->year_month_pattern));
151 datetime->ShortDatePatterns = create_names_array_idx (dfe->short_date_patterns,
152 NUM_SHORT_DATE_PATTERNS);
153 datetime->LongDatePatterns = create_names_array_idx (dfe->long_date_patterns,
154 NUM_LONG_DATE_PATTERNS);
155 datetime->ShortTimePatterns = create_names_array_idx (dfe->short_time_patterns,
156 NUM_SHORT_TIME_PATTERNS);
157 datetime->LongTimePatterns = create_names_array_idx (dfe->long_time_patterns,
158 NUM_LONG_TIME_PATTERNS);
163 ves_icall_System_Globalization_CultureInfo_construct_number_format (MonoCultureInfo *this)
166 MonoNumberFormatInfo *number;
167 const NumberFormatEntry *nfe;
171 g_assert (this->number_format != 0);
173 number = this->number_format;
174 nfe = &number_format_entries [this->number_index];
176 domain = mono_domain_get ();
178 number->currencyDecimalDigits = nfe->currency_decimal_digits;
179 number->currencyDecimalSeparator = mono_string_new (domain,
180 idx2string (nfe->currency_decimal_separator));
181 number->currencyGroupSeparator = mono_string_new (domain,
182 idx2string (nfe->currency_group_separator));
183 number->currencyGroupSizes = create_group_sizes_array (nfe->currency_group_sizes,
185 number->currencyNegativePattern = nfe->currency_negative_pattern;
186 number->currencyPositivePattern = nfe->currency_positive_pattern;
187 number->currencySymbol = mono_string_new (domain, idx2string (nfe->currency_symbol));
188 number->naNSymbol = mono_string_new (domain, idx2string (nfe->nan_symbol));
189 number->negativeInfinitySymbol = mono_string_new (domain,
190 idx2string (nfe->negative_infinity_symbol));
191 number->negativeSign = mono_string_new (domain, idx2string (nfe->negative_sign));
192 number->numberDecimalDigits = nfe->number_decimal_digits;
193 number->numberDecimalSeparator = mono_string_new (domain,
194 idx2string (nfe->number_decimal_separator));
195 number->numberGroupSeparator = mono_string_new (domain, idx2string (nfe->number_group_separator));
196 number->numberGroupSizes = create_group_sizes_array (nfe->number_group_sizes,
198 number->numberNegativePattern = nfe->number_negative_pattern;
199 number->percentDecimalDigits = nfe->percent_decimal_digits;
200 number->percentDecimalSeparator = mono_string_new (domain,
201 idx2string (nfe->percent_decimal_separator));
202 number->percentGroupSeparator = mono_string_new (domain,
203 idx2string (nfe->percent_group_separator));
204 number->percentGroupSizes = create_group_sizes_array (nfe->percent_group_sizes,
206 number->percentNegativePattern = nfe->percent_negative_pattern;
207 number->percentPositivePattern = nfe->percent_positive_pattern;
208 number->percentSymbol = mono_string_new (domain, idx2string (nfe->percent_symbol));
209 number->perMilleSymbol = mono_string_new (domain, idx2string (nfe->per_mille_symbol));
210 number->positiveInfinitySymbol = mono_string_new (domain,
211 idx2string (nfe->positive_infinity_symbol));
212 number->positiveSign = mono_string_new (domain, idx2string (nfe->positive_sign));
216 construct_culture (MonoCultureInfo *this, const CultureInfoEntry *ci)
218 MonoDomain *domain = mono_domain_get ();
220 this->lcid = ci->lcid;
221 this->name = mono_string_new (domain, idx2string (ci->name));
222 this->icu_name = mono_string_new (domain, idx2string (ci->icu_name));
223 this->displayname = mono_string_new (domain, idx2string (ci->displayname));
224 this->englishname = mono_string_new (domain, idx2string (ci->englishname));
225 this->nativename = mono_string_new (domain, idx2string (ci->nativename));
226 this->win3lang = mono_string_new (domain, idx2string (ci->win3lang));
227 this->iso3lang = mono_string_new (domain, idx2string (ci->iso3lang));
228 this->iso2lang = mono_string_new (domain, idx2string (ci->iso2lang));
229 this->parent_lcid = ci->parent_lcid;
230 this->specific_lcid = ci->specific_lcid;
231 this->datetime_index = ci->datetime_format_index;
232 this->number_index = ci->number_format_index;
233 this->calendar_data = ci->calendar_data;
239 construct_culture_from_specific_name (MonoCultureInfo *ci, gchar *name)
241 const CultureInfoEntry *entry;
242 const CultureInfoNameEntry *ne;
246 ne = bsearch (name, culture_name_entries, NUM_CULTURE_ENTRIES,
247 sizeof (CultureInfoNameEntry), culture_name_locator);
252 entry = &culture_entries [ne->culture_entry_index];
254 /* try avoiding another lookup, often the culture is its own specific culture */
255 if (entry->lcid != entry->specific_lcid)
256 entry = culture_info_entry_from_lcid (entry->specific_lcid);
258 return construct_culture (ci, entry);
261 static const CultureInfoEntry*
262 culture_info_entry_from_lcid (int lcid)
264 const CultureInfoEntry *ci;
265 CultureInfoEntry key;
268 ci = bsearch (&key, culture_entries, NUM_CULTURE_ENTRIES, sizeof (CultureInfoEntry), culture_lcid_locator);
274 * The following two methods are modified from the ICU source code. (http://oss.software.ibm.com/icu)
275 * Copyright (c) 1995-2003 International Business Machines Corporation and others
276 * All rights reserved.
279 get_posix_locale (void)
281 const gchar* posix_locale = NULL;
283 posix_locale = g_getenv("LC_ALL");
284 if (posix_locale == 0) {
285 posix_locale = g_getenv("LANG");
286 if (posix_locale == 0) {
287 posix_locale = setlocale(LC_ALL, NULL);
291 if (posix_locale == NULL)
294 if ((strcmp ("C", posix_locale) == 0) || (strchr (posix_locale, ' ') != NULL)
295 || (strchr (posix_locale, '/') != NULL)) {
297 * HPUX returns 'C C C C C C C'
298 * Solaris can return /en_US/C/C/C/C/C on the second try.
299 * Maybe we got some garbage.
304 return g_strdup (posix_locale);
308 get_current_locale_name (void)
311 gchar *corrected = NULL;
315 #ifdef PLATFORM_WIN32
316 locale = g_win32_getlocale ();
318 locale = get_posix_locale ();
324 if ((p = strchr (locale, '.')) != NULL) {
325 /* assume new locale can't be larger than old one? */
326 corrected = malloc (strlen (locale));
327 strncpy (corrected, locale, p - locale);
328 corrected [p - locale] = 0;
330 /* do not copy after the @ */
331 if ((p = strchr (corrected, '@')) != NULL)
332 corrected [p - corrected] = 0;
335 /* Note that we scan the *uncorrected* ID. */
336 if ((p = strrchr (locale, '@')) != NULL) {
339 * In Mono we dont handle the '@' modifier because we do
340 * not have any cultures that use it. We just trim it
341 * off of the end of the name.
344 if (corrected == NULL) {
345 corrected = malloc (strlen (locale));
346 strncpy (corrected, locale, p - locale);
347 corrected [p - locale] = 0;
351 if (corrected == NULL)
356 if ((c = strchr (corrected, '_')) != NULL)
359 g_strdown (corrected);
365 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_current_locale (MonoCultureInfo *ci)
372 locale = get_current_locale_name ();
376 ret = construct_culture_from_specific_name (ci, locale);
383 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_lcid (MonoCultureInfo *this,
386 const CultureInfoEntry *ci;
390 ci = culture_info_entry_from_lcid (lcid);
394 return construct_culture (this, ci);
398 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_name (MonoCultureInfo *this,
401 const CultureInfoNameEntry *ne;
406 n = mono_string_to_utf8 (name);
407 ne = bsearch (n, culture_name_entries, NUM_CULTURE_ENTRIES,
408 sizeof (CultureInfoNameEntry), culture_name_locator);
411 g_print ("ne (%s) is null\n", n);
417 return construct_culture (this, &culture_entries [ne->culture_entry_index]);
421 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_specific_name (MonoCultureInfo *ci,
429 locale = mono_string_to_utf8 (name);
430 ret = construct_culture_from_specific_name (ci, locale);
437 ves_icall_System_Globalization_CultureInfo_internal_get_cultures (MonoBoolean neutral,
438 MonoBoolean specific, MonoBoolean installed)
442 MonoCultureInfo *culture;
444 const CultureInfoEntry *ci;
450 domain = mono_domain_get ();
453 for (i = 0; i < NUM_CULTURE_ENTRIES; i++) {
454 ci = &culture_entries [i];
455 is_neutral = ((ci->lcid & 0xff00) == 0 || ci->specific_lcid == 0);
456 if ((neutral && is_neutral) || (specific && !is_neutral))
460 class = mono_class_from_name (mono_get_corlib (),
461 "System.Globalization", "CultureInfo");
462 ret = mono_array_new (domain, class, len);
468 for (i = 0; i < NUM_CULTURE_ENTRIES; i++) {
469 ci = &culture_entries [i];
470 is_neutral = ((ci->lcid & 0xff00) == 0 || ci->specific_lcid == 0);
471 if ((neutral && is_neutral) || (specific && !is_neutral)) {
472 culture = (MonoCultureInfo *) mono_object_new (domain, class);
473 mono_runtime_object_init ((MonoObject *) culture);
474 construct_culture (culture, ci);
475 mono_array_set (ret, MonoCultureInfo *, len++, culture);
483 * Set is_neutral and return TRUE if the culture is found. If it is not found return FALSE.
486 ves_icall_System_Globalization_CultureInfo_internal_is_lcid_neutral (gint lcid, MonoBoolean *is_neutral)
488 const CultureInfoEntry *entry;
492 entry = culture_info_entry_from_lcid (lcid);
497 *is_neutral = (entry->specific_lcid == 0);
504 #include <unicode/utypes.h>
505 #include <unicode/ustring.h>
506 #include <unicode/ures.h>
507 #include <unicode/ucol.h>
508 #include <unicode/usearch.h>
510 static MonoString *monostring_from_resource_index (const UResourceBundle *bundle, int32_t idx)
517 res_str=(gunichar2 *)ures_getStringByIndex (bundle, idx, &res_strlen,
523 return(mono_string_from_utf16 (res_str));
526 static UResourceBundle *open_subbundle (const UResourceBundle *bundle,
527 const char *name, int32_t req_count)
529 UResourceBundle *subbundle;
534 subbundle=ures_getByKey (bundle, name, NULL, &ec);
536 /* Couldn't find the subbundle */
540 count=ures_countArrayItems (bundle, name, &ec);
542 /* Couldn't count the subbundle */
543 ures_close (subbundle);
547 if(count!=req_count) {
549 ures_close (subbundle);
556 static MonoArray *build_array (const UResourceBundle *bundle,
557 const char *resname, int32_t req_count)
560 UResourceBundle *subbundle;
563 subbundle=open_subbundle (bundle, resname, req_count);
564 if(subbundle!=NULL) {
565 arr=mono_array_new(mono_domain_get (),
566 mono_get_string_class (), req_count);
568 for(i=0; i<req_count; i++) {
569 mono_array_set(arr, MonoString *, i, monostring_from_resource_index (subbundle, i));
572 ures_close (subbundle);
578 static MonoDateTimeFormatInfo *create_DateTimeFormat (const char *locale)
580 MonoDateTimeFormatInfo *new_dtf;
582 UResourceBundle *bundle, *subbundle;
585 class=mono_class_from_name (mono_get_corlib (),
586 "System.Globalization",
587 "DateTimeFormatInfo");
588 new_dtf=(MonoDateTimeFormatInfo *)mono_object_new (mono_domain_get (),
590 mono_runtime_object_init ((MonoObject *)new_dtf);
594 bundle=ures_open (NULL, locale, &ec);
600 subbundle=open_subbundle (bundle, "AmPmMarkers", 2);
601 if(subbundle!=NULL) {
602 new_dtf->AMDesignator=monostring_from_resource_index (subbundle, 0);
603 new_dtf->PMDesignator=monostring_from_resource_index (subbundle, 1);
605 ures_close (subbundle);
608 /* Date/Time patterns. Don't set FullDateTimePattern. As it
609 * seems to always default to LongDatePattern + " " +
610 * LongTimePattern, let the property accessor deal with it.
612 subbundle=open_subbundle (bundle, "DateTimePatterns", 9);
613 if(subbundle!=NULL) {
614 new_dtf->ShortDatePattern=monostring_from_resource_index (subbundle, 7);
615 new_dtf->LongDatePattern=monostring_from_resource_index (subbundle, 5);
616 new_dtf->ShortTimePattern=monostring_from_resource_index (subbundle, 3);
617 new_dtf->LongTimePattern=monostring_from_resource_index (subbundle, 2);
619 /* RFC1123Pattern, SortableDateTimePattern and
620 * UniversalSortableDateTimePattern all seem to be
621 * constant, and all the same as the invariant default
625 ures_close (subbundle);
629 /* Not sure what to do with these yet, so leave them set to
630 * the invariant default
632 set_field_string (new_dtf, "_DateSeparator", str);
633 set_field_string (new_dtf, "_TimeSeparator", str);
634 set_field_string (new_dtf, "_MonthDayPattern", str);
635 set_field_string (new_dtf, "_YearMonthPattern", str);
638 /* Day names. Luckily both ICU and .net start Sunday at index 0 */
639 new_dtf->DayNames=build_array (bundle, "DayNames", 7);
641 /* Abbreviated day names */
642 new_dtf->AbbreviatedDayNames=build_array (bundle, "DayAbbreviations",
646 new_dtf->MonthNames=build_array (bundle, "MonthNames", 12);
648 /* Abbreviated month names */
649 new_dtf->AbbreviatedMonthNames=build_array (bundle,
650 "MonthAbbreviations", 12);
652 /* TODO: DayOfWeek _FirstDayOfWeek, Calendar _Calendar, CalendarWeekRule _CalendarWeekRule */
659 static MonoNumberFormatInfo *create_NumberFormat (const char *locale)
661 MonoNumberFormatInfo *new_nf;
663 MonoMethodDesc* methodDesc;
665 UResourceBundle *bundle, *subbundle, *table_entries;
668 static char country [7]; /* FIXME */
669 const UChar *res_str;
672 class=mono_class_from_name (mono_get_corlib (),
673 "System.Globalization",
675 new_nf=(MonoNumberFormatInfo *)mono_object_new (mono_domain_get (),
677 mono_runtime_object_init ((MonoObject *)new_nf);
681 bundle=ures_open (NULL, locale, &ec);
686 /* Number Elements */
688 subbundle=ures_getByKey (bundle, "NumberElements", NULL, &ec);
690 /* Couldn't find the subbundle */
694 count=ures_countArrayItems (bundle, "NumberElements", &ec);
696 /* Couldn't count the subbundle */
697 ures_close (subbundle);
701 if(subbundle!=NULL) {
702 new_nf->numberDecimalSeparator=monostring_from_resource_index (subbundle, 0);
703 new_nf->numberGroupSeparator=monostring_from_resource_index (subbundle, 1);
704 new_nf->percentDecimalSeparator=monostring_from_resource_index (subbundle, 0);
705 new_nf->percentGroupSeparator=monostring_from_resource_index (subbundle, 1);
706 new_nf->percentSymbol=monostring_from_resource_index (subbundle, 3);
707 new_nf->zeroPattern=monostring_from_resource_index (subbundle, 4);
708 new_nf->digitPattern=monostring_from_resource_index (subbundle, 5);
709 new_nf->negativeSign=monostring_from_resource_index (subbundle, 6);
710 new_nf->perMilleSymbol=monostring_from_resource_index (subbundle, 8);
711 new_nf->positiveInfinitySymbol=monostring_from_resource_index (subbundle, 9);
712 /* we dont have this in CLDR, so copy it from positiveInfinitySymbol */
713 new_nf->negativeInfinitySymbol=monostring_from_resource_index (subbundle, 9);
714 new_nf->naNSymbol=monostring_from_resource_index (subbundle, 10);
715 new_nf->currencyDecimalSeparator=monostring_from_resource_index (subbundle, 0);
716 new_nf->currencyGroupSeparator=monostring_from_resource_index (subbundle, 1);
718 ures_close (subbundle);
721 /* get country name */
723 uloc_getCountry (locale, country, sizeof (country), &ec);
724 if (U_SUCCESS (ec)) {
726 /* find country name in root.CurrencyMap */
727 subbundle = ures_getByKey (bundle, "CurrencyMap", NULL, &ec);
728 if (U_SUCCESS (ec)) {
730 /* get currency id for specified country */
731 table_entries = ures_getByKey (subbundle, country, NULL, &ec);
732 if (U_SUCCESS (ec)) {
733 ures_close (subbundle);
736 res_str = ures_getStringByIndex (
737 table_entries, 0, &res_strlen, &ec);
739 /* now we have currency id string */
740 ures_close (table_entries);
742 u_UCharsToChars (res_str, country,
746 /* find currency string in locale data */
747 subbundle = ures_getByKey (
748 bundle, "Currencies",
751 if (U_SUCCESS (ec)) {
753 /* find currency symbol under specified currency id */
754 table_entries = ures_getByKey (subbundle, country, NULL, &ec);
755 if (U_SUCCESS (ec)) {
756 /* get the first string only,
757 * the second is international currency symbol (not used)*/
758 new_nf->currencySymbol=monostring_from_resource_index (table_entries, 0);
759 ures_close (table_entries);
761 ures_close (subbundle);
769 subbundle=open_subbundle (bundle, "NumberPatterns", 4);
770 if(subbundle!=NULL) {
771 new_nf->decimalFormats=monostring_from_resource_index (subbundle, 0);
772 new_nf->currencyFormats=monostring_from_resource_index (subbundle, 1);
773 new_nf->percentFormats=monostring_from_resource_index (subbundle, 2);
774 ures_close (subbundle);
776 /* calls InitPatterns to parse the patterns
778 methodDesc = mono_method_desc_new (
779 "System.Globalization.NumberFormatInfo:InitPatterns()",
781 method = mono_method_desc_search_in_class (methodDesc, class);
783 mono_runtime_invoke (method, new_nf, NULL, NULL);
785 g_warning (G_GNUC_PRETTY_FUNCTION ": Runtime mismatch with class lib! (Looking for System.Globalization.NumberFormatInfo:InitPatterns())");
794 static char *mono_string_to_icu_locale (MonoString *locale)
797 char *passed_locale, *icu_locale=NULL;
798 int32_t loc_len, ret;
800 passed_locale=mono_string_to_utf8 (locale);
803 ret=uloc_getName (passed_locale, NULL, 0, &ec);
804 if(ec==U_BUFFER_OVERFLOW_ERROR) {
807 icu_locale=(char *)g_malloc0 (sizeof(char)*loc_len);
808 ret=uloc_getName (passed_locale, icu_locale, loc_len, &ec);
810 g_free (passed_locale);
815 void ves_icall_System_Globalization_CultureInfo_construct_internal_locale (MonoCultureInfo *this, MonoString *locale)
821 int32_t str_len, ret;
825 icu_locale=mono_string_to_icu_locale (locale);
826 if(icu_locale==NULL) {
827 /* Something went wrong */
828 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
832 /* Fill in the static fields */
834 /* TODO: Calendar, InstalledUICulture, OptionalCalendars,
838 str_len=256; /* Should be big enough for anything */
839 str=(char *)g_malloc0 (sizeof(char)*str_len);
840 ustr=(UChar *)g_malloc0 (sizeof(UChar)*str_len);
844 ret=uloc_getDisplayName (icu_locale, "en", ustr, str_len, &ec);
845 if(U_SUCCESS (ec) && ret<str_len) {
846 this->englishname=mono_string_from_utf16 ((gunichar2 *)ustr);
849 ret=uloc_getDisplayName (icu_locale, uloc_getDefault (), ustr, str_len,
851 if(U_SUCCESS (ec) && ret<str_len) {
852 this->displayname=mono_string_from_utf16 ((gunichar2 *)ustr);
855 ret=uloc_getDisplayName (icu_locale, icu_locale, ustr, str_len, &ec);
856 if(U_SUCCESS (ec) && ret<str_len) {
857 this->nativename=mono_string_from_utf16 ((gunichar2 *)ustr);
860 this->iso3lang=mono_string_new_wrapper (uloc_getISO3Language (icu_locale));
862 ret=uloc_getLanguage (icu_locale, str, str_len, &ec);
863 if(U_SUCCESS (ec) && ret<str_len) {
864 this->iso2lang=mono_string_new_wrapper (str);
867 this->datetime_format=create_DateTimeFormat (icu_locale);
868 this->number_format=create_NumberFormat (icu_locale);
875 void ves_icall_System_Globalization_CompareInfo_construct_compareinfo (MonoCompareInfo *comp, MonoString *locale)
884 g_message (G_GNUC_PRETTY_FUNCTION ": Constructing collator for locale [%s]", mono_string_to_utf8 (locale));
887 icu_locale=mono_string_to_icu_locale (locale);
888 if(icu_locale==NULL) {
889 /* Something went wrong */
890 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
895 coll=ucol_open (icu_locale, &ec);
897 comp->ICU_collator=coll;
899 comp->ICU_collator=NULL;
905 /* Set up the collator to reflect the options required. Some of these
906 * options clash, as they adjust the collator strength level. Try to
907 * make later checks reduce the strength level, and attempt to take
908 * previous options into account.
910 * Don't bother to check the error returns when setting the
911 * attributes, as a failure here is hardly grounds to error out.
913 static void set_collator_options (UCollator *coll, gint32 options)
915 UErrorCode ec=U_ZERO_ERROR;
917 /* Set up the defaults */
918 ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE,
920 ucol_setAttribute (coll, UCOL_CASE_LEVEL, UCOL_OFF, &ec);
922 /* Do this first so other options will override the quaternary
923 * level strength setting if necessary
925 if(!(options & CompareOptions_IgnoreKanaType)) {
926 ucol_setAttribute (coll, UCOL_HIRAGANA_QUATERNARY_MODE,
928 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_QUATERNARY, &ec);
931 /* Word sort, the default */
932 if(!(options & CompareOptions_StringSort)) {
933 ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING,
935 /* Tertiary strength is the default, but it might have
936 * been set to quaternary above. (We don't want that
937 * here, because that will order all the punctuation
938 * first instead of just ignoring it.)
940 * Unfortunately, tertiary strength with
941 * ALTERNATE_HANDLING==SHIFTED means that '/' and '@'
942 * compare to equal, which has the nasty side effect
943 * of killing mcs :-( (We can't specify a
944 * culture-insensitive compare, because
945 * String.StartsWith doesn't have that option.)
947 * ALTERNATE_HANDLING==SHIFTED is needed to accomplish
948 * the word-sorting-ignoring-punctuation feature. So
949 * we have to live with the slightly mis-ordered
950 * punctuation and a working mcs...
952 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_QUATERNARY, &ec);
955 if(options & CompareOptions_IgnoreCase) {
956 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_SECONDARY, &ec);
957 ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE, &ec);
960 if(options & CompareOptions_IgnoreWidth) {
961 /* Kana width is a tertiary strength difference. This
962 * will totally break the !IgnoreKanaType option
964 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_SECONDARY, &ec);
967 if(options & CompareOptions_IgnoreNonSpace) {
968 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_PRIMARY, &ec);
969 /* We can still compare case even when just checking
972 if(!(options & CompareOptions_IgnoreCase) ||
973 !(options & CompareOptions_IgnoreWidth)) {
974 /* Not sure if CASE_LEVEL handles kana width
976 ucol_setAttribute (coll, UCOL_CASE_LEVEL, UCOL_ON,
981 if(options & CompareOptions_IgnoreSymbols) {
982 /* Don't know what to do here */
985 if(options == CompareOptions_Ordinal) {
986 /* This one is handled elsewhere */
990 gint32 ves_icall_System_Globalization_CompareInfo_internal_compare (MonoCompareInfo *this, MonoString *str1, gint32 off1, gint32 len1, MonoString *str2, gint32 off2, gint32 len2, gint32 options)
993 UCollationResult result;
998 g_message (G_GNUC_PRETTY_FUNCTION ": Comparing [%s] and [%s]", mono_string_to_utf8 (str1), mono_string_to_utf8 (str2));
1001 coll=this->ICU_collator;
1004 g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", this->lcid);
1007 if(coll==NULL || this->lcid==0x007F ||
1008 options & CompareOptions_Ordinal) {
1010 g_message (G_GNUC_PRETTY_FUNCTION ": No collator or invariant, using shortcut");
1013 return(string_invariant_compare (str1, off1, len1, str2, off2,
1017 if (!mono_monitor_enter ((MonoObject *)this))
1020 set_collator_options (coll, options);
1022 result=ucol_strcoll (coll, mono_string_chars (str1)+off1, len1,
1023 mono_string_chars (str2)+off2, len2);
1025 mono_monitor_exit ((MonoObject *)this);
1028 g_message (G_GNUC_PRETTY_FUNCTION ": Comparison of [%s] and [%s] returning %d", mono_string_to_utf8 (str1), mono_string_to_utf8 (str2), result);
1034 void ves_icall_System_Globalization_CompareInfo_free_internal_collator (MonoCompareInfo *this)
1038 MONO_ARCH_SAVE_REGS;
1040 coll=this->ICU_collator;
1046 void ves_icall_System_Globalization_CompareInfo_assign_sortkey (MonoCompareInfo *this, MonoSortKey *key, MonoString *source, gint32 options)
1053 MONO_ARCH_SAVE_REGS;
1055 coll=this->ICU_collator;
1057 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
1061 if (!mono_monitor_enter ((MonoObject *)this))
1064 set_collator_options (coll, options);
1066 keylen=ucol_getSortKey (coll, mono_string_chars (source),
1067 mono_string_length (source), NULL, 0);
1068 keybuf=g_malloc (sizeof(char)* keylen);
1069 ucol_getSortKey (coll, mono_string_chars (source),
1070 mono_string_length (source), keybuf, keylen);
1072 mono_monitor_exit ((MonoObject *)this);
1074 arr=mono_array_new (mono_domain_get (), mono_get_byte_class (),
1076 for(i=0; i<keylen; i++) {
1077 mono_array_set (arr, guint8, i, keybuf[i]);
1085 int ves_icall_System_Globalization_CompareInfo_internal_index (MonoCompareInfo *this, MonoString *source, gint32 sindex, gint32 count, MonoString *value, gint32 options, MonoBoolean first)
1090 UStringSearch *search;
1093 MONO_ARCH_SAVE_REGS;
1096 g_message (G_GNUC_PRETTY_FUNCTION ": Finding %s [%s] in [%s] (sindex %d,count %d)", first?"first":"last", mono_string_to_utf8 (value), mono_string_to_utf8 (source), sindex, count);
1099 coll=this->ICU_collator;
1102 g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", this->lcid);
1105 if(coll==NULL || this->lcid==0x007F ||
1106 options & CompareOptions_Ordinal) {
1108 g_message (G_GNUC_PRETTY_FUNCTION ": No collator or invariant, using shortcut");
1111 return(string_invariant_indexof (source, sindex, count, value,
1115 usrcstr=g_malloc0 (sizeof(UChar)*(count+1));
1117 memcpy (usrcstr, mono_string_chars (source)+sindex,
1118 sizeof(UChar)*count);
1120 memcpy (usrcstr, mono_string_chars (source)+sindex-count+1,
1121 sizeof(UChar)*count);
1124 if (!mono_monitor_enter ((MonoObject *)this))
1129 /* Need to set the collator to a fairly weak level, so that it
1130 * treats characters that can be written differently as
1131 * identical (eg "ß" and "ss", "æ" and "ae" or "ä" etc.) Note
1132 * that this means that the search string and the original
1133 * text might have differing lengths.
1135 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_PRIMARY, &ec);
1137 /* Still notice case differences though (normally a tertiary
1140 ucol_setAttribute (coll, UCOL_CASE_LEVEL, UCOL_ON, &ec);
1142 /* Don't ignore some codepoints */
1143 ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE,
1146 search=usearch_openFromCollator (mono_string_chars (value),
1147 mono_string_length (value),
1148 usrcstr, count, coll, NULL, &ec);
1149 if(U_SUCCESS (ec)) {
1151 pos=usearch_first (search, &ec);
1153 pos=usearch_last (search, &ec);
1156 while (pos!=USEARCH_DONE) {
1159 UCollationResult uret;
1162 g_message (G_GNUC_PRETTY_FUNCTION
1163 ": Got potential match at %d (sindex %d) len %d", pos, sindex, usearch_getMatchedLength (search));
1166 /* ICU usearch currently ignores most of the
1167 * collator attributes :-(
1169 * Check the returned match to see if it
1170 * really does match properly...
1172 match_len = usearch_getMatchedLength (search);
1173 match=(UChar *)g_malloc0 (sizeof(UChar) * (match_len + 1));
1174 usearch_getMatchedText (search, match, match_len, &ec);
1176 uret = ucol_strcoll (coll, match, match_len,
1177 mono_string_chars (value),
1178 mono_string_length (value));
1181 if (uret == UCOL_EQUAL) {
1182 /* OK, we really did get a match */
1184 g_message (G_GNUC_PRETTY_FUNCTION
1185 ": Got match at %d len %d", pos,
1193 pos+=(sindex-count+1);
1200 /* False alarm, keep looking */
1202 pos=usearch_next (search, &ec);
1204 pos=usearch_previous (search, &ec);
1208 g_message (G_GNUC_PRETTY_FUNCTION ": usearch_open error: %s",
1212 usearch_close (search);
1214 mono_monitor_exit ((MonoObject *)this);
1221 int ves_icall_System_Globalization_CompareInfo_internal_index_char (MonoCompareInfo *this, MonoString *source, gint32 sindex, gint32 count, gunichar2 value, gint32 options, MonoBoolean first)
1224 UChar *usrcstr, uvalstr[2]={0, 0};
1226 UStringSearch *search;
1229 MONO_ARCH_SAVE_REGS;
1232 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);
1235 coll=this->ICU_collator;
1238 g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", this->lcid);
1241 if(coll==NULL || this->lcid==0x007F ||
1242 options & CompareOptions_Ordinal) {
1244 g_message (G_GNUC_PRETTY_FUNCTION ": No collator or invariant, using shortcut");
1247 return(string_invariant_indexof_char (source, sindex, count,
1251 usrcstr=g_malloc0 (sizeof(UChar)*(count+1));
1253 memcpy (usrcstr, mono_string_chars (source)+sindex,
1254 sizeof(UChar)*count);
1256 memcpy (usrcstr, mono_string_chars (source)+sindex-count+1,
1257 sizeof(UChar)*count);
1261 if (!mono_monitor_enter ((MonoObject *)this))
1266 /* Need to set the collator to a fairly weak level, so that it
1267 * treats characters that can be written differently as
1268 * identical (eg "ß" and "ss", "æ" and "ae" or "ä" etc.) Note
1269 * that this means that the search string and the original
1270 * text might have differing lengths.
1272 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_PRIMARY, &ec);
1274 /* Still notice case differences though (normally a tertiary
1277 ucol_setAttribute (coll, UCOL_CASE_LEVEL, UCOL_ON, &ec);
1279 /* Don't ignore some codepoints */
1280 ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE,
1283 search=usearch_openFromCollator (uvalstr, 1, usrcstr, count, coll,
1285 if(U_SUCCESS (ec)) {
1287 pos=usearch_first (search, &ec);
1289 pos=usearch_last (search, &ec);
1292 while (pos!=USEARCH_DONE) {
1295 UCollationResult uret;
1298 g_message (G_GNUC_PRETTY_FUNCTION
1299 ": Got potential match at %d (sindex %d) len %d", pos, sindex, usearch_getMatchedLength (search));
1302 /* ICU usearch currently ignores most of the
1303 * collator attributes :-(
1305 * Check the returned match to see if it
1306 * really does match properly...
1308 match_len = usearch_getMatchedLength (search);
1309 match=(UChar *)g_malloc0 (sizeof(UChar) * (match_len + 1));
1310 usearch_getMatchedText (search, match, match_len, &ec);
1312 uret = ucol_strcoll (coll, match, match_len, uvalstr,
1316 if (uret == UCOL_EQUAL) {
1317 /* OK, we really did get a match */
1319 g_message (G_GNUC_PRETTY_FUNCTION
1320 ": Got match at %d len %d", pos,
1328 pos+=(sindex-count+1);
1335 /* False alarm, keep looking */
1337 pos=usearch_next (search, &ec);
1339 pos=usearch_previous (search, &ec);
1343 g_message (G_GNUC_PRETTY_FUNCTION ": usearch_open error: %s",
1347 usearch_close (search);
1349 mono_monitor_exit ((MonoObject *)this);
1356 int ves_icall_System_Threading_Thread_current_lcid (void)
1358 MONO_ARCH_SAVE_REGS;
1360 return(uloc_getLCID (uloc_getDefault ()));
1363 MonoString *ves_icall_System_String_InternalReplace_Str_Comp (MonoString *this, MonoString *old, MonoString *new, MonoCompareInfo *comp)
1365 MonoString *ret=NULL;
1368 UStringSearch *search;
1370 MONO_ARCH_SAVE_REGS;
1373 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));
1376 coll=comp->ICU_collator;
1379 g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", comp->lcid);
1382 if(coll==NULL || comp->lcid==0x007F) {
1384 g_message (G_GNUC_PRETTY_FUNCTION ": No collator or invariant, using shortcut");
1387 return(string_invariant_replace (this, old, new));
1390 if (!mono_monitor_enter ((MonoObject *)comp))
1395 /* Need to set the collator to a fairly weak level, so that it
1396 * treats characters that can be written differently as
1397 * identical (eg "ß" and "ss", "æ" and "ae" or "ä" etc.) Note
1398 * that this means that the search string and the original
1399 * text might have differing lengths.
1401 ucol_setAttribute (coll, UCOL_STRENGTH, UCOL_PRIMARY, &ec);
1403 /* Still notice case differences though (normally a tertiary
1406 ucol_setAttribute (coll, UCOL_CASE_LEVEL, UCOL_ON, &ec);
1408 /* Don't ignore some codepoints */
1409 ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE,
1412 search=usearch_openFromCollator (mono_string_chars (old),
1413 mono_string_length (old),
1414 mono_string_chars (this),
1415 mono_string_length (this),
1417 if(U_SUCCESS (ec)) {
1418 int pos, oldpos, len_delta=0;
1419 int32_t newstr_len=mono_string_length (new), match_len;
1420 UChar *uret, *match;
1422 for(pos=usearch_first (search, &ec);
1424 pos=usearch_next (search, &ec)) {
1425 /* ICU usearch currently ignores most of the collator
1428 * Check the returned match to see if it really
1429 * does match properly...
1431 match_len = usearch_getMatchedLength (search);
1433 if(match_len == 0) {
1437 match=(UChar *)g_malloc0 (sizeof(UChar) * (match_len + 1));
1438 usearch_getMatchedText (search, match, match_len, &ec);
1440 if (ucol_strcoll (coll, match, match_len,
1441 mono_string_chars (old),
1442 mono_string_length (old)) == UCOL_EQUAL) {
1443 /* OK, we really did get a match */
1445 g_message (G_GNUC_PRETTY_FUNCTION
1446 ": Got match at %d len %d", pos,
1450 len_delta += (newstr_len - match_len);
1454 g_message (G_GNUC_PRETTY_FUNCTION
1455 ": Got false match at %d len %d",
1462 g_message (G_GNUC_PRETTY_FUNCTION
1463 ": New string length is %d (delta %d)",
1464 mono_string_length (this)+len_delta, len_delta);
1467 uret=(UChar *)g_malloc0 (sizeof(UChar) * (mono_string_length (this)+len_delta+2));
1469 for(oldpos=0, pos=usearch_first (search, &ec);
1471 pos=usearch_next (search, &ec)) {
1472 match_len = usearch_getMatchedLength (search);
1474 if (match_len == 0) {
1478 match=(UChar *)g_malloc0 (sizeof(UChar) * (match_len + 1));
1479 usearch_getMatchedText (search, match, match_len, &ec);
1481 /* Add the unmatched text */
1482 u_strncat (uret, mono_string_chars (this)+oldpos,
1484 if (ucol_strcoll (coll, match, match_len,
1485 mono_string_chars (old),
1486 mono_string_length (old)) == UCOL_EQUAL) {
1487 /* Then the replacement */
1488 u_strcat (uret, mono_string_chars (new));
1490 /* Then the original, because this is a
1493 u_strncat (uret, mono_string_chars (this)+pos,
1496 oldpos=pos+match_len;
1500 /* Finish off with the trailing unmatched text */
1501 u_strcat (uret, mono_string_chars (this)+oldpos);
1503 ret=mono_string_from_utf16 ((gunichar2 *)uret);
1505 g_message (G_GNUC_PRETTY_FUNCTION ": usearch_open error: %s",
1509 usearch_close (search);
1511 mono_monitor_exit ((MonoObject *)comp);
1514 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));
1520 MonoString *ves_icall_System_String_InternalToLower_Comp (MonoString *this, MonoCultureInfo *cult)
1528 MONO_ARCH_SAVE_REGS;
1531 g_message (G_GNUC_PRETTY_FUNCTION ": [%s]",
1532 mono_string_to_utf8 (this));
1536 g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", cult->lcid);
1539 icu_loc=mono_string_to_icu_locale (cult->icu_name);
1541 mono_raise_exception ((MonoException *)mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1545 udest=(UChar *)g_malloc0 (sizeof(UChar)*(mono_string_length (this)+1));
1547 /* According to the docs, this might result in a longer or
1548 * shorter string than we started with...
1552 len=u_strToLower (udest, mono_string_length (this)+1,
1553 mono_string_chars (this), mono_string_length (this),
1555 if(ec==U_BUFFER_OVERFLOW_ERROR ||
1556 ec==U_STRING_NOT_TERMINATED_WARNING) {
1558 udest=(UChar *)g_malloc0 (sizeof(UChar)*(len+1));
1559 len=u_strToLower (udest, len+1, mono_string_chars (this),
1560 mono_string_length (this), icu_loc, &ec);
1563 if(U_SUCCESS (ec)) {
1564 ret=mono_string_from_utf16 ((gunichar2 *)udest);
1566 g_message (G_GNUC_PRETTY_FUNCTION ": u_strToLower error: %s",
1568 /* return something */
1576 g_message (G_GNUC_PRETTY_FUNCTION ": returning [%s]",
1577 mono_string_to_utf8 (ret));
1583 MonoString *ves_icall_System_String_InternalToUpper_Comp (MonoString *this, MonoCultureInfo *cult)
1591 MONO_ARCH_SAVE_REGS;
1594 g_message (G_GNUC_PRETTY_FUNCTION ": [%s]",
1595 mono_string_to_utf8 (this));
1599 g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", cult->lcid);
1602 icu_loc=mono_string_to_icu_locale (cult->icu_name);
1604 mono_raise_exception ((MonoException *)mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1608 udest=(UChar *)g_malloc0 (sizeof(UChar)*(mono_string_length (this)+1));
1610 /* According to the docs, this might result in a longer or
1611 * shorter string than we started with...
1615 len=u_strToUpper (udest, mono_string_length (this)+1,
1616 mono_string_chars (this), mono_string_length (this),
1618 if(ec==U_BUFFER_OVERFLOW_ERROR ||
1619 ec==U_STRING_NOT_TERMINATED_WARNING) {
1621 udest=(UChar *)g_malloc0 (sizeof(UChar)*(len+1));
1622 len=u_strToUpper (udest, len+1, mono_string_chars (this),
1623 mono_string_length (this), icu_loc, &ec);
1626 if(U_SUCCESS (ec)) {
1627 ret=mono_string_from_utf16 ((gunichar2 *)udest);
1629 g_message (G_GNUC_PRETTY_FUNCTION ": u_strToUpper error: %s",
1631 /* return something */
1639 g_message (G_GNUC_PRETTY_FUNCTION ": returning [%s]",
1640 mono_string_to_utf8 (ret));
1646 gunichar2 ves_icall_System_Char_InternalToUpper_Comp (gunichar2 c, MonoCultureInfo *cult)
1653 MONO_ARCH_SAVE_REGS;
1655 icu_loc=mono_string_to_icu_locale (cult->icu_name);
1657 mono_raise_exception ((MonoException *)mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1662 len=u_strToUpper (&udest, 1, &c, 1, icu_loc, &ec);
1665 if(U_SUCCESS (ec) && len==1) {
1668 /* return something */
1674 gunichar2 ves_icall_System_Char_InternalToLower_Comp (gunichar2 c, MonoCultureInfo *cult)
1681 MONO_ARCH_SAVE_REGS;
1683 icu_loc=mono_string_to_icu_locale (cult->icu_name);
1685 mono_raise_exception ((MonoException *)mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1690 len=u_strToLower (&udest, 1, &c, 1, icu_loc, &ec);
1693 if(U_SUCCESS (ec) && len==1) {
1696 /* return something */
1701 #else /* HAVE_ICU */
1702 static MonoString *string_invariant_tolower (MonoString *this);
1703 static MonoString *string_invariant_toupper (MonoString *this);
1705 void ves_icall_System_Globalization_CultureInfo_construct_internal_locale (MonoCultureInfo *this, MonoString *locale)
1707 MONO_ARCH_SAVE_REGS;
1709 /* Always claim "unknown locale" if we don't have ICU (only
1710 * called for non-invariant locales)
1712 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "ArgumentException"));
1715 void ves_icall_System_Globalization_CompareInfo_construct_compareinfo (MonoCompareInfo *comp, MonoString *locale)
1717 /* Nothing to do here */
1720 int ves_icall_System_Globalization_CompareInfo_internal_compare (MonoCompareInfo *this, MonoString *str1, gint32 off1, gint32 len1, MonoString *str2, gint32 off2, gint32 len2, gint32 options)
1722 MONO_ARCH_SAVE_REGS;
1724 /* Do a normal ascii string compare, as we only know the
1725 * invariant locale if we dont have ICU
1727 return(string_invariant_compare (str1, off1, len1, str2, off2, len2,
1731 void ves_icall_System_Globalization_CompareInfo_free_internal_collator (MonoCompareInfo *this)
1733 /* Nothing to do here */
1736 void ves_icall_System_Globalization_CompareInfo_assign_sortkey (MonoCompareInfo *this, MonoSortKey *key, MonoString *source, gint32 options)
1741 MONO_ARCH_SAVE_REGS;
1743 keylen=mono_string_length (source);
1745 arr=mono_array_new (mono_domain_get (), mono_get_byte_class (),
1747 for(i=0; i<keylen; i++) {
1748 mono_array_set (arr, guint8, i, mono_string_chars (source)[i]);
1754 int ves_icall_System_Globalization_CompareInfo_internal_index (MonoCompareInfo *this, MonoString *source, gint32 sindex, gint32 count, MonoString *value, gint32 options, MonoBoolean first)
1756 MONO_ARCH_SAVE_REGS;
1758 return(string_invariant_indexof (source, sindex, count, value, first));
1761 int ves_icall_System_Globalization_CompareInfo_internal_index_char (MonoCompareInfo *this, MonoString *source, gint32 sindex, gint32 count, gunichar2 value, gint32 options, MonoBoolean first)
1763 MONO_ARCH_SAVE_REGS;
1765 return(string_invariant_indexof_char (source, sindex, count, value,
1769 int ves_icall_System_Threading_Thread_current_lcid (void)
1771 MONO_ARCH_SAVE_REGS;
1777 MonoString *ves_icall_System_String_InternalReplace_Str_Comp (MonoString *this, MonoString *old, MonoString *new, MonoCompareInfo *comp)
1779 MONO_ARCH_SAVE_REGS;
1781 /* Do a normal ascii string compare and replace, as we only
1782 * know the invariant locale if we dont have ICU
1784 return(string_invariant_replace (this, old, new));
1787 MonoString *ves_icall_System_String_InternalToLower_Comp (MonoString *this, MonoCultureInfo *cult)
1789 MONO_ARCH_SAVE_REGS;
1791 return(string_invariant_tolower (this));
1794 MonoString *ves_icall_System_String_InternalToUpper_Comp (MonoString *this, MonoCultureInfo *cult)
1796 MONO_ARCH_SAVE_REGS;
1798 return(string_invariant_toupper (this));
1801 gunichar2 ves_icall_System_Char_InternalToUpper_Comp (gunichar2 c, MonoCultureInfo *cult)
1803 MONO_ARCH_SAVE_REGS;
1805 return g_unichar_toupper (c);
1809 gunichar2 ves_icall_System_Char_InternalToLower_Comp (gunichar2 c, MonoCultureInfo *cult)
1811 MONO_ARCH_SAVE_REGS;
1813 return g_unichar_tolower (c);
1816 static MonoString *string_invariant_tolower (MonoString *this)
1823 ret = mono_string_new_size(mono_domain_get (),
1824 mono_string_length(this));
1826 src = mono_string_chars (this);
1827 dest = mono_string_chars (ret);
1829 for (i = 0; i < mono_string_length (this); ++i) {
1830 dest[i] = g_unichar_tolower(src[i]);
1836 static MonoString *string_invariant_toupper (MonoString *this)
1843 ret = mono_string_new_size(mono_domain_get (),
1844 mono_string_length(this));
1846 src = mono_string_chars (this);
1847 dest = mono_string_chars (ret);
1849 for (i = 0; i < mono_string_length (this); ++i) {
1850 dest[i] = g_unichar_toupper(src[i]);
1856 #endif /* HAVE_ICU */
1858 static gint32 string_invariant_compare_char (gunichar2 c1, gunichar2 c2,
1862 GUnicodeType c1type, c2type;
1864 c1type = g_unichar_type (c1);
1865 c2type = g_unichar_type (c2);
1867 if (options & CompareOptions_IgnoreCase) {
1868 result = (gint32) (c1type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c1) : c1) - (c2type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c2) : c2);
1869 } else if (options & CompareOptions_Ordinal) {
1870 /* Rotor/ms return the full value just not -1 and 1 */
1871 return (gint32) c1 - c2;
1873 /* No options. Kana, symbol and spacing options don't
1874 * apply to the invariant culture.
1877 result = (gint32) c1 - c2;
1880 return ((result < 0) ? -1 : (result > 0) ? 1 : 0);
1883 static gint32 string_invariant_compare (MonoString *str1, gint32 off1,
1884 gint32 len1, MonoString *str2,
1885 gint32 off2, gint32 len2,
1888 /* c translation of C# code from old string.cs.. :) */
1901 ustr1 = mono_string_chars(str1)+off1;
1902 ustr2 = mono_string_chars(str2)+off2;
1906 for (pos = 0; pos != length; pos++) {
1907 if (pos >= len1 || pos >= len2)
1910 charcmp = string_invariant_compare_char(ustr1[pos], ustr2[pos],
1917 /* the lesser wins, so if we have looped until length we just
1918 * need to check the last char
1920 if (pos == length) {
1921 return(string_invariant_compare_char(ustr1[pos - 1],
1922 ustr2[pos - 1], options));
1925 /* Test if one of the strings has been compared to the end */
1932 } else if (pos >= len2) {
1936 /* if not, check our last char only.. (can this happen?) */
1937 return(string_invariant_compare_char(ustr1[pos], ustr2[pos], options));
1940 static MonoString *string_invariant_replace (MonoString *me,
1941 MonoString *oldValue,
1942 MonoString *newValue)
1946 gunichar2 *dest=NULL; /* shut gcc up */
1948 gunichar2 *newstr=NULL; /* shut gcc up here too */
1959 oldstr = mono_string_chars(oldValue);
1960 oldstrlen = mono_string_length(oldValue);
1962 if (NULL != newValue) {
1963 newstr = mono_string_chars(newValue);
1964 newstrlen = mono_string_length(newValue);
1968 src = mono_string_chars(me);
1969 srclen = mono_string_length(me);
1971 if (oldstrlen != newstrlen) {
1973 while (i <= srclen - oldstrlen) {
1974 if (0 == memcmp(src + i, oldstr, oldstrlen * sizeof(gunichar2))) {
1983 newsize = srclen + ((newstrlen - oldstrlen) * occurr);
1989 while (i < srclen) {
1990 if (0 == memcmp(src + i, oldstr, oldstrlen * sizeof(gunichar2))) {
1992 ret = mono_string_new_size( mono_domain_get (), newsize);
1993 dest = mono_string_chars(ret);
1994 memcpy (dest, src, i * sizeof(gunichar2));
1996 if (newstrlen > 0) {
1997 memcpy(dest + destpos, newstr, newstrlen * sizeof(gunichar2));
1998 destpos += newstrlen;
2002 } else if (ret != NULL) {
2003 dest[destpos] = src[i];
2015 static gint32 string_invariant_indexof (MonoString *source, gint32 sindex,
2016 gint32 count, MonoString *value,
2024 lencmpstr = mono_string_length(value);
2026 src = mono_string_chars(source);
2027 cmpstr = mono_string_chars(value);
2031 for(pos=sindex;pos <= sindex+count;pos++) {
2032 for(i=0;src[pos+i]==cmpstr[i];) {
2033 if(++i==lencmpstr) {
2041 for(pos=sindex-lencmpstr+1;pos>sindex-count;pos--) {
2042 if(memcmp (src+pos, cmpstr,
2043 lencmpstr*sizeof(gunichar2))==0) {
2052 static gint32 string_invariant_indexof_char (MonoString *source, gint32 sindex,
2053 gint32 count, gunichar2 value,
2059 src = mono_string_chars(source);
2061 for (pos = sindex; pos != count + sindex; pos++) {
2062 if (src [pos] == value) {
2069 for (pos = sindex; pos > sindex - count; pos--) {
2070 if (src [pos] == value)