util/Alloc: move template functions to ConcatString.hxx

This commit is contained in:
Max Kellermann 2017-05-16 11:01:43 +02:00
parent 59e4f1ee0f
commit fea211a109
3 changed files with 66 additions and 29 deletions

View File

@ -424,6 +424,7 @@ libutil_a_SOURCES = \
src/util/StringBuffer.hxx \
src/util/StringPointer.hxx \
src/util/StringView.cxx src/util/StringView.hxx \
src/util/ConcatString.hxx \
src/util/AllocatedString.cxx src/util/AllocatedString.hxx \
src/util/StringUtil.cxx src/util/StringUtil.hxx \
src/util/StringCompare.cxx src/util/StringCompare.hxx \

View File

@ -19,6 +19,7 @@
#include "config.h"
#include "Alloc.hxx"
#include "ConcatString.hxx"
#include <stdlib.h>
#include <string.h>
@ -76,35 +77,6 @@ xstrndup(const char *s, size_t n)
return p;
}
template<typename... Args>
static inline size_t
FillLengths(size_t *lengths, const char *a, Args&&... args)
{
return FillLengths(lengths, a) + FillLengths(lengths + 1, args...);
}
template<>
inline size_t
FillLengths(size_t *lengths, const char *a)
{
return *lengths = strlen(a);
}
template<typename... Args>
static inline void
StringCat(char *p, const size_t *lengths, const char *a, Args&&... args)
{
StringCat(p, lengths, a);
StringCat(p + *lengths, lengths + 1, args...);
}
template<>
inline void
StringCat(char *p, const size_t *lengths, const char *a)
{
memcpy(p, a, *lengths);
}
template<typename... Args>
gcc_malloc gcc_nonnull_all
static inline char *

64
src/util/ConcatString.hxx Normal file
View File

@ -0,0 +1,64 @@
/*
* Copyright (C) 2014-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 CONCAT_STRING_HXX
#define CONCAT_STRING_HXX
#include <string.h>
template<typename... Args>
size_t
FillLengths(size_t *lengths, const char *a, Args&&... args)
{
return FillLengths(lengths, a) + FillLengths(lengths + 1, args...);
}
template<>
size_t
FillLengths(size_t *lengths, const char *a)
{
return *lengths = strlen(a);
}
template<typename... Args>
void
StringCat(char *p, const size_t *lengths, const char *a, Args&&... args)
{
StringCat(p, lengths, a);
StringCat(p + *lengths, lengths + 1, args...);
}
template<>
void
StringCat(char *p, const size_t *lengths, const char *a)
{
memcpy(p, a, *lengths);
}
#endif