diff --git a/DTLib/Extensions/SpanHelper.cs b/DTLib/Extensions/SpanHelper.cs new file mode 100644 index 0000000..a5eb440 --- /dev/null +++ b/DTLib/Extensions/SpanHelper.cs @@ -0,0 +1,39 @@ +namespace DTLib.Extensions; + +#if NETSTANDARD2_1_OR_GREATER || NET2_1_OR_GREATER +public static class SpanHelper +{ + public static ReadOnlySpan After(this ReadOnlySpan span, char c) + { + var index = span.IndexOf(c); + if (index == -1) + throw new Exception($"char <{c}> not found in span <{span}>"); + return span.Slice(index+1); + } + + public static ReadOnlySpan After(this ReadOnlySpan span, ReadOnlySpan s) + { + var index = span.IndexOf(s); + if (index == -1) + throw new Exception($"span <{s}> not found in span <{span}>"); + return span.Slice(index+s.Length); + } + + + public static ReadOnlySpan Before(this ReadOnlySpan span, char c) + { + var index = span.IndexOf(c); + if (index == -1) + throw new Exception($"char <{c}> not found in span <{span}>"); + return span.Slice(0,index); + } + + public static ReadOnlySpan Before(this ReadOnlySpan span, ReadOnlySpan s) + { + var index = span.IndexOf(s); + if (index == -1) + throw new Exception($"span <{s}> not found in span <{span}>"); + return span.Slice(0,index); + } +} +#endif \ No newline at end of file