Use custom implementation rather the sscanf in str_to_int
This commit is contained in:
parent
166682d802
commit
335c73a09b
|
@ -100,10 +100,19 @@ String indent(StringView str, StringView indent)
|
||||||
|
|
||||||
int str_to_int(StringView str)
|
int str_to_int(StringView str)
|
||||||
{
|
{
|
||||||
int res = 0;
|
unsigned int res = 0;
|
||||||
if (sscanf(str.zstr(), "%i", &res) != 1)
|
bool negative = false;
|
||||||
throw runtime_error(str + "is not a number");
|
for (auto it = str.begin(), end = str.end(); it != end; ++it)
|
||||||
return res;
|
{
|
||||||
|
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)
|
String to_string(int val)
|
||||||
|
|
|
@ -139,6 +139,12 @@ void test_string()
|
||||||
kak_assert(not subsequence_match("tchou kanaky", "tchou kanaky"));
|
kak_assert(not subsequence_match("tchou kanaky", "tchou kanaky"));
|
||||||
|
|
||||||
kak_assert(format("Youhou {1} {} {0} \\{}", 10, "hehe", 5) == "Youhou hehe 5 10 {}");
|
kak_assert(format("Youhou {1} {} {0} \\{}", 10, "hehe", 5) == "Youhou hehe 5 10 {}");
|
||||||
|
|
||||||
|
kak_assert(str_to_int("5") == 5);
|
||||||
|
kak_assert(str_to_int(to_string(INT_MAX)) == INT_MAX);
|
||||||
|
kak_assert(str_to_int(to_string(INT_MIN)) == INT_MIN);
|
||||||
|
kak_assert(str_to_int("00") == 0);
|
||||||
|
kak_assert(str_to_int("-0") == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_keys()
|
void test_keys()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user