1 /**
2 * \file
3 * Culture-sensitive handling
4 *
5 * Authors:
6 * Dick Porter (dick@ximian.com)
7 * Mohammad DAMT (mdamt@cdl2000.com)
8 * Marek Safar (marek.safar@gmail.com)
9 *
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.
15 */
16
17 #include <config.h>
18 #include <glib.h>
19 #include <string.h>
20
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>
30
31 #ifndef DISABLE_NORMALIZATION
32 #include <mono/metadata/normalization-tables.h>
33 #endif
34
35 #include <locale.h>
36 #if defined(__APPLE__)
37 #include <CoreFoundation/CoreFoundation.h>
38 #endif
39
40 #undef DEBUG
41
42 static gint32 string_invariant_compare_char (gunichar2 c1, gunichar2 c2,
43 gint32 options);
44 static gint32 string_invariant_compare (MonoString *str1, gint32 off1,
45 gint32 len1, MonoString *str2,
46 gint32 off2, gint32 len2,
47 gint32 options);
48 static gint32 string_invariant_indexof (MonoString *source, gint32 sindex,
49 gint32 count, MonoString *value,
50 MonoBoolean first);
51 static gint32 string_invariant_indexof_char (MonoString *source, gint32 sindex,
52 gint32 count, gunichar2 value,
53 MonoBoolean first);
54
55 static const CultureInfoEntry* culture_info_entry_from_lcid (int lcid);
56
57 static const RegionInfoEntry* region_info_entry_from_lcid (int lcid);
58
59 /* Lazy class loading functions */
60 static GENERATE_GET_CLASS_WITH_CACHE (culture_info, "System.Globalization", "CultureInfo")
61
62 static int
culture_lcid_locator(const void * a,const void * b)63 culture_lcid_locator (const void *a, const void *b)
64 {
65 const int *lcid = (const int *)a;
66 const CultureInfoEntry *bb = (const CultureInfoEntry *)b;
67
68 return *lcid - bb->lcid;
69 }
70
71 static int
culture_name_locator(const void * a,const void * b)72 culture_name_locator (const void *a, const void *b)
73 {
74 const char *aa = (const char *)a;
75 const CultureInfoNameEntry *bb = (const CultureInfoNameEntry *)b;
76 int ret;
77
78 ret = strcmp (aa, idx2string (bb->name));
79
80 return ret;
81 }
82
83 static int
region_name_locator(const void * a,const void * b)84 region_name_locator (const void *a, const void *b)
85 {
86 const char *aa = (const char *)a;
87 const RegionInfoNameEntry *bb = (const RegionInfoNameEntry *)b;
88 int ret;
89
90 ret = strcmp (aa, idx2string (bb->name));
91
92 return ret;
93 }
94
95 static MonoArray*
create_group_sizes_array(const gint * gs,gint ml,MonoError * error)96 create_group_sizes_array (const gint *gs, gint ml, MonoError *error)
97 {
98 MonoArray *ret;
99 int i, len = 0;
100
101 error_init (error);
102
103 for (i = 0; i < ml; i++) {
104 if (gs [i] == -1)
105 break;
106 len++;
107 }
108
109 ret = mono_array_new_cached (mono_domain_get (),
110 mono_get_int32_class (), len, error);
111 return_val_if_nok (error, NULL);
112
113 for(i = 0; i < len; i++)
114 mono_array_set (ret, gint32, i, gs [i]);
115
116 return ret;
117 }
118
119 static MonoArray*
create_names_array_idx(const guint16 * names,int ml,MonoError * error)120 create_names_array_idx (const guint16 *names, int ml, MonoError *error)
121 {
122 MonoArray *ret;
123 MonoDomain *domain;
124 int i;
125
126 error_init (error);
127
128 if (names == NULL)
129 return NULL;
130
131 domain = mono_domain_get ();
132
133 ret = mono_array_new_cached (mono_domain_get (), mono_get_string_class (), ml, error);
134 return_val_if_nok (error, NULL);
135
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);
140 }
141
142 return ret;
143 }
144
145 static MonoArray*
create_names_array_idx_dynamic(const guint16 * names,int ml,MonoError * error)146 create_names_array_idx_dynamic (const guint16 *names, int ml, MonoError *error)
147 {
148 MonoArray *ret;
149 MonoDomain *domain;
150 int i, len = 0;
151
152 error_init (error);
153
154 if (names == NULL)
155 return NULL;
156
157 domain = mono_domain_get ();
158
159 for (i = 0; i < ml; i++) {
160 if (names [i] == 0)
161 break;
162 len++;
163 }
164
165 ret = mono_array_new_cached (mono_domain_get (), mono_get_string_class (), len, error);
166 return_val_if_nok (error, NULL);
167
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);
172 }
173
174 return ret;
175 }
176
177 MonoBoolean
ves_icall_System_Globalization_CalendarData_fill_calendar_data(MonoCalendarData * this_obj,MonoString * name,gint32 calendar_index)178 ves_icall_System_Globalization_CalendarData_fill_calendar_data (MonoCalendarData *this_obj, MonoString *name, gint32 calendar_index)
179 {
180 MonoError error;
181 MonoDomain *domain;
182 const DateTimeFormatEntry *dfe;
183 const CultureInfoNameEntry *ne;
184 const CultureInfoEntry *ci;
185 char *n;
186
187 n = mono_string_to_utf8_checked (name, &error);
188 if (mono_error_set_pending_exception (&error))
189 return FALSE;
190 ne = (const CultureInfoNameEntry *)mono_binary_search (n, culture_name_entries, NUM_CULTURE_ENTRIES,
191 sizeof (CultureInfoNameEntry), culture_name_locator);
192 g_free (n);
193 if (ne == NULL) {
194 return FALSE;
195 }
196
197 ci = &culture_entries [ne->culture_entry_index];
198 dfe = &datetime_format_entries [ci->datetime_format_index];
199
200 domain = mono_domain_get ();
201
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);
213
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);
218
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);
222
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);
226
227 MonoArray *abbr_day_names = create_names_array_idx (dfe->abbreviated_day_names,
228 NUM_DAYS, &error);
229 return_val_and_set_pending_if_nok (&error, FALSE);
230 MONO_OBJECT_SETREF (this_obj, AbbreviatedDayNames, abbr_day_names);
231
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);
235
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);
239
240 MonoArray *abbr_mon_names = create_names_array_idx (dfe->abbreviated_month_names,
241 NUM_MONTHS, &error);
242 return_val_and_set_pending_if_nok (&error, FALSE);
243 MONO_OBJECT_SETREF (this_obj, AbbreviatedMonthNames, abbr_mon_names);
244
245
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);
249
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);
253
254 return TRUE;
255 }
256
257 void
ves_icall_System_Globalization_CultureData_fill_culture_data(MonoCultureData * this_obj,gint32 datetime_index)258 ves_icall_System_Globalization_CultureData_fill_culture_data (MonoCultureData *this_obj, gint32 datetime_index)
259 {
260 MonoError error;
261 MonoDomain *domain;
262 const DateTimeFormatEntry *dfe;
263
264 g_assert (datetime_index >= 0);
265
266 dfe = &datetime_format_entries [datetime_index];
267
268 domain = mono_domain_get ();
269
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))) \
273 return; \
274 MONO_OBJECT_SETREF((obj), field, _tmp_str); \
275 } while (0)
276
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);
280 #undef SET_STR
281
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))
285 return;
286 MONO_OBJECT_SETREF (this_obj, LongTimePatterns, long_time_patterns);
287
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))
291 return;
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;
295 }
296
297 void
ves_icall_System_Globalization_CultureData_fill_number_data(MonoNumberFormatInfo * number,gint32 number_index)298 ves_icall_System_Globalization_CultureData_fill_number_data (MonoNumberFormatInfo* number, gint32 number_index)
299 {
300 MonoError error;
301 MonoDomain *domain;
302 const NumberFormatEntry *nfe;
303
304 g_assert (number_index >= 0);
305
306 nfe = &number_format_entries [number_index];
307
308 domain = mono_domain_get ();
309
310 number->currencyDecimalDigits = nfe->currency_decimal_digits;
311
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))) \
315 return; \
316 MONO_OBJECT_SETREF((obj), field, _tmp_str); \
317 } while (0)
318
319 SET_STR (number, currencyDecimalSeparator, domain, idx2string (nfe->currency_decimal_separator), &error);
320 SET_STR (number, currencyGroupSeparator, domain, idx2string (nfe->currency_group_separator), &error);
321
322 MonoArray *currency_sizes_arr = create_group_sizes_array (nfe->currency_group_sizes,
323 GROUP_SIZE, &error);
324 if (mono_error_set_pending_exception (&error))
325 return;
326 MONO_OBJECT_SETREF (number, currencyGroupSizes, currency_sizes_arr);
327 number->currencyNegativePattern = nfe->currency_negative_pattern;
328 number->currencyPositivePattern = nfe->currency_positive_pattern;
329
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,
338 GROUP_SIZE, &error);
339 if (mono_error_set_pending_exception (&error))
340 return;
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);
349 #undef SET_STR
350 }
351
352 static MonoBoolean
construct_culture(MonoCultureInfo * this_obj,const CultureInfoEntry * ci,MonoError * error)353 construct_culture (MonoCultureInfo *this_obj, const CultureInfoEntry *ci, MonoError *error)
354 {
355 MonoDomain *domain = mono_domain_get ();
356
357 error_init (error);
358
359 this_obj->lcid = ci->lcid;
360
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); \
365 } while (0)
366
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);
373
374 // It's null for neutral cultures
375 if (ci->territory > 0) {
376 SET_STR (this_obj, territory, domain, idx2string (ci->territory), error);
377 }
378
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;
387 #undef SET_STR
388
389 return TRUE;
390 }
391
392 static MonoBoolean
construct_region(MonoRegionInfo * this_obj,const RegionInfoEntry * ri,MonoError * error)393 construct_region (MonoRegionInfo *this_obj, const RegionInfoEntry *ri, MonoError *error)
394 {
395 MonoDomain *domain = mono_domain_get ();
396
397 error_init (error);
398
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); \
403 } while (0)
404
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);
415
416 #undef SET_STR
417
418 return TRUE;
419 }
420
421 static const CultureInfoEntry*
culture_info_entry_from_lcid(int lcid)422 culture_info_entry_from_lcid (int lcid)
423 {
424 const CultureInfoEntry *ci;
425
426 ci = (const CultureInfoEntry *)mono_binary_search (&lcid, culture_entries, NUM_CULTURE_ENTRIES, sizeof (CultureInfoEntry), culture_lcid_locator);
427
428 return ci;
429 }
430
431 static const RegionInfoEntry*
region_info_entry_from_lcid(int lcid)432 region_info_entry_from_lcid (int lcid)
433 {
434 const RegionInfoEntry *entry;
435 const CultureInfoEntry *ne;
436
437 ne = (const CultureInfoEntry *)mono_binary_search (&lcid, culture_entries, NUM_CULTURE_ENTRIES, sizeof (CultureInfoEntry), culture_lcid_locator);
438
439 if (ne == NULL)
440 return FALSE;
441
442 entry = ®ion_entries [ne->region_entry_index];
443
444 return entry;
445 }
446
447 #if defined (__APPLE__)
448 static gchar*
get_darwin_locale(void)449 get_darwin_locale (void)
450 {
451 static gchar *cached_locale = NULL;
452 gchar *darwin_locale = NULL;
453 CFLocaleRef locale = NULL;
454 CFStringRef locale_language = NULL;
455 CFStringRef locale_country = NULL;
456 CFStringRef locale_script = NULL;
457 CFStringRef locale_cfstr = NULL;
458 CFIndex bytes_converted;
459 CFIndex bytes_written;
460 CFIndex len;
461 int i;
462
463 if (cached_locale != NULL)
464 return g_strdup (cached_locale);
465
466 locale = CFLocaleCopyCurrent ();
467
468 if (locale) {
469 locale_language = CFLocaleGetValue (locale, kCFLocaleLanguageCode);
470 if (locale_language != NULL && CFStringGetBytes(locale_language, CFRangeMake (0, CFStringGetLength (locale_language)), kCFStringEncodingMacRoman, 0, FALSE, NULL, 0, &bytes_converted) > 0) {
471 len = bytes_converted + 1;
472
473 locale_country = CFLocaleGetValue (locale, kCFLocaleCountryCode);
474 if (locale_country != NULL && CFStringGetBytes (locale_country, CFRangeMake (0, CFStringGetLength (locale_country)), kCFStringEncodingMacRoman, 0, FALSE, NULL, 0, &bytes_converted) > 0) {
475 len += bytes_converted + 1;
476
477 locale_script = CFLocaleGetValue (locale, kCFLocaleScriptCode);
478 if (locale_script != NULL && CFStringGetBytes (locale_script, CFRangeMake (0, CFStringGetLength (locale_script)), kCFStringEncodingMacRoman, 0, FALSE, NULL, 0, &bytes_converted) > 0) {
479 len += bytes_converted + 1;
480 }
481
482 darwin_locale = (char *) g_malloc (len + 1);
483 CFStringGetBytes (locale_language, CFRangeMake (0, CFStringGetLength (locale_language)), kCFStringEncodingMacRoman, 0, FALSE, (UInt8 *) darwin_locale, len, &bytes_converted);
484
485 darwin_locale[bytes_converted] = '-';
486 bytes_written = bytes_converted + 1;
487 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) {
488 darwin_locale[bytes_written + bytes_converted] = '-';
489 bytes_written += bytes_converted + 1;
490 }
491
492 CFStringGetBytes (locale_country, CFRangeMake (0, CFStringGetLength (locale_country)), kCFStringEncodingMacRoman, 0, FALSE, (UInt8 *) &darwin_locale[bytes_written], len - bytes_written, &bytes_converted);
493 darwin_locale[bytes_written + bytes_converted] = '\0';
494 }
495 }
496
497 if (darwin_locale == NULL) {
498 locale_cfstr = CFLocaleGetIdentifier (locale);
499
500 if (locale_cfstr) {
501 len = CFStringGetMaximumSizeForEncoding (CFStringGetLength (locale_cfstr), kCFStringEncodingMacRoman) + 1;
502 darwin_locale = (char *) g_malloc (len);
503 if (!CFStringGetCString (locale_cfstr, darwin_locale, len, kCFStringEncodingMacRoman)) {
504 g_free (darwin_locale);
505 CFRelease (locale);
506 cached_locale = NULL;
507 return NULL;
508 }
509
510 for (i = 0; i < strlen (darwin_locale); i++)
511 if (darwin_locale [i] == '_')
512 darwin_locale [i] = '-';
513 }
514 }
515
516 CFRelease (locale);
517 }
518
519 mono_memory_barrier ();
520 cached_locale = darwin_locale;
521 return g_strdup (cached_locale);
522 }
523 #endif
524
525 static char *
get_posix_locale(void)526 get_posix_locale (void)
527 {
528 char *locale;
529
530 locale = g_getenv ("LC_ALL");
531 if (locale == NULL) {
532 locale = g_getenv ("LANG");
533 if (locale == NULL) {
534 char *static_locale = setlocale (LC_ALL, NULL);
535 if (static_locale)
536 locale = g_strdup (static_locale);
537 }
538 }
539 if (locale == NULL)
540 return NULL;
541
542 /* Skip English-only locale 'C' */
543 if (strcmp (locale, "C") == 0) {
544 g_free (locale);
545 return NULL;
546 }
547
548 return locale;
549 }
550
551
552 static gchar *
get_current_locale_name(void)553 get_current_locale_name (void)
554 {
555 char *locale;
556 char *p, *ret;
557
558 #ifdef HOST_WIN32
559 locale = g_win32_getlocale ();
560 #elif defined (__APPLE__)
561 locale = get_darwin_locale ();
562 if (!locale)
563 locale = get_posix_locale ();
564 #else
565 locale = get_posix_locale ();
566 #endif
567
568 if (locale == NULL)
569 return NULL;
570
571 p = strchr (locale, '.');
572 if (p != NULL)
573 *p = 0;
574 p = strchr (locale, '@');
575 if (p != NULL)
576 *p = 0;
577 p = strchr (locale, '_');
578 if (p != NULL)
579 *p = '-';
580
581 ret = g_ascii_strdown (locale, -1);
582 g_free (locale);
583
584 return ret;
585 }
586
587 MonoStringHandle
ves_icall_System_Globalization_CultureInfo_get_current_locale_name(MonoError * error)588 ves_icall_System_Globalization_CultureInfo_get_current_locale_name (MonoError *error)
589 {
590 error_init (error);
591 gchar *locale;
592 MonoDomain *domain;
593
594 locale = get_current_locale_name ();
595 if (locale == NULL)
596 return MONO_HANDLE_CAST (MonoString, NULL_HANDLE);
597
598 domain = mono_domain_get ();
599 MonoStringHandle ret = mono_string_new_handle (domain, locale, error);
600 g_free (locale);
601
602 return ret;
603 }
604
605 MonoBoolean
ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_lcid(MonoCultureInfo * this_obj,gint lcid)606 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_lcid (MonoCultureInfo *this_obj,
607 gint lcid)
608 {
609 MonoError error;
610 const CultureInfoEntry *ci;
611
612 ci = culture_info_entry_from_lcid (lcid);
613 if(ci == NULL)
614 return FALSE;
615
616 if (!construct_culture (this_obj, ci, &error)) {
617 mono_error_set_pending_exception (&error);
618 return FALSE;
619 }
620 return TRUE;
621 }
622
623 MonoBoolean
ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_name(MonoCultureInfo * this_obj,MonoString * name)624 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_name (MonoCultureInfo *this_obj,
625 MonoString *name)
626 {
627 MonoError error;
628 const CultureInfoNameEntry *ne;
629 char *n;
630
631 n = mono_string_to_utf8_checked (name, &error);
632 if (mono_error_set_pending_exception (&error))
633 return FALSE;
634 ne = (const CultureInfoNameEntry *)mono_binary_search (n, culture_name_entries, NUM_CULTURE_ENTRIES,
635 sizeof (CultureInfoNameEntry), culture_name_locator);
636
637 if (ne == NULL) {
638 /*g_print ("ne (%s) is null\n", n);*/
639 g_free (n);
640 return FALSE;
641 }
642 g_free (n);
643
644 if (!construct_culture (this_obj, &culture_entries [ne->culture_entry_index], &error)) {
645 mono_error_set_pending_exception (&error);
646 return FALSE;
647 }
648 return TRUE;
649 }
650 /*
651 MonoBoolean
652 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_specific_name (MonoCultureInfo *ci,
653 MonoString *name)
654 {
655 gchar *locale;
656 gboolean ret;
657
658 locale = mono_string_to_utf8 (name);
659 ret = construct_culture_from_specific_name (ci, locale);
660 g_free (locale);
661
662 return ret;
663 }
664 */
665 MonoBoolean
ves_icall_System_Globalization_RegionInfo_construct_internal_region_from_lcid(MonoRegionInfo * this_obj,gint lcid)666 ves_icall_System_Globalization_RegionInfo_construct_internal_region_from_lcid (MonoRegionInfo *this_obj,
667 gint lcid)
668 {
669 MonoError error;
670 const RegionInfoEntry *ri;
671
672 ri = region_info_entry_from_lcid (lcid);
673 if(ri == NULL)
674 return FALSE;
675
676 MonoBoolean result = construct_region (this_obj, ri, &error);
677 mono_error_set_pending_exception (&error);
678 return result;
679 }
680
681 MonoBoolean
ves_icall_System_Globalization_RegionInfo_construct_internal_region_from_name(MonoRegionInfo * this_obj,MonoString * name)682 ves_icall_System_Globalization_RegionInfo_construct_internal_region_from_name (MonoRegionInfo *this_obj,
683 MonoString *name)
684 {
685 MonoError error;
686 const RegionInfoNameEntry *ne;
687 char *n;
688
689 n = mono_string_to_utf8_checked (name, &error);
690 if (mono_error_set_pending_exception (&error))
691 return FALSE;
692 ne = (const RegionInfoNameEntry *)mono_binary_search (n, region_name_entries, NUM_REGION_ENTRIES,
693 sizeof (RegionInfoNameEntry), region_name_locator);
694
695 if (ne == NULL) {
696 /*g_print ("ne (%s) is null\n", n);*/
697 g_free (n);
698 return FALSE;
699 }
700 g_free (n);
701
702 MonoBoolean result = construct_region (this_obj, ®ion_entries [ne->region_entry_index], &error);
703 mono_error_set_pending_exception (&error);
704 return result;
705 }
706
707 MonoArray*
ves_icall_System_Globalization_CultureInfo_internal_get_cultures(MonoBoolean neutral,MonoBoolean specific,MonoBoolean installed)708 ves_icall_System_Globalization_CultureInfo_internal_get_cultures (MonoBoolean neutral,
709 MonoBoolean specific, MonoBoolean installed)
710 {
711 MonoError error;
712 MonoArray *ret;
713 MonoClass *klass;
714 MonoCultureInfo *culture;
715 MonoDomain *domain;
716 const CultureInfoEntry *ci;
717 gint i, len;
718 gboolean is_neutral;
719
720 domain = mono_domain_get ();
721
722 len = 0;
723 for (i = 0; i < NUM_CULTURE_ENTRIES; i++) {
724 ci = &culture_entries [i];
725 is_neutral = ci->territory == 0;
726 if ((neutral && is_neutral) || (specific && !is_neutral))
727 len++;
728 }
729
730 klass = mono_class_get_culture_info_class ();
731
732 /* The InvariantCulture is not in culture_entries */
733 /* We reserve the first slot in the array for it */
734 if (neutral)
735 len++;
736
737 ret = mono_array_new_checked (domain, klass, len, &error);
738 goto_if_nok (&error, fail);
739
740 if (len == 0)
741 return ret;
742
743 len = 0;
744 if (neutral)
745 mono_array_setref (ret, len++, NULL);
746
747 for (i = 0; i < NUM_CULTURE_ENTRIES; i++) {
748 ci = &culture_entries [i];
749 is_neutral = ci->territory == 0;
750 if ((neutral && is_neutral) || (specific && !is_neutral)) {
751 culture = (MonoCultureInfo *) mono_object_new_checked (domain, klass, &error);
752 goto_if_nok (&error, fail);
753 mono_runtime_object_init_checked ((MonoObject *) culture, &error);
754 goto_if_nok (&error, fail);
755 if (!construct_culture (culture, ci, &error))
756 goto fail;
757 culture->use_user_override = TRUE;
758 mono_array_setref (ret, len++, culture);
759 }
760 }
761
762 return ret;
763
764 fail:
765 mono_error_set_pending_exception (&error);
766 return ret;
767 }
768
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 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)
770 {
771 /* Do a normal ascii string compare, as we only know the
772 * invariant locale if we dont have ICU
773 */
774 return(string_invariant_compare (str1, off1, len1, str2, off2, len2,
775 options));
776 }
777
ves_icall_System_Globalization_CompareInfo_assign_sortkey(MonoCompareInfo * this_obj,MonoSortKey * key,MonoString * source,gint32 options)778 void ves_icall_System_Globalization_CompareInfo_assign_sortkey (MonoCompareInfo *this_obj, MonoSortKey *key, MonoString *source, gint32 options)
779 {
780 MonoError error;
781 MonoArray *arr;
782 gint32 keylen, i;
783
784 keylen=mono_string_length (source);
785
786 arr=mono_array_new_checked (mono_domain_get (), mono_get_byte_class (),
787 keylen, &error);
788 if (mono_error_set_pending_exception (&error))
789 return;
790
791 for(i=0; i<keylen; i++) {
792 mono_array_set (arr, guint8, i, mono_string_chars (source)[i]);
793 }
794
795 MONO_OBJECT_SETREF (key, key, arr);
796 }
797
ves_icall_System_Globalization_CompareInfo_internal_index(MonoCompareInfo * this_obj,MonoString * source,gint32 sindex,gint32 count,MonoString * value,gint32 options,MonoBoolean first)798 int ves_icall_System_Globalization_CompareInfo_internal_index (MonoCompareInfo *this_obj, MonoString *source, gint32 sindex, gint32 count, MonoString *value, gint32 options, MonoBoolean first)
799 {
800 return(string_invariant_indexof (source, sindex, count, value, first));
801 }
802
ves_icall_System_Globalization_CompareInfo_internal_index_char(MonoCompareInfo * this_obj,MonoString * source,gint32 sindex,gint32 count,gunichar2 value,gint32 options,MonoBoolean first)803 int ves_icall_System_Globalization_CompareInfo_internal_index_char (MonoCompareInfo *this_obj, MonoString *source, gint32 sindex, gint32 count, gunichar2 value, gint32 options, MonoBoolean first)
804 {
805 return(string_invariant_indexof_char (source, sindex, count, value,
806 first));
807 }
808
ves_icall_System_Threading_Thread_current_lcid(void)809 int ves_icall_System_Threading_Thread_current_lcid (void)
810 {
811 /* Invariant */
812 return(0x007F);
813 }
814
string_invariant_compare_char(gunichar2 c1,gunichar2 c2,gint32 options)815 static gint32 string_invariant_compare_char (gunichar2 c1, gunichar2 c2,
816 gint32 options)
817 {
818 gint32 result;
819
820 /* Ordinal can not be mixed with other options, and must return the difference, not only -1, 0, 1 */
821 if (options & CompareOptions_Ordinal)
822 return (gint32) c1 - c2;
823
824 if (options & CompareOptions_IgnoreCase) {
825 GUnicodeType c1type, c2type;
826
827 c1type = g_unichar_type (c1);
828 c2type = g_unichar_type (c2);
829
830 result = (gint32) (c1type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c1) : c1) -
831 (c2type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c2) : c2);
832 } else {
833 /*
834 * No options. Kana, symbol and spacing options don't
835 * apply to the invariant culture.
836 */
837
838 /*
839 * FIXME: here we must use the information from c1type and c2type
840 * to find out the proper collation, even on the InvariantCulture, the
841 * sorting is not done by computing the unicode values, but their
842 * actual sort order.
843 */
844 result = (gint32) c1 - c2;
845 }
846
847 return ((result < 0) ? -1 : (result > 0) ? 1 : 0);
848 }
849
string_invariant_compare(MonoString * str1,gint32 off1,gint32 len1,MonoString * str2,gint32 off2,gint32 len2,gint32 options)850 static gint32 string_invariant_compare (MonoString *str1, gint32 off1,
851 gint32 len1, MonoString *str2,
852 gint32 off2, gint32 len2,
853 gint32 options)
854 {
855 /* c translation of C# code from old string.cs.. :) */
856 gint32 length;
857 gint32 charcmp;
858 gunichar2 *ustr1;
859 gunichar2 *ustr2;
860 gint32 pos;
861
862 if(len1 >= len2) {
863 length=len1;
864 } else {
865 length=len2;
866 }
867
868 ustr1 = mono_string_chars(str1)+off1;
869 ustr2 = mono_string_chars(str2)+off2;
870
871 pos = 0;
872
873 for (pos = 0; pos != length; pos++) {
874 if (pos >= len1 || pos >= len2)
875 break;
876
877 charcmp = string_invariant_compare_char(ustr1[pos], ustr2[pos],
878 options);
879 if (charcmp != 0) {
880 return(charcmp);
881 }
882 }
883
884 /* the lesser wins, so if we have looped until length we just
885 * need to check the last char
886 */
887 if (pos == length) {
888 return(string_invariant_compare_char(ustr1[pos - 1],
889 ustr2[pos - 1], options));
890 }
891
892 /* Test if one of the strings has been compared to the end */
893 if (pos >= len1) {
894 if (pos >= len2) {
895 return(0);
896 } else {
897 return(-1);
898 }
899 } else if (pos >= len2) {
900 return(1);
901 }
902
903 /* if not, check our last char only.. (can this happen?) */
904 return(string_invariant_compare_char(ustr1[pos], ustr2[pos], options));
905 }
906
string_invariant_indexof(MonoString * source,gint32 sindex,gint32 count,MonoString * value,MonoBoolean first)907 static gint32 string_invariant_indexof (MonoString *source, gint32 sindex,
908 gint32 count, MonoString *value,
909 MonoBoolean first)
910 {
911 gint32 lencmpstr;
912 gunichar2 *src;
913 gunichar2 *cmpstr;
914 gint32 pos,i;
915
916 lencmpstr = mono_string_length(value);
917
918 src = mono_string_chars(source);
919 cmpstr = mono_string_chars(value);
920
921 if(first) {
922 count -= lencmpstr;
923 for(pos=sindex;pos <= sindex+count;pos++) {
924 for(i=0;src[pos+i]==cmpstr[i];) {
925 if(++i==lencmpstr) {
926 return(pos);
927 }
928 }
929 }
930
931 return(-1);
932 } else {
933 for(pos=sindex-lencmpstr+1;pos>sindex-count;pos--) {
934 if(memcmp (src+pos, cmpstr,
935 lencmpstr*sizeof(gunichar2))==0) {
936 return(pos);
937 }
938 }
939
940 return(-1);
941 }
942 }
943
string_invariant_indexof_char(MonoString * source,gint32 sindex,gint32 count,gunichar2 value,MonoBoolean first)944 static gint32 string_invariant_indexof_char (MonoString *source, gint32 sindex,
945 gint32 count, gunichar2 value,
946 MonoBoolean first)
947 {
948 gint32 pos;
949 gunichar2 *src;
950
951 src = mono_string_chars(source);
952 if(first) {
953 for (pos = sindex; pos != count + sindex; pos++) {
954 if (src [pos] == value) {
955 return(pos);
956 }
957 }
958
959 return(-1);
960 } else {
961 for (pos = sindex; pos > sindex - count; pos--) {
962 if (src [pos] == value)
963 return(pos);
964 }
965
966 return(-1);
967 }
968 }
969
ves_icall_System_Text_Normalization_load_normalization_resource(guint8 ** argProps,guint8 ** argMappedChars,guint8 ** argCharMapIndex,guint8 ** argHelperIndex,guint8 ** argMapIdxToComposite,guint8 ** argCombiningClass,MonoError * error)970 void ves_icall_System_Text_Normalization_load_normalization_resource (guint8 **argProps,
971 guint8 **argMappedChars,
972 guint8 **argCharMapIndex,
973 guint8 **argHelperIndex,
974 guint8 **argMapIdxToComposite,
975 guint8 **argCombiningClass,
976 MonoError *error)
977 {
978 error_init (error);
979 #ifdef DISABLE_NORMALIZATION
980 mono_error_set_not_supported (error, "This runtime has been compiled without string normalization support.");
981 return;
982 #else
983 *argProps = (guint8*)props;
984 *argMappedChars = (guint8*) mappedChars;
985 *argCharMapIndex = (guint8*) charMapIndex;
986 *argHelperIndex = (guint8*) helperIndex;
987 *argMapIdxToComposite = (guint8*) mapIdxToComposite;
988 *argCombiningClass = (guint8*)combiningClass;
989 #endif
990 }
991
992
993