Skip to content

Commit

Permalink
Merge pull request #9 from lpreiner/bugfix/nested-prefixed-set-expans…
Browse files Browse the repository at this point in the history
…ions

Bugfix/nested prefixed set expansions
  • Loading branch information
lpreiner authored Feb 14, 2024
2 parents f13edd1 + dee6de0 commit 0e427be
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
17 changes: 16 additions & 1 deletion BraceExpander.Tests/SetExpansionTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Xunit;
using System;
using System.Linq;
using Xunit;

namespace BraceExpander.Tests
{
Expand Down Expand Up @@ -32,6 +34,19 @@ public void NestedSetExpansion(string expression)
x => Assert.Equal("g", x));
}

[Theory]
[InlineData("a{,b}", "a", "ab")]
[InlineData("a{,b{,c}}", "a", "ab", "abc")]
[InlineData("a{,b{,c,},}", "a", "ab", "abc", "ab", "a")]
public void NestedMixedSetExpansion(string expression, params object[] expectedResults)
{
var results = Expander.Expand(expression);

Assert.Collection(results, expectedResults
.Select(x => (Action<string>)(y => Assert.Equal(x, y)))
.ToArray());
}

[Fact]
public void EmptyElementExpansion()
{
Expand Down
10 changes: 4 additions & 6 deletions BraceExpander/BraceExpander.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,13 @@ public static IEnumerable<string> Expand(string expression)
switch (c)
{
case '{':
if (level == 0)
elementStart = i + 1;
level++;
break;
case '}':
{
level--;
if (level == 0)
if (--level == 0)
{
var element = $"{{{exp[(elementStart)..(i)]}}}";
var element = exp[(elementStart)..(i + 1)];
expansions.AddRange(Expand(element));
elementStart = i + 1;
}
Expand Down Expand Up @@ -152,7 +149,8 @@ public static IEnumerable<string> Expand(string expression)

foreach (var e in expansions)
{
var next = Expand(expression[(match.Index + match.Length)..]);
var nextExp = expression[(match.Index + match.Length)..];
var next = Expand(nextExp);
foreach (var n in next)
yield return expression[..match.Index] + e + n;
}
Expand Down
2 changes: 1 addition & 1 deletion BraceExpander/BraceExpander.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<!-- Package Details -->
<PropertyGroup>
<PackageId>BraceExpander</PackageId>
<Version>1.0.3</Version>
<Version>1.0.4</Version>
<Authors>Luke Preiner</Authors>
<title>BraceExpander</title>
<RepositoryUrl>https://github.com/lpreiner/BraceExpander</RepositoryUrl>
Expand Down

0 comments on commit 0e427be

Please sign in to comment.