util/StringUtil: move CopyString() to TruncateString.cxx
This commit is contained in:
parent
b04679b429
commit
4b94ae8040
@ -426,6 +426,7 @@ libutil_a_SOURCES = \
|
|||||||
src/util/StringView.cxx src/util/StringView.hxx \
|
src/util/StringView.cxx src/util/StringView.hxx \
|
||||||
src/util/ConcatString.hxx \
|
src/util/ConcatString.hxx \
|
||||||
src/util/AllocatedString.cxx src/util/AllocatedString.hxx \
|
src/util/AllocatedString.cxx src/util/AllocatedString.hxx \
|
||||||
|
src/util/TruncateString.cxx src/util/TruncateString.hxx \
|
||||||
src/util/StringUtil.cxx src/util/StringUtil.hxx \
|
src/util/StringUtil.cxx src/util/StringUtil.hxx \
|
||||||
src/util/StringCompare.cxx src/util/StringCompare.hxx \
|
src/util/StringCompare.cxx src/util/StringCompare.hxx \
|
||||||
src/util/WStringCompare.cxx src/util/WStringCompare.hxx \
|
src/util/WStringCompare.cxx src/util/WStringCompare.hxx \
|
||||||
|
@ -25,7 +25,7 @@
|
|||||||
#include "CdioParanoiaInputPlugin.hxx"
|
#include "CdioParanoiaInputPlugin.hxx"
|
||||||
#include "../InputStream.hxx"
|
#include "../InputStream.hxx"
|
||||||
#include "../InputPlugin.hxx"
|
#include "../InputPlugin.hxx"
|
||||||
#include "util/StringUtil.hxx"
|
#include "util/TruncateString.hxx"
|
||||||
#include "util/StringCompare.hxx"
|
#include "util/StringCompare.hxx"
|
||||||
#include "util/RuntimeError.hxx"
|
#include "util/RuntimeError.hxx"
|
||||||
#include "util/Domain.hxx"
|
#include "util/Domain.hxx"
|
||||||
|
@ -28,7 +28,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "String.hxx"
|
#include "String.hxx"
|
||||||
#include "util/StringUtil.hxx"
|
#include "util/TruncateString.hxx"
|
||||||
|
|
||||||
char *
|
char *
|
||||||
Java::String::CopyTo(JNIEnv *env, jstring value,
|
Java::String::CopyTo(JNIEnv *env, jstring value,
|
||||||
|
@ -22,7 +22,7 @@
|
|||||||
#include "tag/Tag.hxx"
|
#include "tag/Tag.hxx"
|
||||||
#include "util/FormatString.hxx"
|
#include "util/FormatString.hxx"
|
||||||
#include "util/AllocatedString.hxx"
|
#include "util/AllocatedString.hxx"
|
||||||
#include "util/StringUtil.hxx"
|
#include "util/TruncateString.hxx"
|
||||||
#include "util/Macros.hxx"
|
#include "util/Macros.hxx"
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
@ -22,7 +22,7 @@
|
|||||||
#include "Tag.hxx"
|
#include "Tag.hxx"
|
||||||
#include "ParseName.hxx"
|
#include "ParseName.hxx"
|
||||||
#include "util/format.h"
|
#include "util/format.h"
|
||||||
#include "util/StringUtil.hxx"
|
#include "util/TruncateString.hxx"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
|
@ -21,24 +21,9 @@
|
|||||||
#include "CharUtil.hxx"
|
#include "CharUtil.hxx"
|
||||||
#include "ASCII.hxx"
|
#include "ASCII.hxx"
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
char *
|
|
||||||
CopyString(char *gcc_restrict dest, const char *gcc_restrict src,
|
|
||||||
size_t size) noexcept
|
|
||||||
{
|
|
||||||
size_t length = strlen(src);
|
|
||||||
if (length >= size)
|
|
||||||
length = size - 1;
|
|
||||||
|
|
||||||
char *p = std::copy_n(src, length, dest);
|
|
||||||
*p = '\0';
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
|
|
||||||
const char *
|
const char *
|
||||||
StripLeft(const char *p) noexcept
|
StripLeft(const char *p) noexcept
|
||||||
{
|
{
|
||||||
|
@ -24,18 +24,6 @@
|
|||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
/**
|
|
||||||
* Copy a string. If the buffer is too small, then the string is
|
|
||||||
* truncated. This is a safer version of strncpy().
|
|
||||||
*
|
|
||||||
* @param size the size of the destination buffer (including the null
|
|
||||||
* terminator)
|
|
||||||
* @return a pointer to the null terminator
|
|
||||||
*/
|
|
||||||
gcc_nonnull_all
|
|
||||||
char *
|
|
||||||
CopyString(char *dest, const char *src, size_t size) noexcept;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a pointer to the first non-whitespace character in the
|
* Returns a pointer to the first non-whitespace character in the
|
||||||
* string, or to the end of the string.
|
* string, or to the end of the string.
|
||||||
|
47
src/util/TruncateString.cxx
Normal file
47
src/util/TruncateString.cxx
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2009-2017 Max Kellermann <max.kellermann@gmail.com>
|
||||||
|
*
|
||||||
|
* 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 "TruncateString.hxx"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
char *
|
||||||
|
CopyString(char *gcc_restrict dest, const char *gcc_restrict src,
|
||||||
|
size_t size) noexcept
|
||||||
|
{
|
||||||
|
size_t length = strlen(src);
|
||||||
|
if (length >= size)
|
||||||
|
length = size - 1;
|
||||||
|
|
||||||
|
char *p = std::copy_n(src, length, dest);
|
||||||
|
*p = '\0';
|
||||||
|
return p;
|
||||||
|
}
|
49
src/util/TruncateString.hxx
Normal file
49
src/util/TruncateString.hxx
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2009-2017 Max Kellermann <max.kellermann@gmail.com>
|
||||||
|
*
|
||||||
|
* 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 TRUNCATE_STRING_HXX
|
||||||
|
#define TRUNCATE_STRING_HXX
|
||||||
|
|
||||||
|
#include "Compiler.h"
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copy a string. If the buffer is too small, then the string is
|
||||||
|
* truncated. This is a safer version of strncpy().
|
||||||
|
*
|
||||||
|
* @param size the size of the destination buffer (including the null
|
||||||
|
* terminator)
|
||||||
|
* @return a pointer to the null terminator
|
||||||
|
*/
|
||||||
|
gcc_nonnull_all
|
||||||
|
char *
|
||||||
|
CopyString(char *dest, const char *src, size_t size) noexcept;
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue
Block a user