Created
June 6, 2024 13:36
-
-
Save bjoerntx/2f9de7e3073bcea1e9180e526c27adcf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static List<BlockInfo> FindMergeBlocks(string input) | |
{ | |
const string pattern = @"\{\{(#foreach\s+\w+|\s*\/foreach\s+\w+)\}\}"; | |
var matches = new List<BlockInfo>(); | |
var stack = new Stack<Match>(); | |
foreach (Match match in Regex.Matches(input, pattern)) | |
{ | |
if (match.Value.StartsWith("{{#foreach")) | |
{ | |
stack.Push(match); | |
} | |
else if (match.Value.StartsWith("{{/foreach") && stack.Count > 0) | |
{ | |
var startMatch = stack.Pop(); | |
var startVar = ExtractVariableName(startMatch.Value); | |
var endVar = ExtractVariableName(match.Value); | |
if (startVar == endVar) | |
{ | |
matches.Add(new BlockInfo | |
{ | |
StartIndex = startMatch.Index + 1, | |
EndIndex = match.Index + 1 + match.Length, | |
BlockName = startVar | |
}); | |
} | |
} | |
} | |
matches.Sort((x, y) => x.StartIndex.CompareTo(y.StartIndex)); | |
return matches; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment