diff --git a/benchmark/test_to_bool.cpp b/benchmark/test_to_bool.cpp new file mode 100644 index 000000000..92f073156 --- /dev/null +++ b/benchmark/test_to_bool.cpp @@ -0,0 +1,29 @@ +#include "bench_framework.hpp" +#include + +class test : public benchmark::test_case +{ + std::string value_; +public: + test(mapnik::parameters const& params) + : test_case(params), + value_("true") {} + bool validate() const + { + bool result = false; + mapnik::util::string2bool(value_.data(),value_.data()+value_.size(),result); + if (!result) return result; + mapnik::util::string2bool(value_,result); + return (result == true); + } + void operator()() const + { + for (std::size_t i=0;ibool") diff --git a/include/mapnik/util/conversions.hpp b/include/mapnik/util/conversions.hpp index 9c7c0a514..33d5301ea 100644 --- a/include/mapnik/util/conversions.hpp +++ b/include/mapnik/util/conversions.hpp @@ -38,8 +38,26 @@ to avoid the compile time overhead given it is included by many other headers inside mapnik. */ -MAPNIK_DECL bool string2bool(std::string const& value, bool & result); -MAPNIK_DECL bool string2bool(const char * iter, const char * end, bool & result); +MAPNIK_DECL inline bool string2bool(std::string const& value, bool & result) +{ + if (value == "true") { + return result = true; + } else if (value == "false") { + return result = false; + } + std::string val(value); + std::transform(val.begin(), val.end(), val.begin(), ::tolower); + if (val == "true" || val == "yes" || val == "1" || val == "on") { + return result = true; + } + return result = false; +} + +MAPNIK_DECL inline bool string2bool(const char * iter, const char * end, bool & result) +{ + std::string val(iter,end); + return string2bool(val,result); +} MAPNIK_DECL bool string2int(std::string const& value, int & result); MAPNIK_DECL bool string2int(const char * iter, const char * end, int & result); diff --git a/src/conversions.cpp b/src/conversions.cpp index 2b5a496b4..915242f9d 100644 --- a/src/conversions.cpp +++ b/src/conversions.cpp @@ -25,6 +25,7 @@ #include #include +#include #include @@ -59,39 +60,6 @@ BOOST_SPIRIT_AUTO(qi, LONGLONG, qi::long_long_type()) BOOST_SPIRIT_AUTO(qi, FLOAT, qi::float_type()) BOOST_SPIRIT_AUTO(qi, DOUBLE, qi::double_type()) -struct bool_symbols : qi::symbols -{ - bool_symbols() - { - add("true",true) - ("false",false) - ("yes",true) - ("no",false) - ("on",true) - ("off",false) - ("1",true) - ("0",false); - } -}; - -bool string2bool(const char * iter, const char * end, bool & result) -{ - boost::spirit::qi::no_case_type no_case; - ascii::space_type space; - bool r = qi::phrase_parse(iter,end,no_case[bool_symbols()],space,result); - return r && (iter == end); -} - -bool string2bool(std::string const& value, bool & result) -{ - boost::spirit::qi::no_case_type no_case; - ascii::space_type space; - std::string::const_iterator str_beg = value.begin(); - std::string::const_iterator str_end = value.end(); - bool r = qi::phrase_parse(str_beg,str_end,no_case[bool_symbols()],space,result); - return r && (str_beg == str_end); -} - bool string2int(const char * iter, const char * end, int & result) { ascii::space_type space;