Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 9124038

Browse files
committed
Change behavior of Left/Right Part to match SplitOnFirst()[0] and SplitOnLast().Last() behavior
1 parent e7c833c commit 9124038

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/ServiceStack.Text/StringExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ public static string RightPart(this string strVal, char needle)
358358
if (strVal == null) return null;
359359
var pos = strVal.IndexOf(needle);
360360
return pos == -1
361-
? null
361+
? strVal
362362
: strVal.Substring(pos + 1);
363363
}
364364

@@ -367,7 +367,7 @@ public static string RightPart(this string strVal, string needle)
367367
if (strVal == null) return null;
368368
var pos = strVal.IndexOf(needle, StringComparison.OrdinalIgnoreCase);
369369
return pos == -1
370-
? null
370+
? strVal
371371
: strVal.Substring(pos + needle.Length);
372372
}
373373

@@ -394,7 +394,7 @@ public static string LastRightPart(this string strVal, char needle)
394394
if (strVal == null) return null;
395395
var pos = strVal.LastIndexOf(needle);
396396
return pos == -1
397-
? null
397+
? strVal
398398
: strVal.Substring(pos + 1);
399399
}
400400

@@ -403,7 +403,7 @@ public static string LastRightPart(this string strVal, string needle)
403403
if (strVal == null) return null;
404404
var pos = strVal.LastIndexOf(needle, StringComparison.OrdinalIgnoreCase);
405405
return pos == -1
406-
? null
406+
? strVal
407407
: strVal.Substring(pos + needle.Length);
408408
}
409409

tests/ServiceStack.Text.Tests/StringExtensionsTests.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@ public void Can_LeftPart_and_LeftPart_char_needle()
2121
{
2222
var str = "user:pass@w:rd";
2323
Assert.That(str.LeftPart(':'), Is.EqualTo("user"));
24+
Assert.That(str.SplitOnFirst(':')[0], Is.EqualTo("user"));
2425
Assert.That(str.RightPart(':'), Is.EqualTo("pass@w:rd"));
26+
Assert.That(str.SplitOnFirst(':').Last(), Is.EqualTo("pass@w:rd"));
2527

2628
Assert.That(str.LeftPart('|'), Is.EqualTo("user:pass@w:rd"));
27-
Assert.That(str.RightPart('|'), Is.Null);
29+
Assert.That(str.SplitOnFirst('|')[0], Is.EqualTo("user:pass@w:rd"));
30+
Assert.That(str.RightPart('|'), Is.EqualTo("user:pass@w:rd"));
31+
Assert.That(str.SplitOnFirst('|').Last(), Is.EqualTo("user:pass@w:rd"));
2832
}
2933

3034
[Test]
@@ -40,10 +44,14 @@ public void Can_LeftPart_and_RightPart_string_needle()
4044
{
4145
var str = "user::pass@w:rd";
4246
Assert.That(str.LeftPart("::"), Is.EqualTo("user"));
47+
Assert.That(str.SplitOnFirst("::")[0], Is.EqualTo("user"));
4348
Assert.That(str.RightPart("::"), Is.EqualTo("pass@w:rd"));
49+
Assert.That(str.SplitOnFirst("::").Last(), Is.EqualTo("pass@w:rd"));
4450

4551
Assert.That(str.LeftPart("||"), Is.EqualTo("user::pass@w:rd"));
46-
Assert.That(str.RightPart("||"), Is.Null);
52+
Assert.That(str.SplitOnFirst("||")[0], Is.EqualTo("user::pass@w:rd"));
53+
Assert.That(str.RightPart("||"), Is.EqualTo("user::pass@w:rd"));
54+
Assert.That(str.SplitOnFirst("||").Last(), Is.EqualTo("user::pass@w:rd"));
4755
}
4856

4957
[Test]
@@ -59,10 +67,14 @@ public void Can_LastLeftPart_and_LastRightPart_char_needle()
5967
{
6068
var str = "user:name:pass@word";
6169
Assert.That(str.LastLeftPart(':'), Is.EqualTo("user:name"));
70+
Assert.That(str.SplitOnLast(':')[0], Is.EqualTo("user:name"));
6271
Assert.That(str.LastRightPart(':'), Is.EqualTo("pass@word"));
72+
Assert.That(str.SplitOnLast(':').Last(), Is.EqualTo("pass@word"));
6373

6474
Assert.That(str.LastLeftPart('|'), Is.EqualTo("user:name:pass@word"));
65-
Assert.That(str.LastRightPart('|'), Is.Null);
75+
Assert.That(str.SplitOnLast('|')[0], Is.EqualTo("user:name:pass@word"));
76+
Assert.That(str.LastRightPart('|'), Is.EqualTo("user:name:pass@word"));
77+
Assert.That(str.SplitOnLast('|').Last(), Is.EqualTo("user:name:pass@word"));
6678
}
6779

6880
[Test]
@@ -78,10 +90,14 @@ public void Can_LastLeftPart_and_LastRightPart_string_needle()
7890
{
7991
var str = "user::name::pass@word";
8092
Assert.That(str.LastLeftPart("::"), Is.EqualTo("user::name"));
93+
Assert.That(str.SplitOnLast("::")[0], Is.EqualTo("user::name"));
8194
Assert.That(str.LastRightPart("::"), Is.EqualTo("pass@word"));
95+
Assert.That(str.SplitOnLast("::").Last(), Is.EqualTo("pass@word"));
8296

8397
Assert.That(str.LastLeftPart("||"), Is.EqualTo("user::name::pass@word"));
84-
Assert.That(str.LastRightPart("||"), Is.Null);
98+
Assert.That(str.SplitOnLast("||")[0], Is.EqualTo("user::name::pass@word"));
99+
Assert.That(str.LastRightPart("||"), Is.EqualTo("user::name::pass@word"));
100+
Assert.That(str.SplitOnLast("||").Last(), Is.EqualTo("user::name::pass@word"));
85101
}
86102

87103
private static readonly char DirSep = Path.DirectorySeparatorChar;

0 commit comments

Comments
 (0)