Skip to content

Commit

Permalink
#2145 Refactor Infrastructure StringExtensions (#2222)
Browse files Browse the repository at this point in the history
  • Loading branch information
raman-m authored Nov 28, 2024
1 parent bbf7e2c commit 2e352bc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/Ocelot.Provider.Consul/DefaultConsulServiceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected virtual string GetServiceId(ServiceEntry entry, Node node)
protected virtual string GetServiceVersion(ServiceEntry entry, Node node)
=> entry.Service.Tags
?.FirstOrDefault(tag => tag.StartsWith(VersionPrefix, StringComparison.Ordinal))
?.TrimStart(VersionPrefix)
?.TrimPrefix(VersionPrefix)
?? string.Empty;

protected virtual IEnumerable<string> GetServiceTags(ServiceEntry entry, Node node)
Expand Down
29 changes: 16 additions & 13 deletions src/Ocelot/Infrastructure/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,32 @@

public static class StringExtensions
{
public static string TrimStart(this string source, string trim, StringComparison stringComparison = StringComparison.Ordinal)
/// <summary>Removes the prefix from the beginning of the string repeatedly until all occurrences are eliminated.</summary>
/// <param name="source">The string to trim.</param>
/// <param name="prefix">The prefix string to remove.</param>
/// <param name="comparison">The 2nd argument of the <see cref="string.StartsWith(string)"/> method.</param>
/// <returns>A new <see cref="string"/> without the prefix all occurrences.</returns>
public static string TrimPrefix(this string source, string prefix, StringComparison comparison = StringComparison.Ordinal)
{
if (source == null)
if (source == null || string.IsNullOrEmpty(prefix))
{
return null;
return source;
}

var s = source;
while (s.StartsWith(trim, stringComparison))
while (s.StartsWith(prefix, comparison))
{
s = s.Substring(trim.Length);
s = s[prefix.Length..];
}

return s;
}

public static string LastCharAsForwardSlash(this string source)
{
if (source.EndsWith('/'))
{
return source;
}
public const char Slash = '/';

return $"{source}/";
}
/// <summary>Ensures that the last char of the string is forward slash, '/'.</summary>
/// <param name="source">The string to check its last slash char.</param>
/// <returns>A <see cref="string"/> witl the last forward slash.</returns>
public static string LastCharAsForwardSlash(this string source)
=> source.EndsWith(Slash) ? source : source + Slash;
}
25 changes: 14 additions & 11 deletions test/Ocelot.UnitTests/Infrastructure/StringExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,25 @@ namespace Ocelot.UnitTests.Infrastructure;
public class StringExtensionsTests
{
[Fact]
public void should_trim_start()
public void TrimPrefix_ArgsCheck_ReturnedSource()
{
var test = "/string";

test = test.TrimStart("/");

test.ShouldBe("string");
((string)null).TrimPrefix("/").ShouldBeNull();
"x".TrimPrefix(null).ShouldBe("x");
"x".TrimPrefix(string.Empty).ShouldBe("x");
}

[Fact]
public void should_return_source()
public void TrimPrefix_HasPrefix_HappyPath()
{
var test = "string";

test = test.LastCharAsForwardSlash();
"/string".TrimPrefix("/").ShouldBe("string");
"///string".TrimPrefix("/").ShouldBe("string");
"ABABstring".TrimPrefix("AB").ShouldBe("string");
}

test.ShouldBe("string/");
[Fact]
public void LastCharAsForwardSlash_HappyPath()
{
"string".LastCharAsForwardSlash().ShouldBe("string/");
"string/".LastCharAsForwardSlash().ShouldBe("string/");
}
}

0 comments on commit 2e352bc

Please sign in to comment.