diff --git a/Makefile.am b/Makefile.am index 08e08c012..fd14b6187 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ diff --git a/src/util/Alloc.cxx b/src/util/Alloc.cxx index 434f90df5..115dc96bb 100644 --- a/src/util/Alloc.cxx +++ b/src/util/Alloc.cxx @@ -19,6 +19,7 @@ #include "config.h" #include "Alloc.hxx" +#include "ConcatString.hxx" #include #include @@ -76,35 +77,6 @@ xstrndup(const char *s, size_t n) return p; } -template -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 -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 gcc_malloc gcc_nonnull_all static inline char * diff --git a/src/util/ConcatString.hxx b/src/util/ConcatString.hxx new file mode 100644 index 000000000..7e63238f5 --- /dev/null +++ b/src/util/ConcatString.hxx @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2014-2017 Max Kellermann + * + * 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 + +template +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 +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