Skip to content

Commit c5e8e47

Browse files
Optimize DetermineService (#3365)
* Add DetermineService tests * Optimize DetermineService --------- Co-authored-by: Daniel Marbach <[email protected]>
1 parent 37a9afe commit c5e8e47

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed

sdk/src/Core/Amazon.Util/AWSSDKUtils.cs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -559,33 +559,32 @@ public static string DetermineRegion(string url)
559559
/// </returns>
560560
public static string DetermineService(string url)
561561
{
562-
int delimIndex = url.IndexOf("//", StringComparison.Ordinal);
563-
if (delimIndex >= 0)
564-
url = url.Substring(delimIndex + 2);
562+
var urlSpan = url.AsSpan();
565563

566-
string[] urlParts = url.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
567-
if (urlParts == null || urlParts.Length == 0)
568-
return string.Empty;
564+
var doubleSlashIndex = urlSpan.IndexOf(DoubleSlash, StringComparison.Ordinal);
565+
if (doubleSlashIndex >= 0)
566+
urlSpan = urlSpan.Slice(doubleSlashIndex + 2);
569567

570-
string servicePart = urlParts[0];
571-
int hyphenated = servicePart.IndexOf('-');
572-
string service;
573-
if (hyphenated < 0)
574-
{ service = servicePart; }
575-
else
576-
{ service = servicePart.Substring(0, hyphenated); }
568+
var dotIndex = urlSpan.IndexOf('.');
577569

578-
// Check for SQS : return "sqs" incase service is determined to be "queue" as per the URL.
579-
if (service.Equals("queue"))
580-
{
581-
return "sqs";
582-
}
583-
else
570+
if (dotIndex < 0)
571+
return string.Empty;
572+
573+
var servicePartSpan = urlSpan.Slice(0, dotIndex);
574+
var hyphenIndex = servicePartSpan.IndexOf('-');
575+
if (hyphenIndex > 0)
584576
{
585-
return service;
577+
servicePartSpan = servicePartSpan.Slice(0, hyphenIndex);
586578
}
579+
580+
// Check for SQS : return "sqs" in case service is determined to be "queue" as per the URL.
581+
return servicePartSpan.Equals(Queue, StringComparison.OrdinalIgnoreCase) ? "sqs" : servicePartSpan.ToString();
587582
}
588583

584+
// Compiler trick to directly refer to static data in the assembly
585+
private static ReadOnlySpan<char> DoubleSlash => new[] { '/', '/' };
586+
private static ReadOnlySpan<char> Queue => new[] { 'q', 'u', 'e', 'u', 'e' };
587+
589588
/// <summary>
590589
/// Utility method for converting Unix epoch seconds to DateTime structure.
591590
/// </summary>

sdk/test/NetStandard/UnitTests/Core/AWSSDKUtilsTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,18 @@ public void CompressSpaces()
5050
var compressed = AWSSDKUtils.CompressSpaces(data);
5151
Assert.Equal("Hello, World!", compressed);
5252
}
53+
54+
[Theory]
55+
[InlineData("https://s3.amazonaws.com", "s3")]
56+
[InlineData("sqs.us-west-2.amazonaws.com", "sqs")]
57+
[InlineData("queue.amazonaws.com", "sqs")]
58+
[InlineData("https://sns.us-west-2.amazonaws.com", "sns")]
59+
[InlineData("https://s3-external-1.amazonaws.com", "s3")]
60+
public void DetermineService(string url, string expectedService)
61+
{
62+
var service = AWSSDKUtils.DetermineService(url);
63+
64+
Assert.Equal(expectedService, service);
65+
}
5366
}
5467
}

0 commit comments

Comments
 (0)