KeywordParser.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. namespace Avalonia.Utilities
  3. {
  4. #if !BUILDTASK
  5. public
  6. #endif
  7. static class KeywordParser
  8. {
  9. public static bool CheckKeyword(this ref CharacterReader r, string keyword)
  10. {
  11. return (CheckKeywordInternal(ref r, keyword) >= 0);
  12. }
  13. static int CheckKeywordInternal(this ref CharacterReader r, string keyword)
  14. {
  15. var ws = r.PeekWhitespace();
  16. var chars = r.TryPeek(ws.Length + keyword.Length);
  17. if (chars.IsEmpty)
  18. return -1;
  19. if (SpanEquals(chars.Slice(ws.Length), keyword.AsSpan()))
  20. return chars.Length;
  21. return -1;
  22. }
  23. static bool SpanEquals(ReadOnlySpan<char> left, ReadOnlySpan<char> right)
  24. {
  25. if (left.Length != right.Length)
  26. return false;
  27. for(var c=0; c<left.Length;c++)
  28. if (left[c] != right[c])
  29. return false;
  30. return true;
  31. }
  32. public static bool TakeIfKeyword(this ref CharacterReader r, string keyword)
  33. {
  34. var l = CheckKeywordInternal(ref r, keyword);
  35. if (l < 0)
  36. return false;
  37. r.Skip(l);
  38. return true;
  39. }
  40. }
  41. }