diff options
author | Max Kellermann <max@duempel.org> | 2015-11-06 09:09:02 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2015-11-06 09:20:18 +0100 |
commit | 733989a284d2f7cce37634b12afad72cc019f659 (patch) | |
tree | b7e0fca51ba23f777fc6cd2d35278cc812988265 /src/util | |
parent | 493f74d94dd6ebf5f185ce68d17a0110516c7337 (diff) | |
download | mpd-733989a284d2f7cce37634b12afad72cc019f659.tar.gz mpd-733989a284d2f7cce37634b12afad72cc019f659.tar.xz mpd-733989a284d2f7cce37634b12afad72cc019f659.zip |
util/StringUtil: move comparison functions to StringCompare.cxx
Diffstat (limited to '')
-rw-r--r-- | src/util/StringCompare.cxx | 82 | ||||
-rw-r--r-- | src/util/StringCompare.hxx | 64 | ||||
-rw-r--r-- | src/util/StringUtil.cxx | 48 | ||||
-rw-r--r-- | src/util/StringUtil.hxx | 29 | ||||
-rw-r--r-- | src/util/WStringCompare.cxx (renamed from src/util/WStringUtil.cxx) | 5 | ||||
-rw-r--r-- | src/util/WStringCompare.hxx | 62 | ||||
-rw-r--r-- | src/util/WStringUtil.hxx | 52 |
7 files changed, 209 insertions, 133 deletions
diff --git a/src/util/StringCompare.cxx b/src/util/StringCompare.cxx new file mode 100644 index 000000000..a7e505d12 --- /dev/null +++ b/src/util/StringCompare.cxx @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2013-2015 Max Kellermann <max@duempel.org> + * + * 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 "StringCompare.hxx" +#include "StringAPI.hxx" + +#include <assert.h> +#include <string.h> + +bool +StringStartsWith(const char *haystack, const char *needle) +{ + const size_t length = strlen(needle); + return memcmp(haystack, needle, length) == 0; +} + +bool +StringEndsWith(const char *haystack, const char *needle) +{ + const size_t haystack_length = strlen(haystack); + const size_t needle_length = strlen(needle); + + return haystack_length >= needle_length && + memcmp(haystack + haystack_length - needle_length, + needle, needle_length) == 0; +} + +const char * +StringAfterPrefix(const char *string, const char *prefix) +{ +#if !CLANG_CHECK_VERSION(3,6) + /* disabled on clang due to -Wtautological-pointer-compare */ + assert(string != nullptr); + assert(prefix != nullptr); +#endif + + size_t prefix_length = strlen(prefix); + return StringIsEqual(string, prefix, prefix_length) + ? string + prefix_length + : nullptr; +} + +const char * +FindStringSuffix(const char *p, const char *suffix) +{ + const size_t p_length = strlen(p); + const size_t suffix_length = strlen(suffix); + + if (p_length < suffix_length) + return nullptr; + + const char *q = p + p_length - suffix_length; + return memcmp(q, suffix, suffix_length) == 0 + ? q + : nullptr; +} diff --git a/src/util/StringCompare.hxx b/src/util/StringCompare.hxx new file mode 100644 index 000000000..2c23b312f --- /dev/null +++ b/src/util/StringCompare.hxx @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2013-2015 Max Kellermann <max@duempel.org> + * + * 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_COMPARE_HXX +#define STRING_COMPARE_HXX + +#include "Compiler.h" + +#ifdef _UNICODE +#include "WStringCompare.hxx" +#endif + +gcc_pure +bool +StringStartsWith(const char *haystack, const char *needle); + +gcc_pure +bool +StringEndsWith(const char *haystack, const char *needle); + +/** + * Returns the portion of the string after a prefix. If the string + * does not begin with the specified prefix, this function returns + * nullptr. + */ +gcc_pure gcc_nonnull_all +const char * +StringAfterPrefix(const char *string, const char *prefix); + +/** + * Check if the given string ends with the specified suffix. If yes, + * returns the position of the suffix, and nullptr otherwise. + */ +gcc_pure +const char * +FindStringSuffix(const char *p, const char *suffix); + +#endif diff --git a/src/util/StringUtil.cxx b/src/util/StringUtil.cxx index b9c99eb4a..337476f18 100644 --- a/src/util/StringUtil.cxx +++ b/src/util/StringUtil.cxx @@ -27,54 +27,6 @@ #include <assert.h> #include <string.h> -bool -StringStartsWith(const char *haystack, const char *needle) -{ - const size_t length = strlen(needle); - return memcmp(haystack, needle, length) == 0; -} - -bool -StringEndsWith(const char *haystack, const char *needle) -{ - const size_t haystack_length = strlen(haystack); - const size_t needle_length = strlen(needle); - - return haystack_length >= needle_length && - memcmp(haystack + haystack_length - needle_length, - needle, needle_length) == 0; -} - -const char * -StringAfterPrefix(const char *string, const char *prefix) -{ -#if !CLANG_CHECK_VERSION(3,6) - /* disabled on clang due to -Wtautological-pointer-compare */ - assert(string != nullptr); - assert(prefix != nullptr); -#endif - - size_t prefix_length = strlen(prefix); - return StringIsEqual(string, prefix, prefix_length) - ? string + prefix_length - : nullptr; -} - -const char * -FindStringSuffix(const char *p, const char *suffix) -{ - const size_t p_length = strlen(p); - const size_t suffix_length = strlen(suffix); - - if (p_length < suffix_length) - return nullptr; - - const char *q = p + p_length - suffix_length; - return memcmp(q, suffix, suffix_length) == 0 - ? q - : nullptr; -} - char * CopyString(char *gcc_restrict dest, const char *gcc_restrict src, size_t size) { diff --git a/src/util/StringUtil.hxx b/src/util/StringUtil.hxx index 7e6dc4d61..9f30d363d 100644 --- a/src/util/StringUtil.hxx +++ b/src/util/StringUtil.hxx @@ -24,35 +24,6 @@ #include <stddef.h> -#ifdef _UNICODE -#include "WStringUtil.hxx" -#endif - -gcc_pure -bool -StringStartsWith(const char *haystack, const char *needle); - -gcc_pure -bool -StringEndsWith(const char *haystack, const char *needle); - -/** - * Returns the portion of the string after a prefix. If the string - * does not begin with the specified prefix, this function returns - * nullptr. - */ -gcc_pure gcc_nonnull_all -const char * -StringAfterPrefix(const char *string, const char *prefix); - -/** - * Check if the given string ends with the specified suffix. If yes, - * returns the position of the suffix, and nullptr otherwise. - */ -gcc_pure -const char * -FindStringSuffix(const char *p, const char *suffix); - /** * Copy a string. If the buffer is too small, then the string is * truncated. This is a safer version of strncpy(). diff --git a/src/util/WStringUtil.cxx b/src/util/WStringCompare.cxx index 19e4fc68d..77c3a8629 100644 --- a/src/util/WStringUtil.cxx +++ b/src/util/WStringCompare.cxx @@ -17,11 +17,8 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include "WStringUtil.hxx" +#include "WStringCompare.hxx" #include "WStringAPI.hxx" -#include "ASCII.hxx" - -#include <algorithm> #include <assert.h> #include <string.h> diff --git a/src/util/WStringCompare.hxx b/src/util/WStringCompare.hxx new file mode 100644 index 000000000..3547076ca --- /dev/null +++ b/src/util/WStringCompare.hxx @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2013-2015 Max Kellermann <max@duempel.org> + * + * 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 WSTRING_COMPARE_HXX +#define WSTRING_COMPARE_HXX + +#include "Compiler.h" + +#include <wchar.h> + +gcc_pure +bool +StringStartsWith(const wchar_t *haystack, const wchar_t *needle); + +gcc_pure +bool +StringEndsWith(const wchar_t *haystack, const wchar_t *needle); + +/** + * Returns the portion of the string after a prefix. If the string + * does not begin with the specified prefix, this function returns + * nullptr. + */ +gcc_nonnull_all +const wchar_t * +StringAfterPrefix(const wchar_t *string, const wchar_t *prefix); + +/** + * Check if the given string ends with the specified suffix. If yes, + * returns the position of the suffix, and nullptr otherwise. + */ +gcc_pure +const wchar_t * +FindStringSuffix(const wchar_t *p, const wchar_t *suffix); + +#endif diff --git a/src/util/WStringUtil.hxx b/src/util/WStringUtil.hxx deleted file mode 100644 index 3dde0162e..000000000 --- a/src/util/WStringUtil.hxx +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (C) 2003-2015 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef WSTRING_UTIL_HXX -#define WSTRING_UTIL_HXX - -#include "Compiler.h" - -#include <wchar.h> - -gcc_pure -bool -StringStartsWith(const wchar_t *haystack, const wchar_t *needle); - -gcc_pure -bool -StringEndsWith(const wchar_t *haystack, const wchar_t *needle); - -/** - * Returns the portion of the string after a prefix. If the string - * does not begin with the specified prefix, this function returns - * nullptr. - */ -gcc_nonnull_all -const wchar_t * -StringAfterPrefix(const wchar_t *string, const wchar_t *prefix); - -/** - * Check if the given string ends with the specified suffix. If yes, - * returns the position of the suffix, and nullptr otherwise. - */ -gcc_pure -const wchar_t * -FindStringSuffix(const wchar_t *p, const wchar_t *suffix); - -#endif |