Commit cfcceff6aa921c45c2a3f0fa7a486ed9f02ccc4a
1 parent
011b1d7e
Replace std::regex_search with string_view methods in QdfFixer::processLines
Showing
1 changed file
with
17 additions
and
25 deletions
qpdf/fix-qdf.cc
| @@ -95,22 +95,13 @@ QdfFixer::fatal(std::string const& msg) | @@ -95,22 +95,13 @@ QdfFixer::fatal(std::string const& msg) | ||
| 95 | void | 95 | void |
| 96 | QdfFixer::processLines(std::string const& input) | 96 | QdfFixer::processLines(std::string const& input) |
| 97 | { | 97 | { |
| 98 | + using namespace std::literals; | ||
| 99 | + | ||
| 98 | static const std::regex re_n_0_obj("^(\\d+) 0 obj\n$"); | 100 | static const std::regex re_n_0_obj("^(\\d+) 0 obj\n$"); |
| 99 | - static const std::regex re_xref("^xref\n$"); | ||
| 100 | - static const std::regex re_stream("^stream\n$"); | ||
| 101 | - static const std::regex re_endobj("^endobj\n$"); | ||
| 102 | - static const std::regex re_type_objstm("/Type /ObjStm"); | ||
| 103 | - static const std::regex re_type_xref("/Type /XRef"); | ||
| 104 | static const std::regex re_extends("/Extends (\\d+ 0 R)"); | 101 | static const std::regex re_extends("/Extends (\\d+ 0 R)"); |
| 105 | static const std::regex re_ostream_obj("^%% Object stream: object (\\d+)"); | 102 | static const std::regex re_ostream_obj("^%% Object stream: object (\\d+)"); |
| 106 | - static const std::regex re_endstream("^endstream\n$"); | ||
| 107 | - static const std::regex re_length_or_w("/(Length|W) "); | ||
| 108 | - static const std::regex re_size("/Size "); | ||
| 109 | - static const std::regex re_ignore_newline("^%QDF: ignore_newline\n$"); | ||
| 110 | static const std::regex re_num("^\\d+\n$"); | 103 | static const std::regex re_num("^\\d+\n$"); |
| 111 | - static const std::regex re_trailer("^trailer <<"); | ||
| 112 | static const std::regex re_size_n("^ /Size \\d+\n$"); | 104 | static const std::regex re_size_n("^ /Size \\d+\n$"); |
| 113 | - static const std::regex re_dict_end("^>>\n$"); | ||
| 114 | 105 | ||
| 115 | auto sv_diff = [](size_t i) { | 106 | auto sv_diff = [](size_t i) { |
| 116 | return static_cast<std::string_view::difference_type>(i); | 107 | return static_cast<std::string_view::difference_type>(i); |
| @@ -151,22 +142,22 @@ QdfFixer::processLines(std::string const& input) | @@ -151,22 +142,22 @@ QdfFixer::processLines(std::string const& input) | ||
| 151 | if (matches(re_n_0_obj)) { | 142 | if (matches(re_n_0_obj)) { |
| 152 | checkObjId(m[1].str()); | 143 | checkObjId(m[1].str()); |
| 153 | state = st_in_obj; | 144 | state = st_in_obj; |
| 154 | - } else if (matches(re_xref)) { | 145 | + } else if (line.compare("xref\n"sv) == 0) { |
| 155 | xref_offset = last_offset; | 146 | xref_offset = last_offset; |
| 156 | state = st_at_xref; | 147 | state = st_at_xref; |
| 157 | } | 148 | } |
| 158 | std::cout << line; | 149 | std::cout << line; |
| 159 | } else if (state == st_in_obj) { | 150 | } else if (state == st_in_obj) { |
| 160 | std::cout << line; | 151 | std::cout << line; |
| 161 | - if (matches(re_stream)) { | 152 | + if (line.compare("stream\n"sv) == 0) { |
| 162 | state = st_in_stream; | 153 | state = st_in_stream; |
| 163 | stream_start = offset; | 154 | stream_start = offset; |
| 164 | - } else if (matches(re_endobj)) { | 155 | + } else if (line.compare("endobj\n"sv) == 0) { |
| 165 | state = st_top; | 156 | state = st_top; |
| 166 | - } else if (matches(re_type_objstm)) { | 157 | + } else if (line.find("/Type /ObjStm"sv) != line.npos) { |
| 167 | state = st_in_ostream_dict; | 158 | state = st_in_ostream_dict; |
| 168 | ostream_id = last_obj; | 159 | ostream_id = last_obj; |
| 169 | - } else if (matches(re_type_xref)) { | 160 | + } else if (line.find("/Type /XRef"sv) != line.npos) { |
| 170 | xref_offset = xref.back().getOffset(); | 161 | xref_offset = xref.back().getOffset(); |
| 171 | xref_f1_nbytes = 0; | 162 | xref_f1_nbytes = 0; |
| 172 | auto t = xref_offset; | 163 | auto t = xref_offset; |
| @@ -198,7 +189,7 @@ QdfFixer::processLines(std::string const& input) | @@ -198,7 +189,7 @@ QdfFixer::processLines(std::string const& input) | ||
| 198 | state = st_in_xref_stream_dict; | 189 | state = st_in_xref_stream_dict; |
| 199 | } | 190 | } |
| 200 | } else if (state == st_in_ostream_dict) { | 191 | } else if (state == st_in_ostream_dict) { |
| 201 | - if (matches(re_stream)) { | 192 | + if (line.compare("stream\n"sv) == 0) { |
| 202 | state = st_in_ostream_offsets; | 193 | state = st_in_ostream_offsets; |
| 203 | } else { | 194 | } else { |
| 204 | ostream_discarded.push_back(line); | 195 | ostream_discarded.push_back(line); |
| @@ -227,21 +218,22 @@ QdfFixer::processLines(std::string const& input) | @@ -227,21 +218,22 @@ QdfFixer::processLines(std::string const& input) | ||
| 227 | if (matches(re_ostream_obj)) { | 218 | if (matches(re_ostream_obj)) { |
| 228 | checkObjId(m[1].str()); | 219 | checkObjId(m[1].str()); |
| 229 | state = st_in_ostream_outer; | 220 | state = st_in_ostream_outer; |
| 230 | - } else if (matches(re_endstream)) { | 221 | + } else if (line.compare("endstream\n"sv) == 0) { |
| 231 | stream_length = QIntC::to_size(last_offset - stream_start); | 222 | stream_length = QIntC::to_size(last_offset - stream_start); |
| 232 | writeOstream(); | 223 | writeOstream(); |
| 233 | state = st_in_obj; | 224 | state = st_in_obj; |
| 234 | } | 225 | } |
| 235 | } else if (state == st_in_xref_stream_dict) { | 226 | } else if (state == st_in_xref_stream_dict) { |
| 236 | - if (matches(re_length_or_w)) { | 227 | + if ((line.find("/Length"sv) != line.npos) || |
| 228 | + (line.find("/W"sv) != line.npos)) { | ||
| 237 | // already printed | 229 | // already printed |
| 238 | - } else if (matches(re_size)) { | 230 | + } else if (line.find("/Size"sv) != line.npos) { |
| 239 | auto xref_size = 1 + xref.size(); | 231 | auto xref_size = 1 + xref.size(); |
| 240 | std::cout << " /Size " << xref_size << "\n"; | 232 | std::cout << " /Size " << xref_size << "\n"; |
| 241 | } else { | 233 | } else { |
| 242 | std::cout << line; | 234 | std::cout << line; |
| 243 | } | 235 | } |
| 244 | - if (matches(re_stream)) { | 236 | + if (line.compare("stream\n"sv) == 0) { |
| 245 | writeBinary(0, 1); | 237 | writeBinary(0, 1); |
| 246 | writeBinary(0, xref_f1_nbytes); | 238 | writeBinary(0, xref_f1_nbytes); |
| 247 | writeBinary(0, xref_f2_nbytes); | 239 | writeBinary(0, xref_f2_nbytes); |
| @@ -265,13 +257,13 @@ QdfFixer::processLines(std::string const& input) | @@ -265,13 +257,13 @@ QdfFixer::processLines(std::string const& input) | ||
| 265 | state = st_done; | 257 | state = st_done; |
| 266 | } | 258 | } |
| 267 | } else if (state == st_in_stream) { | 259 | } else if (state == st_in_stream) { |
| 268 | - if (matches(re_endstream)) { | 260 | + if (line.compare("endstream\n"sv) == 0) { |
| 269 | stream_length = QIntC::to_size(last_offset - stream_start); | 261 | stream_length = QIntC::to_size(last_offset - stream_start); |
| 270 | state = st_after_stream; | 262 | state = st_after_stream; |
| 271 | } | 263 | } |
| 272 | std::cout << line; | 264 | std::cout << line; |
| 273 | } else if (state == st_after_stream) { | 265 | } else if (state == st_after_stream) { |
| 274 | - if (matches(re_ignore_newline)) { | 266 | + if (line.compare("%QDF: ignore_newline\n"sv) == 0) { |
| 275 | if (stream_length > 0) { | 267 | if (stream_length > 0) { |
| 276 | --stream_length; | 268 | --stream_length; |
| 277 | } | 269 | } |
| @@ -300,7 +292,7 @@ QdfFixer::processLines(std::string const& input) | @@ -300,7 +292,7 @@ QdfFixer::processLines(std::string const& input) | ||
| 300 | } | 292 | } |
| 301 | state = st_before_trailer; | 293 | state = st_before_trailer; |
| 302 | } else if (state == st_before_trailer) { | 294 | } else if (state == st_before_trailer) { |
| 303 | - if (matches(re_trailer)) { | 295 | + if (line.compare("trailer <<\n"sv) == 0) { |
| 304 | std::cout << line; | 296 | std::cout << line; |
| 305 | state = st_in_trailer; | 297 | state = st_in_trailer; |
| 306 | } | 298 | } |
| @@ -311,7 +303,7 @@ QdfFixer::processLines(std::string const& input) | @@ -311,7 +303,7 @@ QdfFixer::processLines(std::string const& input) | ||
| 311 | } else { | 303 | } else { |
| 312 | std::cout << line; | 304 | std::cout << line; |
| 313 | } | 305 | } |
| 314 | - if (matches(re_dict_end)) { | 306 | + if (line.compare(">>\n"sv) == 0) { |
| 315 | std::cout << "startxref\n" << xref_offset << "\n%%EOF\n"; | 307 | std::cout << "startxref\n" << xref_offset << "\n%%EOF\n"; |
| 316 | state = st_done; | 308 | state = st_done; |
| 317 | } | 309 | } |