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

Commit 1a71363

Browse files
committed
Add guards against empy spans
1 parent d28ba58 commit 1a71363

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/ServiceStack.Text/CharMemoryExtensions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ public static ReadOnlyMemory<char> Trim(this ReadOnlyMemory<char> span)
388388

389389
public static ReadOnlyMemory<char> TrimStart(this ReadOnlyMemory<char> value)
390390
{
391+
if (value.IsEmpty) return TypeConstants.NullStringMemory;
391392
var span = value.Span;
392393
int start = 0;
393394
for (; start < span.Length; start++)
@@ -400,6 +401,7 @@ public static ReadOnlyMemory<char> TrimStart(this ReadOnlyMemory<char> value)
400401

401402
public static ReadOnlyMemory<char> TrimEnd(this ReadOnlyMemory<char> value)
402403
{
404+
if (value.IsEmpty) return TypeConstants.NullStringMemory;
403405
var span = value.Span;
404406
int end = span.Length - 1;
405407
for (; end >= 0; end--)
@@ -425,9 +427,10 @@ public static ReadOnlyMemory<char> SafeSlice(this ReadOnlyMemory<char> value, in
425427

426428
public static string SubstringWithEllipsis(this ReadOnlyMemory<char> value, int startIndex, int length)
427429
{
430+
if (value.IsEmpty) return string.Empty;
428431
var str = value.Slice(startIndex, length);
429432
return str.Length == length
430-
? str.ToString() + "..."
433+
? str + "..."
431434
: str.ToString();
432435
}
433436

src/ServiceStack.Text/StringSpanExtensions.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,13 +459,15 @@ public static ReadOnlySpan<char> ParentDirectory(this ReadOnlySpan<char> filePat
459459

460460
public static ReadOnlySpan<char> TrimEnd(this ReadOnlySpan<char> value, params char[] trimChars)
461461
{
462+
if (value.IsEmpty) return TypeConstants.NullStringSpan;
462463
if (trimChars == null || trimChars.Length == 0)
463464
return value.TrimHelper(1);
464465
return value.TrimHelper(trimChars, 1);
465466
}
466467

467468
private static ReadOnlySpan<char> TrimHelper(this ReadOnlySpan<char> value, int trimType)
468469
{
470+
if (value.IsEmpty) return TypeConstants.NullStringSpan;
469471
int end = value.Length - 1;
470472
int start = 0;
471473
if (trimType != 1)
@@ -485,6 +487,7 @@ private static ReadOnlySpan<char> TrimHelper(this ReadOnlySpan<char> value, int
485487

486488
private static ReadOnlySpan<char> TrimHelper(this ReadOnlySpan<char> value, char[] trimChars, int trimType)
487489
{
490+
if (value.IsEmpty) return TypeConstants.NullStringSpan;
488491
int end = value.Length - 1;
489492
int start = 0;
490493
if (trimType != 1)
@@ -516,6 +519,7 @@ private static ReadOnlySpan<char> TrimHelper(this ReadOnlySpan<char> value, char
516519

517520
private static ReadOnlySpan<char> CreateTrimmedString(this ReadOnlySpan<char> value, int start, int end)
518521
{
522+
if (value.IsEmpty) return TypeConstants.NullStringSpan;
519523
int length = end - start + 1;
520524
if (length == value.Length)
521525
return value;
@@ -539,6 +543,7 @@ public static ReadOnlySpan<char> SafeSlice(this ReadOnlySpan<char> value, int st
539543

540544
public static string SubstringWithEllipsis(this ReadOnlySpan<char> value, int startIndex, int length)
541545
{
546+
if (value.IsEmpty) return string.Empty;
542547
var str = value.Slice(startIndex, length);
543548
return str.Length == length
544549
? str.ToString() + "..."
@@ -656,6 +661,5 @@ public static int CountOccurrencesOf(this ReadOnlySpan<char> value, char needle)
656661
}
657662
return count;
658663
}
659-
660664
}
661665
}

0 commit comments

Comments
 (0)