From 59d38f876a22ef520c6b897a356e8fb8677481aa Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 7 Aug 2014 15:15:56 +0200 Subject: util/StringUtil: add StripRight() overload with "end" argument --- src/client/ClientRead.cxx | 7 +++---- src/fs/StandardDirectory.cxx | 5 +---- src/input/TextInputStream.cxx | 7 +++---- src/input/plugins/CurlInputPlugin.cxx | 9 +++------ src/util/StringUtil.cxx | 18 ++++++++++++++++++ src/util/StringUtil.hxx | 22 ++++++++++++++++++++++ 6 files changed, 50 insertions(+), 18 deletions(-) diff --git a/src/client/ClientRead.cxx b/src/client/ClientRead.cxx index ae8eda8bb..9cfb1271f 100644 --- a/src/client/ClientRead.cxx +++ b/src/client/ClientRead.cxx @@ -22,7 +22,7 @@ #include "Partition.hxx" #include "Instance.hxx" #include "event/Loop.hxx" -#include "util/CharUtil.hxx" +#include "util/StringUtil.hxx" #include @@ -39,11 +39,10 @@ Client::OnSocketInput(void *data, size_t length) BufferedSocket::ConsumeInput(newline + 1 - p); /* skip whitespace at the end of the line */ - while (newline > p && IsWhitespaceFast(newline[-1])) - --newline; + char *end = StripRight(p, newline); /* terminate the string at the end of the line */ - *newline = 0; + *end = 0; CommandResult result = client_process_line(*this, p); switch (result) { diff --git a/src/fs/StandardDirectory.cxx b/src/fs/StandardDirectory.cxx index c922b22d3..ea91399d1 100644 --- a/src/fs/StandardDirectory.cxx +++ b/src/fs/StandardDirectory.cxx @@ -40,7 +40,6 @@ #endif #ifdef USE_XDG -#include "util/CharUtil.hxx" #include "util/StringUtil.hxx" #include "TextFile.hxx" #include @@ -169,9 +168,7 @@ ParseConfigLine(char *line, const char *dir_name, AllocatedPath &result_dir) if (line_end == nullptr) return true; } else { - line_end = line + strlen(line); - while (line < line_end && IsWhitespaceNotNull(line_end[-1])) - --line_end; + line_end = StripRight(line, line + strlen(line)); } // check for empty result diff --git a/src/input/TextInputStream.cxx b/src/input/TextInputStream.cxx index 36aa5f408..177606d6d 100644 --- a/src/input/TextInputStream.cxx +++ b/src/input/TextInputStream.cxx @@ -20,7 +20,7 @@ #include "config.h" #include "TextInputStream.hxx" #include "InputStream.hxx" -#include "util/CharUtil.hxx" +#include "util/StringUtil.hxx" #include "util/Error.hxx" #include "Log.hxx" @@ -72,8 +72,7 @@ TextInputStream::ReadLine() buffer.Consume(p - src + 1); - while (p > src && IsWhitespaceFast(p[-1])) - --p; - *p = 0; + char *end = StripRight(src, p); + *end = 0; return src; } diff --git a/src/input/plugins/CurlInputPlugin.cxx b/src/input/plugins/CurlInputPlugin.cxx index 4d7671cc2..a174fcca6 100644 --- a/src/input/plugins/CurlInputPlugin.cxx +++ b/src/input/plugins/CurlInputPlugin.cxx @@ -31,7 +31,7 @@ #include "event/Call.hxx" #include "IOThread.hxx" #include "util/ASCII.hxx" -#include "util/CharUtil.hxx" +#include "util/StringUtil.hxx" #include "util/NumberParser.hxx" #include "util/CircularBuffer.hxx" #include "util/HugeAllocator.hxx" @@ -661,11 +661,8 @@ input_curl_headerfunction(void *ptr, size_t size, size_t nmemb, void *stream) /* strip the value */ - while (value < end && IsWhitespaceOrNull(*value)) - ++value; - - while (end > value && IsWhitespaceOrNull(end[-1])) - --end; + value = StripLeft(value, end); + end = StripRight(value, end); c.HeaderReceived(name, std::string(value, end)); return size; diff --git a/src/util/StringUtil.cxx b/src/util/StringUtil.cxx index 49a311a5f..bcade2b3b 100644 --- a/src/util/StringUtil.cxx +++ b/src/util/StringUtil.cxx @@ -35,6 +35,24 @@ StripLeft(const char *p) return p; } +const char * +StripLeft(const char *p, const char *end) +{ + while (p < end && IsWhitespaceOrNull(*p)) + ++p; + + return p; +} + +const char * +StripRight(const char *p, const char *end) +{ + while (end > p && IsWhitespaceOrNull(end[-1])) + --end; + + return end; +} + size_t StripRight(const char *p, size_t length) { diff --git a/src/util/StringUtil.hxx b/src/util/StringUtil.hxx index abed3b45b..9beda5441 100644 --- a/src/util/StringUtil.hxx +++ b/src/util/StringUtil.hxx @@ -39,6 +39,28 @@ StripLeft(char *p) return const_cast(StripLeft((const char *)p)); } +gcc_pure +const char * +StripLeft(const char *p, const char *end); + +/** + * Determine the string's end as if it was stripped on the right side. + */ +gcc_pure +const char * +StripRight(const char *p, const char *end); + +/** + * Determine the string's end as if it was stripped on the right side. + */ +gcc_pure +static inline char * +StripRight(char *p, char *end) +{ + return const_cast(StripRight((const char *)p, + (const char *)end)); +} + /** * Determine the string's length as if it was stripped on the right * side. -- cgit v1.2.3