1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
/* ncmpc (Ncurses MPD Client)
* Copyright 2004-2021 The Music Player Daemon Project
*
* 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 IO_PATH_HXX
#define IO_PATH_HXX
#include <string>
#include <string.h>
namespace PathDetail {
#ifdef _WIN32
static constexpr char SEPARATOR = '\\';
#else
static constexpr char SEPARATOR = '/';
#endif
[[gnu::pure]]
inline size_t
GetLength(const char *s) noexcept
{
return strlen(s);
}
[[gnu::pure]]
inline size_t
GetLength(const std::string &s) noexcept
{
return s.length();
}
template<typename... Args>
inline size_t
FillLengths(size_t *lengths, Args&&... args) noexcept;
template<typename First, typename... Args>
inline size_t
FillLengths(size_t *lengths, First &&first, Args&&... args) noexcept
{
size_t length = GetLength(std::forward<First>(first));
*lengths++ = length;
return length + FillLengths(lengths, std::forward<Args>(args)...);
}
template<>
inline size_t
FillLengths(size_t *) noexcept
{
return 0;
}
inline std::string &
Append(std::string &dest, const std::string &value, size_t length) noexcept
{
return dest.append(value, 0, length);
}
inline std::string &
Append(std::string &dest, const char *value, size_t length) noexcept
{
return dest.append(value, length);
}
template<typename... Args>
inline std::string &
AppendWithSeparators(std::string &dest, const size_t *lengths,
Args&&... args) noexcept;
template<typename First, typename... Args>
inline std::string &
AppendWithSeparators(std::string &dest, const size_t *lengths,
First &&first, Args&&... args) noexcept
{
dest.push_back(SEPARATOR);
return AppendWithSeparators(Append(dest, std::forward<First>(first),
*lengths),
lengths + 1,
std::forward<Args>(args)...);
}
template<>
inline std::string &
AppendWithSeparators(std::string &dest, const size_t *) noexcept
{
return dest;
}
} // namespace PathDetail
template<typename First, typename... Args>
std::string
BuildPath(First &&first, Args&&... args) noexcept
{
constexpr size_t n = sizeof...(args);
size_t lengths[n + 1];
const size_t total = PathDetail::FillLengths(lengths, first, args...);
std::string result;
result.reserve(total + n);
PathDetail::Append(result, std::forward<First>(first), lengths[0]);
PathDetail::AppendWithSeparators(result, lengths + 1,
std::forward<Args>(args)...);
return result;
}
#endif
|