Pārlūkot izejas kodu

In System.Text.RegularExpressions:
Fix #74753.
* Match.cs (Match) [zero-argument variant]: Make private.
* GroupCollection (Item) [string variant]: Don't look for the
group number in an empty match.

In Test/System.Text.RegularExpressions:
* RegexBugs.cs (NameLookupInEmptyMatch): New test from #74753.

svn path=/trunk/mcs/; revision=45958

Raja R Harinath 20 gadi atpakaļ
vecāks
revīzija
4e8fc6ec9a

+ 7 - 0
mcs/class/System/System.Text.RegularExpressions/ChangeLog

@@ -1,3 +1,10 @@
+2005-06-14  Raja R Harinath  <[email protected]>
+
+	Fix #74753.
+	* Match.cs (Match) [zero-argument variant]: Make private.
+	* GroupCollection (Item) [string variant]: Don't look for the
+	group number in an empty match.
+
 2005-06-10  Raja R Harinath  <[email protected]>
 
 	* interpreter.cs (Interpreter.GenerateMatch): Avoid allocating two

+ 4 - 5
mcs/class/System/System.Text.RegularExpressions/GroupCollection.cs

@@ -74,11 +74,10 @@ namespace System.Text.RegularExpressions
 
 		public Group this [string groupName] {
 			get {
-				foreach (Group g in list) {
-					if (!(g is Match))
-						continue;
-
-					int index = ((Match)g).Regex.GroupNumberFromName (groupName);
+				// The 0th group is the match.
+				Match m = (Match) list [0];
+				if (m != Match.Empty) {
+					int index = m.Regex.GroupNumberFromName (groupName);
 					if (index != -1)
 						return this [index];
 				}

+ 1 - 1
mcs/class/System/System.Text.RegularExpressions/Match.cs

@@ -68,7 +68,7 @@ namespace System.Text.RegularExpressions {
 
 		// internal
 
-		internal Match () : base ()
+		private Match () : base ()
 		{
 			this.regex = null;
 			this.machine = null;

+ 4 - 0
mcs/class/System/Test/System.Text.RegularExpressions/ChangeLog

@@ -1,3 +1,7 @@
+2005-06-14  Raja R Harinath  <[email protected]>
+
+	* RegexBugs.cs (NameLookupInEmptyMatch): New test from #74753.
+
 2005-05-25  Raja R Harinath  <[email protected]>
 
 	* RegexReplace.cs: Add a couple more testcases.

+ 20 - 1
mcs/class/System/Test/System.Text.RegularExpressions/RegexBugs.cs

@@ -147,7 +147,7 @@ namespace MonoTests.System.Text.RegularExpressions
 			rex += "type\\s*=\\s*[\"']?text/xml[\"']?\\s*href\\s*=\\s*(?:\"(?<1>[^\"]*)\"|'(?<1>[^']*)'|(?<1>\\S+))";
 			Regex rob = new Regex (rex, RegexOptions.IgnoreCase);
 		}
-		
+
 		[Test]
 		public void UndefinedGroup () // bug 52890
 		{
@@ -277,6 +277,25 @@ namespace MonoTests.System.Text.RegularExpressions
 			x = new Regex ("{a}");
 			x = new Regex ("{,a}");
 		}
+
+		[Test]
+		public void NameLookupInEmptyMatch () // bug 74753
+		{
+			Regex regTime = new Regex (
+					@"(?<hour>[0-9]{1,2})([\:](?<minute>[0-9]{1,2})){0,1}([\:](?<second>[0-9]{1,2})){0,1}\s*(?<ampm>(?i:(am|pm)){0,1})");
+
+			Match mTime = regTime.Match("");
+			AssertEquals ("#01", "", mTime.Groups["hour"].Value);
+			AssertEquals ("#02", "", mTime.Groups["minute"].Value);
+			AssertEquals ("#03", "", mTime.Groups["second"].Value);
+			AssertEquals ("#04", "", mTime.Groups["ampm"].Value);
+
+			mTime = regTime.Match("12:00 pm");
+			AssertEquals ("#05", "12", mTime.Groups["hour"].Value);
+			AssertEquals ("#06", "00", mTime.Groups["minute"].Value);
+			AssertEquals ("#07", "", mTime.Groups["second"].Value);
+			AssertEquals ("#08", "pm", mTime.Groups["ampm"].Value);
+		}
 	}
 }