LeechCraft 0.6.70-19214-g121e5318a7
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
ctstring.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 <algorithm>
12#include <concepts>
13#include <string_view>
14#include <QString>
15
16class QByteArray;
17
18namespace LC::Util
19{
20 template<size_t N, typename Char = char>
21 using RawStr = const Char (&) [N];
22
28 template<size_t N, typename Char = char>
29 struct CtString
30 {
31 using Char_t = Char;
32
35 constexpr static size_t Size = N;
36
37 Char Data_ [Size] {};
38
39 constexpr CtString () noexcept = default;
40
41 constexpr CtString (RawStr<N + 1, Char> s) noexcept
42 {
43 std::copy (s, s + Size, Data_);
44 }
45
46 constexpr auto operator<=> (const CtString&) const = default;
47
48 constexpr static auto FromUnsized (const Char *s) noexcept
49 {
50 CtString result {};
51 std::copy (s, s + Size, result.Data_);
52 return result;
53 }
54
55 template<size_t N2>
56 constexpr auto operator+ (const CtString<N2, Char>& s2) const noexcept
57 {
58 CtString<Size + s2.Size, Char> result;
59 std::copy (Data_, Data_ + Size, result.Data_);
60 std::copy (s2.Data_, s2.Data_ + s2.Size, result.Data_ + Size);
61 return result;
62 }
63
64 template<size_t N2>
65 constexpr auto operator+ (RawStr<N2, Char> s2) const noexcept
66 {
67 return *this + CtString<N2 - 1, Char> { s2 };
68 }
69
70 constexpr auto operator+ (Char ch) const noexcept
71 {
72 return *this + CtString<1, Char> { { ch } };
73 }
74
75 constexpr bool IsEmpty () const noexcept
76 {
77 return !Size;
78 }
79
80 constexpr bool EndsWith (Char ch) const noexcept
81 requires (Size > 0)
82 {
83 return Data_ [Size - 1] == ch;
84 }
85
86 template<size_t Count>
87 requires (Count <= Size)
88 [[nodiscard]] constexpr auto Chop () const noexcept
89 {
91 }
92
93 constexpr Char& operator[] (size_t pos) noexcept
94 {
95 return Data_ [pos];
96 }
97
98 constexpr Char operator[] (size_t pos) const noexcept
99 {
100 return Data_ [pos];
101 }
102
103 constexpr explicit(false) operator QStringView () const noexcept
104 requires std::is_same_v<Char, char16_t>
105 {
106 return QStringView { Data_, Size };
107 }
108
109 constexpr explicit(false) operator std::basic_string_view<Char> () const noexcept
110 {
111 return { Data_, Size };
112 }
113
114 constexpr auto Data () const noexcept
115 {
116 return Data_;
117 }
118
119 template<typename NewChar>
120 constexpr CtString<N, NewChar> CastChars () const noexcept
121 {
123 std::copy (Data_, Data_ + N, result.Data_);
124 return result;
125 }
126
127 template<Char Needle>
128 constexpr size_t Count () const noexcept
129 {
130 return std::count (Data_, Data_ + Size, Needle);
131 }
132 };
133
134 template<CtString Source, auto Needle, size_t N2, typename Char>
135 constexpr auto ReplaceAll (CtString<N2, Char> replacement) noexcept
136 {
137 constexpr auto repCount = Source.template Count<static_cast<Char> (Needle)> ();
138 constexpr auto newSize = Source.Size + repCount * (N2 - 1);
139
141 auto out = result.Data_;
142 for (auto in = Source.Data_; in < Source.Data_ + Source.Size; ++in)
143 if (*in == Needle)
144 out = std::copy (replacement.Data_, replacement.Data_ + N2, out);
145 else
146 *out++ = *in;
147 return result;
148 }
149
150 template<CtString Str>
151 QByteArray ToByteArray () noexcept
152 {
153 constexpr static auto terminated = Str + '\0';
154 // this const_cast is fine-ish, since Qt is doing the same in QByteArrayLiteral()
155 return QByteArray { QByteArrayData { nullptr, const_cast<char*> (terminated.Data_), terminated.Size - 1 } };
156 }
157
158 template<CtString Str>
159 QString ToString () noexcept
160 {
161 if constexpr (std::is_same_v<typename decltype (Str)::Char_t, char16_t>)
162 {
163 constexpr static auto terminated = Str + '\0';
164 // this const_cast is fine-ish, since Qt is doing the same in QtPrivate::qMakeStringPrivate()
165 return QString { QStringPrivate { nullptr, const_cast<char16_t*> (terminated.Data_), terminated.Size - 1 } };
166 }
167 else
168 return ToString<Str.template CastChars<char16_t> ()> ();
169 }
170
171 template<size_t N1, size_t N2, typename Char>
172 constexpr auto operator+ (RawStr<N1, Char> s1, CtString<N2, Char> s2) noexcept
173 {
174 return CtString<N1 - 1, Char> { s1 } + s2;
175 }
176
177 template<typename Char>
178 constexpr size_t StringBufSize (const Char *str) noexcept
179 {
180 size_t result = 0;
181 while (str [result++])
182 ;
183 return result - 1;
184 }
185
186 template<size_t N, typename Char>
188}
189
190namespace LC
191{
192 template<Util::CtString S>
193 constexpr auto operator""_ct () noexcept
194 {
195 return S;
196 }
197}
Q_DECL_IMPORT const QString Count
The new total event count (int).
QString ToString() noexcept
Definition ctstring.h:159
constexpr auto ReplaceAll(CtString< N2, Char > replacement) noexcept
Definition ctstring.h:135
const Char(&)[N] RawStr
Definition ctstring.h:21
QByteArray ToByteArray() noexcept
Definition ctstring.h:151
constexpr size_t StringBufSize(const Char *str) noexcept
Definition ctstring.h:178
constexpr auto operator+(RawStr< N1, Char > s1, CtString< N2, Char > s2) noexcept
Definition ctstring.h:172
Definition constants.h:15
static constexpr auto FromUnsized(const Char *s) noexcept
Definition ctstring.h:48
constexpr auto operator+(const CtString< N2, Char > &s2) const noexcept
Definition ctstring.h:56
constexpr auto Chop() const noexcept
Definition ctstring.h:88
constexpr bool EndsWith(Char ch) const noexcept
Definition ctstring.h:80
Char Data_[Size]
Definition ctstring.h:37
constexpr Char & operator[](size_t pos) noexcept
Definition ctstring.h:93
constexpr CtString() noexcept=default
constexpr bool IsEmpty() const noexcept
Definition ctstring.h:75
static constexpr size_t Size
Definition ctstring.h:35
constexpr auto operator<=>(const CtString &) const =default