Refactor FindAllTextTags method to support multiple start tags and improve tag detection logic

This commit is contained in:
Thorsten Sommer 2025-04-27 13:38:29 +02:00
parent bcde1c2c02
commit 22ce7472f3
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108

View File

@ -133,24 +133,48 @@ public sealed partial class CollectI18NKeysCommand
private List<string> FindAllTextTags(ReadOnlySpan<char> fileContent) private List<string> FindAllTextTags(ReadOnlySpan<char> fileContent)
{ {
const string START_TAG = """ const string START_TAG1 = """
T(" T("
"""; """;
const string START_TAG2 = """
TB("
""";
const string END_TAG = """ const string END_TAG = """
") ")
"""; """;
(int Index, int Len) FindNextStart(ReadOnlySpan<char> content)
{
var startIdx1 = content.IndexOf(START_TAG1);
var startIdx2 = content.IndexOf(START_TAG2);
if (startIdx1 == -1 && startIdx2 == -1)
return (-1, 0);
if (startIdx1 == -1)
return (startIdx2, START_TAG2.Length);
if (startIdx2 == -1)
return (startIdx1, START_TAG1.Length);
if (startIdx1 < startIdx2)
return (startIdx1, START_TAG1.Length);
return (startIdx2, START_TAG2.Length);
}
var matches = new List<string>(); var matches = new List<string>();
var startIdx = fileContent.IndexOf(START_TAG); var startIdx = FindNextStart(fileContent);
var content = fileContent; var content = fileContent;
while (startIdx > -1) while (startIdx.Index > -1)
{ {
// //
// In some cases, after the initial " there follow more " characters. // In some cases, after the initial " there follow more " characters.
// We need to skip them: // We need to skip them:
// //
content = content[(startIdx + START_TAG.Length)..]; content = content[(startIdx.Index + startIdx.Len)..];
while(content[0] == '"') while(content[0] == '"')
content = content[1..]; content = content[1..];
@ -163,7 +187,7 @@ public sealed partial class CollectI18NKeysCommand
match = match[..^1]; match = match[..^1];
matches.Add(match.ToString()); matches.Add(match.ToString());
startIdx = content.IndexOf(START_TAG); startIdx = FindNextStart(content);
} }
return matches; return matches;