Skip to content

Commit 2e352bc

Browse files
authored
#2145 Refactor Infrastructure StringExtensions (#2222)
1 parent bbf7e2c commit 2e352bc

File tree

3 files changed

+31
-25
lines changed

3 files changed

+31
-25
lines changed

src/Ocelot.Provider.Consul/DefaultConsulServiceBuilder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ protected virtual string GetServiceId(ServiceEntry entry, Node node)
102102
protected virtual string GetServiceVersion(ServiceEntry entry, Node node)
103103
=> entry.Service.Tags
104104
?.FirstOrDefault(tag => tag.StartsWith(VersionPrefix, StringComparison.Ordinal))
105-
?.TrimStart(VersionPrefix)
105+
?.TrimPrefix(VersionPrefix)
106106
?? string.Empty;
107107

108108
protected virtual IEnumerable<string> GetServiceTags(ServiceEntry entry, Node node)

src/Ocelot/Infrastructure/Extensions/StringExtensions.cs

+16-13
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,32 @@
22

33
public static class StringExtensions
44
{
5-
public static string TrimStart(this string source, string trim, StringComparison stringComparison = StringComparison.Ordinal)
5+
/// <summary>Removes the prefix from the beginning of the string repeatedly until all occurrences are eliminated.</summary>
6+
/// <param name="source">The string to trim.</param>
7+
/// <param name="prefix">The prefix string to remove.</param>
8+
/// <param name="comparison">The 2nd argument of the <see cref="string.StartsWith(string)"/> method.</param>
9+
/// <returns>A new <see cref="string"/> without the prefix all occurrences.</returns>
10+
public static string TrimPrefix(this string source, string prefix, StringComparison comparison = StringComparison.Ordinal)
611
{
7-
if (source == null)
12+
if (source == null || string.IsNullOrEmpty(prefix))
813
{
9-
return null;
14+
return source;
1015
}
1116

1217
var s = source;
13-
while (s.StartsWith(trim, stringComparison))
18+
while (s.StartsWith(prefix, comparison))
1419
{
15-
s = s.Substring(trim.Length);
20+
s = s[prefix.Length..];
1621
}
1722

1823
return s;
1924
}
2025

21-
public static string LastCharAsForwardSlash(this string source)
22-
{
23-
if (source.EndsWith('/'))
24-
{
25-
return source;
26-
}
26+
public const char Slash = '/';
2727

28-
return $"{source}/";
29-
}
28+
/// <summary>Ensures that the last char of the string is forward slash, '/'.</summary>
29+
/// <param name="source">The string to check its last slash char.</param>
30+
/// <returns>A <see cref="string"/> witl the last forward slash.</returns>
31+
public static string LastCharAsForwardSlash(this string source)
32+
=> source.EndsWith(Slash) ? source : source + Slash;
3033
}

test/Ocelot.UnitTests/Infrastructure/StringExtensionsTests.cs

+14-11
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,25 @@ namespace Ocelot.UnitTests.Infrastructure;
55
public class StringExtensionsTests
66
{
77
[Fact]
8-
public void should_trim_start()
8+
public void TrimPrefix_ArgsCheck_ReturnedSource()
99
{
10-
var test = "/string";
11-
12-
test = test.TrimStart("/");
13-
14-
test.ShouldBe("string");
10+
((string)null).TrimPrefix("/").ShouldBeNull();
11+
"x".TrimPrefix(null).ShouldBe("x");
12+
"x".TrimPrefix(string.Empty).ShouldBe("x");
1513
}
1614

1715
[Fact]
18-
public void should_return_source()
16+
public void TrimPrefix_HasPrefix_HappyPath()
1917
{
20-
var test = "string";
21-
22-
test = test.LastCharAsForwardSlash();
18+
"/string".TrimPrefix("/").ShouldBe("string");
19+
"///string".TrimPrefix("/").ShouldBe("string");
20+
"ABABstring".TrimPrefix("AB").ShouldBe("string");
21+
}
2322

24-
test.ShouldBe("string/");
23+
[Fact]
24+
public void LastCharAsForwardSlash_HappyPath()
25+
{
26+
"string".LastCharAsForwardSlash().ShouldBe("string/");
27+
"string/".LastCharAsForwardSlash().ShouldBe("string/");
2528
}
2629
}

0 commit comments

Comments
 (0)