Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Range #5

Open
wants to merge 4 commits into
base: Range
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions MagicExpression.Test/MagexBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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", " ");
Expand All @@ -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, "$");
Expand All @@ -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, "$");
Expand Down
2 changes: 2 additions & 0 deletions MagicExpression/IMagex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 15 additions & 3 deletions MagicExpression/Magex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -471,13 +471,25 @@ public IMagex Literal(string regex)
return this;
}

/// <summary>
/// Adds a range using the given bounds
/// </summary>
/// <remarks>There must be an even numbers of bounds</remarks>
/// <param name="bounds">An even number of parameters representing the bounds</param>
/// <returns>this</returns>
public IMagex Range(params object[] bounds)
{
this.Literal(Magex.RangeRegex(bounds));
return this;
}

/// <summary>
/// Builds a range using the given bounds
/// </summary>
/// <remarks>There must be an even numbers of bounds</remarks>
/// <param name="bounds">An even number of parameters representing the bounds</param>
/// <returns>The range regex or throws an <see cref="ArgumentException"/> in case of odd number of bounds</returns>
public static string Range(params object[] bounds)
public static string RangeRegex(params object[] bounds)
{
if (bounds.Length % 2 != 0)
throw new ArgumentException(
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions MagicExpression/MagexBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ public static string NumericRange(ulong from, ulong to, RangeOptions options)
/// <example>('a', 'd') -> "[a-d]" OR (3, 5) -> "[3-5]"</example>
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
Expand All @@ -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++)
{
Expand Down