[mono-config] use right type for result of strlen
[mono.git] / mcs / class / corlib / System.Globalization / CultureInfo.cs
1 //
2 // System.Globalization.CultureInfo.cs
3 //
4 // Authors:
5 // Miguel de Icaza (miguel@ximian.com)
6 // Dick Porter (dick@ximian.com)
7 // Marek Safar (marek.safar@gmail.com)
8 //
9 // (C) 2001, 2002, 2003 Ximian, Inc. (http://www.ximian.com)
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 // Copyright (C) 2012 Xamarin Inc (http://www.xamarin.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System.Collections.Generic;
34 using System.Threading;
35 using System.Runtime.CompilerServices;
36 using System.Runtime.InteropServices;
37 using System.Diagnostics.Contracts;
38
39 namespace System.Globalization
40 {
41         [System.Runtime.InteropServices.ComVisible (true)]
42         [Serializable]
43         [StructLayout (LayoutKind.Sequential)]
44         public class CultureInfo : ICloneable, IFormatProvider
45         {
46                 static volatile CultureInfo invariant_culture_info = new CultureInfo (InvariantCultureId, false, true);
47                 static object shared_table_lock = new object ();
48                 static CultureInfo default_current_culture;
49
50 #pragma warning disable 169, 649
51                 bool m_isReadOnly;
52                 int  cultureID;
53                 [NonSerialized]
54                 int parent_lcid;
55                 [NonSerialized]
56                 int datetime_index;
57                 [NonSerialized]
58                 int number_index;
59                 [NonSerialized]
60                 int default_calendar_type;
61                 bool m_useUserOverride;
62                 [NonSerialized]
63                 internal volatile NumberFormatInfo numInfo;
64                 internal volatile DateTimeFormatInfo dateTimeInfo;
65                 volatile TextInfo textInfo;
66                 internal string m_name;
67                 
68                 [NonSerialized]
69                 private string englishname;
70                 [NonSerialized]
71                 private string nativename;
72                 [NonSerialized]
73                 private string iso3lang;
74                 [NonSerialized]
75                 private string iso2lang;
76                 [NonSerialized]
77                 private string win3lang;
78                 [NonSerialized]
79                 private string territory;
80                 [NonSerialized]
81                 string[] native_calendar_names;
82
83                 volatile CompareInfo compareInfo;
84                 [NonSerialized]
85                 private unsafe readonly void *textinfo_data;
86
87                 [StructLayout (LayoutKind.Sequential)]
88                 struct Data {
89                         public int ansi;
90                         public int ebcdic;
91                         public int mac;
92                         public int oem;
93                         public bool right_to_left;
94                         public byte list_sep;
95                 }
96
97                 int m_dataItem;         // MS.NET serializes this.
98 #pragma warning restore 169, 649
99
100                 Calendar calendar;
101
102                 [NonSerialized]
103                 CultureInfo parent_culture;
104
105                 // Deserialized instances will set this to false
106                 [NonSerialized]
107                 bool constructed;
108
109                 [NonSerialized]
110                 // Used by Thread.set_CurrentCulture
111                 internal byte[] cached_serialized_form;
112
113                 [NonSerialized]internal CultureData m_cultureData;
114                 [NonSerialized]internal bool m_isInherited;
115                 
116                 internal const int InvariantCultureId = 0x7F;
117                 const int CalendarTypeBits = 8;
118
119                 const string MSG_READONLY = "This instance is read only";
120                 
121                 public static CultureInfo InvariantCulture {
122                         get {
123                                 return invariant_culture_info;
124                         }
125                 }
126
127                 public static CultureInfo CurrentCulture {
128                         get {
129                                 return Thread.CurrentThread.CurrentCulture;
130                         }
131                 }
132
133                 public static CultureInfo CurrentUICulture { 
134                         get {
135                                 return Thread.CurrentThread.CurrentUICulture;
136                         }
137                 }
138
139                 internal static CultureInfo ConstructCurrentCulture ()
140                 {
141                         if (default_current_culture != null)
142                                 return default_current_culture;
143
144                         var locale_name = get_current_locale_name ();
145                         CultureInfo ci = null;
146                         try {
147                                 ci = CreateSpecificCulture (locale_name);
148                         } catch {
149                         }
150
151                         if (ci == null) {
152                                 ci = InvariantCulture;
153                         } else {
154                                 ci.m_isReadOnly = true;
155                                 ci.m_useUserOverride = true;
156                         }
157
158                         default_current_culture = ci;
159                         return ci;
160                 }
161
162                 internal static CultureInfo ConstructCurrentUICulture ()
163                 {
164                         return ConstructCurrentCulture ();
165                 }
166
167                 // it is used for RegionInfo.
168                 internal string Territory {
169                         get { return territory; }
170                 }
171
172 #if !NET_2_1
173                 // FIXME: It is implemented, but would be hell slow.
174                 [ComVisible (false)]
175                 public CultureTypes CultureTypes {
176                         get {
177                                 CultureTypes ret = (CultureTypes) 0;
178                                 foreach (CultureTypes v in Enum.GetValues (typeof (CultureTypes)))
179                                         if (Array.IndexOf (GetCultures (v), this) >= 0)
180                                                 ret |= v;
181                                 return ret;
182                         }
183                 }
184
185                 [ComVisible (false)]
186                 public CultureInfo GetConsoleFallbackUICulture ()
187                 {
188                         // as documented in MSDN ...
189                         switch (Name) {
190                         case "ar": case "ar-BH": case "ar-EG": case "ar-IQ":
191                         case "ar-JO": case "ar-KW": case "ar-LB": case "ar-LY":
192                         case "ar-QA": case "ar-SA": case "ar-SY": case "ar-AE":
193                         case "ar-YE":
194                         case "dv": case "dv-MV":
195                         case "fa": case "fa-IR":
196                         case "gu": case "gu-IN":
197                         case "he": case "he-IL":
198                         case "hi": case "hi-IN":
199                         case "kn": case "kn-IN":
200                         case "kok": case "kok-IN":
201                         case "mr": case "mr-IN":
202                         case "pa": case "pa-IN":
203                         case "sa": case "sa-IN":
204                         case "syr": case "syr-SY":
205                         case "ta": case "ta-IN":
206                         case "te": case "te-IN":
207                         case "th": case "th-TH":
208                         case "ur": case "ur-PK":
209                         case "vi": case "vi-VN":
210                                 return GetCultureInfo ("en");
211                         case "ar-DZ": case "ar-MA": case "ar-TN":
212                                 return GetCultureInfo ("fr");
213                         }
214                         return (CultureTypes & CultureTypes.WindowsOnlyCultures) != 0 ? CultureInfo.InvariantCulture : this;
215                 }
216
217                 [ComVisible (false)]
218                 public string IetfLanguageTag {
219                         // There could be more consistent way to implement
220                         // it, but right now it works just fine with this...
221                         get {
222                                 switch (Name) {
223                                 case "zh-CHS":
224                                         return "zh-Hans";
225                                 case "zh-CHT":
226                                         return "zh-Hant";
227                                 default:
228                                         return Name;
229                                 }
230                         }
231                 }
232
233                 // For specific cultures it basically returns LCID.
234                 // For neutral cultures it is mapped to the default(?) specific
235                 // culture, where the LCID of the specific culture seems to be
236                 // n + 1024 by default. zh-CHS is the only exception which is 
237                 // mapped to 2052, not 1028 (zh-CHT is mapped to 1028 instead).
238                 // There are very few exceptions, here I simply list them here.
239                 // It is Windows-specific property anyways, so no worthy of
240                 // trying to do some complex things with locale-builder.
241                 [ComVisible (false)]
242                 public virtual int KeyboardLayoutId {
243                         get {
244                                 switch (LCID) {
245                                 case 4: // zh-CHS (neutral)
246                                         return 2052;
247                                 case 1034: // es-ES Spanish 2
248                                         return 3082;
249                                 case 31748: // zh-CHT (neutral)
250                                         return 1028;
251                                 case 31770: // sr (neutral)
252                                         return 2074;
253                                 default:
254                                         return LCID < 1024 ? LCID + 1024 : LCID;
255                                 }
256                         }
257                 }
258 #endif
259
260                 public virtual int LCID {
261                         get {
262                                 return cultureID;
263                         }
264                 }
265
266                 public virtual string Name {
267                         get {
268                                 return(m_name);
269                         }
270                 }
271
272                 public virtual string NativeName {
273                         get {
274                                 if (!constructed) Construct ();
275                                 return nativename;
276                         }
277                 }
278
279                 internal string NativeCalendarName {
280                         get {
281                                 if (!constructed) Construct ();
282                                 return native_calendar_names[(default_calendar_type >> CalendarTypeBits) - 1];
283                         }
284                 }
285                 
286                 public virtual Calendar Calendar {
287                         get {
288                                 if (calendar == null) {
289                                         if (!constructed) Construct ();
290                                         calendar = CreateCalendar (default_calendar_type);
291                                 }
292
293                                 return calendar;
294                         }
295                 }
296
297                 [MonoLimitation ("Optional calendars are not supported only default calendar is returned")]
298                 public virtual Calendar[] OptionalCalendars {
299                         get {
300                                 return new[] { Calendar };
301                         }
302                 }
303
304                 public virtual CultureInfo Parent
305                 {
306                         get {
307                                 if (parent_culture == null) {
308                                         if (!constructed)
309                                                 Construct ();
310                                         if (parent_lcid == cultureID) {
311                                                 //
312                                                 // Parent lcid is same but culture info is not for legacy zh culture
313                                                 //
314                                                 if (parent_lcid == 0x7C04 && EnglishName [EnglishName.Length - 1] == 'y')
315                                                         return parent_culture = new CultureInfo ("zh-Hant");
316                                                 else if (parent_lcid == 0x0004 && EnglishName [EnglishName.Length -1] == 'y')
317                                                         return parent_culture = new CultureInfo ("zh-Hans");
318                                                 return null;
319                                         }
320                                         
321                                         if (parent_lcid == InvariantCultureId)
322                                                 parent_culture = InvariantCulture;
323                                         else if (cultureID == InvariantCultureId)
324                                                 parent_culture = this;
325                                         else if (cultureID == 0x0404) {
326                                                 // zh-tw has parent id 0x7C04 which is in this case zh-cht and not zh-hant
327                                                 parent_culture = new CultureInfo ("zh-CHT");
328                                         } else
329                                                 parent_culture = new CultureInfo (parent_lcid);
330                                 }
331                                 return parent_culture;
332                         }
333                 }
334
335                 public virtual TextInfo TextInfo
336                 {
337                         get {
338                                 if (textInfo == null) {
339                                         if (!constructed) Construct ();
340                                         lock (this) {
341                                                 if(textInfo == null) {
342                                                         textInfo = CreateTextInfo (m_isReadOnly);
343                                                 }
344                                         }
345                                 }
346                                 
347                                 return(textInfo);
348                         }
349                 }
350
351                 public virtual string ThreeLetterISOLanguageName {
352                         get {
353                                 if (!constructed) Construct ();
354                                 return iso3lang;
355                         }
356                 }
357
358                 public virtual string ThreeLetterWindowsLanguageName
359                 {
360                         get {
361                                 if (!constructed) Construct ();
362                                 return(win3lang);
363                         }
364                 }
365
366                 public virtual string TwoLetterISOLanguageName {
367                         get {
368                                 if (!constructed) Construct ();
369                                 return(iso2lang);
370                         }
371                 }
372
373                 public bool UseUserOverride
374                 {
375                         get {
376                                 return m_useUserOverride;
377                         }
378                 }
379
380                 public void ClearCachedData()
381                 {
382                         lock (shared_table_lock) {
383                                 shared_by_number = null;
384                                 shared_by_name = null;
385                         }
386
387                         //
388                         // ClearCachedData method does not refresh the information in
389                         // the Thread.CurrentCulture property for existing threads
390                         //
391                         default_current_culture = null;
392
393                         RegionInfo.ClearCachedData ();
394                         TimeZone.ClearCachedData ();
395                         TimeZoneInfo.ClearCachedData ();
396                 }
397
398                 public virtual object Clone()
399                 {
400                         if (!constructed) Construct ();
401                         CultureInfo ci=(CultureInfo)MemberwiseClone ();
402                         ci.m_isReadOnly=false;
403                         ci.cached_serialized_form=null;
404                         if (!IsNeutralCulture) {
405                                 ci.NumberFormat = (NumberFormatInfo)NumberFormat.Clone ();
406                                 ci.DateTimeFormat = (DateTimeFormatInfo)DateTimeFormat.Clone ();
407                         }
408                         return(ci);
409                 }
410
411                 public override bool Equals (object value)
412                 {
413                         CultureInfo b = value as CultureInfo;
414                         return b != null && b.cultureID == cultureID && b.m_name == m_name;
415                 }
416
417                 public static CultureInfo[] GetCultures(CultureTypes types)
418                 {
419                         bool neutral=((types & CultureTypes.NeutralCultures)!=0);
420                         bool specific=((types & CultureTypes.SpecificCultures)!=0);
421                         bool installed=((types & CultureTypes.InstalledWin32Cultures)!=0);  // TODO
422
423                         CultureInfo [] infos = internal_get_cultures (neutral, specific, installed);
424                         // The runtime returns a NULL in the first position of the array when
425                         // 'neutral' is true. We fill it in with a clone of InvariantCulture
426                         // since it must not be read-only
427                         int i = 0;
428                         if (neutral && infos.Length > 0 && infos [0] == null) {
429                                 infos [i++] = (CultureInfo) InvariantCulture.Clone ();
430                         }
431
432                         for (; i < infos.Length; ++i) {
433                                 var ci = infos [i];
434                                 var ti = ci.GetTextInfoData ();
435                                 infos [i].m_cultureData = CultureData.GetCultureData (ci.m_name, false, ci.datetime_index, ci.CalendarType, ci.number_index, ci.iso2lang,
436                                         ti.ansi, ti.oem, ti.mac, ti.ebcdic, ti.right_to_left, ((char)ti.list_sep).ToString ());
437                         }
438
439                         return infos;
440                 }
441
442                 unsafe Data GetTextInfoData ()
443                 {
444                         return *(Data*) textinfo_data;
445                 }
446
447                 public override int GetHashCode ()
448                 {
449                         return cultureID.GetHashCode ();
450                 }
451
452                 public static CultureInfo ReadOnly(CultureInfo ci)
453                 {
454                         if(ci==null) {
455                                 throw new ArgumentNullException("ci");
456                         }
457
458                         if(ci.m_isReadOnly) {
459                                 return(ci);
460                         } else {
461                                 CultureInfo new_ci=(CultureInfo)ci.Clone ();
462                                 new_ci.m_isReadOnly=true;
463                                 if (new_ci.numInfo != null)
464                                         new_ci.numInfo = NumberFormatInfo.ReadOnly (new_ci.numInfo);
465                                 if (new_ci.dateTimeInfo != null)
466                                         new_ci.dateTimeInfo = DateTimeFormatInfo.ReadOnly (new_ci.dateTimeInfo);
467                                 if (new_ci.textInfo != null)
468                                         new_ci.textInfo = TextInfo.ReadOnly (new_ci.textInfo);
469                                 return(new_ci);
470                         }
471                 }
472
473                 public override string ToString()
474                 {
475                         return(m_name);
476                 }
477                 
478                 public virtual CompareInfo CompareInfo
479                 {
480                         get {
481                                 if(compareInfo==null) {
482                                         if (!constructed)
483                                                 Construct ();
484
485                                         lock (this) {
486                                                 if(compareInfo==null) {
487                                                         compareInfo=new CompareInfo (this);
488                                                 }
489                                         }
490                                 }
491                                 
492                                 return(compareInfo);
493                         }
494                 }
495
496                 public virtual bool IsNeutralCulture {
497                         get {
498                                 if (cultureID == InvariantCultureId)
499                                         return false;
500
501                                 if (!constructed) Construct ();
502                                 return territory == null;
503                         }
504                 }
505
506                 internal void CheckNeutral ()
507                 {
508                 }
509
510                 public virtual NumberFormatInfo NumberFormat {
511                         get {
512                                 if (numInfo == null) {
513                                         NumberFormatInfo temp = new NumberFormatInfo(this.m_cultureData);
514                                         temp.isReadOnly = m_isReadOnly;
515                                         numInfo = temp;
516                                 }
517
518                                 return numInfo;
519                         }
520
521                         set {
522                                 if (!constructed) Construct ();
523                                 if (m_isReadOnly) throw new InvalidOperationException(MSG_READONLY);
524
525                                 if (value == null)
526                                         throw new ArgumentNullException ("NumberFormat");
527                                 
528                                 numInfo = value;
529                         }
530                 }
531
532                 public virtual DateTimeFormatInfo DateTimeFormat {
533                         get {
534                                 if (dateTimeInfo != null)
535                                         return dateTimeInfo;
536
537                                 if (!constructed) Construct ();
538                                 CheckNeutral ();
539
540                                 var temp = new DateTimeFormatInfo (m_cultureData, Calendar);
541                                 temp.m_isReadOnly = m_isReadOnly;
542                                 System.Threading.Thread.MemoryBarrier();
543                                 dateTimeInfo = temp;
544                                 return dateTimeInfo;
545                         }
546
547                         set {
548                                 if (!constructed) Construct ();
549                                 if (m_isReadOnly) throw new InvalidOperationException(MSG_READONLY);
550
551                                 if (value == null)
552                                         throw new ArgumentNullException ("DateTimeFormat");
553                                 
554                                 dateTimeInfo = value;
555                         }
556                 }
557
558                 public virtual string DisplayName {
559                         get {
560                                 // Mono is not localized and will always return english name regardless of OS locale
561                                 return EnglishName;
562                         }
563                 }
564
565                 public virtual string EnglishName {
566                         get {
567                                 if (!constructed) Construct ();
568                                 return englishname;
569                         }
570                 }
571
572                 public static CultureInfo InstalledUICulture {
573                         get {
574                                 return ConstructCurrentCulture ();
575                         }
576                 }
577
578                 public bool IsReadOnly {
579                         get {
580                                 return m_isReadOnly;
581                         }
582                 }
583                 
584
585                 // 
586                 // IFormatProvider implementation
587                 //
588                 public virtual object GetFormat( Type formatType )
589                 {
590                         object format = null;
591
592                         if ( formatType == typeof(NumberFormatInfo) )
593                                 format = NumberFormat;
594                         else if ( formatType == typeof(DateTimeFormatInfo) )
595                                 format = DateTimeFormat;
596                         
597                         return format;
598                 }
599                 
600                 void Construct ()
601                 {
602                         construct_internal_locale_from_lcid (cultureID);
603                         constructed = true;
604                 }
605
606                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
607                 private extern bool construct_internal_locale_from_lcid (int lcid);
608
609                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
610                 private extern bool construct_internal_locale_from_name (string name);
611
612                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
613                 private extern static string get_current_locale_name ();
614
615                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
616                 private extern static CultureInfo [] internal_get_cultures (bool neutral, bool specific, bool installed);
617
618                 private void ConstructInvariant (bool read_only)
619                 {
620                         cultureID = InvariantCultureId;
621
622                         /* NumberFormatInfo defaults to the invariant data */
623                         numInfo=NumberFormatInfo.InvariantInfo;
624
625                         if (!read_only) {
626                                 numInfo = (NumberFormatInfo) numInfo.Clone ();
627                         }
628
629                         textInfo = CreateTextInfo (read_only);
630
631                         m_name=String.Empty;
632                         englishname=
633                         nativename="Invariant Language (Invariant Country)";
634                         iso3lang="IVL";
635                         iso2lang="iv";
636                         win3lang="IVL";
637                         default_calendar_type = 1 << CalendarTypeBits | (int) GregorianCalendarTypes.Localized;
638                 }
639
640                 private unsafe TextInfo CreateTextInfo (bool readOnly)
641                 {
642                         TextInfo tempTextInfo = new TextInfo (this.m_cultureData);
643                         tempTextInfo.SetReadOnlyState (readOnly);
644                         return tempTextInfo;
645                 }
646
647                 public CultureInfo (int culture) : this (culture, true) {}
648
649                 public CultureInfo (int culture, bool useUserOverride) :
650                         this (culture, useUserOverride, false) {}
651
652                 private CultureInfo (int culture, bool useUserOverride, bool read_only)
653                 {
654                         if (culture < 0)
655                                 throw new ArgumentOutOfRangeException ("culture", "Positive "
656                                         + "number required.");
657
658                         constructed = true;
659                         m_isReadOnly = read_only;
660                         m_useUserOverride = useUserOverride;
661
662                         if (culture == InvariantCultureId) {
663                                 /* Short circuit the invariant culture */
664                                 m_cultureData = CultureData.Invariant;
665                                 ConstructInvariant (read_only);
666                                 return;
667                         }
668
669                         if (!construct_internal_locale_from_lcid (culture)) {
670                                 //
671                                 // Be careful not to cause recursive CultureInfo initialization
672                                 //
673                                 var msg = string.Format (InvariantCulture, "Culture ID {0} (0x{1}) is not a supported culture.", culture.ToString (InvariantCulture), culture.ToString ("X4", InvariantCulture));
674                                 throw new CultureNotFoundException ("culture", msg);
675                         }
676
677                         var ti = GetTextInfoData ();
678                         m_cultureData = CultureData.GetCultureData (m_name, m_useUserOverride, datetime_index, CalendarType, number_index, iso2lang,
679                                 ti.ansi, ti.oem, ti.mac, ti.ebcdic, ti.right_to_left, ((char)ti.list_sep).ToString ());
680                 }
681
682                 public CultureInfo (string name) : this (name, true) {}
683
684                 public CultureInfo (string name, bool useUserOverride) :
685                         this (name, useUserOverride, false) {}
686
687                 private CultureInfo (string name, bool useUserOverride, bool read_only)
688                 {
689                         if (name == null)
690                                 throw new ArgumentNullException ("name");
691
692                         constructed = true;
693                         m_isReadOnly = read_only;
694                         m_useUserOverride = useUserOverride;
695                         m_isInherited = GetType() != typeof(System.Globalization.CultureInfo);
696
697                         if (name.Length == 0) {
698                                 /* Short circuit the invariant culture */
699                                 m_cultureData = CultureData.Invariant;
700                                 ConstructInvariant (read_only);
701                                 return;
702                         }
703
704                         if (!construct_internal_locale_from_name (name.ToLowerInvariant ())) {
705                                 throw CreateNotFoundException (name);
706                         }
707
708                         var ti = GetTextInfoData ();
709                         m_cultureData = CultureData.GetCultureData (m_name, useUserOverride, datetime_index, CalendarType, number_index, iso2lang,
710                                 ti.ansi, ti.oem, ti.mac, ti.ebcdic, ti.right_to_left, ((char)ti.list_sep).ToString ());
711                 }
712
713                 // This is used when creating by specific name and creating by
714                 // current locale so we can initialize the object without
715                 // doing any member initialization
716                 private CultureInfo () { constructed = true; }
717                 static Dictionary<int, CultureInfo> shared_by_number;
718                 static Dictionary<string, CultureInfo> shared_by_name;
719                 
720                 static void insert_into_shared_tables (CultureInfo c)
721                 {
722                         if (shared_by_number == null){
723                                 shared_by_number = new Dictionary<int, CultureInfo> ();
724                                 shared_by_name = new Dictionary<string, CultureInfo> ();
725                         }
726                         shared_by_number [c.cultureID] = c;
727                         shared_by_name [c.m_name] = c;
728                 }
729                 
730                 public static CultureInfo GetCultureInfo (int culture)
731                 {
732                         CultureInfo c;
733                         
734                         lock (shared_table_lock){
735                                 if (shared_by_number != null) {
736                                         if (shared_by_number.TryGetValue (culture, out c))
737                                                 return c;
738                                 }
739
740                                 c = new CultureInfo (culture, false, true);
741                                 insert_into_shared_tables (c);
742                                 return c;
743                         }
744                 }
745
746                 public static CultureInfo GetCultureInfo (string name)
747                 {
748                         if (name == null)
749                                 throw new ArgumentNullException ("name");
750
751                         CultureInfo c;
752                         lock (shared_table_lock){
753                                 if (shared_by_name != null){
754                                         if (shared_by_name.TryGetValue (name, out c))
755                                                 return c;
756                                 }
757                                 c = new CultureInfo (name, false, true);
758                                 insert_into_shared_tables (c);
759                                 return c;
760                         }
761                 }
762
763                 [MonoTODO ("Currently it ignores the altName parameter")]
764                 public static CultureInfo GetCultureInfo (string name, string altName) {
765                         if (name == null)
766                                 throw new ArgumentNullException ("null");
767                         if (altName == null)
768                                 throw new ArgumentNullException ("null");
769
770                         return GetCultureInfo (name);
771                 }
772
773                 public static CultureInfo GetCultureInfoByIetfLanguageTag (string name)
774                 {
775                         // There could be more consistent way to implement
776                         // it, but right now it works just fine with this...
777                         switch (name) {
778                         case "zh-Hans":
779                                 return GetCultureInfo ("zh-CHS");
780                         case "zh-Hant":
781                                 return GetCultureInfo ("zh-CHT");
782                         default:
783                                 return GetCultureInfo (name);
784                         }
785                 }
786
787                 // used in runtime (icall.c) to construct CultureInfo for
788                 // AssemblyName of assemblies
789                 internal static CultureInfo CreateCulture (string name, bool reference)
790                 {
791                         bool read_only;
792                         bool use_user_override;
793
794                         bool invariant = name.Length == 0;
795                         if (reference) {
796                                 use_user_override = invariant ? false : true;
797                                 read_only = false;
798                         } else {
799                                 read_only = false;
800                                 use_user_override = invariant ? false : true;
801                         }
802
803                         return new CultureInfo (name, use_user_override, read_only);
804                 }
805
806                 public static CultureInfo CreateSpecificCulture (string name)
807                 {
808                         if (name == null)
809                                 throw new ArgumentNullException ("name");
810
811                         if (name.Length == 0)
812                                 return InvariantCulture;
813
814                         var src_name = name;
815                         name = name.ToLowerInvariant ();
816                         CultureInfo ci = new CultureInfo ();
817
818                         if (!ci.construct_internal_locale_from_name (name)) {
819                                 int idx = name.IndexOf ('-');
820                                 if (idx < 1 || !ci.construct_internal_locale_from_name (name.Substring (0, idx)))
821                                         throw CreateNotFoundException (src_name);
822                         }
823
824                         if (ci.IsNeutralCulture)
825                                 ci = CreateSpecificCultureFromNeutral (ci.Name);
826
827                         var ti = ci.GetTextInfoData ();
828
829                         ci.m_cultureData = CultureData.GetCultureData (ci.m_name, false, ci.datetime_index, ci.CalendarType, ci.number_index, ci.iso2lang,
830                                 ti.ansi, ti.oem, ti.mac, ti.ebcdic, ti.right_to_left, ((char)ti.list_sep).ToString ());
831                         return ci;
832                 }
833
834                 //
835                 // Creates specific culture from neutral culture. Used by CreateSpecificCulture
836                 // only but using separate method we can delay switch underlying Dictionary
837                 // initialization
838                 //
839                 static CultureInfo CreateSpecificCultureFromNeutral (string name)
840                 {
841                         int id;
842
843                         //
844                         // For neutral cultures find predefined default specific culture
845                         //
846                         // Use managed switch because we need this for only some cultures
847                         // and the method is not used frequently
848                         //
849                         // TODO: We could optimize for cultures with single specific culture 
850                         //
851                         switch (name.ToLowerInvariant ()) {
852                         case "af": id = 1078; break;
853                         case "am": id = 1118; break;
854                         case "ar": id = 1025; break;
855                         case "arn": id = 1146; break;
856                         case "as": id = 1101; break;
857                         case "az": id = 1068; break;
858                         case "az-cyrl": id = 2092; break;
859                         case "az-latn": id = 1068; break;
860                         case "ba": id = 1133; break;
861                         case "be": id = 1059; break;
862                         case "bg": id = 1026; break;
863                         case "bn": id = 1093; break;
864                         case "bo": id = 1105; break;
865                         case "br": id = 1150; break;
866                         case "bs": id = 5146; break;
867                         case "bs-cyrl": id = 8218; break;
868                         case "bs-latn": id = 5146; break;
869                         case "ca": id = 1027; break;
870                         case "co": id = 1155; break;
871                         case "cs": id = 1029; break;
872                         case "cy": id = 1106; break;
873                         case "da": id = 1030; break;
874                         case "de": id = 1031; break;
875                         case "dsb": id = 2094; break;
876                         case "dv": id = 1125; break;
877                         case "el": id = 1032; break;
878                         case "en": id = 1033; break;
879                         case "es": id = 3082; break;
880                         case "et": id = 1061; break;
881                         case "eu": id = 1069; break;
882                         case "fa": id = 1065; break;
883                         case "fi": id = 1035; break;
884                         case "fil": id = 1124; break;
885                         case "fo": id = 1080; break;
886                         case "fr": id = 1036; break;
887                         case "fy": id = 1122; break;
888                         case "ga": id = 2108; break;
889                         case "gd": id = 1169; break;
890                         case "gl": id = 1110; break;
891                         case "gsw": id = 1156; break;
892                         case "gu": id = 1095; break;
893                         case "ha": id = 1128; break;
894                         case "ha-latn": id = 1128; break;
895                         case "he": id = 1037; break;
896                         case "hi": id = 1081; break;
897                         case "hr": id = 1050; break;
898                         case "hsb": id = 1070; break;
899                         case "hu": id = 1038; break;
900                         case "hy": id = 1067; break;
901                         case "id": id = 1057; break;
902                         case "ig": id = 1136; break;
903                         case "ii": id = 1144; break;
904                         case "is": id = 1039; break;
905                         case "it": id = 1040; break;
906                         case "iu": id = 2141; break;
907                         case "iu-cans": id = 1117; break;
908                         case "iu-latn": id = 2141; break;
909                         case "ja": id = 1041; break;
910                         case "ka": id = 1079; break;
911                         case "kk": id = 1087; break;
912                         case "kl": id = 1135; break;
913                         case "km": id = 1107; break;
914                         case "kn": id = 1099; break;
915                         case "ko": id = 1042; break;
916                         case "kok": id = 1111; break;
917                         case "ky": id = 1088; break;
918                         case "lb": id = 1134; break;
919                         case "lo": id = 1108; break;
920                         case "lt": id = 1063; break;
921                         case "lv": id = 1062; break;
922                         case "mi": id = 1153; break;
923                         case "mk": id = 1071; break;
924                         case "ml": id = 1100; break;
925                         case "mn": id = 1104; break;
926                         case "mn-cyrl": id = 1104; break;
927                         case "mn-mong": id = 2128; break;
928                         case "moh": id = 1148; break;
929                         case "mr": id = 1102; break;
930                         case "ms": id = 1086; break;
931                         case "mt": id = 1082; break;
932                         case "nb": id = 1044; break;
933                         case "ne": id = 1121; break;
934                         case "nl": id = 1043; break;
935                         case "nn": id = 2068; break;
936                         case "no": id = 1044; break;
937                         case "nso": id = 1132; break;
938                         case "oc": id = 1154; break;
939                         case "or": id = 1096; break;
940                         case "pa": id = 1094; break;
941                         case "pl": id = 1045; break;
942                         case "prs": id = 1164; break;
943                         case "ps": id = 1123; break;
944                         case "pt": id = 1046; break;
945                         case "qut": id = 1158; break;
946                         case "quz": id = 1131; break;
947                         case "rm": id = 1047; break;
948                         case "ro": id = 1048; break;
949                         case "ru": id = 1049; break;
950                         case "rw": id = 1159; break;
951                         case "sa": id = 1103; break;
952                         case "sah": id = 1157; break;
953                         case "se": id = 1083; break;
954                         case "si": id = 1115; break;
955                         case "sk": id = 1051; break;
956                         case "sl": id = 1060; break;
957                         case "sma": id = 7227; break;
958                         case "smj": id = 5179; break;
959                         case "smn": id = 9275; break;
960                         case "sms": id = 8251; break;
961                         case "sq": id = 1052; break;
962                         case "sr": id = 9242; break;
963                         case "sr-cyrl": id = 10266; break;
964                         case "sr-latn": id = 9242; break;
965                         case "sv": id = 1053; break;
966                         case "sw": id = 1089; break;
967                         case "syr": id = 1114; break;
968                         case "ta": id = 1097; break;
969                         case "te": id = 1098; break;
970                         case "tg": id = 1064; break;
971                         case "tg-cyrl": id = 1064; break;
972                         case "th": id = 1054; break;
973                         case "tk": id = 1090; break;
974                         case "tn": id = 1074; break;
975                         case "tr": id = 1055; break;
976                         case "tt": id = 1092; break;
977                         case "tzm": id = 2143; break;
978                         case "tzm-latn": id = 2143; break;
979                         case "ug": id = 1152; break;
980                         case "uk": id = 1058; break;
981                         case "ur": id = 1056; break;
982                         case "uz": id = 1091; break;
983                         case "uz-cyrl": id = 2115; break;
984                         case "uz-latn": id = 1091; break;
985                         case "vi": id = 1066; break;
986                         case "wo": id = 1160; break;
987                         case "xh": id = 1076; break;
988                         case "yo": id = 1130; break;
989                         case "zh": id = 2052; break;
990                         case "zh-chs":
991                         case "zh-hans":
992                                 id = 2052; break;
993                         case "zh-cht":
994                         case "zh-hant":
995                                 id = 3076; break;
996                         case "zu": id = 1077; break;
997                         default:
998                                 throw new NotImplementedException ("Mapping for neutral culture " + name);
999                         }
1000
1001                         return new CultureInfo (id);
1002                 }
1003
1004                 internal int CalendarType {
1005                         get {
1006                                 switch (default_calendar_type >> CalendarTypeBits) {
1007                                 case 1:
1008                                         return Calendar.CAL_GREGORIAN;
1009                                 case 2:
1010                                         return Calendar.CAL_THAI;
1011                                 case 3:
1012                                         return Calendar.CAL_UMALQURA;
1013                                 case 4:
1014                                         return Calendar.CAL_HIJRI;
1015                                 default:
1016                                         throw new NotImplementedException ("CalendarType");
1017                                 }
1018                         }
1019                 }
1020
1021                 static Calendar CreateCalendar (int calendarType)
1022                 {
1023                         string name = null;
1024                         switch (calendarType >> CalendarTypeBits) {
1025                         case 1:
1026                                 GregorianCalendarTypes greg_type;
1027                                 greg_type = (GregorianCalendarTypes) (calendarType & 0xFF);
1028                                 return new GregorianCalendar (greg_type);
1029                         case 2:
1030                                 name = "System.Globalization.ThaiBuddhistCalendar";
1031                                 break;
1032                         case 3:
1033                                 name = "System.Globalization.UmAlQuraCalendar";
1034                                 break;
1035                         case 4:
1036                                 name = "System.Globalization.HijriCalendar";
1037                                 break;
1038                         default:
1039                                 throw new NotImplementedException ("Unknown calendar type: " + calendarType);
1040                         }
1041
1042                         Type type = Type.GetType (name, false);
1043                         if (type == null)
1044                                 return new GregorianCalendar (GregorianCalendarTypes.Localized); // return invariant calendar if not found
1045                         return (Calendar) Activator.CreateInstance (type);
1046                 }
1047
1048                 static Exception CreateNotFoundException (string name)
1049                 {
1050                         return new CultureNotFoundException ("name", "Culture name " + name + " is not supported.");
1051                 }
1052                 
1053                 public static CultureInfo DefaultThreadCurrentCulture {
1054                         get {
1055                                 return Thread.default_culture;
1056                         }
1057                         set {
1058                                 Thread.default_culture = value;
1059                         }
1060                 }
1061                 
1062                 public static CultureInfo DefaultThreadCurrentUICulture {
1063                         get {
1064                                 return Thread.default_ui_culture;
1065                         }
1066                         set {
1067                                 Thread.default_ui_culture = value;
1068                         }
1069                 }
1070
1071                 internal string SortName {
1072                         get {
1073                                 return m_name;
1074                         }
1075                 }
1076
1077 #region reference sources
1078                 // TODO:
1079                 internal static readonly bool IsTaiwanSku;
1080
1081         //
1082         // CheckDomainSafetyObject throw if the object is customized object which cannot be attached to 
1083         // other object (like CultureInfo or DateTimeFormatInfo).
1084         //
1085
1086         internal static void CheckDomainSafetyObject(Object obj, Object container)
1087         {
1088             if (obj.GetType().Assembly != typeof(System.Globalization.CultureInfo).Assembly) {
1089                 
1090                 throw new InvalidOperationException(
1091                             String.Format(
1092                                 CultureInfo.CurrentCulture, 
1093                                 Environment.GetResourceString("InvalidOperation_SubclassedObject"), 
1094                                 obj.GetType(),
1095                                 container.GetType()));
1096             }
1097             Contract.EndContractBlock();
1098         }
1099
1100         // For resource lookup, we consider a culture the invariant culture by name equality.
1101         // We perform this check frequently during resource lookup, so adding a property for
1102         // improved readability.
1103         internal bool HasInvariantCultureName
1104         {
1105             get { return Name == CultureInfo.InvariantCulture.Name; }
1106         }
1107
1108         internal static bool VerifyCultureName(String cultureName, bool throwException)
1109         {
1110             // This function is used by ResourceManager.GetResourceFileName(). 
1111             // ResourceManager searches for resource using CultureInfo.Name,
1112             // so we should check against CultureInfo.Name.
1113
1114             for (int i=0; i<cultureName.Length; i++) {
1115                 char c = cultureName[i];
1116                 // 
1117
1118                 if (Char.IsLetterOrDigit(c) || c=='-' || c=='_') {
1119                     continue;
1120                 }
1121                 if (throwException) {
1122                     throw new ArgumentException(Environment.GetResourceString("Argument_InvalidResourceCultureName", cultureName));
1123                 }
1124                 return false;
1125             }
1126             return true;
1127         }
1128
1129 #endregion
1130         }
1131 }