From b66b93947d6a6ee492a9db75c5e931d5144ce8cc Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Tue, 6 Dec 2022 01:09:36 +0600 Subject: [PATCH] SpanHelper AfterLast and BeforeLast --- DTLib/Extensions/SpanHelper.cs | 61 +++++++++++++++++++++++++++++----- 1 file changed, 53 insertions(+), 8 deletions(-) diff --git a/DTLib/Extensions/SpanHelper.cs b/DTLib/Extensions/SpanHelper.cs index 33971fb..6a3fbeb 100644 --- a/DTLib/Extensions/SpanHelper.cs +++ b/DTLib/Extensions/SpanHelper.cs @@ -3,37 +3,82 @@ namespace DTLib.Extensions; #if NETSTANDARD2_1 || NET6_0 || NET7_0 || NET8_0 public static class SpanHelper { - public static ReadOnlySpan After(this ReadOnlySpan span, char c) + public static ReadOnlySpan After(this ReadOnlySpan span, T c) where T : IEquatable { var index = span.IndexOf(c); if (index == -1) - throw new Exception($"char <{c}> not found in span <{span}>"); + throw new SpanHelperException(c, span); return span.Slice(index+1); } - public static ReadOnlySpan After(this ReadOnlySpan span, ReadOnlySpan s) + public static ReadOnlySpan After(this ReadOnlySpan span, ReadOnlySpan s) where T : IEquatable { var index = span.IndexOf(s); if (index == -1) - throw new Exception($"span <{s}> not found in span <{span}>"); + throw new SpanHelperException(s, span); return span.Slice(index+s.Length); } - public static ReadOnlySpan Before(this ReadOnlySpan span, char c) + public static ReadOnlySpan AfterLast(this ReadOnlySpan span, T c) where T : IEquatable + { + var index = span.LastIndexOf(c); + if (index == -1) + throw new SpanHelperException(c, span); + return span.Slice(index+1); + } + + public static ReadOnlySpan AfterLast(this ReadOnlySpan span, ReadOnlySpan s) where T : IEquatable + { + var index = span.LastIndexOf(s); + if (index == -1) + throw new SpanHelperException(s, span); + return span.Slice(index+s.Length); + } + + + public static ReadOnlySpan Before(this ReadOnlySpan span, T c) where T : IEquatable { var index = span.IndexOf(c); if (index == -1) - throw new Exception($"char <{c}> not found in span <{span}>"); + throw new SpanHelperException(c, span); return span.Slice(0,index); } - public static ReadOnlySpan Before(this ReadOnlySpan span, ReadOnlySpan s) + public static ReadOnlySpan Before(this ReadOnlySpan span, ReadOnlySpan s) where T : IEquatable { var index = span.IndexOf(s); if (index == -1) - throw new Exception($"span <{s}> not found in span <{span}>"); + throw new SpanHelperException(s, span); return span.Slice(0,index); } + + + public static ReadOnlySpan BeforeLast(this ReadOnlySpan span, T c) where T : IEquatable + { + var index = span.LastIndexOf(c); + if (index == -1) + throw new SpanHelperException(c, span); + return span.Slice(0,index); + } + + public static ReadOnlySpan BeforeLast(this ReadOnlySpan span, ReadOnlySpan s) where T : IEquatable + { + var index = span.LastIndexOf(s); + if (index == -1) + throw new SpanHelperException(s, span); + return span.Slice(0,index); + } + + + public class SpanHelperException : Exception + { + public SpanHelperException(T el, ReadOnlySpan span) + : base($"{nameof(T)} {el} not found in span {span.ToString()}") + {} + public SpanHelperException(ReadOnlySpan el, ReadOnlySpan span) + : base($"ReadOnlySpan<{nameof(T)}> {el.ToString()} not found in span {span.ToString()}") + {} + } } #endif \ No newline at end of file