Ver Fonte

2007-05-15 Atsushi Enomoto <[email protected]>

	* RegionInfo.cs, CultureInfo.cs : in 2.0 RegionInfo could be created
	  from locale name. To support that, retrieve CultureInfo from name
	  and use its Territory name if exists.
	  RegionInfo.Equals() still uses lcid, but hash code is based on
	  region ID. Added GeoId, which represents unique region ID (which
	  is returned in 1.x GetHashCode()).

	* RegionInfoTest.cs : new tests.

	* corlib_test.dll.sources : added RegionInfoTest.cs.


svn path=/trunk/mcs/; revision=77408
Atsushi Eno há 18 anos atrás
pai
commit
b184bcaab7

+ 11 - 7
mcs/class/corlib/ChangeLog

@@ -1,3 +1,7 @@
+2007-05-15  Atsushi Enomoto  <[email protected]>
+
+	* corlib_test.dll.sources : added RegionInfoTest.cs.
+
 2007-05-11  Raja R Harinath  <[email protected]>
 
 	* Makefile (cmp_makefrag, cmp_response): Depend on $(depsdir).
@@ -152,7 +156,7 @@ Tue Dec 19 15:31:39 CET 2006 Paolo Molaro <[email protected]>
 	  System.Security.AccessControl/SecurityInfos.cs
 	  System.Security.AccessControl/SystemAcl.cs
 
-2006-07-03  Kornél Pál  <[email protected]>
+2006-07-03  Kornel Pal  <[email protected]>
 
 	* corlib.dll.sources: Added
 	  System.Globalization/CodePageDataItem.cs
@@ -208,7 +212,7 @@ Tue Dec 19 15:31:39 CET 2006 Paolo Molaro <[email protected]>
 
 	* corlib.dll.sources: Added System.Resources/ResourceManagerTest.cs
 
-2006-01-05  Kornél Pál  <[email protected]>
+2006-01-05  Kornel Pal  <[email protected]>
 
 	* corlib.dll.sources: Added DriveNotFoundException.cs in System.IO.
 
@@ -1122,7 +1126,7 @@ Tue Dec 19 15:31:39 CET 2006 Paolo Molaro <[email protected]>
 	* corlib.dll.sources: Added 
 		System.Runtime.Serialization.Formatter.Binary.CodeGenerator.cs
 
-2004-02-10 Carlos Guzmán Álvarez  <[email protected]>
+2004-02-10 Carlos Guzman Alvarez  <[email protected]>
 
     * Mono.Security.Cryptography/PKCS1.cs:
 
@@ -1199,7 +1203,7 @@ Tue Dec 19 15:31:39 CET 2006 Paolo Molaro <[email protected]>
 	* corlib_test.dll.sources: Added unit tests PaddingMode and X509 
 	using CryptoAPI.
 
-2003-12-14  Pedro Martínez Juliá <[email protected]>
+2003-12-14  Pedro Martinez Julia <[email protected]>
 
 	* DateTime.cs: Be sure that the array access is not out of range
 	when looking for ArgumentOutOfRangeException in the year value.
@@ -1290,7 +1294,7 @@ Tue Dec 19 15:31:39 CET 2006 Paolo Molaro <[email protected]>
 	* corlib.dll.sources: Added classes for Authenticode support.
 	* corlib_test.dll.sources: Added unit test for SPC certificates.
 
-2003-09-26  Pedro Martínez Juliá <[email protected]>
+2003-09-26  Pedro Martinez Julia <[email protected]>
 
 	* corlib_test.dll.sources: Remove tests that generate compile
 	errors. The tests are still there but its name is rmoved here.
@@ -1448,7 +1452,7 @@ Tue Dec 19 15:31:39 CET 2006 Paolo Molaro <[email protected]>
 
 	* unix.args: Added System.Runtime.Remoting.Activation/AppDomainLevelActivator.cs
 
-2003-03-16  Pedro Martínez Juliá <[email protected]>
+2003-03-16  Pedro Martinez Julia <[email protected]>
 
 	* unix.args: Added "System.FloatingPointFormatter.cs".
 
@@ -1460,7 +1464,7 @@ Tue Dec 19 15:31:39 CET 2006 Paolo Molaro <[email protected]>
 	* unix.args: Added
 		System.Runtime.InteropServices/HandleRef.cs
 
-2003-03-04  Pedro Martínez Juliá <[email protected]>
+2003-03-04  Pedro Martinez Julia <[email protected]>
 
 	* unix.args: Added "DoubleFormatter.cs" and "SingleFormatter.cs"
 

+ 9 - 0
mcs/class/corlib/System.Globalization/ChangeLog

@@ -1,3 +1,12 @@
+2007-05-15  Atsushi Enomoto  <[email protected]>
+
+	* RegionInfo.cs, CultureInfo.cs : in 2.0 RegionInfo could be created
+	  from locale name. To support that, retrieve CultureInfo from name
+	  and use its Territory name if exists.
+	  RegionInfo.Equals() still uses lcid, but hash code is based on
+	  region ID. Added GeoId, which represents unique region ID (which
+	  is returned in 1.x GetHashCode()).
+
 2007-04-17  Alp Toker  <[email protected]>
 
 	* DateTimeFormatInfo.cs: Provide set accessor for MonthNames.

+ 7 - 0
mcs/class/corlib/System.Globalization/CultureInfo.cs

@@ -78,6 +78,8 @@ namespace System.Globalization
 		private string icu_name;
 		[NonSerialized]
 		private string win3lang;
+		[NonSerialized]
+		private string territory;
 		volatile CompareInfo compareInfo;
 		[NonSerialized]
 		private unsafe readonly int *calendar_data;
@@ -156,6 +158,11 @@ namespace System.Globalization
 			return ConstructCurrentCulture ();
 		}
 
+		// it is used for RegionInfo.
+		internal string Territory {
+			get { return territory; }
+		}
+
 		public virtual int LCID {
 			get {
 				return cultureID;

+ 42 - 0
mcs/class/corlib/System.Globalization/RegionInfo.cs

@@ -56,6 +56,7 @@ namespace System.Globalization
 			}
 		}
 
+		int lcid; // it is used only for Equals() (not even used in GetHashCode()).
 		int regionId;
 		string iso2Name;
 		string iso3Name;
@@ -67,10 +68,16 @@ namespace System.Globalization
 
 		public RegionInfo (int lcid)
 		{
+#if NET_2_0
+			if (!GetByTerritory (CultureInfo.GetCultureInfo (lcid)))
+				throw new ArgumentException (
+					String.Format ("Region ID {0} (0x{0:X4}) is not a supported region.", lcid), "lcid");
+#else
 			if (!construct_internal_region_from_lcid (lcid))
 				throw new ArgumentException (
 					String.Format ("Region ID {0} (0x{0:X4}) is not a " +
 							"supported region.", lcid), "lcid");
+#endif
 		}
 
 		public RegionInfo (string name)
@@ -78,9 +85,28 @@ namespace System.Globalization
 			if (name == null)
 				throw new ArgumentNullException ();
 
+#if NET_2_0
+			if (construct_internal_region_from_name (name.ToUpperInvariant ())) {
+				lcid = name.GetHashCode (); // random-ish
+				return;
+			}
+			if (!GetByTerritory (CultureInfo.GetCultureInfo (name)))
+				throw new ArgumentException (String.Format ("Region name {0} is not supported.", name), "name");
+#else
 			if (!construct_internal_region_from_name (name.ToUpperInvariant ()))
 				throw new ArgumentException ("Region name " + name +
 						" is not supported.", "name");
+#endif
+		}
+
+		bool GetByTerritory (CultureInfo ci)
+		{
+			if (ci == null)
+				throw new Exception ("INTERNAL ERROR: should not happen.");
+			if (ci.IsNeutralCulture || ci.Territory == null)
+				return false;
+			this.lcid = ci.LCID;
+			return construct_internal_region_from_name (ci.Territory.ToUpperInvariant ());
 		}
 
 		[MethodImplAttribute (MethodImplOptions.InternalCall)]
@@ -109,6 +135,12 @@ namespace System.Globalization
 			get { return englishName; }
 		}
 
+#if NET_2_0
+		public virtual int GeoId {
+			get { return regionId; }
+		}
+#endif
+
 		public virtual bool IsMetric {
 			get {
 				switch (iso2Name) {
@@ -157,6 +189,16 @@ namespace System.Globalization
 		// methods
 
 #if NET_2_0
+		public override bool Equals (object value)
+		{
+			RegionInfo other = value as RegionInfo;
+			return other != null && lcid == other.lcid;
+		}
+
+		public override int GetHashCode ()
+		{
+			return (int) (0x80000000 + (regionId << 3) + regionId); // it i still based on regionId
+		}
 #else
 		public override bool Equals (object value)
 		{

+ 4 - 0
mcs/class/corlib/Test/System.Globalization/ChangeLog

@@ -1,3 +1,7 @@
+2007-05-15  Atsushi Enomoto  <[email protected]>
+
+	* RegionInfoTest.cs : new tests.
+
 2007-03-06  Atsushi Enomoto  <[email protected]>
 
 	* CompareInfoTest.cs : added test for bug #79714.

+ 62 - 0
mcs/class/corlib/Test/System.Globalization/RegionInfoTest.cs

@@ -0,0 +1,62 @@
+//
+// System.Globalization.RegionInfoTest.cs
+//
+// Author:
+// 	Atsushi Enomoto  <[email protected]>
+//
+// (c) 2007 Novell, Inc. (http://www.novell.com)
+//
+
+using NUnit.Framework;
+using System.IO;
+using System;
+using System.Globalization;
+using System.Threading;
+
+namespace MonoTests.System.Globalization
+{
+	[TestFixture]
+	public class RegionInfoTest
+	{
+		[Test]
+		public void RegionByName ()
+		{
+			string [] names = new string [] {
+				"AR", "ES", "HK", "TW", "US"};
+
+			foreach (string name in names)
+				new RegionInfo (name);
+
+		}
+
+		[Test]
+		public void RegionByWrongName ()
+		{
+			string [] names = new string [] {
+				"en", "EN"};
+
+			foreach (string name in names) {
+				try {
+					new RegionInfo (name);
+					Assert.Fail ("should be invalid: " + name);
+				} catch (ArgumentException) {
+				}
+			}
+		}
+
+		[Test]
+#if NET_2_0
+#else
+		[ExpectedException (typeof (ArgumentException))]
+#endif
+		public void RegionByLocaleName ()
+		{
+			string [] names = new string [] {
+				"en-US", "zh-TW"};
+
+			foreach (string name in names)
+				new RegionInfo (name);
+		}
+	}
+}
+

+ 1 - 0
mcs/class/corlib/corlib_test.dll.sources

@@ -58,6 +58,7 @@ System.Globalization/CalendarTest.cs
 System.Globalization/CompareInfoTest.cs
 System.Globalization/CultureInfoTest.cs
 System.Globalization/DaylightTimeTest.cs
+System.Globalization/RegionInfoTest.cs
 System.Globalization/TextInfoTest.cs
 System/GuidTest.cs
 System/Int16Test.cs