From 388520158af8bb1b411e6a836b8c83024a9a85a7 Mon Sep 17 00:00:00 2001 From: Christoph Stephan Date: Fri, 25 Mar 2016 14:11:26 +0100 Subject: [PATCH 1/4] Large numbers --- MagicExpression/MagexBuilder.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MagicExpression/MagexBuilder.cs b/MagicExpression/MagexBuilder.cs index ffa3bfc..12b1ef5 100644 --- a/MagicExpression/MagexBuilder.cs +++ b/MagicExpression/MagexBuilder.cs @@ -95,7 +95,7 @@ public static string NumericRange(ulong from, ulong to, RangeOptions options) /// ('a', 'd') -> "[a-d]" OR (3, 5) -> "[3-5]" public static string SimpleRange(object from, object to) { - if (Convert.ToInt64(from) > Convert.ToInt64(to)) + if (Convert.ToUInt64(from) > Convert.ToUInt64(to)) throw new ArgumentException(string.Format("From parameter {0} must be smaller (ASCII-wise) than the to {1} parameter", from, to)); return string.Format("{0}-{1}", from, to); @@ -112,7 +112,7 @@ private static string GetNumericRange(ulong from, ulong to) for (var i = 0; i < ranges.Count - 1; i++) { string strFrom = ranges[i]; - string strTo = ((Convert.ToInt64(ranges[i + 1])) - 1).ToString(CultureInfo.InvariantCulture); + string strTo = ((Convert.ToUInt64(ranges[i + 1])) - 1).ToString(CultureInfo.InvariantCulture); for (var j = 0; j < strFrom.Length; j++) { From 894f0e3c1511ddb4d3ccfafca7adc97fbcfb96f1 Mon Sep 17 00:00:00 2001 From: Christoph Stephan Date: Fri, 25 Mar 2016 15:03:55 +0100 Subject: [PATCH 2/4] Fix: RangeExtendedNumeric and RangeInBetween --- MagicExpression.Test/MagexBuilderTest.cs | 16 ++++++++-------- MagicExpression/IMagex.cs | 2 ++ MagicExpression/Magex.cs | 14 +++++++++++++- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/MagicExpression.Test/MagexBuilderTest.cs b/MagicExpression.Test/MagexBuilderTest.cs index c4138f8..a25f86f 100644 --- a/MagicExpression.Test/MagexBuilderTest.cs +++ b/MagicExpression.Test/MagexBuilderTest.cs @@ -14,7 +14,7 @@ public override void Setup() [TestMethod] public void RangeChar() { - this.Magic.CharacterIn(Magex.Range('a', 'f')); + this.Magic.Range('a', 'f'); this.AssertIsMatching("a", "d"); this.AssertIsNotMatching("k", "52"); @@ -23,7 +23,7 @@ public void RangeChar() [TestMethod] public void RangeNum() { - this.Magic.CharacterIn(Magex.Range('0', '5')); + this.Magic.Range('0', '5'); this.AssertIsMatching("0", "4"); this.AssertIsNotMatching("8", " "); @@ -32,25 +32,25 @@ public void RangeNum() [TestMethod] public void RangeExtendedNumeric() { - this.Magic.CharacterIn(Magex.Range(0, 42)); + this.Magic.Range(0, 42); - this.AssertIsMatching("0", "9", "20", "42"); + this.AssertIsMatching("0", "9", "19", "20", "42"); this.AssertIsNotMatching("43", "52"); } [TestMethod] public void RangeInBetween() { - this.Magic.Character('a').CharacterIn(Magex.Range(0, 42)).Character('a'); + this.Magic.Character('a').Range(0, 42).Character('a'); - this.AssertIsMatching("a0a", "a9a", "a20a", "a42a"); + this.AssertIsMatching("a0a", "a9a", "a19a", "a20a", "a42a"); this.AssertIsNotMatching("", "%", "a9b", "b52a", "4242"); } [TestMethod] public void RangeCharBounds() { - this.Magic.CharacterIn(Magex.Range('a', 'g')).Character('a'); + this.Magic.Range('a', 'g').Character('a'); this.AssertIsMatching("aa", "da"); this.AssertIsNotMatching("a", "5", string.Empty, "$"); @@ -59,7 +59,7 @@ public void RangeCharBounds() [TestMethod] public void RangeMixedBounds() { - this.Magic.CharacterIn(Magex.Range(0, 4, 'a', 'g')); + this.Magic.Range(0, 4, 'a', 'g'); this.AssertIsMatching("0", "3", "a", "d"); this.AssertIsNotMatching("", "5", "k", string.Empty, "$"); diff --git a/MagicExpression/IMagex.cs b/MagicExpression/IMagex.cs index e6551fb..4eead6f 100644 --- a/MagicExpression/IMagex.cs +++ b/MagicExpression/IMagex.cs @@ -20,6 +20,8 @@ public interface IMagex : IExpressionElement IMagex Literal(string regex); + IMagex Range(params object[] bounds); + IRepeatable Group(IExpressionElement grouped); IRepeatable Capture(IExpressionElement captured); IRepeatable CaptureAs(string name, IExpressionElement captured); diff --git a/MagicExpression/Magex.cs b/MagicExpression/Magex.cs index 49d92e5..fa3ec8a 100644 --- a/MagicExpression/Magex.cs +++ b/MagicExpression/Magex.cs @@ -471,13 +471,25 @@ public IMagex Literal(string regex) return this; } + /// + /// Adds a range using the given bounds + /// + /// There must be an even numbers of bounds + /// An even number of parameters representing the bounds + /// this + public IMagex Range(params object[] bounds) + { + this.Literal(Magex.RangeRegex(bounds)); + return this; + } + /// /// Builds a range using the given bounds /// /// There must be an even numbers of bounds /// An even number of parameters representing the bounds /// The range regex or throws an in case of odd number of bounds - public static string Range(params object[] bounds) + public static string RangeRegex(params object[] bounds) { if (bounds.Length % 2 != 0) throw new ArgumentException( From 9c74de130b0aaaf88da93844f1ee3e09b3d89b63 Mon Sep 17 00:00:00 2001 From: Christoph Stephan Date: Fri, 25 Mar 2016 15:07:56 +0100 Subject: [PATCH 3/4] Fix: RangeChar, RangeCharBounds and RangeNum --- MagicExpression/MagexBuilder.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MagicExpression/MagexBuilder.cs b/MagicExpression/MagexBuilder.cs index 12b1ef5..012d472 100644 --- a/MagicExpression/MagexBuilder.cs +++ b/MagicExpression/MagexBuilder.cs @@ -98,7 +98,7 @@ public static string SimpleRange(object from, object to) if (Convert.ToUInt64(from) > Convert.ToUInt64(to)) throw new ArgumentException(string.Format("From parameter {0} must be smaller (ASCII-wise) than the to {1} parameter", from, to)); - return string.Format("{0}-{1}", from, to); + return string.Format("[{0}-{1}]", from, to); } #region Range support functions From fd89fb2426fd106d6d8e8d5dde0befee0c9ba059 Mon Sep 17 00:00:00 2001 From: Christoph Stephan Date: Fri, 25 Mar 2016 15:01:01 +0100 Subject: [PATCH 4/4] Fix: RangeMixedBounds --- MagicExpression/Magex.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MagicExpression/Magex.cs b/MagicExpression/Magex.cs index fa3ec8a..1ec66f4 100644 --- a/MagicExpression/Magex.cs +++ b/MagicExpression/Magex.cs @@ -500,11 +500,11 @@ public static string RangeRegex(params object[] bounds) for (int i = 0; i < bounds.Length; i++) { - outputRange += MagexBuilder.CreateRange(bounds[i], bounds[i + 1]); + outputRange += "|" + MagexBuilder.CreateRange(bounds[i], bounds[i + 1]); i++; } - return outputRange; + return outputRange.Substring(1); } #endregion