summaryrefslogtreecommitdiff
path: root/src/string.cc
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2015-05-01 18:47:22 +0100
committerMaxime Coste <frrrwww@gmail.com>2015-05-01 18:47:22 +0100
commitd7159a9af04d88fa2c741557ec436f08d62ef541 (patch)
treebc53e8a10ff7c8c724c13b18385ab9f2ed32da33 /src/string.cc
parentd3607bc77306c32a86d1b981f1022481ee4acd43 (diff)
Add str_to_int_ifp that returns an Optional<int> instead of throwing
Diffstat (limited to 'src/string.cc')
-rw-r--r--src/string.cc11
1 files changed, 9 insertions, 2 deletions
diff --git a/src/string.cc b/src/string.cc
index bc02b4a3..2735f087 100644
--- a/src/string.cc
+++ b/src/string.cc
@@ -98,7 +98,7 @@ String indent(StringView str, StringView indent)
return res;
}
-int str_to_int(StringView str)
+Optional<int> str_to_int_ifp(StringView str)
{
unsigned int res = 0;
bool negative = false;
@@ -110,11 +110,18 @@ int str_to_int(StringView str)
else if (c >= '0' and c <= '9')
res = res * 10 + c - '0';
else
- throw runtime_error(str + "is not a number");
+ return {};
}
return negative ? -(int)res : (int)res;
}
+int str_to_int(StringView str)
+{
+ if (auto val = str_to_int_ifp(str))
+ return *val;
+ throw str + " is not a number";
+}
+
InplaceString<16> to_string(int val)
{
InplaceString<16> res;