Reader.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 == '+' || ch == '.';
  23. }
  24. bool Reader::matches_delimiter() const
  25. {
  26. return matches_any('(', ')', '<', '>', '[', ']', '{', '}', '/', '%');
  27. }
  28. bool Reader::matches_regular_character() const
  29. {
  30. return !done() && !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. if (matches_eol()) {
  42. consume();
  43. return true;
  44. }
  45. return false;
  46. }
  47. bool Reader::consume_whitespace()
  48. {
  49. bool consumed = false;
  50. while (matches_whitespace()) {
  51. consumed = true;
  52. consume();
  53. }
  54. return consumed;
  55. }
  56. char Reader::consume()
  57. {
  58. return read();
  59. }
  60. void Reader::consume(int amount)
  61. {
  62. for (size_t i = 0; i < static_cast<size_t>(amount); i++)
  63. consume();
  64. }
  65. bool Reader::consume(char ch)
  66. {
  67. return consume() == ch;
  68. }
  69. }