diff options
| author | Maxime Coste <frrrwww@gmail.com> | 2016-11-22 23:51:09 +0000 |
|---|---|---|
| committer | Maxime Coste <frrrwww@gmail.com> | 2016-11-22 23:51:09 +0000 |
| commit | ca0606017f4f3b975f6c696b6f103162e773feed (patch) | |
| tree | f901f1ca90780eaecaa722576ed644a3e2519996 /src | |
| parent | 3f3ed0b333a511d83402d29dd8b3be9151b4173b (diff) | |
Small code refactoring
Diffstat (limited to 'src')
| -rw-r--r-- | src/string.cc | 23 |
1 files changed, 10 insertions, 13 deletions
diff --git a/src/string.cc b/src/string.cc index dbece8e7..e4d4ba96 100644 --- a/src/string.cc +++ b/src/string.cc @@ -283,17 +283,16 @@ String replace(StringView str, StringView substr, StringView replacement) Optional<int> str_to_int_ifp(StringView str) { + bool negative = not str.empty() and str[0] == '-'; + if (negative) + str = str.substr(1_byte); + unsigned int res = 0; - bool negative = false; - for (auto it = str.begin(), end = str.end(); it != end; ++it) + for (auto c : str) { - const char c = *it; - if (it == str.begin() and c == '-') - negative = true; - else if (c >= '0' and c <= '9') - res = res * 10 + c - '0'; - else + if (c < '0' or c > '9') return {}; + res = res * 10 + c - '0'; } return negative ? -(int)res : (int)res; } @@ -474,11 +473,9 @@ void format_impl(StringView fmt, ArrayView<const StringView> params, AppendFunc auto closing = std::find(opening, end, '}'); if (closing == end) throw runtime_error("Format string error, unclosed '{'"); - int index; - if (closing == opening + 1) - index = implicitIndex; - else - index = str_to_int({opening+1, closing}); + + const int index = (closing == opening + 1) ? + implicitIndex : str_to_int({opening+1, closing}); if (index >= params.size()) throw runtime_error("Format string parameter index too big"); |
