SpanHelper

This commit is contained in:
timerix 2022-12-05 19:02:09 +06:00
parent eec2ec60be
commit a9c0e1172f

View File

@ -0,0 +1,39 @@
namespace DTLib.Extensions;
#if NETSTANDARD2_1_OR_GREATER || NET2_1_OR_GREATER
public static class SpanHelper
{
public static ReadOnlySpan<char> After(this ReadOnlySpan<char> 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<char> After(this ReadOnlySpan<char> span, ReadOnlySpan<char> 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<char> Before(this ReadOnlySpan<char> 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<char> Before(this ReadOnlySpan<char> span, ReadOnlySpan<char> 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