aboutsummaryrefslogtreecommitdiff
path: root/src/Jellyfin.Extensions/StringExtensions.cs
diff options
context:
space:
mode:
authorBond_009 <bond.009@outlook.com>2021-09-19 20:53:31 +0200
committerBond_009 <bond.009@outlook.com>2021-09-19 20:53:31 +0200
commita6d1e542e62548f177523f0acd67260f58066731 (patch)
tree280df20523ebaa228995afd689d02cfd599bf3ef /src/Jellyfin.Extensions/StringExtensions.cs
parenta3a8e058bc2f6249b6a17e277cc4e0cf98240ef4 (diff)
Reduce allocations
Diffstat (limited to 'src/Jellyfin.Extensions/StringExtensions.cs')
-rw-r--r--src/Jellyfin.Extensions/StringExtensions.cs34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/Jellyfin.Extensions/StringExtensions.cs b/src/Jellyfin.Extensions/StringExtensions.cs
index acc695ed2..3a7707253 100644
--- a/src/Jellyfin.Extensions/StringExtensions.cs
+++ b/src/Jellyfin.Extensions/StringExtensions.cs
@@ -27,5 +27,39 @@ namespace Jellyfin.Extensions
return count;
}
+
+ /// <summary>
+ /// Returns the part on the left of the <c>needle</c>.
+ /// </summary>
+ /// <param name="haystack">The string to seek.</param>
+ /// <param name="needle">The needle to find.</param>
+ /// <returns>The part left of the <paramref name="needle" />.</returns>
+ public static ReadOnlySpan<char> LeftPart(this ReadOnlySpan<char> haystack, char needle)
+ {
+ var pos = haystack.IndexOf(needle);
+ return pos == -1 ? haystack : haystack[..pos];
+ }
+
+ /// <summary>
+ /// Returns the part on the right of the <c>needle</c>.
+ /// </summary>
+ /// <param name="haystack">The string to seek.</param>
+ /// <param name="needle">The needle to find.</param>
+ /// <returns>The part right of the <paramref name="needle" />.</returns>
+ public static ReadOnlySpan<char> RightPart(this ReadOnlySpan<char> haystack, char needle)
+ {
+ var pos = haystack.LastIndexOf(needle);
+ if (pos == -1)
+ {
+ return haystack;
+ }
+
+ if (pos == haystack.Length - 1)
+ {
+ return ReadOnlySpan<char>.Empty;
+ }
+
+ return haystack[(pos + 1)..];
+ }
}
}