diff options
Diffstat (limited to 'src/paramed_string.h')
| -rw-r--r-- | src/paramed_string.h | 132 |
1 files changed, 70 insertions, 62 deletions
diff --git a/src/paramed_string.h b/src/paramed_string.h index 34de8b5b..81443bc4 100644 --- a/src/paramed_string.h +++ b/src/paramed_string.h @@ -49,39 +49,41 @@ namespace aria2 { namespace paramed_string { -template<typename InputIterator> -InputIterator expandChoice -(std::vector<std::string>& res, InputIterator first, InputIterator last) +template <typename InputIterator> +InputIterator expandChoice(std::vector<std::string>& res, InputIterator first, + InputIterator last) { ++first; InputIterator i = std::find(first, last, '}'); - if(i == last) { + if (i == last) { throw DL_ABORT_EX("Missing '}' in the parameterized string."); } std::vector<Scip> choices; util::splitIter(first, i, std::back_inserter(choices), ',', true, false); std::vector<std::string> res2; - res2.reserve(res.size()*choices.size()); - for(std::vector<std::string>::const_iterator i = res.begin(), - eoi = res.end(); i != eoi; ++i) { - for(std::vector<Scip>::const_iterator j = choices.begin(), - eoj = choices.end(); j != eoj; ++j) { + res2.reserve(res.size() * choices.size()); + for (std::vector<std::string>::const_iterator i = res.begin(), + eoi = res.end(); + i != eoi; ++i) { + for (std::vector<Scip>::const_iterator j = choices.begin(), + eoj = choices.end(); + j != eoj; ++j) { res2.push_back(*i); res2.back().append((*j).first, (*j).second); } } res.swap(res2); - return i+1; + return i + 1; } -template<typename InputIterator> +template <typename InputIterator> int32_t fromBase26(InputIterator first, InputIterator last, char zero) { int32_t res = 0; - for(; first != last; ++first) { + for (; first != last; ++first) { res *= 26; - res += *first-zero; - if(res > static_cast<int32_t>(UINT16_MAX)) { + res += *first - zero; + if (res > static_cast<int32_t>(UINT16_MAX)) { throw DL_ABORT_EX("Loop range overflow."); } } @@ -90,88 +92,95 @@ int32_t fromBase26(InputIterator first, InputIterator last, char zero) std::string toBase26(int32_t n, char zero, size_t width); -template<typename InputIterator> -InputIterator expandLoop -(std::vector<std::string>& res, InputIterator first, InputIterator last) +template <typename InputIterator> +InputIterator expandLoop(std::vector<std::string>& res, InputIterator first, + InputIterator last) { ++first; InputIterator i = std::find(first, last, ']'); - if(i == last) { + if (i == last) { throw DL_ABORT_EX("Missing ']' in the parameterized string."); } InputIterator colon = std::find(first, i, ':'); uint32_t step; - if(colon == i) { + if (colon == i) { step = 1; - } else { - if(!util::parseUIntNoThrow(step, std::string(colon+1, i))) { + } + else { + if (!util::parseUIntNoThrow(step, std::string(colon + 1, i))) { throw DL_ABORT_EX("A step count must be a positive number."); } - if(step > UINT16_MAX) { + if (step > UINT16_MAX) { throw DL_ABORT_EX("Loop step overflow."); } } InputIterator minus = std::find(first, colon, '-'); - if(minus == colon) { + if (minus == colon) { throw DL_ABORT_EX("Loop range missing."); } - if(util::isNumber(first, minus) && util::isNumber(minus+1, colon)) { + if (util::isNumber(first, minus) && util::isNumber(minus + 1, colon)) { uint32_t start, end; - if(!util::parseUIntNoThrow(start, std::string(first, minus)) || - !util::parseUIntNoThrow(end, std::string(minus+1, colon))) { + if (!util::parseUIntNoThrow(start, std::string(first, minus)) || + !util::parseUIntNoThrow(end, std::string(minus + 1, colon))) { throw DL_ABORT_EX("Loop range missing."); } - if(start > UINT16_MAX || end > UINT16_MAX) { + if (start > UINT16_MAX || end > UINT16_MAX) { throw DL_ABORT_EX("Loop range overflow."); } - if(start <= end) { + if (start <= end) { std::string format; - if(minus-first == colon-minus-1) { - format = fmt("%%0%lud", static_cast<unsigned long>(minus-first)); - } else { + if (minus - first == colon - minus - 1) { + format = fmt("%%0%lud", static_cast<unsigned long>(minus - first)); + } + else { format = "%d"; } std::vector<std::string> res2; - res2.reserve(res.size()*((end+1-start)/step)); - for(std::vector<std::string>::const_iterator i = res.begin(), - eoi = res.end(); i != eoi; ++i) { - for(uint32_t j = start; j <= end; j += step) { + res2.reserve(res.size() * ((end + 1 - start) / step)); + for (std::vector<std::string>::const_iterator i = res.begin(), + eoi = res.end(); + i != eoi; ++i) { + for (uint32_t j = start; j <= end; j += step) { res2.push_back(*i); res2.back() += fmt(format.c_str(), j); } } res.swap(res2); } - } else if((util::isLowercase(first, minus) && - util::isLowercase(minus+1, colon)) || - (util::isUppercase(first, minus) && - util::isUppercase(minus+1, colon))) { + } + else if ((util::isLowercase(first, minus) && + util::isLowercase(minus + 1, colon)) || + (util::isUppercase(first, minus) && + util::isUppercase(minus + 1, colon))) { char zero = ('a' <= *first && *first <= 'z' ? 'a' : 'A'); int32_t start, end; start = fromBase26(first, minus, zero); - end = fromBase26(minus+1, colon, zero); - if(start <= end) { + end = fromBase26(minus + 1, colon, zero); + if (start <= end) { size_t width; - if(minus-first == colon-minus-1) { - width = minus-first; - } else { + if (minus - first == colon - minus - 1) { + width = minus - first; + } + else { width = 0; } std::vector<std::string> res2; - res2.reserve(res.size()*((end+1-start)/step)); - for(std::vector<std::string>::const_iterator i = res.begin(), - eoi = res.end(); i != eoi; ++i) { - for(int32_t j = start; j <= end; j += step) { + res2.reserve(res.size() * ((end + 1 - start) / step)); + for (std::vector<std::string>::const_iterator i = res.begin(), + eoi = res.end(); + i != eoi; ++i) { + for (int32_t j = start; j <= end; j += step) { res2.push_back(*i); res2.back() += toBase26(j, zero, width); } } res.swap(res2); } - } else { + } + else { throw DL_ABORT_EX("Invalid loop range."); } - return i+1; + return i + 1; } // Expand parameterized string. @@ -210,31 +219,30 @@ InputIterator expandLoop // // Examples: // "alpha:[foo,bar]:bravo" -> ["alpha:foo:bravo", "alpha:bar:bravo"] -template<typename InputIterator, typename OutputIterator> -void expand -(InputIterator first, - InputIterator last, - OutputIterator out) +template <typename InputIterator, typename OutputIterator> +void expand(InputIterator first, InputIterator last, OutputIterator out) { std::vector<std::string> res; res.push_back(""); - while(first != last) { + while (first != last) { InputIterator i = first; - for(; i != last && *i != '{' && *i != '['; ++i); - for(auto & re : res) { + for (; i != last && *i != '{' && *i != '['; ++i) + ; + for (auto& re : res) { re.append(first, i); } first = i; - if(first == last) { + if (first == last) { break; } - if(*first == '{') { + if (*first == '{') { first = expandChoice(res, first, last); - } else if(*first == '[') { + } + else if (*first == '[') { first = expandLoop(res, first, last); } } - if(res.size() != 1 || !res[0].empty()) { + if (res.size() != 1 || !res[0].empty()) { std::copy(res.begin(), res.end(), out); } } |
