TextPointer.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. namespace Avalonia.Media.TextFormatting
  3. {
  4. /// <summary>
  5. /// References a portion of a text buffer.
  6. /// </summary>
  7. public readonly struct TextPointer
  8. {
  9. public TextPointer(int start, int length)
  10. {
  11. Start = start;
  12. Length = length;
  13. }
  14. /// <summary>
  15. /// Gets the start.
  16. /// </summary>
  17. /// <value>
  18. /// The start.
  19. /// </value>
  20. public int Start { get; }
  21. /// <summary>
  22. /// Gets the length.
  23. /// </summary>
  24. /// <value>
  25. /// The length.
  26. /// </value>
  27. public int Length { get; }
  28. /// <summary>
  29. /// Gets the end.
  30. /// </summary>
  31. /// <value>
  32. /// The end.
  33. /// </value>
  34. public int End => Start + Length - 1;
  35. /// <summary>
  36. /// Returns a specified number of contiguous elements from the start of the slice.
  37. /// </summary>
  38. /// <param name="length">The number of elements to return.</param>
  39. /// <returns>A <see cref="TextPointer"/> that contains the specified number of elements from the start of this slice.</returns>
  40. public TextPointer Take(int length)
  41. {
  42. if (length > Length)
  43. {
  44. throw new ArgumentOutOfRangeException(nameof(length));
  45. }
  46. return new TextPointer(Start, length);
  47. }
  48. /// <summary>
  49. /// Bypasses a specified number of elements in the slice and then returns the remaining elements.
  50. /// </summary>
  51. /// <param name="length">The number of elements to skip before returning the remaining elements.</param>
  52. /// <returns>A <see cref="TextPointer"/> that contains the elements that occur after the specified index in this slice.</returns>
  53. public TextPointer Skip(int length)
  54. {
  55. if (length > Length)
  56. {
  57. throw new ArgumentOutOfRangeException(nameof(length));
  58. }
  59. return new TextPointer(Start + length, Length - length);
  60. }
  61. }
  62. }