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

This commit is contained in:
Max Kellermann 2014-08-07 15:15:56 +02:00
parent 5c5c6a965c
commit 59d38f876a
6 changed files with 50 additions and 18 deletions

View File

@ -22,7 +22,7 @@
#include "Partition.hxx"
#include "Instance.hxx"
#include "event/Loop.hxx"
#include "util/CharUtil.hxx"
#include "util/StringUtil.hxx"
#include <string.h>
@ -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) {

View File

@ -40,7 +40,6 @@
#endif
#ifdef USE_XDG
#include "util/CharUtil.hxx"
#include "util/StringUtil.hxx"
#include "TextFile.hxx"
#include <string.h>
@ -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

View File

@ -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;
}

View File

@ -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;

View File

@ -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)
{

View File

@ -39,6 +39,28 @@ StripLeft(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
* side.