WPILibC++ 2024.3.2
string_concat.h
Go to the documentation of this file.
1// __ _____ _____ _____
2// __| | __| | | | JSON for Modern C++
3// | | |__ | | | | | | version 3.11.2
4// |_____|_____|_____|_|___| https://github.com/nlohmann/json
5//
6// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
7// SPDX-License-Identifier: MIT
8
9#pragma once
10
11#include <cstring> // strlen
12#include <string> // string
13#include <utility> // forward
14
17
19namespace detail
20{
21
22inline std::size_t concat_length()
23{
24 return 0;
25}
26
27template<typename... Args>
28inline std::size_t concat_length(const char* cstr, Args&& ... rest);
29
30template<typename StringType, typename... Args>
31inline std::size_t concat_length(const StringType& str, Args&& ... rest);
32
33template<typename... Args>
34inline std::size_t concat_length(const char /*c*/, Args&& ... rest)
35{
36 return 1 + concat_length(std::forward<Args>(rest)...);
37}
38
39template<typename... Args>
40inline std::size_t concat_length(const char* cstr, Args&& ... rest)
41{
42 // cppcheck-suppress ignoredReturnValue
43 return ::strlen(cstr) + concat_length(std::forward<Args>(rest)...);
44}
45
46template<typename StringType, typename... Args>
47inline std::size_t concat_length(const StringType& str, Args&& ... rest)
48{
49 return str.size() + concat_length(std::forward<Args>(rest)...);
50}
51
52template<typename OutStringType>
53inline void concat_into(OutStringType& /*out*/)
54{}
55
56template<typename StringType, typename Arg>
57using string_can_append = decltype(std::declval<StringType&>().append(std::declval < Arg && > ()));
58
59template<typename StringType, typename Arg>
61
62template<typename StringType, typename Arg>
63using string_can_append_op = decltype(std::declval<StringType&>() += std::declval < Arg && > ());
64
65template<typename StringType, typename Arg>
67
68template<typename StringType, typename Arg>
69using string_can_append_iter = decltype(std::declval<StringType&>().append(std::declval<const Arg&>().begin(), std::declval<const Arg&>().end()));
70
71template<typename StringType, typename Arg>
73
74template<typename StringType, typename Arg>
75using string_can_append_data = decltype(std::declval<StringType&>().append(std::declval<const Arg&>().data(), std::declval<const Arg&>().size()));
76
77template<typename StringType, typename Arg>
79
80template < typename OutStringType, typename Arg, typename... Args,
83inline void concat_into(OutStringType& out, Arg && arg, Args && ... rest);
84
85template < typename OutStringType, typename Arg, typename... Args,
89inline void concat_into(OutStringType& out, const Arg& arg, Args && ... rest);
90
91template < typename OutStringType, typename Arg, typename... Args,
96inline void concat_into(OutStringType& out, const Arg& arg, Args && ... rest);
97
98template<typename OutStringType, typename Arg, typename... Args,
100inline void concat_into(OutStringType& out, Arg && arg, Args && ... rest)
101{
102 out.append(std::forward<Arg>(arg));
103 concat_into(out, std::forward<Args>(rest)...);
104}
105
106template < typename OutStringType, typename Arg, typename... Args,
108 && detect_string_can_append_op<OutStringType, Arg>::value, int > >
109inline void concat_into(OutStringType& out, Arg&& arg, Args&& ... rest)
110{
111 out += std::forward<Arg>(arg);
112 concat_into(out, std::forward<Args>(rest)...);
113}
114
115template < typename OutStringType, typename Arg, typename... Args,
117 && !detect_string_can_append_op<OutStringType, Arg>::value
118 && detect_string_can_append_iter<OutStringType, Arg>::value, int > >
119inline void concat_into(OutStringType& out, const Arg& arg, Args&& ... rest)
120{
121 out.append(arg.begin(), arg.end());
122 concat_into(out, std::forward<Args>(rest)...);
123}
124
125template < typename OutStringType, typename Arg, typename... Args,
127 && !detect_string_can_append_op<OutStringType, Arg>::value
128 && !detect_string_can_append_iter<OutStringType, Arg>::value
129 && detect_string_can_append_data<OutStringType, Arg>::value, int > >
130inline void concat_into(OutStringType& out, const Arg& arg, Args&& ... rest)
131{
132 out.append(arg.data(), arg.size());
133 concat_into(out, std::forward<Args>(rest)...);
134}
135
136template<typename OutStringType = std::string, typename... Args>
137inline OutStringType concat(Args && ... args)
138{
139 OutStringType str;
140 str.reserve(concat_length(std::forward<Args>(args)...));
141 concat_into(str, std::forward<Args>(args)...);
142 return str;
143}
144
145} // namespace detail
#define WPI_JSON_NAMESPACE_END
Definition: abi_macros.h:59
#define WPI_JSON_NAMESPACE_BEGIN
Definition: abi_macros.h:53
typename std::enable_if< B, T >::type enable_if_t
Definition: core.h:256
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
\rst Returns a named argument to be used in a formatting function.
Definition: core.h:1841
detail namespace with internal helper functions
Definition: xchar.h:20
decltype(std::declval< StringType & >()+=std::declval< Arg && >()) string_can_append_op
Definition: string_concat.h:63
OutStringType concat(Args &&... args)
Definition: string_concat.h:137
is_detected< string_can_append_iter, StringType, Arg > detect_string_can_append_iter
Definition: string_concat.h:72
is_detected< string_can_append_op, StringType, Arg > detect_string_can_append_op
Definition: string_concat.h:66
is_detected< string_can_append_data, StringType, Arg > detect_string_can_append_data
Definition: string_concat.h:78
typename std::enable_if< B, T >::type enable_if_t
Definition: cpp_future.h:38
void concat_into(OutStringType &)
Definition: string_concat.h:53
decltype(std::declval< StringType & >().append(std::declval< const Arg & >().begin(), std::declval< const Arg & >().end())) string_can_append_iter
Definition: string_concat.h:69
std::size_t concat_length()
Definition: string_concat.h:22
decltype(std::declval< StringType & >().append(std::declval< Arg && >())) string_can_append
Definition: string_concat.h:57
decltype(std::declval< StringType & >().append(std::declval< const Arg & >().data(), std::declval< const Arg & >().size())) string_can_append_data
Definition: string_concat.h:75
typename detector< nonesuch, void, Op, Args... >::value_t is_detected
Definition: detected.h:48
is_detected< string_can_append, StringType, Arg > detect_string_can_append
Definition: string_concat.h:60
Definition: format.h:1762