Skip to content

Commit

Permalink
Only call ToSpan() once
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenaw committed Jul 26, 2024
1 parent cbeb741 commit 7d220da
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions sdk/src/Core/Amazon.Util/AWSSDKUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,21 +205,22 @@ public static string GetExtension(string path)

#if NET8_0_OR_GREATER
// LastIndexOf and LastIndexOfAny is vectorized on .NET8+ and is
// signifigantly faster for cases where 'path' does not end with a short file
// significantly faster for cases where 'path' does not end with a short file
// extension, such as GUIDs
int extensionIndex = path.AsSpan().LastIndexOf('.');
ReadOnlySpan<char> pathSpan = path.AsSpan();
int extensionIndex = pathSpan.LastIndexOf('.');
if (extensionIndex == -1)
{
return string.Empty;
}

int directoryIndex = path.AsSpan().LastIndexOfAny('/', '\\', ':');
int directoryIndex = pathSpan.LastIndexOfAny('/', '\\', ':');

// extension separator is found and exists before path separator or path separator doesn't exist
// AND it's not the last one in the string
if (directoryIndex < extensionIndex && extensionIndex < path.Length - 1)
if (directoryIndex < extensionIndex && extensionIndex < pathSpan.Length - 1)
{
return path.Substring(extensionIndex);
return pathSpan.Slice(extensionIndex).ToString();
}

return string.Empty;
Expand Down

0 comments on commit 7d220da

Please sign in to comment.