From: David Wells Date: Tue, 3 Jan 2017 17:06:06 +0000 (-0500) Subject: Avoid 'std::string::find' for performance reasons. X-Git-Tag: v8.5.0-rc1~295^2~3 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6f866710494d8a69cf6162f89b58e38acec21d2f;p=dealii.git Avoid 'std::string::find' for performance reasons. This was caught by cppcheck. --- diff --git a/cmake/scripts/expand_instantiations.cc b/cmake/scripts/expand_instantiations.cc index 01f1143596..855b9d6a2d 100644 --- a/cmake/scripts/expand_instantiations.cc +++ b/cmake/scripts/expand_instantiations.cc @@ -44,6 +44,7 @@ // Author: Wolfgang Bangerth, 2007 +#include #include #include #include @@ -61,6 +62,16 @@ std::map > expansion_lists; /* ======================== auxiliary functions ================= */ +// Return whether or not one string starts with a given prefix +bool +has_prefix(const std::string &base, const std::string &prefix) +{ + if (prefix.size() > base.size()) + return false; + else + return std::equal(prefix.begin(), prefix.end(), base.begin()); +} + // replace all occurrences of 'pattern' by 'substitute' in 'in', and // return the result @@ -297,14 +308,14 @@ void read_expansion_lists (const std::string &filename) name = get_substring_with_delim (whole_file, " :"); skip_space (whole_file); - if (whole_file.find (":=") != 0) + if (!has_prefix(whole_file, ":=")) { std::cerr << "Invalid entry <" << name << '>' << std::endl; std::exit (1); } whole_file.erase (0, 2); skip_space (whole_file); - if (whole_file.find ("{") != 0) + if (whole_file[0] != '{') { std::cerr << "Invalid entry <" << name << '>' << std::endl; std::exit (1); @@ -315,7 +326,7 @@ void read_expansion_lists (const std::string &filename) std::string expansion = get_substring_with_delim (whole_file, "}"); - if (whole_file.find ("}") != 0) + if (whole_file[0] != '}') { std::cerr << "Invalid entry <" << name << '>' << std::endl; std::exit (1); @@ -416,14 +427,14 @@ void process_instantiations () while (whole_file.size() != 0) { skip_space (whole_file); - if (whole_file.find ("for") != 0) + if (!has_prefix(whole_file, "for")) { std::cerr << "Invalid instantiation list: missing 'for'" << std::endl; std::exit (1); } whole_file.erase (0, 3); skip_space (whole_file); - if (whole_file.find ("(") != 0) + if (whole_file[0] != '(') { std::cerr << "Invalid instantiation list: missing '('" << std::endl; std::exit (1); @@ -436,7 +447,7 @@ void process_instantiations () = split_string_list (get_substring_with_delim (whole_file, ")"), ';'); - if (whole_file.find (")") != 0) + if (whole_file[0] != ')') { std::cerr << "Invalid instantiation list: missing ')'" << std::endl; std::exit (1); @@ -471,7 +482,7 @@ void process_instantiations () // now read the part in {...} skip_space (whole_file); - if (whole_file.find ("{") != 0) + if (whole_file[0] != '{') { std::cerr << "Invalid substitution text" << std::endl; std::exit (1); diff --git a/source/base/parameter_handler.cc b/source/base/parameter_handler.cc index b7bc4f21da..c507aeefc2 100644 --- a/source/base/parameter_handler.cc +++ b/source/base/parameter_handler.cc @@ -2918,8 +2918,8 @@ ParameterHandler::scan_line (std::string line, return; } // enter subsection - else if ((line.find ("SUBSECTION ") == 0) || - (line.find ("subsection ") == 0)) + else if (Utilities::match_at_string_start(line, "SUBSECTION ") || + Utilities::match_at_string_start(line, "subsection ")) { // delete this prefix line.erase (0, std::string("subsection").length()+1); @@ -2936,8 +2936,8 @@ ParameterHandler::scan_line (std::string line, subsection_path.push_back (subsection); } // exit subsection - else if ((line.find ("END") == 0) || - (line.find ("end") == 0)) + else if (Utilities::match_at_string_start(line, "END") || + Utilities::match_at_string_start(line, "end")) { line.erase (0, 3); while ((line.size() > 0) && (std::isspace(line[0]))) @@ -2954,8 +2954,8 @@ ParameterHandler::scan_line (std::string line, leave_subsection (); } // regular entry - else if ((line.find ("SET ") == 0) || - (line.find ("set ") == 0)) + else if (Utilities::match_at_string_start(line, "SET ") || + Utilities::match_at_string_start(line, "set ")) { // erase "set" statement line.erase (0, 4); @@ -3022,8 +3022,8 @@ ParameterHandler::scan_line (std::string line, } } // an include statement? - else if ((line.find ("INCLUDE ") == 0) || - (line.find ("include ") == 0)) + else if (Utilities::match_at_string_start(line, "include ") || + Utilities::match_at_string_start(line, "INCLUDE ")) { // erase "include " statement and eliminate spaces line.erase (0, 7);