util/StringUtil: move comparison functions to StringCompare.cxx
This commit is contained in:
82
src/util/StringCompare.cxx
Normal file
82
src/util/StringCompare.cxx
Normal 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;
|
||||
}
|
64
src/util/StringCompare.hxx
Normal file
64
src/util/StringCompare.hxx
Normal 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
|
@@ -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)
|
||||
{
|
||||
|
@@ -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().
|
||||
|
@@ -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>
|
62
src/util/WStringCompare.hxx
Normal file
62
src/util/WStringCompare.hxx
Normal 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
|
@@ -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
|
Reference in New Issue
Block a user