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