fix: avoid duplicated-branches in result

when both two types are trivially destructible, both branches of cleanup
function results in the same code...
This commit is contained in:
ToruNiina
2021-07-01 00:46:56 +09:00
parent 0858fbfced
commit ca9e36a484

View File

@@ -364,15 +364,17 @@ inline result<std::string, std::string> parse_escape_sequence(location& loc)
return err(msg); return err(msg);
} }
inline result<none_t, std::ptrdiff_t> check_utf8_validity(const std::string& reg) inline std::ptrdiff_t check_utf8_validity(const std::string& reg)
{ {
location loc("tmp", reg); location loc("tmp", reg);
const auto u8 = repeat<lex_utf8_code, unlimited>::invoke(loc); const auto u8 = repeat<lex_utf8_code, unlimited>::invoke(loc);
if(!u8 || loc.iter() != loc.end()) if(!u8 || loc.iter() != loc.end())
{ {
return err(std::distance(loc.begin(), loc.iter())); const auto error_location = std::distance(loc.begin(), loc.iter());
assert(0 <= error_location);
return error_location;
} }
return ok(none_t{}); return -1;
} }
inline result<std::pair<toml::string, region>, std::string> inline result<std::pair<toml::string, region>, std::string>
@@ -444,14 +446,15 @@ parse_ml_basic_string(location& loc)
} }
} }
if(const auto u8 = check_utf8_validity(token.unwrap().str())) const auto err_loc = check_utf8_validity(token.unwrap().str());
if(err_loc == -1)
{ {
return ok(std::make_pair(toml::string(retval), token.unwrap())); return ok(std::make_pair(toml::string(retval), token.unwrap()));
} }
else else
{ {
inner_loc.reset(first); inner_loc.reset(first);
inner_loc.advance(u8.as_err()); inner_loc.advance(err_loc);
throw syntax_error(format_underline( throw syntax_error(format_underline(
"parse_ml_basic_string: invalid utf8 sequence found", "parse_ml_basic_string: invalid utf8 sequence found",
{{source_location(inner_loc), "here"}}), {{source_location(inner_loc), "here"}}),
@@ -509,14 +512,15 @@ parse_basic_string(location& loc)
quot = lex_quotation_mark::invoke(inner_loc); quot = lex_quotation_mark::invoke(inner_loc);
} }
if(const auto u8 = check_utf8_validity(token.unwrap().str())) const auto err_loc = check_utf8_validity(token.unwrap().str());
if(err_loc == -1)
{ {
return ok(std::make_pair(toml::string(retval), token.unwrap())); return ok(std::make_pair(toml::string(retval), token.unwrap()));
} }
else else
{ {
inner_loc.reset(first); inner_loc.reset(first);
inner_loc.advance(u8.as_err()); inner_loc.advance(err_loc);
throw syntax_error(format_underline( throw syntax_error(format_underline(
"parse_ml_basic_string: invalid utf8 sequence found", "parse_ml_basic_string: invalid utf8 sequence found",
{{source_location(inner_loc), "here"}}), {{source_location(inner_loc), "here"}}),
@@ -583,7 +587,8 @@ parse_ml_literal_string(location& loc)
} }
} }
if(const auto u8 = check_utf8_validity(token.unwrap().str())) const auto err_loc = check_utf8_validity(token.unwrap().str());
if(err_loc == -1)
{ {
return ok(std::make_pair(toml::string(retval, toml::string_t::literal), return ok(std::make_pair(toml::string(retval, toml::string_t::literal),
token.unwrap())); token.unwrap()));
@@ -591,7 +596,7 @@ parse_ml_literal_string(location& loc)
else else
{ {
inner_loc.reset(first); inner_loc.reset(first);
inner_loc.advance(u8.as_err()); inner_loc.advance(err_loc);
throw syntax_error(format_underline( throw syntax_error(format_underline(
"parse_ml_basic_string: invalid utf8 sequence found", "parse_ml_basic_string: invalid utf8 sequence found",
{{source_location(inner_loc), "here"}}), {{source_location(inner_loc), "here"}}),
@@ -635,7 +640,8 @@ parse_literal_string(location& loc)
source_location(inner_loc)); source_location(inner_loc));
} }
if(const auto u8 = check_utf8_validity(token.unwrap().str())) const auto err_loc = check_utf8_validity(token.unwrap().str());
if(err_loc == -1)
{ {
return ok(std::make_pair( return ok(std::make_pair(
toml::string(body.unwrap().str(), toml::string_t::literal), toml::string(body.unwrap().str(), toml::string_t::literal),
@@ -644,7 +650,7 @@ parse_literal_string(location& loc)
else else
{ {
inner_loc.reset(first); inner_loc.reset(first);
inner_loc.advance(u8.as_err()); inner_loc.advance(err_loc);
throw syntax_error(format_underline( throw syntax_error(format_underline(
"parse_ml_basic_string: invalid utf8 sequence found", "parse_ml_basic_string: invalid utf8 sequence found",
{{source_location(inner_loc), "here"}}), {{source_location(inner_loc), "here"}}),