From c82e76a1114338a7ff2520eaa0a71d27f201b08a Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Tue, 16 Apr 2019 21:47:24 +0900 Subject: [PATCH] perf: check string type before parsing it to avoid unncessary error message generation, check the first some characters before parsing it. It makes parsing process faster and is also helpful to generate more accurate error messages. --- toml/parser.hpp | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/toml/parser.hpp b/toml/parser.hpp index cec7968..6e2f17c 100644 --- a/toml/parser.hpp +++ b/toml/parser.hpp @@ -539,10 +539,30 @@ template result>, std::string> parse_string(location& loc) { - if(const auto rslt = parse_ml_basic_string(loc)) {return rslt;} - if(const auto rslt = parse_ml_literal_string(loc)) {return rslt;} - if(const auto rslt = parse_basic_string(loc)) {return rslt;} - if(const auto rslt = parse_literal_string(loc)) {return rslt;} + if(loc.iter() != loc.end() && *(loc.iter()) == '"') + { + if(loc.iter() + 1 != loc.end() && *(loc.iter() + 1) == '"' && + loc.iter() + 2 != loc.end() && *(loc.iter() + 2) == '"') + { + return parse_ml_basic_string(loc); + } + else + { + return parse_basic_string(loc); + } + } + else if(loc.iter() != loc.end() && *(loc.iter()) == '\'') + { + if(loc.iter() + 1 != loc.end() && *(loc.iter() + 1) == '\'' && + loc.iter() + 2 != loc.end() && *(loc.iter() + 2) == '\'') + { + return parse_ml_literal_string(loc); + } + else + { + return parse_literal_string(loc); + } + } return err(format_underline("[error] toml::parse_string: ", {{std::addressof(loc), "the next token is not a string"}})); }