You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.2 KiB

  1. #region License
  2. // Copyright (c) 2007 James Newton-King
  3. //
  4. // Permission is hereby granted, free of charge, to any person
  5. // obtaining a copy of this software and associated documentation
  6. // files (the "Software"), to deal in the Software without
  7. // restriction, including without limitation the rights to use,
  8. // copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the
  10. // Software is furnished to do so, subject to the following
  11. // conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be
  14. // included in all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  18. // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  20. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. // OTHER DEALINGS IN THE SOFTWARE.
  24. #endregion
  25. using System;
  26. namespace Newtonsoft.Json.Utilities
  27. {
  28. internal readonly struct StringReference
  29. {
  30. private readonly char[] _chars;
  31. private readonly int _startIndex;
  32. private readonly int _length;
  33. public char this[int i] => _chars[i];
  34. public char[] Chars => _chars;
  35. public int StartIndex => _startIndex;
  36. public int Length => _length;
  37. public StringReference(char[] chars, int startIndex, int length)
  38. {
  39. _chars = chars;
  40. _startIndex = startIndex;
  41. _length = length;
  42. }
  43. public override string ToString()
  44. {
  45. return new string(_chars, _startIndex, _length);
  46. }
  47. }
  48. internal static class StringReferenceExtensions
  49. {
  50. public static int IndexOf(this StringReference s, char c, int startIndex, int length)
  51. {
  52. int index = Array.IndexOf(s.Chars, c, s.StartIndex + startIndex, length);
  53. if (index == -1)
  54. {
  55. return -1;
  56. }
  57. return index - s.StartIndex;
  58. }
  59. public static bool StartsWith(this StringReference s, string text)
  60. {
  61. if (text.Length > s.Length)
  62. {
  63. return false;
  64. }
  65. char[] chars = s.Chars;
  66. for (int i = 0; i < text.Length; i++)
  67. {
  68. if (text[i] != chars[i + s.StartIndex])
  69. {
  70. return false;
  71. }
  72. }
  73. return true;
  74. }
  75. public static bool EndsWith(this StringReference s, string text)
  76. {
  77. if (text.Length > s.Length)
  78. {
  79. return false;
  80. }
  81. char[] chars = s.Chars;
  82. int start = s.StartIndex + s.Length - text.Length;
  83. for (int i = 0; i < text.Length; i++)
  84. {
  85. if (text[i] != chars[i + start])
  86. {
  87. return false;
  88. }
  89. }
  90. return true;
  91. }
  92. }
  93. }