fix try_parse behaviour with empty string input

This commit is contained in:
2019-07-12 19:18:08 +07:00
parent bc986da464
commit 0f41221083
2 changed files with 9 additions and 3 deletions

View File

@@ -197,7 +197,7 @@ namespace e2d::strings
std::memcpy(str, src.data(), src.size());
const i64 tmp = std::strtoll(str, &end, 10);
if ( end != str + src.size() || errno ) {
if ( str == end || end != str + src.size() || errno ) {
return false;
}
@@ -228,7 +228,7 @@ namespace e2d::strings
std::memcpy(str, src.data(), src.size());
const i64 tmp = std::strtoll(str, &end, 10);
if ( end != str + src.size() || errno ) {
if ( str == end || end != str + src.size() || errno ) {
return false;
}
@@ -265,7 +265,7 @@ namespace e2d::strings
std::memcpy(str, src.data(), src.size());
const f32 tmp = std::strtof(str, &end);
if ( end != str + src.size() || errno ) {
if ( str == end || end != str + src.size() || errno ) {
return false;
}

View File

@@ -118,6 +118,7 @@ TEST_CASE("strings") {
i8 v{111};
REQUIRE((!try_parse(str_view(), v) && v == 111));
REQUIRE((!try_parse("", v) && v == 111));
REQUIRE((!try_parse(" \t", v) && v == 111));
REQUIRE((!try_parse("42hello", v) && v == 111));
REQUIRE((!try_parse("world42", v) && v == 111));
REQUIRE((!try_parse("42 ", v) && v == 111));
@@ -128,6 +129,7 @@ TEST_CASE("strings") {
u8 uv{111};
REQUIRE((!try_parse(str_view(), uv) && uv == 111));
REQUIRE((!try_parse("", uv) && uv == 111));
REQUIRE((!try_parse(" \t", uv) && uv == 111));
REQUIRE((!try_parse("42hello", uv) && uv == 111));
REQUIRE((!try_parse("world42", uv) && uv == 111));
REQUIRE((!try_parse("42 ", uv) && uv == 111));
@@ -150,12 +152,16 @@ TEST_CASE("strings") {
}
{
f32 v32{11.22f};
REQUIRE((!try_parse("", v32) && math::approximately(v32, 11.22f)));
REQUIRE((!try_parse(" \t", v32) && math::approximately(v32, 11.22f)));
REQUIRE((!try_parse("1.0E100", v32) && math::approximately(v32, 11.22f)));
REQUIRE((!try_parse("1..4", v32) && math::approximately(v32, 11.22f)));
REQUIRE((!try_parse("..14", v32) && math::approximately(v32, 11.22f)));
REQUIRE((!try_parse("14..", v32) && math::approximately(v32, 11.22f)));
f64 v64{11.22};
REQUIRE((!try_parse("", v64) && math::approximately(v64, 11.22)));
REQUIRE((!try_parse(" \t", v64) && math::approximately(v64, 11.22)));
REQUIRE((!try_parse("1.0E400", v64) && math::approximately(v64, 11.22)));
REQUIRE((!try_parse("1..4", v64) && math::approximately(v64, 11.22)));
REQUIRE((!try_parse("..14", v64) && math::approximately(v64, 11.22)));