summaryrefslogtreecommitdiff
path: root/src/string.cc
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2015-03-30 23:34:08 +0100
committerMaxime Coste <frrrwww@gmail.com>2015-03-30 23:37:55 +0100
commit335c73a09b038e80faa6e61906041e7392c15532 (patch)
treed1a72419979b0114437c13e394cacf215f46e743 /src/string.cc
parent166682d80231118f85ec07af23118c14b7e22a22 (diff)
Use custom implementation rather the sscanf in str_to_int
Diffstat (limited to 'src/string.cc')
-rw-r--r--src/string.cc17
1 files changed, 13 insertions, 4 deletions
diff --git a/src/string.cc b/src/string.cc
index ae454264..a9840d45 100644
--- a/src/string.cc
+++ b/src/string.cc
@@ -100,10 +100,19 @@ String indent(StringView str, StringView indent)
int str_to_int(StringView str)
{
- int res = 0;
- if (sscanf(str.zstr(), "%i", &res) != 1)
- throw runtime_error(str + "is not a number");
- return res;
+ unsigned int res = 0;
+ bool negative = false;
+ for (auto it = str.begin(), end = str.end(); it != end; ++it)
+ {
+ 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
+ throw runtime_error(str + "is not a number");
+ }
+ return negative ? -(int)res : (int)res;
}
String to_string(int val)