2 * locales.c: Culture-sensitive handling
5 * Dick Porter (dick@ximian.com)
6 * Mohammad DAMT (mdamt@cdl2000.com)
7 * Marek Safar (marek.safar@gmail.com)
9 * Copyright 2003 Ximian, Inc (http://www.ximian.com)
10 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
11 * (C) 2003 PT Cakram Datalingga Duaribu http://www.cdl2000.com
12 * Copyright (C) 2012 Xamarin Inc (http://www.xamarin.com)
19 #include <mono/metadata/debug-helpers.h>
20 #include <mono/metadata/object.h>
21 #include <mono/metadata/appdomain.h>
22 #include <mono/metadata/exception.h>
23 #include <mono/metadata/monitor.h>
24 #include <mono/metadata/locales.h>
25 #include <mono/metadata/culture-info.h>
26 #include <mono/metadata/culture-info-tables.h>
27 #include <mono/utils/bsearch.h>
29 #ifndef DISABLE_NORMALIZATION
30 #include <mono/metadata/normalization-tables.h>
34 #if defined(__APPLE__)
35 #include <CoreFoundation/CoreFoundation.h>
40 static gint32 string_invariant_compare_char (gunichar2 c1, gunichar2 c2,
42 static gint32 string_invariant_compare (MonoString *str1, gint32 off1,
43 gint32 len1, MonoString *str2,
44 gint32 off2, gint32 len2,
46 static MonoString *string_invariant_replace (MonoString *me,
48 MonoString *newValue);
49 static gint32 string_invariant_indexof (MonoString *source, gint32 sindex,
50 gint32 count, MonoString *value,
52 static gint32 string_invariant_indexof_char (MonoString *source, gint32 sindex,
53 gint32 count, gunichar2 value,
56 static const CultureInfoEntry* culture_info_entry_from_lcid (int lcid);
58 static const RegionInfoEntry* region_info_entry_from_lcid (int lcid);
60 /* Lazy class loading functions */
61 static GENERATE_GET_CLASS_WITH_CACHE (culture_info, System.Globalization, CultureInfo)
64 culture_lcid_locator (const void *a, const void *b)
66 const int *lcid = (const int *)a;
67 const CultureInfoEntry *bb = (const CultureInfoEntry *)b;
69 return *lcid - bb->lcid;
73 culture_name_locator (const void *a, const void *b)
75 const char *aa = (const char *)a;
76 const CultureInfoNameEntry *bb = (const CultureInfoNameEntry *)b;
79 ret = strcmp (aa, idx2string (bb->name));
85 region_name_locator (const void *a, const void *b)
87 const char *aa = (const char *)a;
88 const RegionInfoNameEntry *bb = (const RegionInfoNameEntry *)b;
91 ret = strcmp (aa, idx2string (bb->name));
97 create_group_sizes_array (const gint *gs, gint ml)
102 for (i = 0; i < ml; i++) {
108 ret = mono_array_new_cached (mono_domain_get (),
109 mono_get_int32_class (), len);
111 for(i = 0; i < len; i++)
112 mono_array_set (ret, gint32, i, gs [i]);
118 create_names_array_idx (const guint16 *names, int ml)
127 domain = mono_domain_get ();
129 ret = mono_array_new_cached (mono_domain_get (), mono_get_string_class (), ml);
131 for(i = 0; i < ml; i++)
132 mono_array_setref (ret, i, mono_string_new (domain, idx2string (names [i])));
138 create_names_array_idx_dynamic (const guint16 *names, int ml)
147 domain = mono_domain_get ();
149 for (i = 0; i < ml; i++) {
155 ret = mono_array_new_cached (mono_domain_get (), mono_get_string_class (), len);
157 for(i = 0; i < len; i++)
158 mono_array_setref (ret, i, mono_string_new (domain, idx2string (names [i])));
164 ves_icall_System_Globalization_CalendarData_fill_calendar_data (MonoCalendarData *this_obj, MonoString *name, gint32 calendar_index)
167 const DateTimeFormatEntry *dfe;
168 const CultureInfoNameEntry *ne;
169 const CultureInfoEntry *ci;
172 n = mono_string_to_utf8 (name);
173 ne = (const CultureInfoNameEntry *)mono_binary_search (n, culture_name_entries, NUM_CULTURE_ENTRIES,
174 sizeof (CultureInfoNameEntry), culture_name_locator);
180 ci = &culture_entries [ne->culture_entry_index];
181 dfe = &datetime_format_entries [ci->datetime_format_index];
183 domain = mono_domain_get ();
185 MONO_OBJECT_SETREF (this_obj, NativeName, mono_string_new (domain, idx2string (ci->nativename)));
186 MONO_OBJECT_SETREF (this_obj, ShortDatePatterns, create_names_array_idx_dynamic (dfe->short_date_patterns,
187 NUM_SHORT_DATE_PATTERNS));
188 MONO_OBJECT_SETREF (this_obj, YearMonthPatterns, create_names_array_idx_dynamic (dfe->year_month_patterns,
189 NUM_YEAR_MONTH_PATTERNS));
191 MONO_OBJECT_SETREF (this_obj, LongDatePatterns, create_names_array_idx_dynamic (dfe->long_date_patterns,
192 NUM_LONG_DATE_PATTERNS));
193 MONO_OBJECT_SETREF (this_obj, MonthDayPattern, mono_string_new (domain, idx2string (dfe->month_day_pattern)));
195 MONO_OBJECT_SETREF (this_obj, DayNames, create_names_array_idx (dfe->day_names, NUM_DAYS));
196 MONO_OBJECT_SETREF (this_obj, AbbreviatedDayNames, create_names_array_idx (dfe->abbreviated_day_names,
198 MONO_OBJECT_SETREF (this_obj, SuperShortDayNames, create_names_array_idx (dfe->shortest_day_names, NUM_DAYS));
199 MONO_OBJECT_SETREF (this_obj, MonthNames, create_names_array_idx (dfe->month_names, NUM_MONTHS));
200 MONO_OBJECT_SETREF (this_obj, AbbreviatedMonthNames, create_names_array_idx (dfe->abbreviated_month_names,
202 MONO_OBJECT_SETREF (this_obj, GenitiveMonthNames, create_names_array_idx (dfe->month_genitive_names, NUM_MONTHS));
203 MONO_OBJECT_SETREF (this_obj, GenitiveAbbreviatedMonthNames, create_names_array_idx (dfe->abbreviated_month_genitive_names, NUM_MONTHS));
209 ves_icall_System_Globalization_CultureData_fill_culture_data (MonoCultureData *this_obj, gint32 datetime_index)
212 const DateTimeFormatEntry *dfe;
214 g_assert (datetime_index >= 0);
216 dfe = &datetime_format_entries [datetime_index];
218 domain = mono_domain_get ();
220 MONO_OBJECT_SETREF (this_obj, AMDesignator, mono_string_new (domain, idx2string (dfe->am_designator)));
221 MONO_OBJECT_SETREF (this_obj, PMDesignator, mono_string_new (domain, idx2string (dfe->pm_designator)));
222 MONO_OBJECT_SETREF (this_obj, TimeSeparator, mono_string_new (domain, idx2string (dfe->time_separator)));
223 MONO_OBJECT_SETREF (this_obj, LongTimePatterns, create_names_array_idx_dynamic (dfe->long_time_patterns,
224 NUM_LONG_TIME_PATTERNS));
225 MONO_OBJECT_SETREF (this_obj, ShortTimePatterns, create_names_array_idx_dynamic (dfe->short_time_patterns,
226 NUM_SHORT_TIME_PATTERNS));
227 this_obj->FirstDayOfWeek = dfe->first_day_of_week;
228 this_obj->CalendarWeekRule = dfe->calendar_week_rule;
232 ves_icall_System_Globalization_CultureData_fill_number_data (MonoNumberFormatInfo* number, gint32 number_index)
235 const NumberFormatEntry *nfe;
237 g_assert (number_index >= 0);
239 nfe = &number_format_entries [number_index];
241 domain = mono_domain_get ();
243 number->currencyDecimalDigits = nfe->currency_decimal_digits;
244 MONO_OBJECT_SETREF (number, currencyDecimalSeparator, mono_string_new (domain,
245 idx2string (nfe->currency_decimal_separator)));
246 MONO_OBJECT_SETREF (number, currencyGroupSeparator, mono_string_new (domain,
247 idx2string (nfe->currency_group_separator)));
248 MONO_OBJECT_SETREF (number, currencyGroupSizes, create_group_sizes_array (nfe->currency_group_sizes,
250 number->currencyNegativePattern = nfe->currency_negative_pattern;
251 number->currencyPositivePattern = nfe->currency_positive_pattern;
252 MONO_OBJECT_SETREF (number, currencySymbol, mono_string_new (domain, idx2string (nfe->currency_symbol)));
253 MONO_OBJECT_SETREF (number, naNSymbol, mono_string_new (domain, idx2string (nfe->nan_symbol)));
254 MONO_OBJECT_SETREF (number, negativeInfinitySymbol, mono_string_new (domain,
255 idx2string (nfe->negative_infinity_symbol)));
256 MONO_OBJECT_SETREF (number, negativeSign, mono_string_new (domain, idx2string (nfe->negative_sign)));
257 number->numberDecimalDigits = nfe->number_decimal_digits;
258 MONO_OBJECT_SETREF (number, numberDecimalSeparator, mono_string_new (domain,
259 idx2string (nfe->number_decimal_separator)));
260 MONO_OBJECT_SETREF (number, numberGroupSeparator, mono_string_new (domain, idx2string (nfe->number_group_separator)));
261 MONO_OBJECT_SETREF (number, numberGroupSizes, create_group_sizes_array (nfe->number_group_sizes,
263 number->numberNegativePattern = nfe->number_negative_pattern;
264 number->percentNegativePattern = nfe->percent_negative_pattern;
265 number->percentPositivePattern = nfe->percent_positive_pattern;
266 MONO_OBJECT_SETREF (number, percentSymbol, mono_string_new (domain, idx2string (nfe->percent_symbol)));
267 MONO_OBJECT_SETREF (number, perMilleSymbol, mono_string_new (domain, idx2string (nfe->per_mille_symbol)));
268 MONO_OBJECT_SETREF (number, positiveInfinitySymbol, mono_string_new (domain,
269 idx2string (nfe->positive_infinity_symbol)));
270 MONO_OBJECT_SETREF (number, positiveSign, mono_string_new (domain, idx2string (nfe->positive_sign)));
274 construct_culture (MonoCultureInfo *this_obj, const CultureInfoEntry *ci)
276 MonoDomain *domain = mono_domain_get ();
278 this_obj->lcid = ci->lcid;
279 MONO_OBJECT_SETREF (this_obj, name, mono_string_new (domain, idx2string (ci->name)));
280 MONO_OBJECT_SETREF (this_obj, englishname, mono_string_new (domain, idx2string (ci->englishname)));
281 MONO_OBJECT_SETREF (this_obj, nativename, mono_string_new (domain, idx2string (ci->nativename)));
282 MONO_OBJECT_SETREF (this_obj, win3lang, mono_string_new (domain, idx2string (ci->win3lang)));
283 MONO_OBJECT_SETREF (this_obj, iso3lang, mono_string_new (domain, idx2string (ci->iso3lang)));
284 MONO_OBJECT_SETREF (this_obj, iso2lang, mono_string_new (domain, idx2string (ci->iso2lang)));
286 // It's null for neutral cultures
287 if (ci->territory > 0)
288 MONO_OBJECT_SETREF (this_obj, territory, mono_string_new (domain, idx2string (ci->territory)));
289 MONO_OBJECT_SETREF (this_obj, native_calendar_names, create_names_array_idx (ci->native_calendar_names, NUM_CALENDARS));
290 this_obj->parent_lcid = ci->parent_lcid;
291 this_obj->datetime_index = ci->datetime_format_index;
292 this_obj->number_index = ci->number_format_index;
293 this_obj->calendar_type = ci->calendar_type;
294 this_obj->text_info_data = &ci->text_info;
300 construct_region (MonoRegionInfo *this_obj, const RegionInfoEntry *ri)
302 MonoDomain *domain = mono_domain_get ();
304 this_obj->geo_id = ri->geo_id;
305 MONO_OBJECT_SETREF (this_obj, iso2name, mono_string_new (domain, idx2string (ri->iso2name)));
306 MONO_OBJECT_SETREF (this_obj, iso3name, mono_string_new (domain, idx2string (ri->iso3name)));
307 MONO_OBJECT_SETREF (this_obj, win3name, mono_string_new (domain, idx2string (ri->win3name)));
308 MONO_OBJECT_SETREF (this_obj, english_name, mono_string_new (domain, idx2string (ri->english_name)));
309 MONO_OBJECT_SETREF (this_obj, native_name, mono_string_new (domain, idx2string (ri->native_name)));
310 MONO_OBJECT_SETREF (this_obj, currency_symbol, mono_string_new (domain, idx2string (ri->currency_symbol)));
311 MONO_OBJECT_SETREF (this_obj, iso_currency_symbol, mono_string_new (domain, idx2string (ri->iso_currency_symbol)));
312 MONO_OBJECT_SETREF (this_obj, currency_english_name, mono_string_new (domain, idx2string (ri->currency_english_name)));
313 MONO_OBJECT_SETREF (this_obj, currency_native_name, mono_string_new (domain, idx2string (ri->currency_native_name)));
318 static const CultureInfoEntry*
319 culture_info_entry_from_lcid (int lcid)
321 const CultureInfoEntry *ci;
323 ci = (const CultureInfoEntry *)mono_binary_search (&lcid, culture_entries, NUM_CULTURE_ENTRIES, sizeof (CultureInfoEntry), culture_lcid_locator);
328 static const RegionInfoEntry*
329 region_info_entry_from_lcid (int lcid)
331 const RegionInfoEntry *entry;
332 const CultureInfoEntry *ne;
334 ne = (const CultureInfoEntry *)mono_binary_search (&lcid, culture_entries, NUM_CULTURE_ENTRIES, sizeof (CultureInfoEntry), culture_lcid_locator);
339 entry = ®ion_entries [ne->region_entry_index];
344 #if defined (__APPLE__)
346 get_darwin_locale (void)
348 static gchar *darwin_locale = NULL;
349 CFLocaleRef locale = NULL;
350 CFStringRef locale_language = NULL;
351 CFStringRef locale_country = NULL;
352 CFStringRef locale_script = NULL;
353 CFStringRef locale_cfstr = NULL;
354 CFIndex bytes_converted;
355 CFIndex bytes_written;
359 if (darwin_locale != NULL)
360 return g_strdup (darwin_locale);
362 locale = CFLocaleCopyCurrent ();
365 locale_language = CFLocaleGetValue (locale, kCFLocaleLanguageCode);
366 if (locale_language != NULL && CFStringGetBytes(locale_language, CFRangeMake (0, CFStringGetLength (locale_language)), kCFStringEncodingMacRoman, 0, FALSE, NULL, 0, &bytes_converted) > 0) {
367 len = bytes_converted + 1;
369 locale_country = CFLocaleGetValue (locale, kCFLocaleCountryCode);
370 if (locale_country != NULL && CFStringGetBytes (locale_country, CFRangeMake (0, CFStringGetLength (locale_country)), kCFStringEncodingMacRoman, 0, FALSE, NULL, 0, &bytes_converted) > 0) {
371 len += bytes_converted + 1;
373 locale_script = CFLocaleGetValue (locale, kCFLocaleScriptCode);
374 if (locale_script != NULL && CFStringGetBytes (locale_script, CFRangeMake (0, CFStringGetLength (locale_script)), kCFStringEncodingMacRoman, 0, FALSE, NULL, 0, &bytes_converted) > 0) {
375 len += bytes_converted + 1;
378 darwin_locale = (char *) malloc (len + 1);
379 CFStringGetBytes (locale_language, CFRangeMake (0, CFStringGetLength (locale_language)), kCFStringEncodingMacRoman, 0, FALSE, (UInt8 *) darwin_locale, len, &bytes_converted);
381 darwin_locale[bytes_converted] = '-';
382 bytes_written = bytes_converted + 1;
383 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) {
384 darwin_locale[bytes_written + bytes_converted] = '-';
385 bytes_written += bytes_converted + 1;
388 CFStringGetBytes (locale_country, CFRangeMake (0, CFStringGetLength (locale_country)), kCFStringEncodingMacRoman, 0, FALSE, (UInt8 *) &darwin_locale[bytes_written], len - bytes_written, &bytes_converted);
389 darwin_locale[bytes_written + bytes_converted] = '\0';
393 if (darwin_locale == NULL) {
394 locale_cfstr = CFLocaleGetIdentifier (locale);
397 len = CFStringGetMaximumSizeForEncoding (CFStringGetLength (locale_cfstr), kCFStringEncodingMacRoman) + 1;
398 darwin_locale = (char *) malloc (len);
399 if (!CFStringGetCString (locale_cfstr, darwin_locale, len, kCFStringEncodingMacRoman)) {
400 free (darwin_locale);
402 darwin_locale = NULL;
406 for (i = 0; i < strlen (darwin_locale); i++)
407 if (darwin_locale [i] == '_')
408 darwin_locale [i] = '-';
415 return g_strdup (darwin_locale);
420 get_posix_locale (void)
424 locale = g_getenv ("LC_ALL");
425 if (locale == NULL) {
426 locale = g_getenv ("LANG");
428 locale = setlocale (LC_ALL, NULL);
433 /* Skip English-only locale 'C' */
434 if (strcmp (locale, "C") == 0)
437 return g_strdup (locale);
442 get_current_locale_name (void)
448 locale = g_win32_getlocale ();
449 #elif defined (__APPLE__)
450 locale = get_darwin_locale ();
452 locale = get_posix_locale ();
454 locale = get_posix_locale ();
460 p = strchr (locale, '.');
463 p = strchr (locale, '@');
466 p = strchr (locale, '_');
470 ret = g_ascii_strdown (locale, -1);
477 ves_icall_System_Globalization_CultureInfo_get_current_locale_name (void)
483 locale = get_current_locale_name ();
487 domain = mono_domain_get ();
488 ret = mono_string_new (domain, locale);
495 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_lcid (MonoCultureInfo *this_obj,
498 const CultureInfoEntry *ci;
500 ci = culture_info_entry_from_lcid (lcid);
504 return construct_culture (this_obj, ci);
508 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_name (MonoCultureInfo *this_obj,
511 const CultureInfoNameEntry *ne;
514 n = mono_string_to_utf8 (name);
515 ne = (const CultureInfoNameEntry *)mono_binary_search (n, culture_name_entries, NUM_CULTURE_ENTRIES,
516 sizeof (CultureInfoNameEntry), culture_name_locator);
519 /*g_print ("ne (%s) is null\n", n);*/
525 return construct_culture (this_obj, &culture_entries [ne->culture_entry_index]);
529 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_specific_name (MonoCultureInfo *ci,
535 locale = mono_string_to_utf8 (name);
536 ret = construct_culture_from_specific_name (ci, locale);
543 ves_icall_System_Globalization_RegionInfo_construct_internal_region_from_lcid (MonoRegionInfo *this_obj,
546 const RegionInfoEntry *ri;
548 ri = region_info_entry_from_lcid (lcid);
552 return construct_region (this_obj, ri);
556 ves_icall_System_Globalization_RegionInfo_construct_internal_region_from_name (MonoRegionInfo *this_obj,
559 const RegionInfoNameEntry *ne;
562 n = mono_string_to_utf8 (name);
563 ne = (const RegionInfoNameEntry *)mono_binary_search (n, region_name_entries, NUM_REGION_ENTRIES,
564 sizeof (RegionInfoNameEntry), region_name_locator);
567 /*g_print ("ne (%s) is null\n", n);*/
573 return construct_region (this_obj, ®ion_entries [ne->region_entry_index]);
577 ves_icall_System_Globalization_CultureInfo_internal_get_cultures (MonoBoolean neutral,
578 MonoBoolean specific, MonoBoolean installed)
583 MonoCultureInfo *culture;
585 const CultureInfoEntry *ci;
589 domain = mono_domain_get ();
592 for (i = 0; i < NUM_CULTURE_ENTRIES; i++) {
593 ci = &culture_entries [i];
594 is_neutral = ci->territory == 0;
595 if ((neutral && is_neutral) || (specific && !is_neutral))
599 klass = mono_class_get_culture_info_class ();
601 /* The InvariantCulture is not in culture_entries */
602 /* We reserve the first slot in the array for it */
606 ret = mono_array_new (domain, klass, len);
613 mono_array_setref (ret, len++, NULL);
615 for (i = 0; i < NUM_CULTURE_ENTRIES; i++) {
616 ci = &culture_entries [i];
617 is_neutral = ci->territory == 0;
618 if ((neutral && is_neutral) || (specific && !is_neutral)) {
619 culture = (MonoCultureInfo *) mono_object_new_checked (domain, klass, &error);
620 mono_error_raise_exception (&error);
621 mono_runtime_object_init ((MonoObject *) culture);
622 construct_culture (culture, ci);
623 culture->use_user_override = TRUE;
624 mono_array_setref (ret, len++, culture);
631 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)
633 /* Do a normal ascii string compare, as we only know the
634 * invariant locale if we dont have ICU
636 return(string_invariant_compare (str1, off1, len1, str2, off2, len2,
640 void ves_icall_System_Globalization_CompareInfo_assign_sortkey (MonoCompareInfo *this_obj, MonoSortKey *key, MonoString *source, gint32 options)
645 keylen=mono_string_length (source);
647 arr=mono_array_new (mono_domain_get (), mono_get_byte_class (),
649 for(i=0; i<keylen; i++) {
650 mono_array_set (arr, guint8, i, mono_string_chars (source)[i]);
653 MONO_OBJECT_SETREF (key, key, arr);
656 int ves_icall_System_Globalization_CompareInfo_internal_index (MonoCompareInfo *this_obj, MonoString *source, gint32 sindex, gint32 count, MonoString *value, gint32 options, MonoBoolean first)
658 return(string_invariant_indexof (source, sindex, count, value, first));
661 int ves_icall_System_Globalization_CompareInfo_internal_index_char (MonoCompareInfo *this_obj, MonoString *source, gint32 sindex, gint32 count, gunichar2 value, gint32 options, MonoBoolean first)
663 return(string_invariant_indexof_char (source, sindex, count, value,
667 int ves_icall_System_Threading_Thread_current_lcid (void)
673 MonoString *ves_icall_System_String_InternalReplace_Str_Comp (MonoString *this_obj, MonoString *old, MonoString *new_, MonoCompareInfo *comp)
675 /* Do a normal ascii string compare and replace, as we only
676 * know the invariant locale if we dont have ICU
678 return(string_invariant_replace (this_obj, old, new_));
681 static gint32 string_invariant_compare_char (gunichar2 c1, gunichar2 c2,
686 /* Ordinal can not be mixed with other options, and must return the difference, not only -1, 0, 1 */
687 if (options & CompareOptions_Ordinal)
688 return (gint32) c1 - c2;
690 if (options & CompareOptions_IgnoreCase) {
691 GUnicodeType c1type, c2type;
693 c1type = g_unichar_type (c1);
694 c2type = g_unichar_type (c2);
696 result = (gint32) (c1type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c1) : c1) -
697 (c2type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c2) : c2);
700 * No options. Kana, symbol and spacing options don't
701 * apply to the invariant culture.
705 * FIXME: here we must use the information from c1type and c2type
706 * to find out the proper collation, even on the InvariantCulture, the
707 * sorting is not done by computing the unicode values, but their
710 result = (gint32) c1 - c2;
713 return ((result < 0) ? -1 : (result > 0) ? 1 : 0);
716 static gint32 string_invariant_compare (MonoString *str1, gint32 off1,
717 gint32 len1, MonoString *str2,
718 gint32 off2, gint32 len2,
721 /* c translation of C# code from old string.cs.. :) */
734 ustr1 = mono_string_chars(str1)+off1;
735 ustr2 = mono_string_chars(str2)+off2;
739 for (pos = 0; pos != length; pos++) {
740 if (pos >= len1 || pos >= len2)
743 charcmp = string_invariant_compare_char(ustr1[pos], ustr2[pos],
750 /* the lesser wins, so if we have looped until length we just
751 * need to check the last char
754 return(string_invariant_compare_char(ustr1[pos - 1],
755 ustr2[pos - 1], options));
758 /* Test if one of the strings has been compared to the end */
765 } else if (pos >= len2) {
769 /* if not, check our last char only.. (can this happen?) */
770 return(string_invariant_compare_char(ustr1[pos], ustr2[pos], options));
773 static MonoString *string_invariant_replace (MonoString *me,
774 MonoString *oldValue,
775 MonoString *newValue)
780 gunichar2 *dest=NULL; /* shut gcc up */
782 gunichar2 *newstr=NULL; /* shut gcc up here too */
793 oldstr = mono_string_chars(oldValue);
794 oldstrlen = mono_string_length(oldValue);
796 if (NULL != newValue) {
797 newstr = mono_string_chars(newValue);
798 newstrlen = mono_string_length(newValue);
802 src = mono_string_chars(me);
803 srclen = mono_string_length(me);
805 if (oldstrlen != newstrlen) {
807 while (i <= srclen - oldstrlen) {
808 if (0 == memcmp(src + i, oldstr, oldstrlen * sizeof(gunichar2))) {
817 newsize = srclen + ((newstrlen - oldstrlen) * occurr);
824 if (0 == memcmp(src + i, oldstr, oldstrlen * sizeof(gunichar2))) {
826 ret = mono_string_new_size_checked (mono_domain_get (), newsize, &error);
827 mono_error_raise_exception (&error); /* FIXME don't raise here */
828 dest = mono_string_chars(ret);
829 memcpy (dest, src, i * sizeof(gunichar2));
832 memcpy(dest + destpos, newstr, newstrlen * sizeof(gunichar2));
833 destpos += newstrlen;
837 } else if (ret != NULL) {
838 dest[destpos] = src[i];
850 static gint32 string_invariant_indexof (MonoString *source, gint32 sindex,
851 gint32 count, MonoString *value,
859 lencmpstr = mono_string_length(value);
861 src = mono_string_chars(source);
862 cmpstr = mono_string_chars(value);
866 for(pos=sindex;pos <= sindex+count;pos++) {
867 for(i=0;src[pos+i]==cmpstr[i];) {
876 for(pos=sindex-lencmpstr+1;pos>sindex-count;pos--) {
877 if(memcmp (src+pos, cmpstr,
878 lencmpstr*sizeof(gunichar2))==0) {
887 static gint32 string_invariant_indexof_char (MonoString *source, gint32 sindex,
888 gint32 count, gunichar2 value,
894 src = mono_string_chars(source);
896 for (pos = sindex; pos != count + sindex; pos++) {
897 if (src [pos] == value) {
904 for (pos = sindex; pos > sindex - count; pos--) {
905 if (src [pos] == value)
913 void load_normalization_resource (guint8 **argProps,
914 guint8 **argMappedChars,
915 guint8 **argCharMapIndex,
916 guint8 **argHelperIndex,
917 guint8 **argMapIdxToComposite,
918 guint8 **argCombiningClass)
920 #ifdef DISABLE_NORMALIZATION
921 mono_set_pending_exception (mono_get_exception_not_supported ("This runtime has been compiled without string normalization support."));
924 *argProps = (guint8*)props;
925 *argMappedChars = (guint8*) mappedChars;
926 *argCharMapIndex = (guint8*) charMapIndex;
927 *argHelperIndex = (guint8*) helperIndex;
928 *argMapIdxToComposite = (guint8*) mapIdxToComposite;
929 *argCombiningClass = (guint8*)combiningClass;