replace boost::trim with faster custom trim - closes #1633

This commit is contained in:
Dane Springmeyer 2012-12-07 14:06:13 -08:00
parent f25626f678
commit 7c58bf9fcb
9 changed files with 109 additions and 43 deletions

View file

@ -0,0 +1,76 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2012 Artem Pavlenko
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
#ifndef MAPNIK_TRIM_HPP
#define MAPNIK_TRIM_HPP
// boost
//#include <boost/algorithm/string/trim.hpp>
// stl
#include <string>
#include <iostream>
namespace mapnik { namespace util {
/*
faster trim (than boost::trim)
that intentionally does not respect
std::locale to avoid overhead in cases
where the locale is not critical
*/
static inline bool not_whitespace(int ch)
{
if (ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t') return false;
return true;
}
// trim from start
static inline std::string & ltrim(std::string & s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), not_whitespace));
return s;
}
// trim from end
static inline std::string & rtrim(std::string & s)
{
s.erase(std::find_if(s.rbegin(), s.rend(), not_whitespace).base(), s.end());
return s;
}
// trim from both ends
static inline void trim(std::string & s)
{
ltrim(rtrim(s));
}
static inline std::string trim_copy(std::string s)
{
return ltrim(rtrim(s));
}
}} // end of namespace mapnik
#endif // MAPNIK_TRIM_HPP

View file

@ -41,6 +41,7 @@
#include <mapnik/util/geometry_to_ds_type.hpp> #include <mapnik/util/geometry_to_ds_type.hpp>
#include <mapnik/util/conversions.hpp> #include <mapnik/util/conversions.hpp>
#include <mapnik/boolean.hpp> #include <mapnik/boolean.hpp>
#include <mapnik/util/trim.hpp>
// stl // stl
#include <sstream> #include <sstream>
@ -68,7 +69,7 @@ csv_datasource::csv_datasource(parameters const& params, bool bind)
separator_(*params_.get<std::string>("separator", "")), separator_(*params_.get<std::string>("separator", "")),
quote_(*params_.get<std::string>("quote", "")), quote_(*params_.get<std::string>("quote", "")),
headers_(), headers_(),
manual_headers_(boost::trim_copy(*params_.get<std::string>("headers", ""))), manual_headers_(mapnik::util::trim_copy(*params_.get<std::string>("headers", ""))),
strict_(*params_.get<mapnik::boolean>("strict", false)), strict_(*params_.get<mapnik::boolean>("strict", false)),
quiet_(*params_.get<mapnik::boolean>("quiet", false)), quiet_(*params_.get<mapnik::boolean>("quiet", false)),
filesize_max_(*params_.get<float>("filesize_max", 20.0)), // MB filesize_max_(*params_.get<float>("filesize_max", 20.0)), // MB
@ -196,7 +197,7 @@ void csv_datasource::parse_csv(T & stream,
// if user has not passed a separator manually // if user has not passed a separator manually
// then attempt to detect by reading first line // then attempt to detect by reading first line
std::string sep = boost::trim_copy(separator); std::string sep = mapnik::util::trim_copy(separator);
if (sep.empty()) if (sep.empty())
{ {
// default to ',' // default to ','
@ -240,10 +241,10 @@ void csv_datasource::parse_csv(T & stream,
typedef boost::escaped_list_separator<char> escape_type; typedef boost::escaped_list_separator<char> escape_type;
std::string esc = boost::trim_copy(escape); std::string esc = mapnik::util::trim_copy(escape);
if (esc.empty()) esc = "\\"; if (esc.empty()) esc = "\\";
std::string quo = boost::trim_copy(quote); std::string quo = mapnik::util::trim_copy(quote);
if (quo.empty()) quo = "\""; if (quo.empty()) quo = "\"";
MAPNIK_LOG_DEBUG(csv) << "csv_datasource: csv grammar: sep: '" << sep MAPNIK_LOG_DEBUG(csv) << "csv_datasource: csv grammar: sep: '" << sep
@ -281,7 +282,7 @@ void csv_datasource::parse_csv(T & stream,
unsigned idx(0); unsigned idx(0);
for (; beg != tok.end(); ++beg) for (; beg != tok.end(); ++beg)
{ {
std::string val = boost::trim_copy(*beg); std::string val = mapnik::util::trim_copy(*beg);
std::string lower_val = boost::algorithm::to_lower_copy(val); std::string lower_val = boost::algorithm::to_lower_copy(val);
if (lower_val == "wkt" if (lower_val == "wkt"
|| (lower_val.find("geom") != std::string::npos)) || (lower_val.find("geom") != std::string::npos))
@ -324,7 +325,7 @@ void csv_datasource::parse_csv(T & stream,
Tokenizer::iterator beg = tok.begin(); Tokenizer::iterator beg = tok.begin();
std::string val; std::string val;
if (beg != tok.end()) if (beg != tok.end())
val = boost::trim_copy(*beg); val = mapnik::util::trim_copy(*beg);
// skip blank lines // skip blank lines
if (val.empty()) if (val.empty())
@ -338,7 +339,7 @@ void csv_datasource::parse_csv(T & stream,
for (; beg != tok.end(); ++beg) for (; beg != tok.end(); ++beg)
{ {
++idx; ++idx;
val = boost::trim_copy(*beg); val = mapnik::util::trim_copy(*beg);
if (val.empty()) if (val.empty())
{ {
if (strict_) if (strict_)
@ -522,7 +523,7 @@ void csv_datasource::parse_csv(T & stream,
} }
else else
{ {
value = boost::trim_copy(*beg); value = mapnik::util::trim_copy(*beg);
++beg; ++beg;
} }

View file

@ -32,9 +32,9 @@
#include <mapnik/sql_utils.hpp> #include <mapnik/sql_utils.hpp>
#include <mapnik/feature_factory.hpp> #include <mapnik/feature_factory.hpp>
#include <mapnik/util/conversions.hpp> #include <mapnik/util/conversions.hpp>
#include <mapnik/util/trim.hpp>
// boost // boost
#include <boost/algorithm/string.hpp>
#include <boost/spirit/include/qi.hpp> #include <boost/spirit/include/qi.hpp>
// stl // stl
@ -182,8 +182,7 @@ feature_ptr postgis_featureset::next()
case 1042: //bpchar case 1042: //bpchar
{ {
std::string str(buf); std::string str = mapnik::util::trim_copy(buf);
boost::trim(str);
feature->put(name, tr_->transcode(str.c_str())); feature->put(name, tr_->transcode(str.c_str()));
break; break;
} }

View file

@ -22,13 +22,13 @@
// mapnik // mapnik
#include <mapnik/box2d.hpp> #include <mapnik/box2d.hpp>
#include <mapnik/util/trim.hpp>
// stl // stl
#include <stdexcept> #include <stdexcept>
// boost // boost
#include <boost/tokenizer.hpp> #include <boost/tokenizer.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/spirit/include/qi.hpp> #include <boost/spirit/include/qi.hpp>
// agg // agg
@ -359,14 +359,17 @@ bool box2d<T>::from_string(std::string const& s)
for (boost::tokenizer<boost::char_separator<char> >::iterator beg = tok.begin(); for (boost::tokenizer<boost::char_separator<char> >::iterator beg = tok.begin();
beg != tok.end(); ++beg) beg != tok.end(); ++beg)
{ {
std::string item(*beg); std::string item = mapnik::util::trim_copy(*beg);
boost::trim(item);
// note: we intentionally do not use mapnik::util::conversions::string2double // note: we intentionally do not use mapnik::util::conversions::string2double
// here to ensure that shapeindex can statically compile mapnik::box2d without // here to ensure that shapeindex can statically compile mapnik::box2d without
// needing to link to libmapnik // needing to link to libmapnik
std::string::const_iterator str_beg = item.begin(); std::string::const_iterator str_beg = item.begin();
std::string::const_iterator str_end = item.end(); std::string::const_iterator str_end = item.end();
bool r = boost::spirit::qi::phrase_parse(str_beg,str_end,boost::spirit::qi::double_,boost::spirit::ascii::space,d[i]); bool r = boost::spirit::qi::phrase_parse(str_beg,
str_end,
boost::spirit::qi::double_,
boost::spirit::ascii::space,
d[i]);
if (!(r && (str_beg == str_end))) if (!(r && (str_beg == str_end)))
{ {
break; break;

View file

@ -23,15 +23,14 @@
#ifdef HAVE_LIBXML2 #ifdef HAVE_LIBXML2
// mapnik // mapnik
#include <mapnik/debug.hpp>
#include <mapnik/xml_loader.hpp> #include <mapnik/xml_loader.hpp>
#include <mapnik/xml_node.hpp> #include <mapnik/xml_node.hpp>
#include <mapnik/config_error.hpp> #include <mapnik/config_error.hpp>
#include <mapnik/util/trim.hpp>
// boost // boost
#include <boost/utility.hpp> #include <boost/utility.hpp>
#include <boost/filesystem/operations.hpp> #include <boost/filesystem/operations.hpp>
#include <boost/algorithm/string/trim.hpp>
// libxml // libxml
#include <libxml/parser.h> #include <libxml/parser.h>
@ -39,8 +38,6 @@
#include <libxml/parserInternals.h> #include <libxml/parserInternals.h>
#include <libxml/xinclude.h> #include <libxml/xinclude.h>
using namespace std;
#define DEFAULT_OPTIONS (XML_PARSE_NOERROR | XML_PARSE_NOENT | XML_PARSE_NOBLANKS | XML_PARSE_DTDLOAD | XML_PARSE_NOCDATA) #define DEFAULT_OPTIONS (XML_PARSE_NOERROR | XML_PARSE_NOENT | XML_PARSE_NOBLANKS | XML_PARSE_DTDLOAD | XML_PARSE_NOCDATA)
namespace mapnik namespace mapnik
@ -75,7 +72,7 @@ public:
boost::filesystem::path path(filename); boost::filesystem::path path(filename);
if (!boost::filesystem::exists(path)) if (!boost::filesystem::exists(path))
{ {
throw config_error(string("Could not load map file: File does not exist"), 0, filename); throw config_error(std::string("Could not load map file: File does not exist"), 0, filename);
} }
xmlDocPtr doc = xmlCtxtReadFile(ctx_, filename.c_str(), encoding_, options_); xmlDocPtr doc = xmlCtxtReadFile(ctx_, filename.c_str(), encoding_, options_);
@ -92,13 +89,6 @@ public:
throw config_error(msg, error->line, error->file); throw config_error(msg, error->line, error->file);
} }
} }
/*
if ( ! ctx->valid )
{
MAPNIK_LOG_WARN(libxml2_loader) << "libxml2_loader: Failed to validate DTD.";
}
*/
load(doc, node); load(doc, node);
} }
@ -114,7 +104,7 @@ public:
{ {
boost::filesystem::path path(base_path); boost::filesystem::path path(base_path);
if (!boost::filesystem::exists(path)) { if (!boost::filesystem::exists(path)) {
throw config_error(string("Could not locate base_path '") + throw config_error(std::string("Could not locate base_path '") +
base_path + "': file or directory does not exist"); base_path + "': file or directory does not exist");
} }
} }
@ -186,7 +176,7 @@ private:
case XML_TEXT_NODE: case XML_TEXT_NODE:
{ {
std::string trimmed((const char*)cur_node->content); std::string trimmed((const char*)cur_node->content);
boost::algorithm::trim(trimmed); mapnik::util::trim(trimmed);
if (trimmed.empty()) break; //Don't add empty text nodes if (trimmed.empty()) break; //Don't add empty text nodes
node.add_child(trimmed, cur_node->line, true); node.add_child(trimmed, cur_node->line, true);
} }

View file

@ -48,12 +48,12 @@
#include <mapnik/config_error.hpp> #include <mapnik/config_error.hpp>
#include <mapnik/util/dasharray_parser.hpp> #include <mapnik/util/dasharray_parser.hpp>
#include <mapnik/util/conversions.hpp> #include <mapnik/util/conversions.hpp>
#include <mapnik/util/trim.hpp>
#include <mapnik/marker_cache.hpp> #include <mapnik/marker_cache.hpp>
// boost // boost
#include <boost/optional.hpp> #include <boost/optional.hpp>
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/tokenizer.hpp> #include <boost/tokenizer.hpp>
#include <boost/property_tree/ptree.hpp> #include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp> #include <boost/property_tree/xml_parser.hpp>
@ -255,8 +255,7 @@ void map_parser::parse_map(Map & map, xml_node const& pt, std::string const& bas
for (boost::tokenizer<boost::char_separator<char> >::iterator beg = tokens.begin(); for (boost::tokenizer<boost::char_separator<char> >::iterator beg = tokens.begin();
beg != tokens.end(); ++beg) beg != tokens.end(); ++beg)
{ {
std::string item(*beg); std::string item = mapnik::util::trim_copy(*beg);
boost::trim(item);
if (!mapnik::util::string2int(item,n[i])) if (!mapnik::util::string2int(item,n[i]))
{ {
throw config_error(std::string("Invalid version string encountered: '") throw config_error(std::string("Invalid version string encountered: '")

View file

@ -23,9 +23,7 @@
// mapnik // mapnik
#include <mapnik/projection.hpp> #include <mapnik/projection.hpp>
#include <mapnik/utils.hpp> #include <mapnik/utils.hpp>
#include <mapnik/util/trim.hpp>
// boost
#include <boost/algorithm/string.hpp>
// proj4 // proj4
#include <proj_api.h> #include <proj_api.h>
@ -151,10 +149,7 @@ void projection::init()
std::string projection::expanded() const std::string projection::expanded() const
{ {
if (proj_) { if (proj_) {
std::string def(pj_get_def( proj_, 0 )); return mapnik::util::trim_copy(pj_get_def( proj_, 0 ));
//boost::algorithm::ireplace_first(def,params_,"");
boost::trim(def);
return def;
} }
return std::string(""); return std::string("");
} }

View file

@ -31,16 +31,15 @@
#include <boost/property_tree/detail/xml_parser_read_rapidxml.hpp> #include <boost/property_tree/detail/xml_parser_read_rapidxml.hpp>
#include <mapnik/xml_node.hpp> #include <mapnik/xml_node.hpp>
#include <mapnik/config_error.hpp> #include <mapnik/config_error.hpp>
#include <mapnik/util/trim.hpp>
// boost // boost
#include <boost/utility.hpp> #include <boost/utility.hpp>
#include <boost/algorithm/string/trim.hpp>
// stl // stl
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
using namespace std;
namespace rapidxml = boost::property_tree::detail::rapidxml; namespace rapidxml = boost::property_tree::detail::rapidxml;
namespace mapnik namespace mapnik
{ {
@ -108,7 +107,7 @@ public:
// { // {
// boost::filesystem::path path(base_path); // boost::filesystem::path path(base_path);
// if (!boost::filesystem::exists(path)) { // if (!boost::filesystem::exists(path)) {
// throw config_error(string("Could not locate base_path '") + // throw config_error(std::string("Could not locate base_path '") +
// base_path + "': file or directory does not exist"); // base_path + "': file or directory does not exist");
// } // }
// } // }
@ -145,7 +144,7 @@ private:
case rapidxml::node_cdata: case rapidxml::node_cdata:
{ {
std::string trimmed(cur_node->value()); std::string trimmed(cur_node->value());
boost::trim(trimmed); mapnik::util::trim(trimmed);
if (trimmed.empty()) break; //Don't add empty text nodes if (trimmed.empty()) break; //Don't add empty text nodes
node.add_child(trimmed, 0, true); node.add_child(trimmed, 0, true);
} }

View file

@ -0,0 +1,4 @@
<Map>
<!-- backspace-->

</Map>