LeechCraft 0.6.70-18450-gabe19ee3b0
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
ctstringutils.h
Go to the documentation of this file.
1/**********************************************************************
2 * LeechCraft - modular cross-platform feature rich internet client.
3 * Copyright (C) 2006-2014 Georg Rudoy
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
9#pragma once
10
11#include "ctstring.h"
12#include <charconv>
13#include <QDebug>
14
15namespace LC::Util
16{
17 constexpr auto Join (auto&&) noexcept
18 {
19 return ""_ct;
20 }
21
22 constexpr auto Join (auto&& sep, auto&& first, auto&&... strings) noexcept
23 {
24 if constexpr (sizeof... (strings))
25 return first + ((sep + strings) + ...);
26 else
27 return first;
28 }
29
30 constexpr auto JoinTup (auto&& stringsTuple, auto&& sep) noexcept
31 {
32 return std::apply ([&sep]<typename... Ts> (Ts&&... args) { return Join (sep, std::forward<Ts> (args)...); },
33 stringsTuple);
34 }
35
36 namespace detail
37 {
38 constexpr size_t DecimalWidth (std::integral auto v, int base = 10)
39 {
40 if (!v)
41 return 1;
42
43 size_t count = static_cast<bool> (v < 0);
44 while (v)
45 {
46 ++count;
47 v /= base;
48 }
49 return count;
50 }
51 }
52
53 template<std::integral auto Value, typename Char = char>
54 constexpr auto IntegralToString ()
55 {
56 constexpr auto width = detail::DecimalWidth (Value);
58 std::to_chars (result.Data_, result.Data_ + width, Value);
59 return result;
60 }
61
62 template<typename Tup1, typename Tup2,
63 size_t Tup1Size = std::tuple_size_v<std::decay_t<Tup1>>,
64 size_t Tup2Size = std::tuple_size_v<std::decay_t<Tup2>>
65 >
66 requires (Tup1Size == Tup2Size)
67 constexpr auto ZipWith (Tup1&& tup1, auto&& sep, Tup2&& tup2) noexcept
68 {
69 return [&]<size_t... I> (std::index_sequence<I...>)
70 {
71 return std::tuple { (std::get<I> (tup1) + sep + std::get<I> (tup2))... };
72 } (std::make_index_sequence<Tup1Size> {});
73 }
74
75 namespace detail
76 {
77 template<typename T1, typename T2>
78 consteval bool JMEq (const T1& v1, const T2& v2)
79 {
80 if constexpr (!std::is_same_v<T1, T2>)
81 return false;
82 else
83 return v1 == v2;
84 }
85 }
86
87 template<const auto& F>
88 constexpr auto Nub ()
89 {
90 constexpr auto tup = F ();
91 constexpr auto indices = std::make_index_sequence<std::tuple_size_v<decltype (tup)>> {};
92
93 return [&]<std::size_t... Ix> (std::index_sequence<Ix...>)
94 {
95 return std::tuple_cat ([&]
96 {
97 constexpr auto thisIndex = Ix;
98 constexpr auto item = std::get<thisIndex> (tup);
99
100 constexpr auto itemResult = [&]<std::size_t... IxOther> (std::index_sequence<IxOther...>)
101 {
102 if constexpr (((detail::JMEq (item, std::get<IxOther> (tup)) && IxOther < thisIndex) || ...))
103 return std::tuple {};
104 else
105 return std::tuple { item };
106 } (indices);
107 return itemResult;
108 } ()...);
109 } (indices);
110 }
111
112 template<size_t N, typename Char>
113 QDebug operator<< (QDebug dbg, const CtString<N, Char>& str)
114 {
115 QDebugStateSaver saver { dbg };
116 dbg.nospace () << "CtString[" << N << "] { ";
117 for (size_t i = 0; i < N; ++i)
118 dbg.nospace () << str.Data_ [i];
119 dbg.nospace () << " }";
120 return dbg;
121 }
122}
consteval bool JMEq(const T1 &v1, const T2 &v2)
constexpr size_t DecimalWidth(std::integral auto v, int base=10)
constexpr auto JoinTup(auto &&stringsTuple, auto &&sep) noexcept
constexpr auto IntegralToString()
constexpr auto ZipWith(Tup1 &&tup1, auto &&sep, Tup2 &&tup2) noexcept
QDebug operator<<(QDebug dbg, const CtString< N, Char > &str)
constexpr auto Nub()
constexpr auto Join(auto &&) noexcept
Char Data_[Size]
Definition ctstring.h:36