Reader.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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::is_eol(char c)
  10. {
  11. return c == 0xa || c == 0xd;
  12. }
  13. bool Reader::is_whitespace(char c)
  14. {
  15. // 3.1.1 Character Set
  16. return is_eol(c) || c == 0 || c == 0x9 || c == 0xc || c == ' ';
  17. }
  18. bool Reader::matches_eol() const
  19. {
  20. return !done() && is_eol(peek());
  21. }
  22. bool Reader::matches_whitespace() const
  23. {
  24. return !done() && is_whitespace(peek());
  25. }
  26. bool Reader::matches_number() const
  27. {
  28. if (done())
  29. return false;
  30. auto ch = peek();
  31. return isdigit(ch) || ch == '-' || ch == '+' || ch == '.';
  32. }
  33. bool Reader::matches_delimiter() const
  34. {
  35. return matches_any('(', ')', '<', '>', '[', ']', '{', '}', '/', '%');
  36. }
  37. bool Reader::matches_regular_character() const
  38. {
  39. return !done() && !matches_delimiter() && !matches_whitespace();
  40. }
  41. bool Reader::consume_eol()
  42. {
  43. if (done()) {
  44. return false;
  45. }
  46. if (matches("\r\n")) {
  47. consume(2);
  48. return true;
  49. }
  50. if (matches_eol()) {
  51. consume();
  52. return true;
  53. }
  54. return false;
  55. }
  56. bool Reader::consume_whitespace()
  57. {
  58. bool consumed = false;
  59. while (matches_whitespace()) {
  60. consumed = true;
  61. consume();
  62. }
  63. return consumed;
  64. }
  65. char Reader::consume()
  66. {
  67. return read();
  68. }
  69. void Reader::consume(int amount)
  70. {
  71. for (size_t i = 0; i < static_cast<size_t>(amount); i++)
  72. consume();
  73. }
  74. bool Reader::consume(char ch)
  75. {
  76. return consume() == ch;
  77. }
  78. }