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..1ec66f4 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(
@@ -488,11 +500,11 @@ public static string Range(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
diff --git a/MagicExpression/MagexBuilder.cs b/MagicExpression/MagexBuilder.cs
index ffa3bfc..012d472 100644
--- a/MagicExpression/MagexBuilder.cs
+++ b/MagicExpression/MagexBuilder.cs
@@ -95,10 +95,10 @@ 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);
+ return string.Format("[{0}-{1}]", from, to);
}
#region Range support functions
@@ -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++)
{