Reader.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright (c) 2021, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibPDF/Reader.h>
  7. #include <ctype.h>
  8. namespace PDF {
  9. bool Reader::matches_eol() const
  10. {
  11. return matches_any(0xa, 0xd);
  12. }
  13. bool Reader::matches_whitespace() const
  14. {
  15. return matches_eol() || matches_any(0, 0x9, 0xc, ' ');
  16. }
  17. bool Reader::matches_number() const
  18. {
  19. if (done())
  20. return false;
  21. auto ch = peek();
  22. return isdigit(ch) || ch == '-' || ch == '+';
  23. }
  24. bool Reader::matches_delimiter() const
  25. {
  26. return matches_any('(', ')', '<', '>', '[', ']', '{', '}', '/', '%');
  27. }
  28. bool Reader::matches_regular_character() const
  29. {
  30. return !matches_delimiter() && !matches_whitespace();
  31. }
  32. bool Reader::consume_eol()
  33. {
  34. if (done()) {
  35. return false;
  36. }
  37. if (matches("\r\n")) {
  38. consume(2);
  39. return true;
  40. }
  41. auto consumed = consume();
  42. return consumed == 0xd || consumed == 0xa;
  43. }
  44. bool Reader::consume_whitespace()
  45. {
  46. bool consumed = false;
  47. while (matches_whitespace()) {
  48. consumed = true;
  49. consume();
  50. }
  51. return consumed;
  52. }
  53. char Reader::consume()
  54. {
  55. return read();
  56. }
  57. void Reader::consume(int amount)
  58. {
  59. for (size_t i = 0; i < static_cast<size_t>(amount); i++)
  60. consume();
  61. }
  62. bool Reader::consume(char ch)
  63. {
  64. return consume() == ch;
  65. }
  66. }