diff --git a/Makefile.am b/Makefile.am index d6b70219b..70be090b9 100644 --- a/Makefile.am +++ b/Makefile.am @@ -427,6 +427,7 @@ libutil_a_SOURCES = \ src/util/ConcatString.hxx \ src/util/AllocatedString.cxx src/util/AllocatedString.hxx \ src/util/TruncateString.cxx src/util/TruncateString.hxx \ + src/util/StringStrip.cxx src/util/StringStrip.hxx \ src/util/StringUtil.cxx src/util/StringUtil.hxx \ src/util/StringCompare.cxx src/util/StringCompare.hxx \ src/util/WStringCompare.cxx src/util/WStringCompare.hxx \ diff --git a/src/LogBackend.cxx b/src/LogBackend.cxx index 940d28c1b..433462cf9 100644 --- a/src/LogBackend.cxx +++ b/src/LogBackend.cxx @@ -21,7 +21,7 @@ #include "LogBackend.hxx" #include "Log.hxx" #include "util/Domain.hxx" -#include "util/StringUtil.hxx" +#include "util/StringStrip.hxx" #include #include diff --git a/src/PlaylistDatabase.cxx b/src/PlaylistDatabase.cxx index 348b3ac86..1075d7024 100644 --- a/src/PlaylistDatabase.cxx +++ b/src/PlaylistDatabase.cxx @@ -22,7 +22,7 @@ #include "db/PlaylistVector.hxx" #include "fs/io/TextFile.hxx" #include "fs/io/BufferedOutputStream.hxx" -#include "util/StringUtil.hxx" +#include "util/StringStrip.hxx" #include "util/RuntimeError.hxx" #include diff --git a/src/SongSave.cxx b/src/SongSave.cxx index 983fd7586..f551b7670 100644 --- a/src/SongSave.cxx +++ b/src/SongSave.cxx @@ -27,7 +27,7 @@ #include "tag/ParseName.hxx" #include "tag/Tag.hxx" #include "tag/Builder.hxx" -#include "util/StringUtil.hxx" +#include "util/StringStrip.hxx" #include "util/RuntimeError.hxx" #include diff --git a/src/client/ClientRead.cxx b/src/client/ClientRead.cxx index 552b312bf..a598cf812 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/StringUtil.hxx" +#include "util/StringStrip.hxx" #include diff --git a/src/config/ConfigFile.cxx b/src/config/ConfigFile.cxx index 5223dac99..6cdd22009 100644 --- a/src/config/ConfigFile.cxx +++ b/src/config/ConfigFile.cxx @@ -24,7 +24,7 @@ #include "Block.hxx" #include "ConfigTemplates.hxx" #include "util/Tokenizer.hxx" -#include "util/StringUtil.hxx" +#include "util/StringStrip.hxx" #include "util/Domain.hxx" #include "util/RuntimeError.hxx" #include "fs/Path.hxx" diff --git a/src/db/update/ExcludeList.cxx b/src/db/update/ExcludeList.cxx index e54999f8c..82bf9be28 100644 --- a/src/db/update/ExcludeList.cxx +++ b/src/db/update/ExcludeList.cxx @@ -28,6 +28,7 @@ #include "fs/NarrowPath.hxx" #include "fs/io/TextFile.hxx" #include "system/Error.hxx" +#include "util/StringStrip.hxx" #include "Log.hxx" #include diff --git a/src/filter/plugins/RouteFilterPlugin.cxx b/src/filter/plugins/RouteFilterPlugin.cxx index 719d35720..682dd06a3 100644 --- a/src/filter/plugins/RouteFilterPlugin.cxx +++ b/src/filter/plugins/RouteFilterPlugin.cxx @@ -48,7 +48,7 @@ #include "filter/FilterRegistry.hxx" #include "pcm/PcmBuffer.hxx" #include "pcm/Silence.hxx" -#include "util/StringUtil.hxx" +#include "util/StringStrip.hxx" #include "util/RuntimeError.hxx" #include "util/ConstBuffer.hxx" #include "util/WritableBuffer.hxx" diff --git a/src/fs/StandardDirectory.cxx b/src/fs/StandardDirectory.cxx index 1ea92cf2c..41b5fee7a 100644 --- a/src/fs/StandardDirectory.cxx +++ b/src/fs/StandardDirectory.cxx @@ -39,7 +39,7 @@ #endif #ifdef USE_XDG -#include "util/StringUtil.hxx" +#include "util/StringStrip.hxx" #include "util/StringCompare.hxx" #include "io/TextFile.hxx" #include diff --git a/src/lib/curl/Request.cxx b/src/lib/curl/Request.cxx index d8301cbba..c51ab6282 100644 --- a/src/lib/curl/Request.cxx +++ b/src/lib/curl/Request.cxx @@ -33,7 +33,7 @@ #include "Version.hxx" #include "Handler.hxx" #include "util/RuntimeError.hxx" -#include "util/StringUtil.hxx" +#include "util/StringStrip.hxx" #include "util/StringView.hxx" #include "util/CharUtil.hxx" diff --git a/src/playlist/cue/CueParser.cxx b/src/playlist/cue/CueParser.cxx index 2b4c80383..b04eede76 100644 --- a/src/playlist/cue/CueParser.cxx +++ b/src/playlist/cue/CueParser.cxx @@ -21,7 +21,7 @@ #include "CueParser.hxx" #include "tag/ParseName.hxx" #include "util/Alloc.hxx" -#include "util/StringUtil.hxx" +#include "util/StringStrip.hxx" #include "util/CharUtil.hxx" #include diff --git a/src/playlist/plugins/ExtM3uPlaylistPlugin.cxx b/src/playlist/plugins/ExtM3uPlaylistPlugin.cxx index c275c1f3f..627597be0 100644 --- a/src/playlist/plugins/ExtM3uPlaylistPlugin.cxx +++ b/src/playlist/plugins/ExtM3uPlaylistPlugin.cxx @@ -24,7 +24,7 @@ #include "DetachedSong.hxx" #include "tag/Tag.hxx" #include "tag/Builder.hxx" -#include "util/StringUtil.hxx" +#include "util/StringStrip.hxx" #include "util/StringCompare.hxx" #include "input/TextInputStream.hxx" #include "input/InputStream.hxx" diff --git a/src/playlist/plugins/M3uPlaylistPlugin.cxx b/src/playlist/plugins/M3uPlaylistPlugin.cxx index b76181805..6980cda21 100644 --- a/src/playlist/plugins/M3uPlaylistPlugin.cxx +++ b/src/playlist/plugins/M3uPlaylistPlugin.cxx @@ -22,8 +22,8 @@ #include "../PlaylistPlugin.hxx" #include "../SongEnumerator.hxx" #include "DetachedSong.hxx" -#include "util/StringUtil.hxx" #include "input/TextInputStream.hxx" +#include "util/StringStrip.hxx" class M3uPlaylist final : public SongEnumerator { TextInputStream tis; diff --git a/src/playlist/plugins/PlsPlaylistPlugin.cxx b/src/playlist/plugins/PlsPlaylistPlugin.cxx index 0013d043b..e8cc443cd 100644 --- a/src/playlist/plugins/PlsPlaylistPlugin.cxx +++ b/src/playlist/plugins/PlsPlaylistPlugin.cxx @@ -26,7 +26,7 @@ #include "DetachedSong.hxx" #include "tag/Builder.hxx" #include "util/ASCII.hxx" -#include "util/StringUtil.hxx" +#include "util/StringStrip.hxx" #include "util/DivideString.hxx" #include diff --git a/src/tag/Config.cxx b/src/tag/Config.cxx index 4f2711e6d..619b94158 100644 --- a/src/tag/Config.cxx +++ b/src/tag/Config.cxx @@ -26,7 +26,7 @@ #include "system/FatalError.hxx" #include "util/Alloc.hxx" #include "util/ASCII.hxx" -#include "util/StringUtil.hxx" +#include "util/StringStrip.hxx" #include diff --git a/src/tag/Id3Scan.cxx b/src/tag/Id3Scan.cxx index 9330fbff9..670eadaf6 100644 --- a/src/tag/Id3Scan.cxx +++ b/src/tag/Id3Scan.cxx @@ -25,7 +25,7 @@ #include "Builder.hxx" #include "util/Alloc.hxx" #include "util/ScopeExit.hxx" -#include "util/StringUtil.hxx" +#include "util/StringStrip.hxx" #include "Log.hxx" #include diff --git a/src/util/DivideString.cxx b/src/util/DivideString.cxx index 49f6b94ad..7e7d00610 100644 --- a/src/util/DivideString.cxx +++ b/src/util/DivideString.cxx @@ -18,7 +18,7 @@ */ #include "DivideString.hxx" -#include "StringUtil.hxx" +#include "StringStrip.hxx" #include diff --git a/src/util/SplitString.cxx b/src/util/SplitString.cxx index 91be8e801..e7c50dc26 100644 --- a/src/util/SplitString.cxx +++ b/src/util/SplitString.cxx @@ -29,7 +29,7 @@ #include "SplitString.hxx" #include "IterableSplitString.hxx" -#include "StringUtil.hxx" +#include "StringStrip.hxx" #include diff --git a/src/util/StringStrip.cxx b/src/util/StringStrip.cxx new file mode 100644 index 000000000..295f4ef1c --- /dev/null +++ b/src/util/StringStrip.cxx @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2009-2017 Max Kellermann + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "StringStrip.hxx" +#include "CharUtil.hxx" + +#include + +const char * +StripLeft(const char *p) noexcept +{ + while (IsWhitespaceNotNull(*p)) + ++p; + + return p; +} + +const char * +StripLeft(const char *p, const char *end) noexcept +{ + while (p < end && IsWhitespaceOrNull(*p)) + ++p; + + return p; +} + +const char * +StripRight(const char *p, const char *end) noexcept +{ + while (end > p && IsWhitespaceOrNull(end[-1])) + --end; + + return end; +} + +size_t +StripRight(const char *p, size_t length) noexcept +{ + while (length > 0 && IsWhitespaceOrNull(p[length - 1])) + --length; + + return length; +} + +void +StripRight(char *p) noexcept +{ + size_t old_length = strlen(p); + size_t new_length = StripRight(p, old_length); + p[new_length] = 0; +} + +char * +Strip(char *p) noexcept +{ + p = StripLeft(p); + StripRight(p); + return p; +} diff --git a/src/util/StringStrip.hxx b/src/util/StringStrip.hxx new file mode 100644 index 000000000..033c1a05a --- /dev/null +++ b/src/util/StringStrip.hxx @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2009-2017 Max Kellermann + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef STRING_STRIP_HXX +#define STRING_STRIP_HXX + +#include "Compiler.h" + +#include + +/** + * Returns a pointer to the first non-whitespace character in the + * string, or to the end of the string. + */ +gcc_pure +const char * +StripLeft(const char *p) noexcept; + +gcc_pure +static inline char * +StripLeft(char *p) noexcept +{ + return const_cast(StripLeft((const char *)p)); +} + +gcc_pure +const char * +StripLeft(const char *p, const char *end) noexcept; + +/** + * 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) noexcept; + +/** + * Determine the string's end as if it was stripped on the right side. + */ +gcc_pure +static inline char * +StripRight(char *p, char *end) noexcept +{ + return const_cast(StripRight((const char *)p, + (const char *)end)); +} + +/** + * 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) noexcept; + +/** + * Strip trailing whitespace by null-terminating the string. + */ +void +StripRight(char *p) noexcept; + +/** + * Skip whitespace at the beginning and terminate the string after the + * last non-whitespace character. + */ +char * +Strip(char *p) noexcept; + +#endif diff --git a/src/util/StringUtil.cxx b/src/util/StringUtil.cxx index db6e13e38..c74a03ec7 100644 --- a/src/util/StringUtil.cxx +++ b/src/util/StringUtil.cxx @@ -22,59 +22,6 @@ #include "ASCII.hxx" #include -#include - -const char * -StripLeft(const char *p) noexcept -{ - while (IsWhitespaceNotNull(*p)) - ++p; - - return p; -} - -const char * -StripLeft(const char *p, const char *end) noexcept -{ - while (p < end && IsWhitespaceOrNull(*p)) - ++p; - - return p; -} - -const char * -StripRight(const char *p, const char *end) noexcept -{ - while (end > p && IsWhitespaceOrNull(end[-1])) - --end; - - return end; -} - -size_t -StripRight(const char *p, size_t length) noexcept -{ - while (length > 0 && IsWhitespaceOrNull(p[length - 1])) - --length; - - return length; -} - -void -StripRight(char *p) noexcept -{ - size_t old_length = strlen(p); - size_t new_length = StripRight(p, old_length); - p[new_length] = 0; -} - -char * -Strip(char *p) noexcept -{ - p = StripLeft(p); - StripRight(p); - return p; -} bool StringArrayContainsCase(const char *const*haystack, diff --git a/src/util/StringUtil.hxx b/src/util/StringUtil.hxx index bec90e2bd..df1ca49d1 100644 --- a/src/util/StringUtil.hxx +++ b/src/util/StringUtil.hxx @@ -24,64 +24,6 @@ #include -/** - * Returns a pointer to the first non-whitespace character in the - * string, or to the end of the string. - */ -gcc_pure -const char * -StripLeft(const char *p) noexcept; - -gcc_pure -static inline char * -StripLeft(char *p) noexcept -{ - return const_cast(StripLeft((const char *)p)); -} - -gcc_pure -const char * -StripLeft(const char *p, const char *end) noexcept; - -/** - * 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) noexcept; - -/** - * Determine the string's end as if it was stripped on the right side. - */ -gcc_pure -static inline char * -StripRight(char *p, char *end) noexcept -{ - return const_cast(StripRight((const char *)p, - (const char *)end)); -} - -/** - * 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) noexcept; - -/** - * Strip trailing whitespace by null-terminating the string. - */ -void -StripRight(char *p) noexcept; - -/** - * Skip whitespace at the beginning and terminate the string after the - * last non-whitespace character. - */ -char * -Strip(char *p) noexcept; - /** * Checks whether a string array contains the specified string. * diff --git a/src/util/Tokenizer.cxx b/src/util/Tokenizer.cxx index 12f020259..100ffd3ed 100644 --- a/src/util/Tokenizer.cxx +++ b/src/util/Tokenizer.cxx @@ -30,7 +30,7 @@ #include "config.h" #include "Tokenizer.hxx" #include "CharUtil.hxx" -#include "StringUtil.hxx" +#include "StringStrip.hxx" #include