optimize string2bool
This commit is contained in:
parent
10ce32696a
commit
b7b1acaa50
3 changed files with 50 additions and 35 deletions
29
benchmark/test_to_bool.cpp
Normal file
29
benchmark/test_to_bool.cpp
Normal file
|
@ -0,0 +1,29 @@
|
|||
#include "bench_framework.hpp"
|
||||
#include <mapnik/util/conversions.hpp>
|
||||
|
||||
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;i<iterations_;++i) {
|
||||
bool result = false;
|
||||
mapnik::util::string2bool(value_,result);
|
||||
mapnik::util::string2bool(value_.data(),value_.data()+value_.size(),result);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
BENCHMARK(test,"string->bool")
|
|
@ -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);
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
#include <mapnik/value_types.hpp>
|
||||
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
|
||||
#include <boost/spirit/include/qi.hpp>
|
||||
|
||||
|
@ -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<char,bool>
|
||||
{
|
||||
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;
|
||||
|
|
Loading…
Reference in a new issue