Skip to content

Commit

Permalink
Narrow conditions for using Parse method
Browse files Browse the repository at this point in the history
Closes #15
  • Loading branch information
sblom committed Dec 23, 2023
1 parent 790ffa1 commit 0c41ca8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
19 changes: 19 additions & 0 deletions RegExtract.Test/Usage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,25 @@ public void a012()
Assert.Equivalent(("sxc", new List<Rule> { new Conditional(new Condition('x', '>', 2414), new Workflow("jtp")), new Conditional(new Condition('s', '>', 954), new Reject()), new Conditional(new Condition('m', '>', 2406), new Accept()), new Absolute(new Workflow("xfz")) }), result);
}

record triple(int a, int b, int c) {

Check warning on line 223 in RegExtract.Test/Usage.cs

View workflow job for this annotation

GitHub Actions / build

The type name 'triple' only contains lower-cased ascii characters. Such names may become reserved for the language.

Check warning on line 223 in RegExtract.Test/Usage.cs

View workflow job for this annotation

GitHub Actions / build

The type name 'triple' only contains lower-cased ascii characters. Such names may become reserved for the language.

Check warning on line 223 in RegExtract.Test/Usage.cs

View workflow job for this annotation

GitHub Actions / build

The type name 'triple' only contains lower-cased ascii characters. Such names may become reserved for the language.
public static triple Parse(string str)
{
return new triple(0,0,0);
}
}

private Regex rxa013 = new(@"(\d+) (\d+) (\d+)");

[Fact]
public void a013()
{
var plan = CreateAndLogPlan<triple>(rxa013);

var result = plan.Extract("123 456 789");

Assert.Equal(new triple(123,456,789), result);
}

[Fact]
public void f001()
{
Expand Down
4 changes: 2 additions & 2 deletions RegExtract/ExtractionPlan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ private ExtractionPlanNode AssignTypesToTree(RegexCaptureGroupNode tree, Extract
List<ExtractionPlanNode> groups = new();
List<ExtractionPlanNode> namedgroups = new();

if (type.IsDirectlyConstructable)
if (tree.children is [] or [{children: []}] && type.IsDirectlyConstructable)
{
// We're at a leaf in the type hierarchy, and all we need is a string.
// If there's an inner capture group, use it to narrow the match.
if (tree.children.Count() == 1)
if (tree.children.Length == 1)
{
tree = tree.children.Single();
}
Expand Down

0 comments on commit 0c41ca8

Please sign in to comment.