Commit 7e7cede8b1209e71b151cc872e647a2ba3d7eade
1 parent
0c9b6fe9
Refactor `is::OffsetBuffer` to rename `cur_offset` to `pos` and consolidate redu…
…ndant methods for improved code clarity.
Showing
2 changed files
with
30 additions
and
45 deletions
libqpdf/BufferInputSource.cc
| @@ -146,57 +146,57 @@ BufferInputSource::unreadCh(char ch) | @@ -146,57 +146,57 @@ BufferInputSource::unreadCh(char ch) | ||
| 146 | } | 146 | } |
| 147 | 147 | ||
| 148 | qpdf_offset_t | 148 | qpdf_offset_t |
| 149 | -is::OffsetBuffer::findAndSkipNextEOL_internal() | 149 | +is::OffsetBuffer::findAndSkipNextEOL() |
| 150 | { | 150 | { |
| 151 | - if (cur_offset < 0) { | 151 | + if (pos < 0) { |
| 152 | throw std::logic_error("INTERNAL ERROR: is::OffsetBuffer offset < 0"); | 152 | throw std::logic_error("INTERNAL ERROR: is::OffsetBuffer offset < 0"); |
| 153 | } | 153 | } |
| 154 | auto end_pos = static_cast<qpdf_offset_t>(view_.size()); | 154 | auto end_pos = static_cast<qpdf_offset_t>(view_.size()); |
| 155 | - if (cur_offset >= end_pos) { | 155 | + if (pos >= end_pos) { |
| 156 | last_offset = end_pos + global_offset; | 156 | last_offset = end_pos + global_offset; |
| 157 | - cur_offset = end_pos; | ||
| 158 | - return end_pos; | 157 | + pos = end_pos; |
| 158 | + return end_pos + global_offset; | ||
| 159 | } | 159 | } |
| 160 | 160 | ||
| 161 | qpdf_offset_t result = 0; | 161 | qpdf_offset_t result = 0; |
| 162 | auto buffer = view_.begin(); | 162 | auto buffer = view_.begin(); |
| 163 | auto end = view_.end(); | 163 | auto end = view_.end(); |
| 164 | - auto p = buffer + cur_offset; | 164 | + auto p = buffer + pos; |
| 165 | 165 | ||
| 166 | while (p < end && !(*p == '\r' || *p == '\n')) { | 166 | while (p < end && !(*p == '\r' || *p == '\n')) { |
| 167 | ++p; | 167 | ++p; |
| 168 | } | 168 | } |
| 169 | if (p < end) { | 169 | if (p < end) { |
| 170 | result = p - buffer; | 170 | result = p - buffer; |
| 171 | - cur_offset = result + 1; | 171 | + pos = result + 1; |
| 172 | ++p; | 172 | ++p; |
| 173 | - while (cur_offset < end_pos && (*p == '\r' || *p == '\n')) { | 173 | + while (pos < end_pos && (*p == '\r' || *p == '\n')) { |
| 174 | ++p; | 174 | ++p; |
| 175 | - ++cur_offset; | 175 | + ++pos; |
| 176 | } | 176 | } |
| 177 | } else { | 177 | } else { |
| 178 | - cur_offset = end_pos; | 178 | + pos = end_pos; |
| 179 | result = end_pos; | 179 | result = end_pos; |
| 180 | } | 180 | } |
| 181 | - return result; | 181 | + return result + global_offset; |
| 182 | } | 182 | } |
| 183 | 183 | ||
| 184 | void | 184 | void |
| 185 | -is::OffsetBuffer::seek_internal(qpdf_offset_t offset, int whence) | 185 | +is::OffsetBuffer::seek(qpdf_offset_t offset, int whence) |
| 186 | { | 186 | { |
| 187 | switch (whence) { | 187 | switch (whence) { |
| 188 | case SEEK_SET: | 188 | case SEEK_SET: |
| 189 | - cur_offset = offset; | 189 | + pos = offset - global_offset; |
| 190 | break; | 190 | break; |
| 191 | 191 | ||
| 192 | case SEEK_END: | 192 | case SEEK_END: |
| 193 | QIntC::range_check(static_cast<qpdf_offset_t>(view_.size()), offset); | 193 | QIntC::range_check(static_cast<qpdf_offset_t>(view_.size()), offset); |
| 194 | - cur_offset = static_cast<qpdf_offset_t>(view_.size()) + offset; | 194 | + pos = static_cast<qpdf_offset_t>(view_.size()) + offset; |
| 195 | break; | 195 | break; |
| 196 | 196 | ||
| 197 | case SEEK_CUR: | 197 | case SEEK_CUR: |
| 198 | - QIntC::range_check(cur_offset, offset); | ||
| 199 | - cur_offset += offset; | 198 | + QIntC::range_check(pos, offset); |
| 199 | + pos += offset; | ||
| 200 | break; | 200 | break; |
| 201 | 201 | ||
| 202 | default: | 202 | default: |
| @@ -204,7 +204,7 @@ is::OffsetBuffer::seek_internal(qpdf_offset_t offset, int whence) | @@ -204,7 +204,7 @@ is::OffsetBuffer::seek_internal(qpdf_offset_t offset, int whence) | ||
| 204 | break; | 204 | break; |
| 205 | } | 205 | } |
| 206 | 206 | ||
| 207 | - if (cur_offset < 0) { | 207 | + if (pos < 0) { |
| 208 | throw std::runtime_error(description + ": seek before beginning of buffer"); | 208 | throw std::runtime_error(description + ": seek before beginning of buffer"); |
| 209 | } | 209 | } |
| 210 | } | 210 | } |
| @@ -212,18 +212,18 @@ is::OffsetBuffer::seek_internal(qpdf_offset_t offset, int whence) | @@ -212,18 +212,18 @@ is::OffsetBuffer::seek_internal(qpdf_offset_t offset, int whence) | ||
| 212 | size_t | 212 | size_t |
| 213 | is::OffsetBuffer::read(char* buffer, size_t length) | 213 | is::OffsetBuffer::read(char* buffer, size_t length) |
| 214 | { | 214 | { |
| 215 | - if (cur_offset < 0) { | 215 | + if (pos < 0) { |
| 216 | throw std::logic_error("INTERNAL ERROR: is::OffsetBuffer offset < 0"); | 216 | throw std::logic_error("INTERNAL ERROR: is::OffsetBuffer offset < 0"); |
| 217 | } | 217 | } |
| 218 | auto end_pos = static_cast<qpdf_offset_t>(view_.size()); | 218 | auto end_pos = static_cast<qpdf_offset_t>(view_.size()); |
| 219 | - if (cur_offset >= end_pos) { | 219 | + if (pos >= end_pos) { |
| 220 | last_offset = end_pos + global_offset; | 220 | last_offset = end_pos + global_offset; |
| 221 | return 0; | 221 | return 0; |
| 222 | } | 222 | } |
| 223 | 223 | ||
| 224 | - last_offset = cur_offset + global_offset; | ||
| 225 | - size_t len = std::min(QIntC::to_size(end_pos - cur_offset), length); | ||
| 226 | - memcpy(buffer, view_.data() + cur_offset, len); | ||
| 227 | - cur_offset += QIntC::to_offset(len); | 224 | + last_offset = pos + global_offset; |
| 225 | + size_t len = std::min(QIntC::to_size(end_pos - pos), length); | ||
| 226 | + memcpy(buffer, view_.data() + pos, len); | ||
| 227 | + pos += QIntC::to_offset(len); | ||
| 228 | return len; | 228 | return len; |
| 229 | } | 229 | } |
libqpdf/qpdf/InputSource_private.hh
| @@ -30,11 +30,7 @@ namespace qpdf::is | @@ -30,11 +30,7 @@ namespace qpdf::is | ||
| 30 | 30 | ||
| 31 | ~OffsetBuffer() final = default; | 31 | ~OffsetBuffer() final = default; |
| 32 | 32 | ||
| 33 | - qpdf_offset_t | ||
| 34 | - findAndSkipNextEOL() final | ||
| 35 | - { | ||
| 36 | - return findAndSkipNextEOL_internal() + global_offset; | ||
| 37 | - } | 33 | + qpdf_offset_t findAndSkipNextEOL() final; |
| 38 | 34 | ||
| 39 | std::string const& | 35 | std::string const& |
| 40 | getName() const final | 36 | getName() const final |
| @@ -45,23 +41,15 @@ namespace qpdf::is | @@ -45,23 +41,15 @@ namespace qpdf::is | ||
| 45 | qpdf_offset_t | 41 | qpdf_offset_t |
| 46 | tell() final | 42 | tell() final |
| 47 | { | 43 | { |
| 48 | - return cur_offset + global_offset; | 44 | + return pos + global_offset; |
| 49 | } | 45 | } |
| 50 | 46 | ||
| 51 | - void | ||
| 52 | - seek(qpdf_offset_t offset, int whence) final | ||
| 53 | - { | ||
| 54 | - if (whence == SEEK_SET) { | ||
| 55 | - seek_internal(offset - global_offset, whence); | ||
| 56 | - } else { | ||
| 57 | - seek_internal(offset, whence); | ||
| 58 | - } | ||
| 59 | - } | 47 | + void seek(qpdf_offset_t offset, int whence) final; |
| 60 | 48 | ||
| 61 | void | 49 | void |
| 62 | rewind() final | 50 | rewind() final |
| 63 | { | 51 | { |
| 64 | - seek_internal(0, SEEK_SET); | 52 | + pos = 0; |
| 65 | } | 53 | } |
| 66 | 54 | ||
| 67 | size_t read(char* buffer, size_t length) final; | 55 | size_t read(char* buffer, size_t length) final; |
| @@ -69,17 +57,14 @@ namespace qpdf::is | @@ -69,17 +57,14 @@ namespace qpdf::is | ||
| 69 | void | 57 | void |
| 70 | unreadCh(char ch) final | 58 | unreadCh(char ch) final |
| 71 | { | 59 | { |
| 72 | - if (cur_offset > 0) { | ||
| 73 | - --cur_offset; | 60 | + if (pos > 0) { |
| 61 | + --pos; | ||
| 74 | } | 62 | } |
| 75 | } | 63 | } |
| 76 | 64 | ||
| 77 | private: | 65 | private: |
| 78 | - qpdf_offset_t findAndSkipNextEOL_internal(); | ||
| 79 | - void seek_internal(qpdf_offset_t offset, int whence); | ||
| 80 | - | ||
| 81 | std::string description; | 66 | std::string description; |
| 82 | - qpdf_offset_t cur_offset{0}; | 67 | + qpdf_offset_t pos{0}; |
| 83 | std::string_view view_; | 68 | std::string_view view_; |
| 84 | qpdf_offset_t global_offset; | 69 | qpdf_offset_t global_offset; |
| 85 | }; | 70 | }; |