Skip to content

Commit

Permalink
Add option for IntegerNode to prefer longer numbers.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jcparkyn committed Dec 2, 2020
1 parent 72495df commit 752d7ec
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Nodexr/Shared/IntegerRangeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ private List<string> GenerateRegexRanges(int start, int end)

/**
* return the regular expressions that match the ranges in the given
* list of integers. The list is in the form firstRangeStart, firstRangeEnd,
* list of integers. The list is in the form firstRangeStart, firstRangeEnd,
* secondRangeStart, secondRangeEnd, etc. Each regular expression is 0-left-padded,
* if necessary, to match strings of the given width.
*/
private List<string> FormatPairsToRegEx(List<Pair> pairs)
private static List<string> FormatPairsToRegEx(List<Pair> pairs)
{
List<string> list = new List<string>();
for (int i = 0; i < pairs.Count; i ++)
Expand Down
30 changes: 25 additions & 5 deletions Nodexr/Shared/NodeTypes/IntegerNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ public class IntegerNode : Node
Description = "Allow leading zeros in the number?",
};

[NodeInput]
public InputCheckbox InputPreferLongest { get; } = new InputCheckbox(true)
{
Title = "Prefer longest",
Description = "If unchecked, the expression may only match part of a longer number. Leaving this checked may slightly reduce performance.",
};

public enum LimitType
{
Nothing,
Expand Down Expand Up @@ -86,11 +93,9 @@ protected override NodeResultBuilder GetValue()
string number = InputLimitBy.Value switch
{
LimitType.Value => GetIntegerRangeRegex(InputValueRange.Min ?? 0, InputValueRange.Max ?? 0),
LimitType.Digits => (InputDigitRange.Min ?? 0) == InputDigitRange.Max ?
$"\\d{{{InputDigitRange.Min}}}" :
$"\\d{{{InputDigitRange.Min ?? 0},{InputDigitRange.Max}}}",
LimitType.Nothing => "\\d+",
_ => "\\d+",
LimitType.Digits => GetQuantifierRangeSuffix() + QuantifierSuffix,
LimitType.Nothing => "\\d+" + QuantifierSuffix,
_ => "\\d+" + QuantifierSuffix,
};
var builder = new NodeResultBuilder(number, this);

Expand All @@ -106,6 +111,17 @@ protected override NodeResultBuilder GetValue()
return builder;
}

private string GetQuantifierRangeSuffix()
{
int min = InputDigitRange.Min ?? 0;
return min == InputDigitRange.Max ?
$"\\d{{{min}}}" :
$"\\d{{{min},{InputDigitRange.Max}}}";
}

private string QuantifierSuffix =>
InputPreferLongest.Checked ? "" : "?";

private void AddSign(NodeResultBuilder builder)
{
switch (InputSign.Value)
Expand All @@ -122,6 +138,10 @@ private void AddSign(NodeResultBuilder builder)
private string GetIntegerRangeRegex(int min, int max)
{
var ranges = rangeGenerator.GenerateRegexRange(min, max);
if (InputPreferLongest.Checked)
{
ranges.Reverse();
}
return string.Join('|', ranges);
}
}
Expand Down

0 comments on commit 752d7ec

Please sign in to comment.