Skip to content

Commit

Permalink
Fix handling of case-sensitive set loops in RegexPrefixAnalyzer.FindP…
Browse files Browse the repository at this point in the history
…refixes

For an expression like `[Aa]{2}`, we were generating the strings "AA" and "aa" but not "Aa" or "aA".

This code isn't exercised yet, as we're currently only using FindPrefixes for case-insensitive, but I'm trying to enable it for case-sensitive as well, and hit this. I'm not adding new tests here as plenty of existing tests catch it once it's enabled.
  • Loading branch information
stephentoub committed Apr 26, 2024
1 parent 5c01ed2 commit 0b4bd8f
Showing 1 changed file with 16 additions and 13 deletions.
Expand Up @@ -162,23 +162,26 @@ static bool FindPrefixesCore(RegexNode node, List<StringBuilder> results, bool i
int reps = node.Kind is RegexNodeKind.Set ? 1 : Math.Min(node.M, MaxPrefixLength);
if (!ignoreCase)
{
int existingCount = results.Count;

// Duplicate all of the existing strings for all of the new suffixes, other than the first.
foreach (char suffix in setChars.Slice(1, charCount - 1))
for (int rep = 0; rep < reps; rep++)
{
for (int existing = 0; existing < existingCount; existing++)
int existingCount = results.Count;

// Duplicate all of the existing strings for all of the new suffixes, other than the first.
foreach (char suffix in setChars.Slice(1, charCount - 1))
{
StringBuilder newSb = new StringBuilder().Append(results[existing]);
newSb.Append(suffix, reps);
results.Add(newSb);
for (int existing = 0; existing < existingCount; existing++)
{
StringBuilder newSb = new StringBuilder().Append(results[existing]);
newSb.Append(suffix);
results.Add(newSb);
}
}
}

// Then append the first suffix to all of the existing strings.
for (int existing = 0; existing < existingCount; existing++)
{
results[existing].Append(setChars[0], reps);
// Then append the first suffix to all of the existing strings.
for (int existing = 0; existing < existingCount; existing++)
{
results[existing].Append(setChars[0]);
}
}
}
else
Expand Down

0 comments on commit 0b4bd8f

Please sign in to comment.