3 * Culture-sensitive handling
6 * Dick Porter (dick@ximian.com)
7 * Mohammad DAMT (mdamt@cdl2000.com)
8 * Marek Safar (marek.safar@gmail.com)
10 * Copyright 2003 Ximian, Inc (http://www.ximian.com)
11 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
12 * (C) 2003 PT Cakram Datalingga Duaribu http://www.cdl2000.com
13 * Copyright (C) 2012 Xamarin Inc (http://www.xamarin.com)
14 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
21 #include <mono/metadata/debug-helpers.h>
22 #include <mono/metadata/object.h>
23 #include <mono/metadata/appdomain.h>
24 #include <mono/metadata/exception.h>
25 #include <mono/metadata/monitor.h>
26 #include <mono/metadata/locales.h>
27 #include <mono/metadata/culture-info.h>
28 #include <mono/metadata/culture-info-tables.h>
29 #include <mono/utils/bsearch.h>
31 #ifndef DISABLE_NORMALIZATION
32 #include <mono/metadata/normalization-tables.h>
36 #if defined(__APPLE__)
37 #include <CoreFoundation/CoreFoundation.h>
42 static gint32 string_invariant_compare_char (gunichar2 c1, gunichar2 c2,
44 static gint32 string_invariant_compare (MonoString *str1, gint32 off1,
45 gint32 len1, MonoString *str2,
46 gint32 off2, gint32 len2,
48 static gint32 string_invariant_indexof (MonoString *source, gint32 sindex,
49 gint32 count, MonoString *value,
51 static gint32 string_invariant_indexof_char (MonoString *source, gint32 sindex,
52 gint32 count, gunichar2 value,
55 static const CultureInfoEntry* culture_info_entry_from_lcid (int lcid);
57 static const RegionInfoEntry* region_info_entry_from_lcid (int lcid);
59 /* Lazy class loading functions */
60 static GENERATE_GET_CLASS_WITH_CACHE (culture_info, "System.Globalization", "CultureInfo")
63 culture_lcid_locator (const void *a, const void *b)
65 const int *lcid = (const int *)a;
66 const CultureInfoEntry *bb = (const CultureInfoEntry *)b;
68 return *lcid - bb->lcid;
72 culture_name_locator (const void *a, const void *b)
74 const char *aa = (const char *)a;
75 const CultureInfoNameEntry *bb = (const CultureInfoNameEntry *)b;
78 ret = strcmp (aa, idx2string (bb->name));
84 region_name_locator (const void *a, const void *b)
86 const char *aa = (const char *)a;
87 const RegionInfoNameEntry *bb = (const RegionInfoNameEntry *)b;
90 ret = strcmp (aa, idx2string (bb->name));
96 create_group_sizes_array (const gint *gs, gint ml, MonoError *error)
103 for (i = 0; i < ml; i++) {
109 ret = mono_array_new_cached (mono_domain_get (),
110 mono_get_int32_class (), len, error);
111 return_val_if_nok (error, NULL);
113 for(i = 0; i < len; i++)
114 mono_array_set (ret, gint32, i, gs [i]);
120 create_names_array_idx (const guint16 *names, int ml, MonoError *error)
131 domain = mono_domain_get ();
133 ret = mono_array_new_cached (mono_domain_get (), mono_get_string_class (), ml, error);
134 return_val_if_nok (error, NULL);
136 for(i = 0; i < ml; i++) {
137 MonoString *s = mono_string_new_checked (domain, dtidx2string (names [i]), error);
138 return_val_if_nok (error, NULL);
139 mono_array_setref (ret, i, s);
146 create_names_array_idx_dynamic (const guint16 *names, int ml, MonoError *error)
157 domain = mono_domain_get ();
159 for (i = 0; i < ml; i++) {
165 ret = mono_array_new_cached (mono_domain_get (), mono_get_string_class (), len, error);
166 return_val_if_nok (error, NULL);
168 for(i = 0; i < len; i++) {
169 MonoString *s = mono_string_new_checked (domain, pattern2string (names [i]), error);
170 return_val_if_nok (error, NULL);
171 mono_array_setref (ret, i, s);
178 ves_icall_System_Globalization_CalendarData_fill_calendar_data (MonoCalendarData *this_obj, MonoString *name, gint32 calendar_index)
182 const DateTimeFormatEntry *dfe;
183 const CultureInfoNameEntry *ne;
184 const CultureInfoEntry *ci;
187 n = mono_string_to_utf8_checked (name, &error);
188 if (mono_error_set_pending_exception (&error))
190 ne = (const CultureInfoNameEntry *)mono_binary_search (n, culture_name_entries, NUM_CULTURE_ENTRIES,
191 sizeof (CultureInfoNameEntry), culture_name_locator);
197 ci = &culture_entries [ne->culture_entry_index];
198 dfe = &datetime_format_entries [ci->datetime_format_index];
200 domain = mono_domain_get ();
202 MonoString *native_name = mono_string_new_checked (domain, idx2string (ci->nativename), &error);
203 return_val_and_set_pending_if_nok (&error, FALSE);
204 MONO_OBJECT_SETREF (this_obj, NativeName, native_name);
205 MonoArray *short_date_patterns = create_names_array_idx_dynamic (dfe->short_date_patterns,
206 NUM_SHORT_DATE_PATTERNS, &error);
207 return_val_and_set_pending_if_nok (&error, FALSE);
208 MONO_OBJECT_SETREF (this_obj, ShortDatePatterns, short_date_patterns);
209 MonoArray *year_month_patterns =create_names_array_idx_dynamic (dfe->year_month_patterns,
210 NUM_YEAR_MONTH_PATTERNS, &error);
211 return_val_and_set_pending_if_nok (&error, FALSE);
212 MONO_OBJECT_SETREF (this_obj, YearMonthPatterns, year_month_patterns);
214 MonoArray *long_date_patterns = create_names_array_idx_dynamic (dfe->long_date_patterns,
215 NUM_LONG_DATE_PATTERNS, &error);
216 return_val_and_set_pending_if_nok (&error, FALSE);
217 MONO_OBJECT_SETREF (this_obj, LongDatePatterns, long_date_patterns);
219 MonoString *month_day_pattern = mono_string_new_checked (domain, pattern2string (dfe->month_day_pattern), &error);
220 return_val_and_set_pending_if_nok (&error, FALSE);
221 MONO_OBJECT_SETREF (this_obj, MonthDayPattern, month_day_pattern);
223 MonoArray *day_names = create_names_array_idx (dfe->day_names, NUM_DAYS, &error);
224 return_val_and_set_pending_if_nok (&error, FALSE);
225 MONO_OBJECT_SETREF (this_obj, DayNames, day_names);
227 MonoArray *abbr_day_names = create_names_array_idx (dfe->abbreviated_day_names,
229 return_val_and_set_pending_if_nok (&error, FALSE);
230 MONO_OBJECT_SETREF (this_obj, AbbreviatedDayNames, abbr_day_names);
232 MonoArray *ss_day_names = create_names_array_idx (dfe->shortest_day_names, NUM_DAYS, &error);
233 return_val_and_set_pending_if_nok (&error, FALSE);
234 MONO_OBJECT_SETREF (this_obj, SuperShortDayNames, ss_day_names);
236 MonoArray *month_names = create_names_array_idx (dfe->month_names, NUM_MONTHS, &error);
237 return_val_and_set_pending_if_nok (&error, FALSE);
238 MONO_OBJECT_SETREF (this_obj, MonthNames, month_names);
240 MonoArray *abbr_mon_names = create_names_array_idx (dfe->abbreviated_month_names,
242 return_val_and_set_pending_if_nok (&error, FALSE);
243 MONO_OBJECT_SETREF (this_obj, AbbreviatedMonthNames, abbr_mon_names);
246 MonoArray *gen_month_names = create_names_array_idx (dfe->month_genitive_names, NUM_MONTHS, &error);
247 return_val_and_set_pending_if_nok (&error, FALSE);
248 MONO_OBJECT_SETREF (this_obj, GenitiveMonthNames, gen_month_names);
250 MonoArray *gen_abbr_mon_names = create_names_array_idx (dfe->abbreviated_month_genitive_names, NUM_MONTHS, &error);
251 return_val_and_set_pending_if_nok (&error, FALSE);
252 MONO_OBJECT_SETREF (this_obj, GenitiveAbbreviatedMonthNames, gen_abbr_mon_names);
258 ves_icall_System_Globalization_CultureData_fill_culture_data (MonoCultureData *this_obj, gint32 datetime_index)
262 const DateTimeFormatEntry *dfe;
264 g_assert (datetime_index >= 0);
266 dfe = &datetime_format_entries [datetime_index];
268 domain = mono_domain_get ();
270 #define SET_STR(obj,field,domain,expr,err) do { \
271 MonoString *_tmp_str = mono_string_new_checked ((domain), (expr), (err)); \
272 if (mono_error_set_pending_exception ((err))) \
274 MONO_OBJECT_SETREF((obj), field, _tmp_str); \
277 SET_STR (this_obj, AMDesignator, domain, idx2string (dfe->am_designator), &error);
278 SET_STR (this_obj, PMDesignator, domain, idx2string (dfe->pm_designator), &error);
279 SET_STR (this_obj, TimeSeparator, domain, idx2string (dfe->time_separator), &error);
282 MonoArray *long_time_patterns = create_names_array_idx_dynamic (dfe->long_time_patterns,
283 NUM_LONG_TIME_PATTERNS, &error);
284 if (mono_error_set_pending_exception (&error))
286 MONO_OBJECT_SETREF (this_obj, LongTimePatterns, long_time_patterns);
288 MonoArray *short_time_patterns = create_names_array_idx_dynamic (dfe->short_time_patterns,
289 NUM_SHORT_TIME_PATTERNS, &error);
290 if (mono_error_set_pending_exception (&error))
292 MONO_OBJECT_SETREF (this_obj, ShortTimePatterns, short_time_patterns);
293 this_obj->FirstDayOfWeek = dfe->first_day_of_week;
294 this_obj->CalendarWeekRule = dfe->calendar_week_rule;
298 ves_icall_System_Globalization_CultureData_fill_number_data (MonoNumberFormatInfo* number, gint32 number_index)
302 const NumberFormatEntry *nfe;
304 g_assert (number_index >= 0);
306 nfe = &number_format_entries [number_index];
308 domain = mono_domain_get ();
310 number->currencyDecimalDigits = nfe->currency_decimal_digits;
312 #define SET_STR(obj,field,domain,expr,err) do { \
313 MonoString *_tmp_str = mono_string_new_checked ((domain), (expr), (err)); \
314 if (mono_error_set_pending_exception ((err))) \
316 MONO_OBJECT_SETREF((obj), field, _tmp_str); \
319 SET_STR (number, currencyDecimalSeparator, domain, idx2string (nfe->currency_decimal_separator), &error);
320 SET_STR (number, currencyGroupSeparator, domain, idx2string (nfe->currency_group_separator), &error);
322 MonoArray *currency_sizes_arr = create_group_sizes_array (nfe->currency_group_sizes,
324 if (mono_error_set_pending_exception (&error))
326 MONO_OBJECT_SETREF (number, currencyGroupSizes, currency_sizes_arr);
327 number->currencyNegativePattern = nfe->currency_negative_pattern;
328 number->currencyPositivePattern = nfe->currency_positive_pattern;
330 SET_STR (number, currencySymbol, domain, idx2string (nfe->currency_symbol), &error);
331 SET_STR (number, naNSymbol, domain, idx2string (nfe->nan_symbol), &error);
332 SET_STR (number, negativeInfinitySymbol, domain, idx2string (nfe->negative_infinity_symbol), &error);
333 SET_STR (number, negativeSign, domain, idx2string (nfe->negative_sign), &error);
334 number->numberDecimalDigits = nfe->number_decimal_digits;
335 SET_STR (number, numberDecimalSeparator, domain, idx2string (nfe->number_decimal_separator), &error);
336 SET_STR (number, numberGroupSeparator, domain, idx2string (nfe->number_group_separator), &error);
337 MonoArray *number_sizes_arr = create_group_sizes_array (nfe->number_group_sizes,
339 if (mono_error_set_pending_exception (&error))
341 MONO_OBJECT_SETREF (number, numberGroupSizes, number_sizes_arr);
342 number->numberNegativePattern = nfe->number_negative_pattern;
343 number->percentNegativePattern = nfe->percent_negative_pattern;
344 number->percentPositivePattern = nfe->percent_positive_pattern;
345 SET_STR (number, percentSymbol, domain, idx2string (nfe->percent_symbol), &error);
346 SET_STR (number, perMilleSymbol, domain, idx2string (nfe->per_mille_symbol), &error);
347 SET_STR (number, positiveInfinitySymbol, domain, idx2string (nfe->positive_infinity_symbol), &error);
348 SET_STR (number, positiveSign, domain, idx2string (nfe->positive_sign), &error);
353 construct_culture (MonoCultureInfo *this_obj, const CultureInfoEntry *ci, MonoError *error)
355 MonoDomain *domain = mono_domain_get ();
359 this_obj->lcid = ci->lcid;
361 #define SET_STR(obj,field,domain,expr,err) do { \
362 MonoString *_tmp_str = mono_string_new_checked ((domain), (expr), (err)); \
363 return_val_if_nok (err, FALSE); \
364 MONO_OBJECT_SETREF((obj), field, _tmp_str); \
367 SET_STR (this_obj, name, domain, idx2string (ci->name), error);
368 SET_STR (this_obj, englishname, domain, idx2string (ci->englishname), error);
369 SET_STR (this_obj, nativename, domain, idx2string (ci->nativename), error);
370 SET_STR (this_obj, win3lang, domain, idx2string (ci->win3lang), error);
371 SET_STR (this_obj, iso3lang, domain, idx2string (ci->iso3lang), error);
372 SET_STR (this_obj, iso2lang, domain, idx2string (ci->iso2lang), error);
374 // It's null for neutral cultures
375 if (ci->territory > 0) {
376 SET_STR (this_obj, territory, domain, idx2string (ci->territory), error);
379 MonoArray *native_calendar_names = create_names_array_idx (ci->native_calendar_names, NUM_CALENDARS, error);
380 return_val_if_nok (error, FALSE);
381 MONO_OBJECT_SETREF (this_obj, native_calendar_names, native_calendar_names);
382 this_obj->parent_lcid = ci->parent_lcid;
383 this_obj->datetime_index = ci->datetime_format_index;
384 this_obj->number_index = ci->number_format_index;
385 this_obj->calendar_type = ci->calendar_type;
386 this_obj->text_info_data = &ci->text_info;
393 construct_region (MonoRegionInfo *this_obj, const RegionInfoEntry *ri, MonoError *error)
395 MonoDomain *domain = mono_domain_get ();
399 #define SET_STR(obj,field,domain,expr,err) do { \
400 MonoString *_tmp_str = mono_string_new_checked ((domain), (expr), (err)); \
401 return_val_if_nok (err, FALSE); \
402 MONO_OBJECT_SETREF((obj), field, _tmp_str); \
405 this_obj->geo_id = ri->geo_id;
406 SET_STR (this_obj, iso2name, domain, idx2string (ri->iso2name), error);
407 SET_STR (this_obj, iso3name, domain, idx2string (ri->iso3name), error);
408 SET_STR (this_obj, win3name, domain, idx2string (ri->win3name), error);
409 SET_STR (this_obj, english_name, domain, idx2string (ri->english_name), error);
410 SET_STR (this_obj, native_name, domain, idx2string (ri->native_name), error);
411 SET_STR (this_obj, currency_symbol, domain, idx2string (ri->currency_symbol), error);
412 SET_STR (this_obj, iso_currency_symbol, domain, idx2string (ri->iso_currency_symbol), error);
413 SET_STR (this_obj, currency_english_name, domain, idx2string (ri->currency_english_name), error);
414 SET_STR (this_obj, currency_native_name, domain, idx2string (ri->currency_native_name), error);
421 static const CultureInfoEntry*
422 culture_info_entry_from_lcid (int lcid)
424 const CultureInfoEntry *ci;
426 ci = (const CultureInfoEntry *)mono_binary_search (&lcid, culture_entries, NUM_CULTURE_ENTRIES, sizeof (CultureInfoEntry), culture_lcid_locator);
431 static const RegionInfoEntry*
432 region_info_entry_from_lcid (int lcid)
434 const RegionInfoEntry *entry;
435 const CultureInfoEntry *ne;
437 ne = (const CultureInfoEntry *)mono_binary_search (&lcid, culture_entries, NUM_CULTURE_ENTRIES, sizeof (CultureInfoEntry), culture_lcid_locator);
442 entry = ®ion_entries [ne->region_entry_index];
447 #if defined (__APPLE__)
449 get_darwin_locale (void)
451 static gchar *darwin_locale = NULL;
452 CFLocaleRef locale = NULL;
453 CFStringRef locale_language = NULL;
454 CFStringRef locale_country = NULL;
455 CFStringRef locale_script = NULL;
456 CFStringRef locale_cfstr = NULL;
457 CFIndex bytes_converted;
458 CFIndex bytes_written;
462 if (darwin_locale != NULL)
463 return g_strdup (darwin_locale);
465 locale = CFLocaleCopyCurrent ();
468 locale_language = CFLocaleGetValue (locale, kCFLocaleLanguageCode);
469 if (locale_language != NULL && CFStringGetBytes(locale_language, CFRangeMake (0, CFStringGetLength (locale_language)), kCFStringEncodingMacRoman, 0, FALSE, NULL, 0, &bytes_converted) > 0) {
470 len = bytes_converted + 1;
472 locale_country = CFLocaleGetValue (locale, kCFLocaleCountryCode);
473 if (locale_country != NULL && CFStringGetBytes (locale_country, CFRangeMake (0, CFStringGetLength (locale_country)), kCFStringEncodingMacRoman, 0, FALSE, NULL, 0, &bytes_converted) > 0) {
474 len += bytes_converted + 1;
476 locale_script = CFLocaleGetValue (locale, kCFLocaleScriptCode);
477 if (locale_script != NULL && CFStringGetBytes (locale_script, CFRangeMake (0, CFStringGetLength (locale_script)), kCFStringEncodingMacRoman, 0, FALSE, NULL, 0, &bytes_converted) > 0) {
478 len += bytes_converted + 1;
481 darwin_locale = (char *) g_malloc (len + 1);
482 CFStringGetBytes (locale_language, CFRangeMake (0, CFStringGetLength (locale_language)), kCFStringEncodingMacRoman, 0, FALSE, (UInt8 *) darwin_locale, len, &bytes_converted);
484 darwin_locale[bytes_converted] = '-';
485 bytes_written = bytes_converted + 1;
486 if (locale_script != NULL && CFStringGetBytes (locale_script, CFRangeMake (0, CFStringGetLength (locale_script)), kCFStringEncodingMacRoman, 0, FALSE, (UInt8 *) &darwin_locale[bytes_written], len - bytes_written, &bytes_converted) > 0) {
487 darwin_locale[bytes_written + bytes_converted] = '-';
488 bytes_written += bytes_converted + 1;
491 CFStringGetBytes (locale_country, CFRangeMake (0, CFStringGetLength (locale_country)), kCFStringEncodingMacRoman, 0, FALSE, (UInt8 *) &darwin_locale[bytes_written], len - bytes_written, &bytes_converted);
492 darwin_locale[bytes_written + bytes_converted] = '\0';
496 if (darwin_locale == NULL) {
497 locale_cfstr = CFLocaleGetIdentifier (locale);
500 len = CFStringGetMaximumSizeForEncoding (CFStringGetLength (locale_cfstr), kCFStringEncodingMacRoman) + 1;
501 darwin_locale = (char *) g_malloc (len);
502 if (!CFStringGetCString (locale_cfstr, darwin_locale, len, kCFStringEncodingMacRoman)) {
503 g_free (darwin_locale);
505 darwin_locale = NULL;
509 for (i = 0; i < strlen (darwin_locale); i++)
510 if (darwin_locale [i] == '_')
511 darwin_locale [i] = '-';
518 return g_strdup (darwin_locale);
523 get_posix_locale (void)
527 locale = g_getenv ("LC_ALL");
528 if (locale == NULL) {
529 locale = g_getenv ("LANG");
530 if (locale == NULL) {
531 char *static_locale = setlocale (LC_ALL, NULL);
533 locale = g_strdup (static_locale);
539 /* Skip English-only locale 'C' */
540 if (strcmp (locale, "C") == 0) {
550 get_current_locale_name (void)
556 locale = g_win32_getlocale ();
557 #elif defined (__APPLE__)
558 locale = get_darwin_locale ();
560 locale = get_posix_locale ();
562 locale = get_posix_locale ();
568 p = strchr (locale, '.');
571 p = strchr (locale, '@');
574 p = strchr (locale, '_');
578 ret = g_ascii_strdown (locale, -1);
585 ves_icall_System_Globalization_CultureInfo_get_current_locale_name (MonoError *error)
591 locale = get_current_locale_name ();
593 return MONO_HANDLE_CAST (MonoString, NULL_HANDLE);
595 domain = mono_domain_get ();
596 MonoStringHandle ret = mono_string_new_handle (domain, locale, error);
603 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_lcid (MonoCultureInfo *this_obj,
607 const CultureInfoEntry *ci;
609 ci = culture_info_entry_from_lcid (lcid);
613 if (!construct_culture (this_obj, ci, &error)) {
614 mono_error_set_pending_exception (&error);
621 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_name (MonoCultureInfo *this_obj,
625 const CultureInfoNameEntry *ne;
628 n = mono_string_to_utf8_checked (name, &error);
629 if (mono_error_set_pending_exception (&error))
631 ne = (const CultureInfoNameEntry *)mono_binary_search (n, culture_name_entries, NUM_CULTURE_ENTRIES,
632 sizeof (CultureInfoNameEntry), culture_name_locator);
635 /*g_print ("ne (%s) is null\n", n);*/
641 if (!construct_culture (this_obj, &culture_entries [ne->culture_entry_index], &error)) {
642 mono_error_set_pending_exception (&error);
649 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_specific_name (MonoCultureInfo *ci,
655 locale = mono_string_to_utf8 (name);
656 ret = construct_culture_from_specific_name (ci, locale);
663 ves_icall_System_Globalization_RegionInfo_construct_internal_region_from_lcid (MonoRegionInfo *this_obj,
667 const RegionInfoEntry *ri;
669 ri = region_info_entry_from_lcid (lcid);
673 MonoBoolean result = construct_region (this_obj, ri, &error);
674 mono_error_set_pending_exception (&error);
679 ves_icall_System_Globalization_RegionInfo_construct_internal_region_from_name (MonoRegionInfo *this_obj,
683 const RegionInfoNameEntry *ne;
686 n = mono_string_to_utf8_checked (name, &error);
687 if (mono_error_set_pending_exception (&error))
689 ne = (const RegionInfoNameEntry *)mono_binary_search (n, region_name_entries, NUM_REGION_ENTRIES,
690 sizeof (RegionInfoNameEntry), region_name_locator);
693 /*g_print ("ne (%s) is null\n", n);*/
699 MonoBoolean result = construct_region (this_obj, ®ion_entries [ne->region_entry_index], &error);
700 mono_error_set_pending_exception (&error);
705 ves_icall_System_Globalization_CultureInfo_internal_get_cultures (MonoBoolean neutral,
706 MonoBoolean specific, MonoBoolean installed)
711 MonoCultureInfo *culture;
713 const CultureInfoEntry *ci;
717 domain = mono_domain_get ();
720 for (i = 0; i < NUM_CULTURE_ENTRIES; i++) {
721 ci = &culture_entries [i];
722 is_neutral = ci->territory == 0;
723 if ((neutral && is_neutral) || (specific && !is_neutral))
727 klass = mono_class_get_culture_info_class ();
729 /* The InvariantCulture is not in culture_entries */
730 /* We reserve the first slot in the array for it */
734 ret = mono_array_new_checked (domain, klass, len, &error);
743 mono_array_setref (ret, len++, NULL);
745 for (i = 0; i < NUM_CULTURE_ENTRIES; i++) {
746 ci = &culture_entries [i];
747 is_neutral = ci->territory == 0;
748 if ((neutral && is_neutral) || (specific && !is_neutral)) {
749 culture = (MonoCultureInfo *) mono_object_new_checked (domain, klass, &error);
750 if (!is_ok (&error)) goto fail;
751 mono_runtime_object_init_checked ((MonoObject *) culture, &error);
752 if (!is_ok (&error)) goto fail;
753 if (!construct_culture (culture, ci, &error))
755 culture->use_user_override = TRUE;
756 mono_array_setref (ret, len++, culture);
763 mono_error_set_pending_exception (&error);
767 int ves_icall_System_Globalization_CompareInfo_internal_compare (MonoCompareInfo *this_obj, MonoString *str1, gint32 off1, gint32 len1, MonoString *str2, gint32 off2, gint32 len2, gint32 options)
769 /* Do a normal ascii string compare, as we only know the
770 * invariant locale if we dont have ICU
772 return(string_invariant_compare (str1, off1, len1, str2, off2, len2,
776 void ves_icall_System_Globalization_CompareInfo_assign_sortkey (MonoCompareInfo *this_obj, MonoSortKey *key, MonoString *source, gint32 options)
782 keylen=mono_string_length (source);
784 arr=mono_array_new_checked (mono_domain_get (), mono_get_byte_class (),
786 if (mono_error_set_pending_exception (&error))
789 for(i=0; i<keylen; i++) {
790 mono_array_set (arr, guint8, i, mono_string_chars (source)[i]);
793 MONO_OBJECT_SETREF (key, key, arr);
796 int ves_icall_System_Globalization_CompareInfo_internal_index (MonoCompareInfo *this_obj, MonoString *source, gint32 sindex, gint32 count, MonoString *value, gint32 options, MonoBoolean first)
798 return(string_invariant_indexof (source, sindex, count, value, first));
801 int ves_icall_System_Globalization_CompareInfo_internal_index_char (MonoCompareInfo *this_obj, MonoString *source, gint32 sindex, gint32 count, gunichar2 value, gint32 options, MonoBoolean first)
803 return(string_invariant_indexof_char (source, sindex, count, value,
807 int ves_icall_System_Threading_Thread_current_lcid (void)
813 static gint32 string_invariant_compare_char (gunichar2 c1, gunichar2 c2,
818 /* Ordinal can not be mixed with other options, and must return the difference, not only -1, 0, 1 */
819 if (options & CompareOptions_Ordinal)
820 return (gint32) c1 - c2;
822 if (options & CompareOptions_IgnoreCase) {
823 GUnicodeType c1type, c2type;
825 c1type = g_unichar_type (c1);
826 c2type = g_unichar_type (c2);
828 result = (gint32) (c1type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c1) : c1) -
829 (c2type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c2) : c2);
832 * No options. Kana, symbol and spacing options don't
833 * apply to the invariant culture.
837 * FIXME: here we must use the information from c1type and c2type
838 * to find out the proper collation, even on the InvariantCulture, the
839 * sorting is not done by computing the unicode values, but their
842 result = (gint32) c1 - c2;
845 return ((result < 0) ? -1 : (result > 0) ? 1 : 0);
848 static gint32 string_invariant_compare (MonoString *str1, gint32 off1,
849 gint32 len1, MonoString *str2,
850 gint32 off2, gint32 len2,
853 /* c translation of C# code from old string.cs.. :) */
866 ustr1 = mono_string_chars(str1)+off1;
867 ustr2 = mono_string_chars(str2)+off2;
871 for (pos = 0; pos != length; pos++) {
872 if (pos >= len1 || pos >= len2)
875 charcmp = string_invariant_compare_char(ustr1[pos], ustr2[pos],
882 /* the lesser wins, so if we have looped until length we just
883 * need to check the last char
886 return(string_invariant_compare_char(ustr1[pos - 1],
887 ustr2[pos - 1], options));
890 /* Test if one of the strings has been compared to the end */
897 } else if (pos >= len2) {
901 /* if not, check our last char only.. (can this happen?) */
902 return(string_invariant_compare_char(ustr1[pos], ustr2[pos], options));
905 static gint32 string_invariant_indexof (MonoString *source, gint32 sindex,
906 gint32 count, MonoString *value,
914 lencmpstr = mono_string_length(value);
916 src = mono_string_chars(source);
917 cmpstr = mono_string_chars(value);
921 for(pos=sindex;pos <= sindex+count;pos++) {
922 for(i=0;src[pos+i]==cmpstr[i];) {
931 for(pos=sindex-lencmpstr+1;pos>sindex-count;pos--) {
932 if(memcmp (src+pos, cmpstr,
933 lencmpstr*sizeof(gunichar2))==0) {
942 static gint32 string_invariant_indexof_char (MonoString *source, gint32 sindex,
943 gint32 count, gunichar2 value,
949 src = mono_string_chars(source);
951 for (pos = sindex; pos != count + sindex; pos++) {
952 if (src [pos] == value) {
959 for (pos = sindex; pos > sindex - count; pos--) {
960 if (src [pos] == value)
968 void ves_icall_System_Text_Normalization_load_normalization_resource (guint8 **argProps,
969 guint8 **argMappedChars,
970 guint8 **argCharMapIndex,
971 guint8 **argHelperIndex,
972 guint8 **argMapIdxToComposite,
973 guint8 **argCombiningClass,
977 #ifdef DISABLE_NORMALIZATION
978 mono_error_set_not_supported (error, "This runtime has been compiled without string normalization support.");
981 *argProps = (guint8*)props;
982 *argMappedChars = (guint8*) mappedChars;
983 *argCharMapIndex = (guint8*) charMapIndex;
984 *argHelperIndex = (guint8*) helperIndex;
985 *argMapIdxToComposite = (guint8*) mapIdxToComposite;
986 *argCombiningClass = (guint8*)combiningClass;