Reader.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2021, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Debug.h>
  8. #include <AK/Function.h>
  9. #include <AK/ScopeGuard.h>
  10. #include <AK/Span.h>
  11. #include <AK/String.h>
  12. #include <AK/Vector.h>
  13. namespace PDF {
  14. class Reader {
  15. public:
  16. explicit Reader(ReadonlyBytes bytes)
  17. : m_bytes(bytes)
  18. {
  19. }
  20. ALWAYS_INLINE ReadonlyBytes bytes() const { return m_bytes; }
  21. ALWAYS_INLINE size_t offset() const { return m_offset; }
  22. bool done() const
  23. {
  24. if (m_forwards)
  25. return offset() >= bytes().size();
  26. return m_offset < 0;
  27. }
  28. size_t remaining() const
  29. {
  30. if (done())
  31. return 0;
  32. if (m_forwards)
  33. return bytes().size() - offset() - 1;
  34. return offset() + 1;
  35. }
  36. void move_by(size_t count)
  37. {
  38. if (m_forwards) {
  39. m_offset += static_cast<ssize_t>(count);
  40. } else {
  41. m_offset -= static_cast<ssize_t>(count);
  42. }
  43. }
  44. template<typename T = char>
  45. T read()
  46. {
  47. T value = reinterpret_cast<const T*>(m_bytes.offset(m_offset))[0];
  48. move_by(sizeof(T));
  49. return value;
  50. }
  51. char peek(size_t shift = 0) const
  52. {
  53. auto offset = m_offset + shift * (m_forwards ? 1 : -1);
  54. return static_cast<char>(m_bytes.at(offset));
  55. }
  56. template<typename... T>
  57. bool matches_any(T... elements) const
  58. {
  59. if (done())
  60. return false;
  61. auto ch = peek();
  62. return ((ch == elements) || ...);
  63. }
  64. bool matches(char ch) const
  65. {
  66. return !done() && peek() == ch;
  67. }
  68. bool matches(char const* chars) const
  69. {
  70. String string(chars);
  71. if (remaining() < string.length())
  72. return false;
  73. if (!m_forwards)
  74. string = string.reverse();
  75. for (size_t i = 0; i < string.length(); i++) {
  76. if (peek(i) != string[i])
  77. return false;
  78. }
  79. return true;
  80. }
  81. template<typename T = char>
  82. void move_to(size_t offset)
  83. {
  84. VERIFY(offset < m_bytes.size());
  85. m_offset = static_cast<ssize_t>(offset);
  86. }
  87. void move_until(char ch)
  88. {
  89. while (!done() && peek() != ch)
  90. move_by(1);
  91. }
  92. void move_until(Function<bool(char)> predicate)
  93. {
  94. while (!done() && !predicate(peek()))
  95. move_by(1);
  96. }
  97. ALWAYS_INLINE void move_while(Function<bool(char)> predicate)
  98. {
  99. move_until([&predicate](char t) { return !predicate(t); });
  100. }
  101. ALWAYS_INLINE void set_reading_forwards() { m_forwards = true; }
  102. ALWAYS_INLINE void set_reading_backwards() { m_forwards = false; }
  103. ALWAYS_INLINE void save() { m_saved_offsets.append(m_offset); }
  104. ALWAYS_INLINE void load() { m_offset = m_saved_offsets.take_last(); }
  105. ALWAYS_INLINE void discard() { m_saved_offsets.take_last(); }
  106. #ifdef PDF_DEBUG
  107. void dump_state() const
  108. {
  109. dbgln("Reader State (offset={} size={})", offset(), bytes().size());
  110. size_t from = max(0, static_cast<int>(offset()) - 10);
  111. size_t to = min(bytes().size() - 1, offset() + 10);
  112. for (auto i = from; i <= to; i++) {
  113. char value = static_cast<char>(bytes().at(i));
  114. auto line = String::formatted(" {}: '{}' (value={:3d}) ", i, value, static_cast<u8>(value));
  115. if (i == offset()) {
  116. dbgln("{} <<< current location, forwards={}", line, m_forwards);
  117. } else {
  118. dbgln("{}", line);
  119. }
  120. }
  121. dbgln();
  122. }
  123. #endif
  124. private:
  125. ReadonlyBytes m_bytes;
  126. ssize_t m_offset { 0 };
  127. Vector<ssize_t> m_saved_offsets;
  128. bool m_forwards { true };
  129. };
  130. }