From 55737e4ff63df685f1603eadc3105cda38b7da9b Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sat, 18 Jan 2014 13:36:50 +0100 Subject: db/upnp/Util: trimstring() constructs string from buffer Reduce overhead by omitting the part of the buffer that consists only of whitespace. --- src/db/upnp/Util.cxx | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'src/db/upnp/Util.cxx') diff --git a/src/db/upnp/Util.cxx b/src/db/upnp/Util.cxx index afe68e619..fc9ef6281 100644 --- a/src/db/upnp/Util.cxx +++ b/src/db/upnp/Util.cxx @@ -18,6 +18,7 @@ */ #include "Util.hxx" +#include "util/CharUtil.hxx" #include #include @@ -27,19 +28,17 @@ #include /** Get rid of white space at both ends */ -void -trimstring(std::string &s, const char *ws) +std::string +trimstring(const char *p, size_t length) { - auto pos = s.find_first_not_of(ws); - if (pos == std::string::npos) { - s.clear(); - return; - } - s.replace(0, pos, std::string()); + while (length > 0 && IsWhitespaceOrNull(p[length - 1])) + --length; + + const char *end = p + length; + while (p != end && IsWhitespaceOrNull(*p)) + ++p; - pos = s.find_last_not_of(ws); - if (pos != std::string::npos && pos != s.length()-1) - s.replace(pos + 1, std::string::npos, std::string()); + return std::string(p, end); } std::string -- cgit v1.2.3