Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

PredictiveParser.cs 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. namespace UnityEngine.InputSystem.Utilities
  3. {
  4. // this parser uses Span<T> so it's only available from later unity versions
  5. #if UNITY_2021_2_OR_NEWER
  6. internal struct PredictiveParser
  7. {
  8. public void ExpectSingleChar(ReadOnlySpan<char> str, char c)
  9. {
  10. if (str[m_Position] != c)
  11. throw new InvalidOperationException($"Expected a '{c}' character at position {m_Position} in : {str.ToString()}");
  12. ++m_Position;
  13. }
  14. public int ExpectInt(ReadOnlySpan<char> str)
  15. {
  16. int pos = m_Position;
  17. int sign = 1;
  18. if (str[pos] == '-')
  19. {
  20. sign = -1;
  21. ++pos;
  22. }
  23. int value = 0;
  24. for (;;)
  25. {
  26. var n = str[pos];
  27. if (n >= '0' && n <= '9')
  28. {
  29. value *= 10;
  30. value += n - '0';
  31. ++pos;
  32. }
  33. else
  34. break;
  35. }
  36. if (m_Position == pos)
  37. throw new InvalidOperationException($"Expected an int at position {m_Position} in {str.ToString()}");
  38. m_Position = pos;
  39. return value * sign;
  40. }
  41. public ReadOnlySpan<char> ExpectString(ReadOnlySpan<char> str)
  42. {
  43. var startPos = m_Position;
  44. if (str[startPos] != '\"')
  45. throw new InvalidOperationException($"Expected a '\"' character at position {m_Position} in {str.ToString()}");
  46. ++m_Position;
  47. for (;;)
  48. {
  49. var c = str[m_Position];
  50. c |= ' ';
  51. if (c >= 'a' && c <= 'z')
  52. {
  53. ++m_Position;
  54. continue;
  55. }
  56. break;
  57. }
  58. // if the first non-alpha character is not a quote, throw
  59. if (str[m_Position] != '\"')
  60. throw new InvalidOperationException($"Expected a closing '\"' character at position {m_Position} in string: {str.ToString()}");
  61. if (m_Position - startPos == 1)
  62. return ReadOnlySpan<char>.Empty;
  63. var output = str.Slice(startPos + 1, m_Position - startPos - 1);
  64. ++m_Position;
  65. return output;
  66. }
  67. public bool AcceptSingleChar(ReadOnlySpan<char> str, char c)
  68. {
  69. if (str[m_Position] != c)
  70. return false;
  71. m_Position++;
  72. return true;
  73. }
  74. public bool AcceptString(ReadOnlySpan<char> input, out ReadOnlySpan<char> output)
  75. {
  76. output = default;
  77. var startPos = m_Position;
  78. var endPos = startPos;
  79. if (input[endPos] != '\"')
  80. return false;
  81. ++endPos;
  82. for (;;)
  83. {
  84. var c = input[endPos];
  85. c |= ' ';
  86. if (c >= 'a' && c <= 'z')
  87. {
  88. ++endPos;
  89. continue;
  90. }
  91. break;
  92. }
  93. // if the first non-alpha character is not a quote, throw
  94. if (input[endPos] != '\"')
  95. return false;
  96. // empty string?
  97. if (m_Position - startPos == 1)
  98. output = ReadOnlySpan<char>.Empty;
  99. else
  100. output = input.Slice(startPos + 1, endPos - startPos - 1);
  101. m_Position = endPos + 1;
  102. return true;
  103. }
  104. public void AcceptInt(ReadOnlySpan<char> str)
  105. {
  106. // skip negative sign
  107. if (str[m_Position] == '-')
  108. ++m_Position;
  109. for (;;)
  110. {
  111. var n = str[m_Position];
  112. if (n >= '0' && n <= '9')
  113. ++m_Position;
  114. else
  115. break;
  116. }
  117. }
  118. private int m_Position;
  119. }
  120. #endif
  121. }