Skip to content

Commit 5f87709

Browse files
committed
Refactor: Use ValueStringBuilder in PathStartsWithDataDirectory for performance and normalize input paths
1 parent 81dcf6f commit 5f87709

1 file changed

Lines changed: 18 additions & 7 deletions

File tree

src/PetroglyphTools/PG.StarWarsGame.Engine/IO/Repositories/GameRepository.Files.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,21 +160,32 @@ protected FileFoundInfo FileFromAltExists(ReadOnlySpan<char> filePath, IList<str
160160
return default;
161161
}
162162

163-
private static bool PathStartsWithDataDirectory(ReadOnlySpan<char> path, out int cutoffLength)
163+
private bool PathStartsWithDataDirectory(ReadOnlySpan<char> path, out int cutoffLength)
164164
{
165165
cutoffLength = 0;
166166
if (path.Length < 5)
167167
return false;
168-
foreach (var prefix in DataPathPrefixes)
168+
169+
var sb = new ValueStringBuilder(stackalloc char[265]);
170+
sb.Append(path);
171+
PGFileSystem.NormalizePath(ref sb);
172+
try
169173
{
170-
if (path.StartsWith(prefix.AsSpan(), StringComparison.OrdinalIgnoreCase))
174+
foreach (var prefix in DataPathPrefixes)
171175
{
172-
if (path[0] == '.')
173-
cutoffLength = 2;
174-
return true;
176+
if (sb.AsSpan().StartsWith(prefix.AsSpan(), StringComparison.OrdinalIgnoreCase))
177+
{
178+
if (path[0] == '.')
179+
cutoffLength = 2;
180+
return true;
181+
}
175182
}
183+
return false;
184+
}
185+
finally
186+
{
187+
sb.Dispose();
176188
}
177-
return false;
178189
}
179190

180191
internal Stream? OpenFileCore(FileFoundInfo fileFoundInfo)

0 commit comments

Comments
 (0)