From f860a2fbd693cc9ce166cdba7491cdd5071ca34a Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 7 Aug 2014 14:44:31 +0200 Subject: util/StringUtil: move code to StripRight() --- src/util/StringUtil.cxx | 23 +++++++++++++++++------ src/util/StringUtil.hxx | 14 ++++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) (limited to 'src/util') diff --git a/src/util/StringUtil.cxx b/src/util/StringUtil.cxx index 153846e9b..e68c98444 100644 --- a/src/util/StringUtil.cxx +++ b/src/util/StringUtil.cxx @@ -35,17 +35,28 @@ StripLeft(const char *p) return p; } -char * -Strip(char *p) +size_t +StripRight(const char *p, size_t length) { - p = StripLeft(p); - - size_t length = strlen(p); while (length > 0 && IsWhitespaceNotNull(p[length - 1])) --length; - p[length] = 0; + return length; +} + +void +StripRight(char *p) +{ + size_t old_length = strlen(p); + size_t new_length = StripRight(p, old_length); + p[new_length] = 0; +} +char * +Strip(char *p) +{ + p = StripLeft(p); + StripRight(p); return p; } diff --git a/src/util/StringUtil.hxx b/src/util/StringUtil.hxx index 6bccbeffe..abed3b45b 100644 --- a/src/util/StringUtil.hxx +++ b/src/util/StringUtil.hxx @@ -39,6 +39,20 @@ StripLeft(char *p) return const_cast(StripLeft((const char *)p)); } +/** + * Determine the string's length as if it was stripped on the right + * side. + */ +gcc_pure +size_t +StripRight(const char *p, size_t length); + +/** + * Strip trailing whitespace by null-terminating the string. + */ +void +StripRight(char *p); + /** * Skip whitespace at the beginning and terminate the string after the * last non-whitespace character. -- cgit v1.2.3