Skip to content

Commit 0b4bd8f

Browse files
committed
Fix handling of case-sensitive set loops in RegexPrefixAnalyzer.FindPrefixes
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.
1 parent 5c01ed2 commit 0b4bd8f

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexPrefixAnalyzer.cs

+16-13
Original file line numberDiff line numberDiff line change
@@ -162,23 +162,26 @@ static bool FindPrefixesCore(RegexNode node, List<StringBuilder> results, bool i
162162
int reps = node.Kind is RegexNodeKind.Set ? 1 : Math.Min(node.M, MaxPrefixLength);
163163
if (!ignoreCase)
164164
{
165-
int existingCount = results.Count;
166-
167-
// Duplicate all of the existing strings for all of the new suffixes, other than the first.
168-
foreach (char suffix in setChars.Slice(1, charCount - 1))
165+
for (int rep = 0; rep < reps; rep++)
169166
{
170-
for (int existing = 0; existing < existingCount; existing++)
167+
int existingCount = results.Count;
168+
169+
// Duplicate all of the existing strings for all of the new suffixes, other than the first.
170+
foreach (char suffix in setChars.Slice(1, charCount - 1))
171171
{
172-
StringBuilder newSb = new StringBuilder().Append(results[existing]);
173-
newSb.Append(suffix, reps);
174-
results.Add(newSb);
172+
for (int existing = 0; existing < existingCount; existing++)
173+
{
174+
StringBuilder newSb = new StringBuilder().Append(results[existing]);
175+
newSb.Append(suffix);
176+
results.Add(newSb);
177+
}
175178
}
176-
}
177179

178-
// Then append the first suffix to all of the existing strings.
179-
for (int existing = 0; existing < existingCount; existing++)
180-
{
181-
results[existing].Append(setChars[0], reps);
180+
// Then append the first suffix to all of the existing strings.
181+
for (int existing = 0; existing < existingCount; existing++)
182+
{
183+
results[existing].Append(setChars[0]);
184+
}
182185
}
183186
}
184187
else

0 commit comments

Comments
 (0)