aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common/Extensions/StringExtensions.cs
diff options
context:
space:
mode:
authorDavid <daullmer@gmail.com>2020-06-14 12:49:43 +0200
committerDavid <daullmer@gmail.com>2020-06-14 12:49:43 +0200
commit8a8bf4b6866e804c60baace62dbda9b690c5ece7 (patch)
tree047d47638ba62138e6fe30a2eb22c3231b244e72 /MediaBrowser.Common/Extensions/StringExtensions.cs
parentfff3c789b98aad08f8ea66da275ff82c71dd8f2b (diff)
parent0011e8df47380936742302ef40639a4626a780ed (diff)
Merge remote-tracking branch 'upstream/api-migration' into api-sessionservice
Diffstat (limited to 'MediaBrowser.Common/Extensions/StringExtensions.cs')
-rw-r--r--MediaBrowser.Common/Extensions/StringExtensions.cs37
1 files changed, 37 insertions, 0 deletions
diff --git a/MediaBrowser.Common/Extensions/StringExtensions.cs b/MediaBrowser.Common/Extensions/StringExtensions.cs
new file mode 100644
index 000000000..764301741
--- /dev/null
+++ b/MediaBrowser.Common/Extensions/StringExtensions.cs
@@ -0,0 +1,37 @@
+#nullable enable
+
+using System;
+
+namespace MediaBrowser.Common.Extensions
+{
+ /// <summary>
+ /// Extensions methods to simplify string operations.
+ /// </summary>
+ public static class StringExtensions
+ {
+ /// <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 left of the <c>needle</c>.
+ /// </summary>
+ /// <param name="haystack">The string to seek.</param>
+ /// <param name="needle">The needle to find.</param>
+ /// <param name="stringComparison">One of the enumeration values that specifies the rules for the search.</param>
+ /// <returns>The part left of the <c>needle</c>.</returns>
+ public static ReadOnlySpan<char> LeftPart(this ReadOnlySpan<char> haystack, ReadOnlySpan<char> needle, StringComparison stringComparison = default)
+ {
+ var pos = haystack.IndexOf(needle, stringComparison);
+ return pos == -1 ? haystack : haystack[..pos];
+ }
+ }
+}