Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Runtime.CompilerServices;
- namespace ContentPatcher.Framework;
- internal readonly struct IndexedString :
- IEquatable<IndexedString>,
- IEquatable<string>,
- IComparable<IndexedString>
- {
- internal static IndexedString Empty => new(string.Empty);
- internal static readonly StringComparer Comparer = StringComparer.OrdinalIgnoreCase;
- internal const StringComparison Comparison = StringComparison.OrdinalIgnoreCase;
- internal readonly string Value => IndexedStringTable.GetString(this.Index);
- internal readonly int Index;
- internal readonly int Length => this.Value.Length;
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public IndexedString()
- {
- this.Index = IndexedString.Empty.Index;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal IndexedString(string value)
- {
- (this.Index, _) = IndexedStringTable.GetIndex(value);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal bool Contains(string value)
- {
- return this.Value.Contains(value, Comparison);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal bool Contains(char value)
- {
- return this.Value.Contains(value);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static explicit operator string(IndexedString indexedString) => indexedString.Value;
- public static bool operator ==(IndexedString left, IndexedString right) => left.Index == right.Index;
- public static bool operator !=(IndexedString left, IndexedString right) => left.Index != right.Index;
- public static bool operator ==(IndexedString left, string right) => left.Value == right;
- public static bool operator !=(IndexedString left, string right) => left.Value != right;
- public static bool operator ==(string left, IndexedString right) => left == right.Value;
- public static bool operator !=(string left, IndexedString right) => left != right.Value;
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public int CompareTo(IndexedString other)
- {
- return this.Index.CompareTo(other.Index);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public override bool Equals(object? other)
- {
- return other switch
- {
- IndexedString indexedString => this.Index == indexedString.Index,
- string normalString => this.Equals(normalString),
- _ => false
- };
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool Equals(IndexedString other)
- {
- return this.Index == other.Index;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool Equals(string? other)
- {
- return Comparer.Equals(this.Value, other);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public override string ToString()
- {
- return this.Value;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public override int GetHashCode()
- {
- return this.Index.GetHashCode();
- }
- }
- internal static class IndexedStringExt
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal static IndexedString AsIndexed(this string value) => value.Length == 0 ? IndexedString.Empty : new(value);
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal static IndexedString ToIndexedString<T>(this T obj) where T : notnull => obj.ToString()?.AsIndexed() ?? IndexedString.Empty;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement