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

Commit df2c94c

Browse files
committed
Merge pull request #414 from jerbri/patch-1
Renamed SafeSubstring param and supporting logic
2 parents 9892c73 + b755ab1 commit df2c94c

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/ServiceStack.Text/StringExtensions.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -601,11 +601,9 @@ public static string ToLowercaseUnderscore(this string value)
601601
return sb.ToString();
602602
}
603603

604-
public static string SafeSubstring(this string value, int length)
604+
public static string SafeSubstring(this string value, int startIndex)
605605
{
606-
return String.IsNullOrEmpty(value)
607-
? String.Empty
608-
: value.Substring(Math.Min(length, value.Length));
606+
return SafeSubstring(value, startIndex, value.Length);
609607
}
610608

611609
public static string SafeSubstring(this string value, int startIndex, int length)

tests/ServiceStack.Text.Tests/StringExtensionsTests.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,5 +226,26 @@ public void Can_ParseKeyValueText()
226226
Assert.That("a:b\nc:d".ParseKeyValueText()["c"], Is.EqualTo("d"));
227227
Assert.That("a:b\r\nc:d".ParseKeyValueText()["c"], Is.EqualTo("d"));
228228
}
229+
230+
[Test]
231+
public void Can_SafeSubstring_with_no_length {
232+
233+
var input = "TestString";
234+
Assert.That(input.SafeSubstring(0), Is.EqualTo("TestString"));
235+
Assert.That(input.SafeSubstring(2), Is.EqualTo("stString"));
236+
Assert.That(input.SafeSubstring(20), Is.EqualTo(""));
237+
}
238+
239+
[Test]
240+
public void Can_SafeSubstring_with_length {
241+
var input = "TestString";
242+
Assert.That(input.SafeSubstring(0,4), Is.EqualTo("Test"));
243+
Assert.That(input.SafeSubstring(2,4), Is.EqualTo("stSt"));
244+
Assert.That(input.SafeSubstring(20,4), Is.EqualTo(""));
245+
Assert.That(input.SafeSubstring(0,20), Is.EqualTo("TestString"));
246+
247+
}
248+
}
249+
}
229250
}
230-
}
251+
}

0 commit comments

Comments
 (0)