123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- using System;
- namespace UnityEngine.InputSystem.Utilities
- {
- // Work with substrings without actually allocating strings.
- internal struct Substring : IComparable<Substring>, IEquatable<Substring>
- {
- private readonly string m_String;
- private readonly int m_Index;
- private readonly int m_Length;
- public bool isEmpty => m_Length == 0;
- public Substring(string str)
- {
- m_String = str;
- m_Index = 0;
- if (str != null)
- m_Length = str.Length;
- else
- m_Length = 0;
- }
- public Substring(string str, int index, int length)
- {
- Debug.Assert(str == null || index < str.Length);
- Debug.Assert(str != null || length == 0);
- m_String = str;
- m_Index = index;
- m_Length = length;
- }
- public Substring(string str, int index)
- {
- Debug.Assert(str == null || index < str.Length);
- m_String = str;
- m_Index = index;
- m_Length = str.Length - index;
- }
- public override bool Equals(object obj)
- {
- if (obj is Substring other)
- return Equals(other);
- if (obj is string str)
- return Equals(str);
- return false;
- }
- public bool Equals(string other)
- {
- if (string.IsNullOrEmpty(other))
- return m_Length == 0;
- if (other.Length != m_Length)
- return false;
- for (var i = 0; i < m_Length; ++i)
- if (other[i] != m_String[m_Index + i])
- return false;
- return true;
- }
- public bool Equals(Substring other)
- {
- return CompareTo(other) == 0;
- }
- public bool Equals(InternedString other)
- {
- if (length != other.length)
- return false;
- return string.Compare(m_String, m_Index, other.ToString(), 0, length,
- StringComparison.OrdinalIgnoreCase) == 0;
- }
- public int CompareTo(Substring other)
- {
- return Compare(this, other, StringComparison.CurrentCulture);
- }
- public static int Compare(Substring left, Substring right, StringComparison comparison)
- {
- if (left.m_Length != right.m_Length)
- {
- if (left.m_Length < right.m_Length)
- return -1;
- return 1;
- }
- return string.Compare(left.m_String, left.m_Index, right.m_String, right.m_Index, left.m_Length,
- comparison);
- }
- public bool StartsWith(string str)
- {
- if (str.Length > length)
- return false;
- for (var i = 0; i < str.Length; ++i)
- if (m_String[m_Index + i] != str[i])
- return false;
- return true;
- }
- public string Substr(int index = 0, int length = -1)
- {
- if (length < 0)
- length = this.length - index;
- return m_String.Substring(m_Index + index, length);
- }
- public override string ToString()
- {
- if (m_String == null)
- return string.Empty;
- return m_String.Substring(m_Index, m_Length);
- }
- public override int GetHashCode()
- {
- if (m_String == null)
- return 0;
- if (m_Index == 0 && m_Length == m_String.Length)
- return m_String.GetHashCode();
- ////FIXME: this is bad... shouldn't allocate
- return ToString().GetHashCode();
- }
- public static bool operator==(Substring a, Substring b)
- {
- return a.Equals(b);
- }
- public static bool operator!=(Substring a, Substring b)
- {
- return !a.Equals(b);
- }
- public static bool operator==(Substring a, InternedString b)
- {
- return a.Equals(b);
- }
- public static bool operator!=(Substring a, InternedString b)
- {
- return !a.Equals(b);
- }
- public static bool operator==(InternedString a, Substring b)
- {
- return b.Equals(a);
- }
- public static bool operator!=(InternedString a, Substring b)
- {
- return !b.Equals(a);
- }
- public static implicit operator Substring(string s)
- {
- return new Substring(s);
- }
- public int length => m_Length;
- public int index => m_Index;
- public char this[int index]
- {
- get
- {
- if (index < 0 || index >= m_Length)
- throw new ArgumentOutOfRangeException(nameof(index));
- return m_String[m_Index + index];
- }
- }
- }
- }
|