db/upnp/Util: trimstring() constructs string from buffer

Reduce overhead by omitting the part of the buffer that consists only
of whitespace.
This commit is contained in:
Max Kellermann 2014-01-18 13:36:50 +01:00
parent f3b4ddee6c
commit 55737e4ff6
4 changed files with 15 additions and 17 deletions

View File

@ -56,8 +56,7 @@ protected:
}
virtual void CharacterData(const XML_Char *s, int len) {
std::string str(s, len);
trimstring(str);
std::string str = trimstring(s, len);
switch (m_path.back()[0]) {
case 'c':
if (!m_path.back().compare("controlURL"))

View File

@ -156,8 +156,7 @@ protected:
virtual void CharacterData(const XML_Char *s, int len)
{
std::string str(s, len);
trimstring(str);
std::string str = trimstring(s, len);
TagType type = tag_table_lookup(upnp_tags,
m_path.back().c_str());

View File

@ -18,6 +18,7 @@
*/
#include "Util.hxx"
#include "util/CharUtil.hxx"
#include <string>
#include <map>
@ -27,19 +28,17 @@
#include <upnp/ixml.h>
/** 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;
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());
const char *end = p + length;
while (p != end && IsWhitespaceOrNull(*p))
++p;
return std::string(p, end);
}
std::string

View File

@ -28,8 +28,9 @@
std::string
caturl(const std::string& s1, const std::string& s2);
void
trimstring(std::string &s, const char *ws = " \t\n");
gcc_pure
std::string
trimstring(const char *p, size_t length);
std::string
path_getfather(const std::string &s);