Commit 7e7cede8b1209e71b151cc872e647a2ba3d7eade

Authored by m-holger
1 parent 0c9b6fe9

Refactor `is::OffsetBuffer` to rename `cur_offset` to `pos` and consolidate redu…

…ndant methods for improved code clarity.
libqpdf/BufferInputSource.cc
... ... @@ -146,57 +146,57 @@ BufferInputSource::unreadCh(char ch)
146 146 }
147 147  
148 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 152 throw std::logic_error("INTERNAL ERROR: is::OffsetBuffer offset < 0");
153 153 }
154 154 auto end_pos = static_cast<qpdf_offset_t>(view_.size());
155   - if (cur_offset >= end_pos) {
  155 + if (pos >= end_pos) {
156 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 161 qpdf_offset_t result = 0;
162 162 auto buffer = view_.begin();
163 163 auto end = view_.end();
164   - auto p = buffer + cur_offset;
  164 + auto p = buffer + pos;
165 165  
166 166 while (p < end && !(*p == '\r' || *p == '\n')) {
167 167 ++p;
168 168 }
169 169 if (p < end) {
170 170 result = p - buffer;
171   - cur_offset = result + 1;
  171 + pos = result + 1;
172 172 ++p;
173   - while (cur_offset < end_pos && (*p == '\r' || *p == '\n')) {
  173 + while (pos < end_pos && (*p == '\r' || *p == '\n')) {
174 174 ++p;
175   - ++cur_offset;
  175 + ++pos;
176 176 }
177 177 } else {
178   - cur_offset = end_pos;
  178 + pos = end_pos;
179 179 result = end_pos;
180 180 }
181   - return result;
  181 + return result + global_offset;
182 182 }
183 183  
184 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 187 switch (whence) {
188 188 case SEEK_SET:
189   - cur_offset = offset;
  189 + pos = offset - global_offset;
190 190 break;
191 191  
192 192 case SEEK_END:
193 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 195 break;
196 196  
197 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 200 break;
201 201  
202 202 default:
... ... @@ -204,7 +204,7 @@ is::OffsetBuffer::seek_internal(qpdf_offset_t offset, int whence)
204 204 break;
205 205 }
206 206  
207   - if (cur_offset < 0) {
  207 + if (pos < 0) {
208 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 212 size_t
213 213 is::OffsetBuffer::read(char* buffer, size_t length)
214 214 {
215   - if (cur_offset < 0) {
  215 + if (pos < 0) {
216 216 throw std::logic_error("INTERNAL ERROR: is::OffsetBuffer offset < 0");
217 217 }
218 218 auto end_pos = static_cast<qpdf_offset_t>(view_.size());
219   - if (cur_offset >= end_pos) {
  219 + if (pos >= end_pos) {
220 220 last_offset = end_pos + global_offset;
221 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 228 return len;
229 229 }
... ...
libqpdf/qpdf/InputSource_private.hh
... ... @@ -30,11 +30,7 @@ namespace qpdf::is
30 30  
31 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 35 std::string const&
40 36 getName() const final
... ... @@ -45,23 +41,15 @@ namespace qpdf::is
45 41 qpdf_offset_t
46 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 49 void
62 50 rewind() final
63 51 {
64   - seek_internal(0, SEEK_SET);
  52 + pos = 0;
65 53 }
66 54  
67 55 size_t read(char* buffer, size_t length) final;
... ... @@ -69,17 +57,14 @@ namespace qpdf::is
69 57 void
70 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 65 private:
78   - qpdf_offset_t findAndSkipNextEOL_internal();
79   - void seek_internal(qpdf_offset_t offset, int whence);
80   -
81 66 std::string description;
82   - qpdf_offset_t cur_offset{0};
  67 + qpdf_offset_t pos{0};
83 68 std::string_view view_;
84 69 qpdf_offset_t global_offset;
85 70 };
... ...