75 static Interval
parse(
const char *input,
const char **rest) {
76 const char *s = input;
78 bool hadRangeError =
false, isEmpty =
false;
79 while (isspace(*s)) ++s;
81 if (!strcmp(s,
"all")) {
83 return Interval::whole();
86 if (!strcmp(s,
"empty")) {
93 uint64_t least64 = rose_strtoull(s, &r, 0);
95 throw std::runtime_error(
"unsigned integer expected for interval minimum");
98 typename Interval::Value least{};
100 least = boost::numeric_cast<typename Interval::Value>(least64);
101 }
catch (
const boost::bad_numeric_cast&) {
102 hadRangeError =
true;
107 typename Interval::Value greatest = least;
108 while (isspace(*s)) ++s;
111 if (0 == strncmp(s,
"max", 3)) {
112 greatest = Interval::whole().greatest();
114 r =
const_cast<char*
>(s);
117 uint64_t greatest64 = rose_strtoull(s, &r, 0);
119 throw std::runtime_error(
"unsigned integer expected for interval maximum");
121 hadRangeError =
true;
123 greatest = boost::numeric_cast<typename Interval::Value>(greatest64);
124 }
catch (
const boost::bad_numeric_cast&) {
125 hadRangeError =
true;
129 }
else if (
'-' == *s) {
132 uint64_t greatest64 = rose_strtoull(s, &r, 0);
134 throw std::runtime_error(
"unsigned integer expected for interval end");
136 hadRangeError =
true;
138 greatest = boost::numeric_cast<typename Interval::Value>(greatest64);
139 }
catch (
const boost::bad_numeric_cast&) {
140 hadRangeError =
true;
142 if (greatest == least)
146 }
else if (
'+' == *s) {
149 uint64_t size64 = rose_strtoull(s, &r, 0);
151 throw std::runtime_error(
"unsigned integer expected for interval size");
153 hadRangeError =
true;
154 typename Interval::Value size{};
156 size = boost::numeric_cast<typename Interval::Value>(size64);
157 }
catch (
const boost::bad_numeric_cast&) {
158 hadRangeError =
true;
162 greatest = least + size - 1;
170 std::string parsed(input, *rest - input);
172 throw std::range_error(
"overflow when parsing \"" + parsed +
"\"");
173 if (greatest < least)
174 throw std::range_error(
"interval seems backward: \"" + parsed +
"\"");
177 return Interval::hull(least, greatest);
199 static std::string
toString(
const Interval &interval) {
200 if (interval.isEmpty()) {
202 }
else if (interval == Interval::whole()) {
204 }
else if (interval.least() == interval.greatest()) {
205 if (interval.least() < 256) {
206 return boost::lexical_cast<std::string>(interval.least());
211 if (interval.greatest() < 256) {
212 return boost::lexical_cast<std::string>(interval.least()) +
"," +
213 boost::lexical_cast<std::string>(interval.greatest());