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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
|
#ifndef CONVERSION_UTILITIES_STRINGBUILDER_H
#define CONVERSION_UTILITIES_STRINGBUILDER_H
#include "../misc/traits.h"
#include "./stringconversion.h"
#include <string>
#include <tuple>
namespace CppUtilities {
/// \cond
namespace Helper {
template <class StringType, class ViewType> using IsStringViewType = std::is_same<ViewType, std::basic_string_view<typename StringType::value_type>>;
template <class StringType, class CharType> using IsCharType = std::is_same<typename StringType::value_type, CharType>;
namespace Detail {
template <typename StringType, typename T>
auto IsStringType(int)
-> decltype(std::declval<StringType &>().append(std::declval<const T &>()), std::declval<const T &>().size(), Traits::Bool<true>{});
template <typename StringType, typename T> Traits::Bool<false> IsStringType(...);
template <typename StringType> void functionTakingConstStringRef(const StringType &str);
template <typename StringType, typename T>
auto IsConvertibleToConstStringRef(int) -> decltype(functionTakingConstStringRef<StringType>(std::declval<const T &>()), Traits::Bool<true>{});
template <typename StringType, typename T> Traits::Bool<false> IsConvertibleToConstStringRef(...);
template <typename StringType, typename T>
auto IsConvertibleToConstStringRefViaNative(int)
-> decltype(functionTakingConstStringRef<StringType>(std::declval<const T &>().native()), Traits::Bool<true>{});
template <typename StringType, typename T> Traits::Bool<false> IsConvertibleToConstStringRefViaNative(...);
} // namespace Detail
template <typename StringType, typename StringType2>
using IsStringType = Traits::All<Traits::Not<IsStringViewType<StringType, StringType2>>, decltype(Detail::IsStringType<StringType, StringType2>(0))>;
template <typename StringType, typename StringType2>
using IsConvertibleToConstStringRefViaNative = Traits::All<Traits::Not<IsStringType<StringType, StringType2>>,
decltype(Detail::IsConvertibleToConstStringRefViaNative<StringType, StringType2>(0))>;
template <typename StringType, typename StringType2>
using IsConvertibleToConstStringRef = Traits::All<Traits::Not<Traits::Any<IsStringType<StringType, StringType2>, IsCharType<StringType, StringType2>,
IsConvertibleToConstStringRefViaNative<StringType, StringType2>>>,
decltype(Detail::IsConvertibleToConstStringRef<StringType, StringType2>(0))>;
template <class StringType, class StringType2, Traits::EnableIf<IsStringType<StringType, StringType2>> * = nullptr>
inline std::size_t computeTupleElementSize(const StringType2 *str)
{
return str->size();
}
template <class StringType, class StringType2, Traits::EnableIf<IsStringType<StringType, StringType2>> * = nullptr>
inline std::size_t computeTupleElementSize(const StringType2 &str)
{
return str.size();
}
template <class StringType, class ConvertibleType, Traits::EnableIf<IsConvertibleToConstStringRef<StringType, ConvertibleType>> * = nullptr>
inline std::size_t computeTupleElementSize(const ConvertibleType *str)
{
return computeTupleElementSize<StringType, StringType>(*str);
}
template <class StringType, class ConvertibleType, Traits::EnableIf<IsConvertibleToConstStringRef<StringType, ConvertibleType>> * = nullptr>
inline std::size_t computeTupleElementSize(const ConvertibleType &str)
{
return computeTupleElementSize<StringType, StringType>(str);
}
template <class StringType, class ConvertibleType, Traits::EnableIf<IsConvertibleToConstStringRefViaNative<StringType, ConvertibleType>> * = nullptr>
inline std::size_t computeTupleElementSize(const ConvertibleType *str)
{
return computeTupleElementSize<StringType, StringType>(str->native());
}
template <class StringType, class ConvertibleType, Traits::EnableIf<IsConvertibleToConstStringRefViaNative<StringType, ConvertibleType>> * = nullptr>
inline std::size_t computeTupleElementSize(const ConvertibleType &str)
{
return computeTupleElementSize<StringType, StringType>(str.native());
}
template <class StringType, class ViewType, Traits::EnableIf<IsStringViewType<StringType, ViewType>> * = nullptr>
inline std::size_t computeTupleElementSize(const ViewType *str)
{
return str->size();
}
template <class StringType, class ViewType, Traits::EnableIf<IsStringViewType<StringType, ViewType>> * = nullptr>
inline std::size_t computeTupleElementSize(ViewType str)
{
return str.size();
}
template <class StringType, class CharType, Traits::EnableIf<IsCharType<StringType, CharType>> * = nullptr>
constexpr std::size_t computeTupleElementSize(const CharType *str)
{
return std::char_traits<CharType>::length(str);
}
template <class StringType, class CharType, Traits::EnableIf<IsCharType<StringType, CharType>> * = nullptr>
constexpr std::size_t computeTupleElementSize(CharType)
{
return 1;
}
template <class StringType, typename IntegralType,
Traits::EnableIf<Traits::Not<std::is_same<typename StringType::value_type, IntegralType>>, std::is_integral<IntegralType>,
std::is_unsigned<IntegralType>> * = nullptr>
constexpr std::size_t computeTupleElementSize(IntegralType number, IntegralType base = 10)
{
auto size = std::size_t(0u);
for (auto n = number; n; n /= base, ++size)
;
return size;
}
template <class StringType, typename IntegralType,
Traits::EnableIf<Traits::Not<std::is_same<typename StringType::value_type, IntegralType>>, std::is_integral<IntegralType>,
std::is_signed<IntegralType>> * = nullptr>
constexpr std::size_t computeTupleElementSize(IntegralType number, IntegralType base = 10)
{
auto size = std::size_t(number < 0 ? 1u : 0u);
for (auto n = number; n; n /= base, ++size)
;
return size;
}
template <class StringType, typename TupleType, Traits::EnableIf<Traits::IsSpecializationOf<std::decay_t<TupleType>, std::tuple>> * = nullptr>
constexpr std::size_t computeTupleElementSize(TupleType &&tuple, typename StringType::value_type base = 10);
template <class StringType, class StringType2,
Traits::EnableIfAny<IsStringType<StringType, StringType2>, IsConvertibleToConstStringRef<StringType, StringType2>> * = nullptr>
inline void append(StringType &target, const StringType2 *str)
{
target.append(*str);
}
template <class StringType, class StringType2,
Traits::EnableIfAny<IsStringType<StringType, StringType2>, IsConvertibleToConstStringRef<StringType, StringType2>> * = nullptr>
inline void append(StringType &target, const StringType2 &str)
{
target.append(str);
}
template <class StringType, class StringType2, Traits::EnableIf<IsConvertibleToConstStringRefViaNative<StringType, StringType2>> * = nullptr>
inline void append(StringType &target, const StringType2 *str)
{
target.append(str->native());
}
template <class StringType, class StringType2, Traits::EnableIf<IsConvertibleToConstStringRefViaNative<StringType, StringType2>> * = nullptr>
inline void append(StringType &target, const StringType2 &str)
{
target.append(str.native());
}
template <class StringType, class ViewType, Traits::EnableIf<IsStringViewType<StringType, ViewType>> * = nullptr>
inline void append(StringType &target, const ViewType *str)
{
target.append(*str);
}
template <class StringType, class ViewType, Traits::EnableIf<IsStringViewType<StringType, ViewType>> * = nullptr>
inline void append(StringType &target, ViewType str)
{
target.append(str);
}
template <class StringType, class CharType, Traits::EnableIf<IsCharType<StringType, CharType>> * = nullptr>
inline void append(StringType &target, const CharType *str)
{
target.append(str);
}
template <class StringType, class CharType, Traits::EnableIf<IsCharType<StringType, CharType>> * = nullptr>
inline void append(StringType &target, CharType c)
{
target += c;
}
template <class StringType, typename IntegralType,
Traits::EnableIf<Traits::Not<std::is_same<typename StringType::value_type, IntegralType>>, std::is_integral<IntegralType>,
std::is_unsigned<IntegralType>> * = nullptr>
inline void append(StringType &target, IntegralType number, IntegralType base = 10)
{
const auto start = target.begin() + static_cast<typename StringType::difference_type>(target.size());
do {
target.insert(start, static_cast<typename StringType::value_type>(digitToChar<IntegralType>(number % base)));
number /= base;
} while (number);
}
template <class StringType, typename IntegralType,
Traits::EnableIf<Traits::Not<std::is_same<typename StringType::value_type, IntegralType>>, std::is_integral<IntegralType>,
std::is_signed<IntegralType>> * = nullptr>
inline void append(StringType &target, IntegralType number, IntegralType base = 10)
{
if (number < 0) {
target += '-';
number = -number;
}
const auto start = target.begin() + static_cast<typename StringType::difference_type>(target.size());
do {
target.insert(start, static_cast<typename StringType::value_type>(digitToChar<IntegralType>(number % base)));
number /= base;
} while (number);
}
template <class StringType, typename TupleType, Traits::EnableIf<Traits::IsSpecializationOf<std::decay_t<TupleType>, std::tuple>> * = nullptr>
constexpr void append(StringType &target, TupleType &&tuple, typename StringType::value_type base = 10);
template <class StringType, class Tuple, std::size_t N> struct TupleToString {
static inline std::size_t precomputeSize(const Tuple &tuple)
{
return TupleToString<StringType, Tuple, N - 1>::precomputeSize(tuple) + computeTupleElementSize<StringType>(std::get<N - 1>(tuple));
}
static inline void append(const Tuple &tuple, StringType &str)
{
TupleToString<StringType, Tuple, N - 1>::append(tuple, str);
Helper::append(str, std::get<N - 1>(tuple));
}
};
template <class StringType, class Tuple> struct TupleToString<StringType, Tuple, 1> {
static inline std::size_t precomputeSize(const Tuple &tuple)
{
return computeTupleElementSize<StringType>(std::get<0>(tuple));
}
static inline void append(const Tuple &tuple, StringType &str)
{
Helper::append(str, std::get<0>(tuple));
}
};
template <class StringType, typename TupleType, Traits::EnableIf<Traits::IsSpecializationOf<std::decay_t<TupleType>, std::tuple>> *>
constexpr std::size_t computeTupleElementSize(TupleType &&tuple, typename StringType::value_type base)
{
CPP_UTILITIES_UNUSED(base)
return TupleToString<StringType, TupleType, std::tuple_size_v<std::decay_t<TupleType>>>::precomputeSize(std::forward<TupleType>(tuple));
}
template <class StringType, typename TupleType, Traits::EnableIf<Traits::IsSpecializationOf<std::decay_t<TupleType>, std::tuple>> *>
constexpr void append(StringType &target, TupleType &&tuple, typename StringType::value_type base)
{
CPP_UTILITIES_UNUSED(base)
return TupleToString<StringType, TupleType, std::tuple_size_v<std::decay_t<TupleType>>>::append(std::forward<TupleType>(tuple), target);
}
} // namespace Helper
/// \endcond
/*!
* \brief Concatenates all strings hold by the specified \a tuple.
*/
template <class StringType = std::string, class... Args> inline StringType tupleToString(const std::tuple<Args...> &tuple)
{
StringType res;
res.reserve(Helper::TupleToString<StringType, decltype(tuple), sizeof...(Args)>::precomputeSize(tuple));
Helper::TupleToString<StringType, decltype(tuple), sizeof...(Args)>::append(tuple, res);
return res;
}
template <class StringType = std::string, class... Args> inline StringType argsToString(Args &&...args)
{
return tupleToString(std::tuple<Args &&...>(std::forward<Args>(args)...));
}
/*!
* \brief Allows construction of string-tuples via %-operator, eg. string1 % "string2" % string3.
*/
template <class Tuple, class StringType,
Traits::EnableIf<Traits::IsSpecializationOf<Tuple, std::tuple>,
Traits::IsSpecializingAnyOf<StringType, std::basic_string, std::basic_string_view>> * = nullptr>
constexpr auto operator%(const Tuple &lhs, const StringType &rhs) -> decltype(std::tuple_cat(lhs, std::tuple<const StringType &>(rhs)))
{
return std::tuple_cat(lhs, std::tuple<const StringType &>(rhs));
}
/*!
* \brief Allows construction of string-tuples via %-operator, eg. string1 % "string2" % string3.
*/
template <class Tuple, Traits::EnableIf<Traits::IsSpecializationOf<Tuple, std::tuple>> * = nullptr>
constexpr auto operator%(const Tuple &lhs, const char *rhs) -> decltype(std::tuple_cat(lhs, std::tuple<const char *>(rhs)))
{
return std::tuple_cat(lhs, std::tuple<const char *>(rhs));
}
/*!
* \brief Allows construction of string-tuples via %-operator, eg. string1 % "string2" % string3.
*/
template <class Tuple, typename IntegralType,
Traits::EnableIf<Traits::IsSpecializationOf<Tuple, std::tuple>, std::is_integral<IntegralType>> * = nullptr>
constexpr auto operator%(const Tuple &lhs, IntegralType rhs) -> decltype(std::tuple_cat(lhs, std::tuple<IntegralType>(rhs)))
{
return std::tuple_cat(lhs, std::tuple<IntegralType>(rhs));
}
/*!
* \brief Allows construction of string-tuples via %-operator, eg. string1 % "string2" % string3.
*/
template <class StringType,
Traits::EnableIfAny<Traits::IsSpecializationOf<StringType, std::basic_string>, Traits::IsSpecializationOf<StringType, std::basic_string_view>>
* = nullptr>
constexpr auto operator%(const StringType &lhs, const StringType &rhs) -> decltype(std::tuple<const StringType &, const StringType &>(lhs, rhs))
{
return std::tuple<const StringType &, const StringType &>(lhs, rhs);
}
/*!
* \brief Allows construction of string-tuples via %-operator, eg. string1 % "string2" % string3.
*/
template <class StringType,
Traits::EnableIfAny<Traits::IsSpecializationOf<StringType, std::basic_string>, Traits::IsSpecializationOf<StringType, std::basic_string_view>>
* = nullptr>
constexpr auto operator%(const char *lhs, const StringType &rhs) -> decltype(std::tuple<const char *, const StringType &>(lhs, rhs))
{
return std::tuple<const char *, const StringType &>(lhs, rhs);
}
/*!
* \brief Allows construction of string-tuples via %-operator, eg. string1 % "string2" % string3.
*/
template <class StringType,
Traits::EnableIfAny<Traits::IsSpecializationOf<StringType, std::basic_string>, Traits::IsSpecializationOf<StringType, std::basic_string_view>>
* = nullptr>
constexpr auto operator%(const StringType &lhs, const char *rhs) -> decltype(std::tuple<const StringType &, const char *>(lhs, rhs))
{
return std::tuple<const StringType &, const char *>(lhs, rhs);
}
/*!
* \brief Allows construction of string-tuples via %-operator, eg. string1 % "string2" % string3.
*/
template <class StringType,
Traits::EnableIfAny<Traits::IsSpecializationOf<StringType, std::basic_string>, Traits::IsSpecializationOf<StringType, std::basic_string_view>>
* = nullptr>
constexpr auto operator%(const StringType &lhs, char rhs) -> decltype(std::tuple<const StringType &, char>(lhs, rhs))
{
return std::tuple<const StringType &, char>(lhs, rhs);
}
/*!
* \brief Allows construction of string-tuples via %-operator, eg. string1 % "string2" % string3.
*/
template <class StringType,
Traits::EnableIfAny<Traits::IsSpecializationOf<StringType, std::basic_string>, Traits::IsSpecializationOf<StringType, std::basic_string_view>>
* = nullptr>
constexpr auto operator%(char lhs, const StringType &rhs) -> decltype(std::tuple<char, const StringType &>(lhs, rhs))
{
return std::tuple<char, const StringType &>(lhs, rhs);
}
/*!
* \brief Allows construction of final string from previously constructed string-tuple and trailing string via +-operator.
*
* This is meant to be used for fast string building without multiple heap allocation, eg.
*
* ```
* printVelocity("velocity: " % numberToString(velocityExample) % " km/h (" % numberToString(velocityExample / 3.6) + " m/s)"));
* ```
*/
template <class Tuple, class StringType,
Traits::EnableIf<Traits::IsSpecializationOf<Tuple, std::tuple>,
Traits::Any<Traits::IsSpecializationOf<StringType, std::basic_string>, Traits::IsSpecializationOf<StringType, std::basic_string_view>>>
* = nullptr>
inline std::string operator+(const Tuple &lhs, const StringType &rhs)
{
return tupleToString(std::tuple_cat(lhs, std::tuple<const StringType &>(rhs)));
}
/*!
* \brief Allows construction of final string from previously constructed string-tuple and trailing string via +-operator.
*
* This is meant to be used for fast string building without multiple heap allocation, eg.
*
* ```
* printVelocity("velocity: " % numberToString(velocityExample) % " km/h (" % numberToString(velocityExample / 3.6) + " m/s)"));
* ```
*/
template <class Tuple, Traits::EnableIf<Traits::IsSpecializationOf<Tuple, std::tuple>> * = nullptr>
inline std::string operator+(const Tuple &lhs, const char *rhs)
{
return tupleToString(std::tuple_cat(lhs, std::tuple<const char *>(rhs)));
}
/*!
* \brief Allows construction of final string from previously constructed string-tuple and trailing char via +-operator.
*
* This is meant to be used for fast string building without multiple heap allocation, eg.
*
* ```
* printVelocity("velocity: " % numberToString(velocityExample) % " km/h (" % numberToString(velocityExample / 3.6) + " m/s)"));
* ```
*/
template <class Tuple, typename IntegralType,
Traits::EnableIf<Traits::IsSpecializationOf<Tuple, std::tuple>, std::is_integral<IntegralType>> * = nullptr>
inline std::string operator+(const Tuple &lhs, IntegralType rhs)
{
return tupleToString(std::tuple_cat(lhs, std::tuple<IntegralType>(rhs)));
}
} // namespace CppUtilities
#endif // CONVERSION_UTILITIES_STRINGBUILDER_H
|