Reader.cpp 1.9 KB

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