StringSegment.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. namespace FontStashSharp
  3. {
  4. public readonly ref struct StringSegment
  5. {
  6. public readonly string String;
  7. public readonly int Offset;
  8. public readonly int Length;
  9. public char this[int index] => String[Offset + index];
  10. public bool IsNullOrEmpty
  11. {
  12. get
  13. {
  14. if (String == null) return true;
  15. return Offset >= String.Length;
  16. }
  17. }
  18. public StringSegment(string value)
  19. {
  20. String = value;
  21. Offset = 0;
  22. Length = value != null ? value.Length : 0;
  23. }
  24. public StringSegment(string value, int offset)
  25. {
  26. String = value;
  27. Offset = offset;
  28. Length = value != null ? value.Length - offset : 0;
  29. }
  30. public StringSegment(string value, int offset, int length)
  31. {
  32. String = value;
  33. Offset = offset;
  34. Length = length;
  35. }
  36. public bool Equals(StringSegment b) => String == b.String && Offset == b.Offset && Length == b.Length;
  37. public override bool Equals(object obj) => throw new NotSupportedException();
  38. public override int GetHashCode() => IsNullOrEmpty ? 0 : String.GetHashCode() ^ Offset ^ Length;
  39. public override string ToString() => IsNullOrEmpty ? string.Empty : String.Substring(Offset, Length);
  40. public static bool operator ==(StringSegment a, StringSegment b) => a.Equals(b);
  41. public static bool operator !=(StringSegment a, StringSegment b) => !a.Equals(b);
  42. }
  43. }