SpanHelper AfterLast and BeforeLast

This commit is contained in:
Timerix22 2022-12-06 01:09:36 +06:00
parent f472c40f1f
commit b66b93947d

View File

@ -3,37 +3,82 @@ namespace DTLib.Extensions;
#if NETSTANDARD2_1 || NET6_0 || NET7_0 || NET8_0 #if NETSTANDARD2_1 || NET6_0 || NET7_0 || NET8_0
public static class SpanHelper public static class SpanHelper
{ {
public static ReadOnlySpan<char> After(this ReadOnlySpan<char> span, char c) public static ReadOnlySpan<T> After<T>(this ReadOnlySpan<T> span, T c) where T : IEquatable<T>
{ {
var index = span.IndexOf(c); var index = span.IndexOf(c);
if (index == -1) if (index == -1)
throw new Exception($"char <{c}> not found in span <{span}>"); throw new SpanHelperException<T>(c, span);
return span.Slice(index+1); return span.Slice(index+1);
} }
public static ReadOnlySpan<char> After(this ReadOnlySpan<char> span, ReadOnlySpan<char> s) public static ReadOnlySpan<T> After<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> s) where T : IEquatable<T>
{ {
var index = span.IndexOf(s); var index = span.IndexOf(s);
if (index == -1) if (index == -1)
throw new Exception($"span <{s}> not found in span <{span}>"); throw new SpanHelperException<T>(s, span);
return span.Slice(index+s.Length); return span.Slice(index+s.Length);
} }
public static ReadOnlySpan<char> Before(this ReadOnlySpan<char> span, char c) public static ReadOnlySpan<T> AfterLast<T>(this ReadOnlySpan<T> span, T c) where T : IEquatable<T>
{
var index = span.LastIndexOf(c);
if (index == -1)
throw new SpanHelperException<T>(c, span);
return span.Slice(index+1);
}
public static ReadOnlySpan<T> AfterLast<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> s) where T : IEquatable<T>
{
var index = span.LastIndexOf(s);
if (index == -1)
throw new SpanHelperException<T>(s, span);
return span.Slice(index+s.Length);
}
public static ReadOnlySpan<T> Before<T>(this ReadOnlySpan<T> span, T c) where T : IEquatable<T>
{ {
var index = span.IndexOf(c); var index = span.IndexOf(c);
if (index == -1) if (index == -1)
throw new Exception($"char <{c}> not found in span <{span}>"); throw new SpanHelperException<T>(c, span);
return span.Slice(0,index); return span.Slice(0,index);
} }
public static ReadOnlySpan<char> Before(this ReadOnlySpan<char> span, ReadOnlySpan<char> s) public static ReadOnlySpan<T> Before<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> s) where T : IEquatable<T>
{ {
var index = span.IndexOf(s); var index = span.IndexOf(s);
if (index == -1) if (index == -1)
throw new Exception($"span <{s}> not found in span <{span}>"); throw new SpanHelperException<T>(s, span);
return span.Slice(0,index); return span.Slice(0,index);
} }
public static ReadOnlySpan<T> BeforeLast<T>(this ReadOnlySpan<T> span, T c) where T : IEquatable<T>
{
var index = span.LastIndexOf(c);
if (index == -1)
throw new SpanHelperException<T>(c, span);
return span.Slice(0,index);
}
public static ReadOnlySpan<T> BeforeLast<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> s) where T : IEquatable<T>
{
var index = span.LastIndexOf(s);
if (index == -1)
throw new SpanHelperException<T>(s, span);
return span.Slice(0,index);
}
public class SpanHelperException<T> : Exception
{
public SpanHelperException(T el, ReadOnlySpan<T> span)
: base($"{nameof(T)} {el} not found in span {span.ToString()}")
{}
public SpanHelperException(ReadOnlySpan<T> el, ReadOnlySpan<T> span)
: base($"ReadOnlySpan<{nameof(T)}> {el.ToString()} not found in span {span.ToString()}")
{}
}
} }
#endif #endif