mirror of
https://github.com/ahwootech/BRUTAL.git
synced 2026-11-26 04:58:24 +01:00
first
This commit is contained in:
parent
bd62d51ec9
commit
b578433e37
5 changed files with 784842 additions and 0 deletions
118
Brutal.Strings/String8/RefString8.cs
Normal file
118
Brutal.Strings/String8/RefString8.cs
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
using System.Runtime.InteropServices.Marshalling;
|
||||
using Brutal.Attributes;
|
||||
|
||||
namespace Brutal.Strings;
|
||||
|
||||
/// <summary>
|
||||
/// A reference to a null-terminated string of bytes.
|
||||
/// </summary>
|
||||
[DebuggerDisplay("{ToString16()}")]
|
||||
[NativeMarshalling(typeof(RefString8Marshaller))]
|
||||
public readonly ref struct RefString8 : IString8
|
||||
{
|
||||
private readonly ref Char8 _firstChar;
|
||||
|
||||
public static RefString8 Empty => String8View.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Create a safe reference to a null-terminated string of bytes.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public RefString8(String8View cString)
|
||||
{
|
||||
_firstChar = ref MemoryMarshal.GetReference(cString);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create an unsafe reference to a null-terminated string of bytes.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public unsafe RefString8([Fixed] Ptr<Char8> pointer)
|
||||
{
|
||||
_firstChar = ref Unsafe.AsRef<Char8>(pointer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a reference to the first character of the string.
|
||||
/// </summary>
|
||||
public ref Char8 FirstChar
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => ref _firstChar;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implicit conversion from a null-terminated string of bytes to a RefString8.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static implicit operator RefString8([Fixed] Ptr<Char8> cString)
|
||||
{
|
||||
return new RefString8(cString);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implicit conversion from a String8View to a RefString8.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static implicit operator RefString8(String8View bytes)
|
||||
{
|
||||
return new RefString8(bytes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implicit conversion from a Span<Char8> to a RefString8.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static implicit operator RefString8(Span<Char8> bytes)
|
||||
{
|
||||
return new RefString8(bytes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implicit conversion from a byte array to a RefString8.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static implicit operator RefString8(Char8[] bytes)
|
||||
{
|
||||
return new RefString8(bytes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implicit conversion from a String8 to a RefString8.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static implicit operator RefString8(String8 cString)
|
||||
{
|
||||
return new RefString8(cString.AsSpan());
|
||||
}
|
||||
|
||||
public static implicit operator String8View(RefString8 str)
|
||||
{
|
||||
return str.Chars;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a reference to the first character of the string.
|
||||
/// </summary>
|
||||
public ref Char8 GetPinnableReference()
|
||||
{
|
||||
return ref _firstChar;
|
||||
}
|
||||
|
||||
public unsafe String8View Chars => Utf8.GetNullTerminatedSpan((Ptr<Char8>)Unsafe.AsPointer(ref _firstChar));
|
||||
public int Length => Chars.Length;
|
||||
public bool IsEmpty => Unsafe.IsNullRef(ref _firstChar) || _firstChar == 0;
|
||||
|
||||
public Char8 this[int index] => Chars[index];
|
||||
public String8View this[Range range] => Chars[range];
|
||||
|
||||
public String8View AsSpan()
|
||||
{
|
||||
return Chars;
|
||||
}
|
||||
|
||||
public String16 ToString16()
|
||||
{
|
||||
return Utf8.GetString(Chars);
|
||||
}
|
||||
}
|
||||
33
Brutal.Strings/String8/RefString8Marshaller.cs
Normal file
33
Brutal.Strings/String8/RefString8Marshaller.cs
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
using System.Runtime.InteropServices.Marshalling;
|
||||
using Brutal.Attributes;
|
||||
|
||||
namespace Brutal.Strings;
|
||||
|
||||
[CustomMarshaller(
|
||||
managedType: typeof(RefString8),
|
||||
marshalMode: MarshalMode.ManagedToUnmanagedIn,
|
||||
marshallerType: typeof(RefString8Marshaller)
|
||||
)]
|
||||
[CustomMarshaller(
|
||||
managedType: typeof(RefString8),
|
||||
marshalMode: MarshalMode.UnmanagedToManagedIn,
|
||||
marshallerType: typeof(RefString8Marshaller)
|
||||
)]
|
||||
public static class RefString8Marshaller
|
||||
{
|
||||
public static unsafe Char8* ConvertToUnmanaged(RefString8 managed)
|
||||
{
|
||||
return (Char8*)Unsafe.AsPointer(ref managed.GetPinnableReference());
|
||||
}
|
||||
|
||||
// pointer is [Fixed] because it originates from native interop / owned by the unmanaged side
|
||||
public static unsafe RefString8 ConvertToManaged([Fixed] Char8* unmanaged)
|
||||
{
|
||||
return new RefString8(unmanaged);
|
||||
}
|
||||
|
||||
public static ref Char8 GetPinnableReference(RefString8 managed)
|
||||
{
|
||||
return ref managed.GetPinnableReference();
|
||||
}
|
||||
}
|
||||
204
Brutal.Strings/String8/String8.cs
Normal file
204
Brutal.Strings/String8/String8.cs
Normal file
|
|
@ -0,0 +1,204 @@
|
|||
namespace Brutal.Strings;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a string backed by an array of bytes.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Typically, but not necessarily, the bytes are UTF-8 encoded. Note that this type doesn't provide any special UTF-8 functionality.
|
||||
/// </remarks>
|
||||
[DebuggerDisplay("{ToString16()}")]
|
||||
public class String8 : IString8, IEquatable<String8?>, IComparable, IComparable<String8?>, ICloneable
|
||||
{
|
||||
/// the default array backing any empty-initialized String8
|
||||
private static readonly Char8[] EmptyChars = [0];
|
||||
|
||||
/// <summary>
|
||||
/// An empty string8.
|
||||
/// </summary>
|
||||
public static readonly String8 Empty = new();
|
||||
|
||||
/// WARNING: This array contains a trailing null byte.
|
||||
private readonly Char8[] _bytes;
|
||||
|
||||
public String8View Chars => _bytes.AsSpan()[..^1];
|
||||
|
||||
/// <summary>
|
||||
/// Construct an empty string.
|
||||
/// </summary>
|
||||
public String8()
|
||||
{
|
||||
_bytes = EmptyChars;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Construct a string from a list of characters (converting to UTF-8 in the process).
|
||||
/// </summary>
|
||||
public String8(String16View characters)
|
||||
{
|
||||
if (characters.IsEmpty)
|
||||
{
|
||||
_bytes = EmptyChars;
|
||||
}
|
||||
else
|
||||
{
|
||||
_bytes = new Char8[Utf8.GetByteCount(characters) + 1];
|
||||
Utf8.GetBytes(characters, _bytes);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Construct a string from a list of bytes (no conversion, but copies).
|
||||
/// </summary>
|
||||
/// <param name="bytes"></param>
|
||||
public String8(String8View bytes)
|
||||
{
|
||||
if (bytes.IsEmpty)
|
||||
{
|
||||
_bytes = EmptyChars;
|
||||
}
|
||||
else
|
||||
{
|
||||
_bytes = new Char8[bytes.Length + 1];
|
||||
bytes.CopyTo(_bytes);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Construct a string from an existing array of bytes.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Private because this behavior is dangerous if the array can be modified after construction.
|
||||
/// </remarks>
|
||||
private String8(Char8[] bytes)
|
||||
{
|
||||
if (bytes[^1] != 0)
|
||||
throw new ArgumentException("'bytes' must be null-terminated!", nameof(bytes));
|
||||
|
||||
_bytes = bytes.Length != 0 ? bytes : EmptyChars;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The length of the string in bytes.
|
||||
/// </summary>
|
||||
public int Length => _bytes.Length - 1;
|
||||
|
||||
public bool IsEmpty => Length == 0;
|
||||
|
||||
/// <summary>
|
||||
/// Create a new String8 from a slice of the current string.
|
||||
/// </summary>
|
||||
public String8 Slice(int startIndex)
|
||||
{
|
||||
return new String8(Chars[startIndex..]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a new String8 from a slice of the current string.
|
||||
/// </summary>
|
||||
public String8 Slice(int startIndex, int count)
|
||||
{
|
||||
return new String8(Chars[startIndex..(startIndex + count)]);
|
||||
}
|
||||
|
||||
public Char8 this[int index] => Chars[index];
|
||||
public String8 this[Range range] => new String8(Chars[range]);
|
||||
|
||||
public object Clone()
|
||||
{
|
||||
// do not call the byte[] constructor here!
|
||||
return new String8(AsSpan());
|
||||
}
|
||||
|
||||
#region Operators
|
||||
|
||||
public static implicit operator String8View(String8 str)
|
||||
{
|
||||
return str.Chars;
|
||||
}
|
||||
|
||||
public static implicit operator String8(String8View str)
|
||||
{
|
||||
return new String8(str);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public String8View AsSpan()
|
||||
{
|
||||
return Chars;
|
||||
}
|
||||
|
||||
public string ToString16()
|
||||
{
|
||||
return Utf8.GetString(Chars);
|
||||
}
|
||||
|
||||
// Equality and comparison implementations
|
||||
public bool Equals(String8? other)
|
||||
{
|
||||
if (ReferenceEquals(this, other))
|
||||
return true;
|
||||
if (other is null || Length != other.Length)
|
||||
return false;
|
||||
return Chars.SequenceEqual(other.Chars);
|
||||
}
|
||||
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
return obj is String8 other && Equals(other);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return Chars.GetDjb2HashCode();
|
||||
}
|
||||
|
||||
public int CompareTo(object? obj)
|
||||
{
|
||||
if (obj is null)
|
||||
return 1;
|
||||
if (obj is not String8 other)
|
||||
throw new ArgumentException("Object must be of type String8.", nameof(obj));
|
||||
return CompareTo(other);
|
||||
}
|
||||
|
||||
public int CompareTo(String8? other)
|
||||
{
|
||||
if (other is null)
|
||||
return 1;
|
||||
return Chars.AsBytes().SequenceCompareTo(other.Chars);
|
||||
}
|
||||
|
||||
public static bool operator ==(String8? a, String8? b)
|
||||
{
|
||||
if (ReferenceEquals(a, b))
|
||||
return true;
|
||||
if (a is null || b is null)
|
||||
return false;
|
||||
return a.Equals(b);
|
||||
}
|
||||
|
||||
public static bool operator !=(String8? a, String8? b)
|
||||
{
|
||||
return !(a == b);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a new string from existing memory without copying.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// WARNING: Do not retain the provided bytes after constructing the String8. The String8 instance will take ownership of the memory.
|
||||
/// </remarks>
|
||||
public static String8 Move(Char8[] bytes)
|
||||
{
|
||||
if (bytes.Length != 0 && bytes[^1] != 0)
|
||||
return new String8(bytes.AsSpan());
|
||||
|
||||
return new String8(bytes);
|
||||
}
|
||||
|
||||
public ref Char8 GetPinnableReference()
|
||||
{
|
||||
return ref _bytes[0];
|
||||
}
|
||||
}
|
||||
126
Brutal.Strings/String8/String8Extensions.Comparison.cs
Normal file
126
Brutal.Strings/String8/String8Extensions.Comparison.cs
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
namespace Brutal.Strings;
|
||||
|
||||
public static partial class String8Extensions
|
||||
{
|
||||
private static class Helpers
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int CompareOrdinalHelper(
|
||||
String8View strA,
|
||||
int indexA,
|
||||
int countA,
|
||||
String8View strB,
|
||||
int indexB,
|
||||
int countB
|
||||
)
|
||||
{
|
||||
Debug.Assert(!strA.IsEmpty);
|
||||
Debug.Assert(!strB.IsEmpty);
|
||||
Debug.Assert(indexA >= 0 && indexB >= 0);
|
||||
Debug.Assert(countA >= 0 && countB >= 0);
|
||||
Debug.Assert(indexA + countA <= strA.Length && indexB + countB <= strB.Length);
|
||||
|
||||
return strA.Slice(indexA, countA).SequenceCompareTo(strB.Slice(indexB, countB));
|
||||
}
|
||||
|
||||
public static int CompareOrdinalHelper(String8View strA, String8View strB)
|
||||
{
|
||||
return strA.SequenceCompareTo(strB);
|
||||
}
|
||||
}
|
||||
|
||||
// Provides an ordinal string comparison. strA is compared to strB
|
||||
// to determine whether it is lexicographically less, equal, or greater, and then a
|
||||
// negative integer, 0, or a positive integer is returned; respectively.
|
||||
public static int Compare<TString8>(this TString8 strA, String8View strB)
|
||||
where TString8 : IString8, allows ref struct
|
||||
{
|
||||
// They can't both be null at this point.
|
||||
if (strA.IsEmpty)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (strB.IsEmpty)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
return Helpers.CompareOrdinalHelper(strA.Chars, strB);
|
||||
}
|
||||
|
||||
// Compares strA and strB using an ordinal (code-point) comparison.
|
||||
public static int Compare<TString8>(this TString8 strA, int indexA, String8View strB, int indexB, int length)
|
||||
where TString8 : IString8, allows ref struct
|
||||
{
|
||||
if (strA.IsEmpty || strB.IsEmpty)
|
||||
{
|
||||
return strA.IsEmpty ? -1 : 1;
|
||||
}
|
||||
|
||||
ArgumentOutOfRangeException.ThrowIfNegative(length);
|
||||
|
||||
if (indexA < 0 || indexB < 0)
|
||||
{
|
||||
string paramName = indexA < 0 ? nameof(indexA) : nameof(indexB);
|
||||
throw new ArgumentOutOfRangeException(paramName, "Index cannot be negative.");
|
||||
}
|
||||
|
||||
int lengthA = Math.Min(length, strA.Length - indexA);
|
||||
int lengthB = Math.Min(length, strB.Length - indexB);
|
||||
|
||||
if (lengthA < 0 || lengthB < 0)
|
||||
{
|
||||
string paramName = lengthA < 0 ? nameof(indexA) : nameof(indexB);
|
||||
throw new ArgumentOutOfRangeException(
|
||||
paramName,
|
||||
"Index and length must refer to a location within the string."
|
||||
);
|
||||
}
|
||||
|
||||
if (length == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return Helpers.CompareOrdinalHelper(strA.Chars, indexA, lengthA, strB, indexB, lengthB);
|
||||
}
|
||||
|
||||
// Determines whether a specified string is a suffix of the current instance.
|
||||
public static bool EndsWith<TString8>(this TString8 str, String8View value)
|
||||
where TString8 : IString8, allows ref struct
|
||||
{
|
||||
int offset = str.Length - value.Length;
|
||||
return (uint)offset <= (uint)str.Length && str.Chars[offset..].SequenceEqual(value);
|
||||
}
|
||||
|
||||
public static bool EndsWith<TString8>(this TString8 str, Char8 value)
|
||||
where TString8 : IString8, allows ref struct
|
||||
{
|
||||
int lastPos = str.Length - 1;
|
||||
return ((uint)lastPos < (uint)str.Length) && str[lastPos].Equals(value);
|
||||
}
|
||||
|
||||
// Determines whether a specified string is a prefix of the current instance
|
||||
public static bool StartsWith<TString8>(this TString8 str, String8View value)
|
||||
where TString8 : IString8, allows ref struct
|
||||
{
|
||||
if (value.IsEmpty)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (value.Length > str.Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return str.Chars[..value.Length].SequenceEqual(value);
|
||||
}
|
||||
|
||||
public static bool StartsWith<TString8>(this TString8 str, Char8 value)
|
||||
where TString8 : IString8, allows ref struct
|
||||
{
|
||||
return str.Length != 0 && str[0].Equals(value);
|
||||
}
|
||||
}
|
||||
146
Brutal.Strings/String8/String8Extensions.Searching.cs
Normal file
146
Brutal.Strings/String8/String8Extensions.Searching.cs
Normal file
|
|
@ -0,0 +1,146 @@
|
|||
namespace Brutal.Strings;
|
||||
|
||||
public static partial class String8Extensions
|
||||
{
|
||||
public static bool Contains<TString8>(this TString8 str, String8View value)
|
||||
where TString8 : IString8, allows ref struct
|
||||
{
|
||||
return str.IndexOf(value) >= 0;
|
||||
}
|
||||
|
||||
public static bool Contains<TString8>(this TString8 str, Char8 value)
|
||||
where TString8 : IString8, allows ref struct
|
||||
{
|
||||
return str.Chars.Contains(value);
|
||||
}
|
||||
|
||||
// Returns the index of the first occurrence of a specified character in the current instance.
|
||||
// The search starts at startIndex and runs thorough the next count characters.
|
||||
public static int IndexOf<TString8>(this TString8 str, Char8 value)
|
||||
where TString8 : IString8, allows ref struct => str.Chars.IndexOf(value);
|
||||
|
||||
public static int IndexOf<TString8>(this TString8 str, Char8 value, int startIndex)
|
||||
where TString8 : IString8, allows ref struct
|
||||
{
|
||||
return IndexOf(str, value, startIndex, str.Length - startIndex);
|
||||
}
|
||||
|
||||
public static int IndexOf<TString8>(this TString8 str, Char8 value, int startIndex, int count)
|
||||
where TString8 : IString8, allows ref struct
|
||||
{
|
||||
var result = str.Chars.Slice(startIndex, count).IndexOf(value);
|
||||
return result < 0 ? result : result + startIndex;
|
||||
}
|
||||
|
||||
// Returns the index of the first occurrence of any specified character in the current instance.
|
||||
// The search starts at startIndex and runs to startIndex + count - 1.
|
||||
//
|
||||
public static int IndexOfAny<TString8>(this TString8 str, String8View anyOf)
|
||||
where TString8 : IString8, allows ref struct
|
||||
{
|
||||
return str.Chars.IndexOfAny(anyOf);
|
||||
}
|
||||
|
||||
public static int IndexOfAny<TString8>(this TString8 str, String8View anyOf, int startIndex)
|
||||
where TString8 : IString8, allows ref struct
|
||||
{
|
||||
return IndexOfAny(str, anyOf, startIndex, str.Length - startIndex);
|
||||
}
|
||||
|
||||
public static int IndexOfAny<TString8>(this TString8 str, String8View anyOf, int startIndex, int count)
|
||||
where TString8 : IString8, allows ref struct
|
||||
{
|
||||
var result = str.Chars.Slice(startIndex, count).IndexOfAny(anyOf);
|
||||
return result < 0 ? result : result + startIndex;
|
||||
}
|
||||
|
||||
public static int IndexOf<TString8>(this TString8 str, String8View value)
|
||||
where TString8 : IString8, allows ref struct
|
||||
{
|
||||
return str.Chars.IndexOf(value);
|
||||
}
|
||||
|
||||
public static int IndexOf<TString8>(this TString8 str, String8View value, int startIndex)
|
||||
where TString8 : IString8, allows ref struct
|
||||
{
|
||||
var result = str.Chars[startIndex..].IndexOf(value);
|
||||
return result < 0 ? result : result + startIndex;
|
||||
}
|
||||
|
||||
public static int IndexOf<TString8>(this TString8 str, String8View value, int startIndex, int count)
|
||||
where TString8 : IString8, allows ref struct
|
||||
{
|
||||
var result = str.Chars.Slice(startIndex, count).IndexOf(value);
|
||||
return result < 0 ? result : result + startIndex;
|
||||
}
|
||||
|
||||
// Returns the index of the last occurrence of a specified character in the current instance.
|
||||
// The search starts at startIndex and runs backwards to startIndex - count + 1.
|
||||
// The character at position startIndex is included in the search. startIndex is the larger
|
||||
// index within the string.
|
||||
public static int LastIndexOf<TString8>(this TString8 str, Char8 value)
|
||||
where TString8 : IString8, allows ref struct => str.Chars.LastIndexOf(value);
|
||||
|
||||
public static int LastIndexOf<TString8>(this TString8 str, Char8 value, int startIndex)
|
||||
where TString8 : IString8, allows ref struct
|
||||
{
|
||||
return LastIndexOf(str, value, startIndex, startIndex + 1);
|
||||
}
|
||||
|
||||
public static int LastIndexOf<TString8>(this TString8 str, Char8 value, int startIndex, int count)
|
||||
where TString8 : IString8, allows ref struct
|
||||
{
|
||||
var result = str.Chars.Slice(startIndex, count).LastIndexOf(value);
|
||||
return result < 0 ? result : result + startIndex;
|
||||
}
|
||||
|
||||
// Returns the index of the last occurrence of any specified character in the current instance.
|
||||
// The search starts at startIndex and runs backwards to startIndex - count + 1.
|
||||
// The character at position startIndex is included in the search. startIndex is the larger
|
||||
// index within the string.
|
||||
//
|
||||
public static int LastIndexOfAny<TString8>(this TString8 str, String8View anyOf)
|
||||
where TString8 : IString8, allows ref struct
|
||||
{
|
||||
return str.Chars.LastIndexOfAny(anyOf);
|
||||
}
|
||||
|
||||
public static int LastIndexOfAny<TString8>(this TString8 str, String8View anyOf, int startIndex)
|
||||
where TString8 : IString8, allows ref struct
|
||||
{
|
||||
return LastIndexOfAny(str, anyOf, startIndex, startIndex + 1);
|
||||
}
|
||||
|
||||
public static int LastIndexOfAny<TString8>(this TString8 str, String8View anyOf, int startIndex, int count)
|
||||
where TString8 : IString8, allows ref struct
|
||||
{
|
||||
var actualStartIndex = startIndex - count + 1;
|
||||
var result = str.Chars.Slice(actualStartIndex, count).LastIndexOfAny(anyOf);
|
||||
return result < 0 ? result : result + actualStartIndex;
|
||||
}
|
||||
|
||||
// Returns the index of the last occurrence of any character in value in the current instance.
|
||||
// The search starts at startIndex and runs backwards to startIndex - count + 1.
|
||||
// The character at position startIndex is included in the search. startIndex is the larger
|
||||
// index within the string.
|
||||
//
|
||||
public static int LastIndexOf<TString8>(this TString8 str, String8View value)
|
||||
where TString8 : IString8, allows ref struct
|
||||
{
|
||||
return LastIndexOf(str, value, str.Length - 1, str.Length);
|
||||
}
|
||||
|
||||
public static int LastIndexOf<TString8>(this TString8 str, String8View value, int startIndex)
|
||||
where TString8 : IString8, allows ref struct
|
||||
{
|
||||
return LastIndexOf(str, value, startIndex, startIndex + 1);
|
||||
}
|
||||
|
||||
public static int LastIndexOf<TString8>(this TString8 str, String8View value, int startIndex, int count)
|
||||
where TString8 : IString8, allows ref struct
|
||||
{
|
||||
var actualStartIndex = startIndex - count + 1;
|
||||
var result = str.Chars.Slice(actualStartIndex, count).LastIndexOf(value);
|
||||
return result < 0 ? result : result + actualStartIndex;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue