Commit 4b94ae80 authored by Max Kellermann's avatar Max Kellermann

util/StringUtil: move CopyString() to TruncateString.cxx

parent b04679b4
......@@ -426,6 +426,7 @@ libutil_a_SOURCES = \
src/util/StringView.cxx src/util/StringView.hxx \
src/util/ConcatString.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/StringCompare.cxx src/util/StringCompare.hxx \
src/util/WStringCompare.cxx src/util/WStringCompare.hxx \
......
......@@ -25,7 +25,7 @@
#include "CdioParanoiaInputPlugin.hxx"
#include "../InputStream.hxx"
#include "../InputPlugin.hxx"
#include "util/StringUtil.hxx"
#include "util/TruncateString.hxx"
#include "util/StringCompare.hxx"
#include "util/RuntimeError.hxx"
#include "util/Domain.hxx"
......
......@@ -28,7 +28,7 @@
*/
#include "String.hxx"
#include "util/StringUtil.hxx"
#include "util/TruncateString.hxx"
char *
Java::String::CopyTo(JNIEnv *env, jstring value,
......
......@@ -22,7 +22,7 @@
#include "tag/Tag.hxx"
#include "util/FormatString.hxx"
#include "util/AllocatedString.hxx"
#include "util/StringUtil.hxx"
#include "util/TruncateString.hxx"
#include "util/Macros.hxx"
#include <string.h>
......
......@@ -22,7 +22,7 @@
#include "Tag.hxx"
#include "ParseName.hxx"
#include "util/format.h"
#include "util/StringUtil.hxx"
#include "util/TruncateString.hxx"
#include <algorithm>
......
......@@ -21,24 +21,9 @@
#include "CharUtil.hxx"
#include "ASCII.hxx"
#include <algorithm>
#include <assert.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 *
StripLeft(const char *p) noexcept
{
......
......@@ -25,18 +25,6 @@
#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
* string, or to the end of the string.
*/
......
/*
* 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;
}
/*
* 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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment