Skip to content

Commit

Permalink
Fix ListOfList bug and another unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
sblom committed Dec 15, 2023
1 parent f0711f4 commit 8253829
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
18 changes: 16 additions & 2 deletions RegExtract.Test/Usage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ public void a008()
var result = plan.Extract(@"rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7");
}

[Fact]
public void a009()
{
var plan = ExtractionPlan<List<List<List<char>>>>.CreatePlan(new Regex(@"(((\w)+ ?)+,? ?)+"));
var str = plan.ToString("x");
output.WriteLine(str);

var result = plan.Extract(@"asdf lkj, wero oiu");
}

[Fact]
public void slow()
{
Expand Down Expand Up @@ -335,7 +345,11 @@ public void nested_extraction()
[Fact]
public void nested_extraction_of_list()
{
var result = "The quick brown fox jumps over the lazy dog.".Extract<List<List<char>>>(@"(?:((\w)+) ?)+");
var plan = ExtractionPlan<List<List<char>>>.CreatePlan(new Regex(@"(((\w)+) ?)+"));
var str = plan.ToString("x");
output.WriteLine(str);

var result = plan.Extract("The quick brown fox jumps over the lazy dog.");
}

[Fact]
Expand Down Expand Up @@ -418,7 +432,7 @@ public void CreateTreePlan()
var plan = ExtractionPlan<((int?, int?)?, char, string)?>.CreatePlan(regex);
object? result = plan.Extract(regex.Match("2-12 c: abcdefgji"));

regex = new Regex(@"(?:((\w)+) ?)+");
regex = new Regex(@"(((\w)+) ?)+");
var plan2 = ExtractionPlan<List<List<char>>>.CreatePlan(regex);

result = plan2.Extract(regex.Match("The quick brown fox jumps over the lazy dog"));
Expand Down
6 changes: 4 additions & 2 deletions RegExtract/ExtractionPlan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ internal void InitializePlan(Regex regex)

// We use C#'s definition of an initializable collection, which is any type that implements IEnumerable and has a public Add() method.
// In our case, we also require that the Add() method has parameters of the same type as the collection's generic parameters.
protected bool IsInitializableCollection(Type type)
protected bool IsInitializableCollection(Type? type)
{
if (type == null)
return false;
var genericParameters = type.GetGenericArguments();
var addMethod = type.GetMethod("Add", BindingFlags.Public | BindingFlags.Instance, null, genericParameters, null);

Expand Down Expand Up @@ -239,7 +241,7 @@ private ExtractionPlanNode AssignTypesToTree(RegexCaptureGroupNode tree, Type ty
return new VirtualUnaryTupleNode(tree.name, type, new[] { AssignTypesToTree(tree.children.Single(), type) }, new ExtractionPlanNode[0]);
}

if (typeParams.Length < 2)
if (typeParams.Length < 2 && !IsInitializableCollection(typeParams.FirstOrDefault()))
{
return ExtractionPlanNode.Bind(tree.name, type, new[] { BindConstructorPlan(tree, type, 0, 1, stack) }, new ExtractionPlanNode[0]);
}
Expand Down

0 comments on commit 8253829

Please sign in to comment.