util/StringUtil: move comparison functions to StringCompare.cxx

This commit is contained in:
Max Kellermann 2015-11-06 09:09:02 +01:00
parent 493f74d94d
commit 733989a284
31 changed files with 236 additions and 156 deletions

View File

@ -406,7 +406,8 @@ libutil_a_SOURCES = \
src/util/StringView.cxx src/util/StringView.hxx \
src/util/AllocatedString.cxx src/util/AllocatedString.hxx \
src/util/StringUtil.cxx src/util/StringUtil.hxx \
src/util/WStringUtil.cxx src/util/WStringUtil.hxx \
src/util/StringCompare.cxx src/util/StringCompare.hxx \
src/util/WStringCompare.cxx src/util/WStringCompare.hxx \
src/util/StringAPI.hxx \
src/util/WStringAPI.hxx \
src/util/DivideString.cxx src/util/DivideString.hxx \
@ -1761,10 +1762,10 @@ test_run_gzip_SOURCES = test/run_gzip.cxx
test_run_gunzip_SOURCES = test/run_gunzip.cxx \
src/Log.cxx src/LogBackend.cxx
test_run_gunzip_LDADD = \
libutil.a \
$(FS_LIBS) \
$(ICU_LDADD) \
libsystem.a
libsystem.a \
libutil.a
endif

View File

@ -23,7 +23,7 @@
#include "fs/AllocatedPath.hxx"
#include "ls.hxx"
#include "util/UriUtil.hxx"
#include "util/StringUtil.hxx"
#include "util/StringCompare.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"

View File

@ -41,7 +41,7 @@
#include "fs/FileInfo.hxx"
#include "fs/DirectoryReader.hxx"
#include "util/Macros.hxx"
#include "util/StringUtil.hxx"
#include "util/StringCompare.hxx"
#include "util/UriUtil.hxx"
#include "util/Error.hxx"

View File

@ -28,7 +28,7 @@
#include "tag/Tag.hxx"
#include "tag/Settings.hxx"
#include "fs/Charset.hxx"
#include "util/StringUtil.hxx"
#include "util/StringCompare.hxx"
#include "util/Error.hxx"
#include "Log.hxx"

View File

@ -26,7 +26,7 @@
#include "PlaylistDatabase.hxx"
#include "fs/io/TextFile.hxx"
#include "fs/io/BufferedOutputStream.hxx"
#include "util/StringUtil.hxx"
#include "util/StringCompare.hxx"
#include "util/NumberParser.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"

View File

@ -28,7 +28,7 @@
#include "tag/ReplayGain.hxx"
#include "tag/MixRamp.hxx"
#include "CheckAudioFormat.hxx"
#include "util/StringUtil.hxx"
#include "util/StringCompare.hxx"
#include "util/ASCII.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"

View File

@ -42,6 +42,7 @@
#ifdef USE_XDG
#include "util/Error.hxx"
#include "util/StringUtil.hxx"
#include "util/StringCompare.hxx"
#include "io/TextFile.hxx"
#include <string.h>
#include <utility>

View File

@ -19,7 +19,7 @@
#include "config.h"
#include "Traits.hxx"
#include "util/StringUtil.hxx"
#include "util/StringCompare.hxx"
#include <string.h>

View File

@ -20,7 +20,7 @@
#include "config.h"
#include "InputStream.hxx"
#include "thread/Cond.hxx"
#include "util/StringUtil.hxx"
#include "util/StringCompare.hxx"
#include <assert.h>

View File

@ -30,7 +30,7 @@
#include "../InputStream.hxx"
#include "util/Domain.hxx"
#include "util/Error.hxx"
#include "util/StringUtil.hxx"
#include "util/StringCompare.hxx"
#include "util/ReusableArray.hxx"
#include "Log.hxx"

View File

@ -26,6 +26,7 @@
#include "../InputStream.hxx"
#include "../InputPlugin.hxx"
#include "util/StringUtil.hxx"
#include "util/StringCompare.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "system/ByteOrder.hxx"

View File

@ -27,7 +27,7 @@
#include "lib/ffmpeg/Error.hxx"
#include "../InputStream.hxx"
#include "../InputPlugin.hxx"
#include "util/StringUtil.hxx"
#include "util/StringCompare.hxx"
#include "util/Error.hxx"
extern "C" {

View File

@ -21,7 +21,7 @@
#include "MmsInputPlugin.hxx"
#include "input/ThreadInputStream.hxx"
#include "input/InputPlugin.hxx"
#include "util/StringUtil.hxx"
#include "util/StringCompare.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"

View File

@ -25,7 +25,7 @@
#include "lib/nfs/Glue.hxx"
#include "lib/nfs/FileReader.hxx"
#include "util/HugeAllocator.hxx"
#include "util/StringUtil.hxx"
#include "util/StringCompare.hxx"
#include "util/Error.hxx"
#include <string.h>

View File

@ -23,7 +23,7 @@
#include "lib/smbclient/Mutex.hxx"
#include "../InputStream.hxx"
#include "../InputPlugin.hxx"
#include "util/StringUtil.hxx"
#include "util/StringCompare.hxx"
#include "util/Error.hxx"
#include <libsmbclient.h>

View File

@ -25,7 +25,7 @@
#include "Domain.hxx"
#include "event/Call.hxx"
#include "IOThread.hxx"
#include "util/StringUtil.hxx"
#include "util/StringCompare.hxx"
#include "util/Error.hxx"
#include <utility>

View File

@ -20,7 +20,7 @@
#include "config.h"
#include "ls.hxx"
#include "client/Response.hxx"
#include "util/StringUtil.hxx"
#include "util/StringCompare.hxx"
#include "util/UriUtil.hxx"
#include <assert.h>

View File

@ -21,7 +21,7 @@
#include "Volume.hxx"
#include "output/MultipleOutputs.hxx"
#include "Idle.hxx"
#include "util/StringUtil.hxx"
#include "util/StringCompare.hxx"
#include "util/Domain.hxx"
#include "system/PeriodClock.hxx"
#include "fs/io/BufferedOutputStream.hxx"

View File

@ -29,7 +29,7 @@
#include "Domain.hxx"
#include "Log.hxx"
#include "fs/io/BufferedOutputStream.hxx"
#include "util/StringUtil.hxx"
#include "util/StringCompare.hxx"
#include <assert.h>
#include <stdlib.h>

View File

@ -26,7 +26,7 @@
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "util/Macros.hxx"
#include "util/StringUtil.hxx"
#include "util/StringCompare.hxx"
#include <stdlib.h>
#include <string.h>

View File

@ -25,6 +25,7 @@
#include "tag/Tag.hxx"
#include "tag/TagBuilder.hxx"
#include "util/StringUtil.hxx"
#include "util/StringCompare.hxx"
#include "input/TextInputStream.hxx"
#include <string.h>

View File

@ -24,7 +24,7 @@
#include "config/Block.hxx"
#include "input/InputStream.hxx"
#include "tag/TagBuilder.hxx"
#include "util/StringUtil.hxx"
#include "util/StringCompare.hxx"
#include "util/Alloc.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"

View File

@ -33,7 +33,7 @@
#include "config/ConfigGlobal.hxx"
#include "config/ConfigOption.hxx"
#include "util/CharUtil.hxx"
#include "util/StringUtil.hxx"
#include "util/StringCompare.hxx"
#include "Log.hxx"
#include <string.h>

View File

@ -27,7 +27,7 @@
#include "playlist/PlaylistSong.hxx"
#include "fs/io/TextFile.hxx"
#include "fs/io/BufferedOutputStream.hxx"
#include "util/StringUtil.hxx"
#include "util/StringCompare.hxx"
#include "util/Error.hxx"
#include "fs/Traits.hxx"
#include "Log.hxx"

View File

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

View File

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

View File

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

View File

@ -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().

View File

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

View File

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

View File

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