Advertisement
Ameisen

Untitled

May 18th, 2022
592
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4.  
  5. namespace ContentPatcher.Framework;
  6.  
  7. internal readonly struct IndexedString :
  8.     IEquatable<IndexedString>,
  9.     IEquatable<string>,
  10.     IComparable<IndexedString>
  11. {
  12.     internal static IndexedString Empty => new(string.Empty);
  13.     internal static readonly StringComparer Comparer = StringComparer.OrdinalIgnoreCase;
  14.     internal const StringComparison Comparison = StringComparison.OrdinalIgnoreCase;
  15.  
  16.     internal readonly string Value => IndexedStringTable.GetString(this.Index);
  17.     internal readonly int Index;
  18.  
  19.     internal readonly int Length => this.Value.Length;
  20.  
  21.     [MethodImpl(MethodImplOptions.AggressiveInlining)]
  22.     public IndexedString()
  23.     {
  24.         this.Index = IndexedString.Empty.Index;
  25.     }
  26.  
  27.     [MethodImpl(MethodImplOptions.AggressiveInlining)]
  28.     internal IndexedString(string value)
  29.     {
  30.         (this.Index, _) = IndexedStringTable.GetIndex(value);
  31.     }
  32.  
  33.     [MethodImpl(MethodImplOptions.AggressiveInlining)]
  34.     internal bool Contains(string value)
  35.     {
  36.         return this.Value.Contains(value, Comparison);
  37.     }
  38.  
  39.     [MethodImpl(MethodImplOptions.AggressiveInlining)]
  40.     internal bool Contains(char value)
  41.     {
  42.         return this.Value.Contains(value);
  43.     }
  44.  
  45.     [MethodImpl(MethodImplOptions.AggressiveInlining)]
  46.     public static explicit operator string(IndexedString indexedString) => indexedString.Value;
  47.  
  48.     public static bool operator ==(IndexedString left, IndexedString right) => left.Index == right.Index;
  49.     public static bool operator !=(IndexedString left, IndexedString right) => left.Index != right.Index;
  50.  
  51.     public static bool operator ==(IndexedString left, string right) => left.Value == right;
  52.     public static bool operator !=(IndexedString left, string right) => left.Value != right;
  53.  
  54.     public static bool operator ==(string left, IndexedString right) => left == right.Value;
  55.     public static bool operator !=(string left, IndexedString right) => left != right.Value;
  56.  
  57.     [MethodImpl(MethodImplOptions.AggressiveInlining)]
  58.     public int CompareTo(IndexedString other)
  59.     {
  60.         return this.Index.CompareTo(other.Index);
  61.     }
  62.  
  63.  
  64.     [MethodImpl(MethodImplOptions.AggressiveInlining)]
  65.     public override bool Equals(object? other)
  66.     {
  67.         return other switch
  68.         {
  69.             IndexedString indexedString => this.Index == indexedString.Index,
  70.             string normalString => this.Equals(normalString),
  71.             _ => false
  72.         };
  73.     }
  74.  
  75.     [MethodImpl(MethodImplOptions.AggressiveInlining)]
  76.     public bool Equals(IndexedString other)
  77.     {
  78.         return this.Index == other.Index;
  79.     }
  80.  
  81.     [MethodImpl(MethodImplOptions.AggressiveInlining)]
  82.     public bool Equals(string? other)
  83.     {
  84.         return Comparer.Equals(this.Value, other);
  85.     }
  86.  
  87.     [MethodImpl(MethodImplOptions.AggressiveInlining)]
  88.     public override string ToString()
  89.     {
  90.         return this.Value;
  91.     }
  92.  
  93.     [MethodImpl(MethodImplOptions.AggressiveInlining)]
  94.     public override int GetHashCode()
  95.     {
  96.         return this.Index.GetHashCode();
  97.     }
  98. }
  99.  
  100. internal static class IndexedStringExt
  101. {
  102.     [MethodImpl(MethodImplOptions.AggressiveInlining)]
  103.     internal static IndexedString AsIndexed(this string value) => value.Length == 0 ? IndexedString.Empty : new(value);
  104.  
  105.     [MethodImpl(MethodImplOptions.AggressiveInlining)]
  106.     internal static IndexedString ToIndexedString<T>(this T obj) where T : notnull => obj.ToString()?.AsIndexed() ?? IndexedString.Empty;
  107. }
  108.  
Advertisement
RAW Paste Data Copied
Advertisement