From a9c0e1172fe56eb0db7c556a25621076e35b1cb9 Mon Sep 17 00:00:00 2001 From: timerix Date: Mon, 5 Dec 2022 19:02:09 +0600 Subject: [PATCH] SpanHelper --- DTLib/Extensions/SpanHelper.cs | 39 ++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 DTLib/Extensions/SpanHelper.cs 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