From c4478e5249f935abe852b11275ffe48c29d8f997 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Sat, 9 Nov 2019 11:54:16 -0500 Subject: Allow odd/even modifiers in numeric range (fixes #364) --- libqpdf/QUtil.cc | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'libqpdf') diff --git a/libqpdf/QUtil.cc b/libqpdf/QUtil.cc index db52bdb3..5fda01c9 100644 --- a/libqpdf/QUtil.cc +++ b/libqpdf/QUtil.cc @@ -1116,6 +1116,8 @@ QUtil::parse_numrange(char const* range, int max) std::vector work; static int const comma = -1; static int const dash = -2; + size_t start_idx = 0; + size_t skip = 1; enum { st_top, st_in_number, @@ -1182,6 +1184,14 @@ QUtil::parse_numrange(char const* range, int max) work.push_back(dash); } } + else if (ch == ':') + { + if (! ((state == st_in_number) || (state == st_after_number))) + { + throw std::runtime_error("unexpected colon"); + } + break; + } else { throw std::runtime_error("unexpected character"); @@ -1197,6 +1207,22 @@ QUtil::parse_numrange(char const* range, int max) { throw std::runtime_error("number expected"); } + if (*p == ':') + { + if (strcmp(p, ":odd") == 0) + { + skip = 2; + } + else if (strcmp(p, ":even") == 0) + { + skip = 2; + start_idx = 1; + } + else + { + throw std::runtime_error("unexpected even/odd modifier"); + } + } p = 0; for (size_t i = 0; i < work.size(); i += 2) @@ -1245,6 +1271,15 @@ QUtil::parse_numrange(char const* range, int max) } } } + if ((start_idx > 0) || (skip != 1)) + { + auto t = result; + result.clear(); + for (size_t i = start_idx; i < t.size(); i += skip) + { + result.push_back(t.at(i)); + } + } } catch (std::runtime_error const& e) { -- cgit v1.2.3-70-g09d2