Skip to content

Commit

Permalink
fix: Use ordinal string operations
Browse files Browse the repository at this point in the history
Checked:

- IndexOf
- StartsWith
- EndsWith
- OrderBy
- Sort
- Compare

(Annoyingly, for StartsWith and EndsWith there are various
single-character cases where a character literal would do the right
thing - but those overloads aren't supported on .NET Framework.)
  • Loading branch information
jskeet committed Sep 26, 2023
1 parent 1af2b4e commit 9456e22
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Google.Api.Gax.Grpc/Rest/ReadHttpResponseMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ internal static Rpc.Status CreateRpcStatus(HttpStatusCode statusCode, string con
// Error response messages for streaming APIs are embedded within arrays.
// If we detect that the content looks like a JSON array, extract it and parse
// expecting a single object.
if (content.StartsWith("[") && content.EndsWith("]"))
if (content.StartsWith("[", StringComparison.Ordinal) && content.EndsWith("]", StringComparison.Ordinal))
{
content = content.Substring(1, content.Length - 2);
}
Expand Down
3 changes: 2 additions & 1 deletion Google.Api.Gax.Grpc/Rest/RestChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ internal RestChannel(
// Reuse a single CallInvoker however many times CreateCallInvoker is called.
_callInvoker = new RestCallInvoker(this);
// TODO: Handle endpoints better...
var endpointWithScheme = endpoint.StartsWith("https://") || endpoint.StartsWith("http://")
var endpointWithScheme =
endpoint.StartsWith("https://", StringComparison.Ordinal) || endpoint.StartsWith("http://", StringComparison.Ordinal)
? endpoint
: $"https://{endpoint}";
var baseAddress = new Uri(endpointWithScheme);
Expand Down
8 changes: 4 additions & 4 deletions Google.Api.Gax/PathTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ private string TryParseNameInternal(string name, out TemplatedResourceName resul
{
GaxPreconditions.CheckNotNull(name, nameof(name));
string serviceName = null;
if (name.StartsWith("//"))
if (name.StartsWith("//", StringComparison.Ordinal))
{
int nameEnd = name.IndexOf('/', 2);
// Can't call ValidateServiceName as we don't want to throw...
Expand Down Expand Up @@ -371,7 +371,7 @@ internal void ValidateWildcardValue(string value)
case SegmentKind.Literal:
throw new InvalidOperationException("Values cannot be specified for literal segments");
case SegmentKind.PathWildcard:
if (value.StartsWith("/") || value.EndsWith("/"))
if (value.StartsWith("/", StringComparison.Ordinal) || value.EndsWith("/", StringComparison.Ordinal))
{
throw new ArgumentException("Path wildcard values must not start or end with /", nameof(value));
}
Expand Down Expand Up @@ -403,8 +403,8 @@ internal static Segment Parse(string segment)
{
return s_unnamedPathWildcard;
}
bool startsWithBrace = segment.StartsWith("{");
bool endsWithBrace = segment.EndsWith("}");
bool startsWithBrace = segment.StartsWith("{", StringComparison.Ordinal);
bool endsWithBrace = segment.EndsWith("}", StringComparison.Ordinal);
if (startsWithBrace != endsWithBrace)
{
throw new ArgumentException($"Invalid template segment: {segment}", nameof(segment));
Expand Down

0 comments on commit 9456e22

Please sign in to comment.