Commit 59d38f87 authored by Max Kellermann's avatar Max Kellermann

util/StringUtil: add StripRight() overload with "end" argument

parent 5c5c6a96
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
#include "Partition.hxx" #include "Partition.hxx"
#include "Instance.hxx" #include "Instance.hxx"
#include "event/Loop.hxx" #include "event/Loop.hxx"
#include "util/CharUtil.hxx" #include "util/StringUtil.hxx"
#include <string.h> #include <string.h>
...@@ -39,11 +39,10 @@ Client::OnSocketInput(void *data, size_t length) ...@@ -39,11 +39,10 @@ Client::OnSocketInput(void *data, size_t length)
BufferedSocket::ConsumeInput(newline + 1 - p); BufferedSocket::ConsumeInput(newline + 1 - p);
/* skip whitespace at the end of the line */ /* skip whitespace at the end of the line */
while (newline > p && IsWhitespaceFast(newline[-1])) char *end = StripRight(p, newline);
--newline;
/* terminate the string at the end of the line */ /* terminate the string at the end of the line */
*newline = 0; *end = 0;
CommandResult result = client_process_line(*this, p); CommandResult result = client_process_line(*this, p);
switch (result) { switch (result) {
......
...@@ -40,7 +40,6 @@ ...@@ -40,7 +40,6 @@
#endif #endif
#ifdef USE_XDG #ifdef USE_XDG
#include "util/CharUtil.hxx"
#include "util/StringUtil.hxx" #include "util/StringUtil.hxx"
#include "TextFile.hxx" #include "TextFile.hxx"
#include <string.h> #include <string.h>
...@@ -169,9 +168,7 @@ ParseConfigLine(char *line, const char *dir_name, AllocatedPath &result_dir) ...@@ -169,9 +168,7 @@ ParseConfigLine(char *line, const char *dir_name, AllocatedPath &result_dir)
if (line_end == nullptr) if (line_end == nullptr)
return true; return true;
} else { } else {
line_end = line + strlen(line); line_end = StripRight(line, line + strlen(line));
while (line < line_end && IsWhitespaceNotNull(line_end[-1]))
--line_end;
} }
// check for empty result // check for empty result
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
#include "config.h" #include "config.h"
#include "TextInputStream.hxx" #include "TextInputStream.hxx"
#include "InputStream.hxx" #include "InputStream.hxx"
#include "util/CharUtil.hxx" #include "util/StringUtil.hxx"
#include "util/Error.hxx" #include "util/Error.hxx"
#include "Log.hxx" #include "Log.hxx"
...@@ -72,8 +72,7 @@ TextInputStream::ReadLine() ...@@ -72,8 +72,7 @@ TextInputStream::ReadLine()
buffer.Consume(p - src + 1); buffer.Consume(p - src + 1);
while (p > src && IsWhitespaceFast(p[-1])) char *end = StripRight(src, p);
--p; *end = 0;
*p = 0;
return src; return src;
} }
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
#include "event/Call.hxx" #include "event/Call.hxx"
#include "IOThread.hxx" #include "IOThread.hxx"
#include "util/ASCII.hxx" #include "util/ASCII.hxx"
#include "util/CharUtil.hxx" #include "util/StringUtil.hxx"
#include "util/NumberParser.hxx" #include "util/NumberParser.hxx"
#include "util/CircularBuffer.hxx" #include "util/CircularBuffer.hxx"
#include "util/HugeAllocator.hxx" #include "util/HugeAllocator.hxx"
...@@ -661,11 +661,8 @@ input_curl_headerfunction(void *ptr, size_t size, size_t nmemb, void *stream) ...@@ -661,11 +661,8 @@ input_curl_headerfunction(void *ptr, size_t size, size_t nmemb, void *stream)
/* strip the value */ /* strip the value */
while (value < end && IsWhitespaceOrNull(*value)) value = StripLeft(value, end);
++value; end = StripRight(value, end);
while (end > value && IsWhitespaceOrNull(end[-1]))
--end;
c.HeaderReceived(name, std::string(value, end)); c.HeaderReceived(name, std::string(value, end));
return size; return size;
......
...@@ -35,6 +35,24 @@ StripLeft(const char *p) ...@@ -35,6 +35,24 @@ StripLeft(const char *p)
return 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 size_t
StripRight(const char *p, size_t length) StripRight(const char *p, size_t length)
{ {
......
...@@ -39,6 +39,28 @@ StripLeft(char *p) ...@@ -39,6 +39,28 @@ StripLeft(char *p)
return const_cast<char *>(StripLeft((const char *)p)); return const_cast<char *>(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<char *>(StripRight((const char *)p,
(const char *)end));
}
/** /**
* Determine the string's length as if it was stripped on the right * Determine the string's length as if it was stripped on the right
* side. * side.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment