LeechCraft 0.6.70-19424-g027e980fd1
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_ + Size, 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 constexpr bool IsAscii () const noexcept
134 {
135 return std::all_of (Data_, Data_ + Size, [] (std::make_unsigned_t<Char> c) { return c < 0x80; });
136 }
137 };
138
139 template<CtString Source, auto Needle, size_t N2, typename Char>
140 constexpr auto ReplaceAll (CtString<N2, Char> replacement) noexcept
141 {
142 constexpr auto repCount = Source.template Count<static_cast<Char> (Needle)> ();
143 constexpr auto newSize = Source.Size + repCount * (N2 - 1);
144
146 auto out = result.Data_;
147 for (auto in = Source.Data_; in < Source.Data_ + Source.Size; ++in)
148 if (*in == Needle)
149 out = std::copy (replacement.Data_, replacement.Data_ + N2, out);
150 else
151 *out++ = *in;
152 return result;
153 }
154
155 template<CtString Str>
156 QByteArray ToByteArray () noexcept
157 {
158 constexpr static auto terminated = Str + '\0';
159 // this const_cast is fine-ish, since Qt is doing the same in QByteArrayLiteral()
160 return QByteArray { QByteArrayData { nullptr, const_cast<char*> (terminated.Data_), terminated.Size - 1 } };
161 }
162
163 template<CtString Str>
164 QString ToString () noexcept
165 {
166 if constexpr (std::is_same_v<typename decltype (Str)::Char_t, char16_t>)
167 {
168 constexpr static auto terminated = Str + '\0';
169 // this const_cast is fine-ish, since Qt is doing the same in QtPrivate::qMakeStringPrivate()
170 return QString { QStringPrivate { nullptr, const_cast<char16_t*> (terminated.Data_), terminated.Size - 1 } };
171 }
172 else
173 {
174 static_assert (Str.IsAscii (),
175 "cannot convert non-ASCII `char` string to `QString`, consider using the `u` literal prefix");
176 return ToString<Str.template CastChars<char16_t> ()> ();
177 }
178 }
179
180 template<size_t N1, size_t N2, typename Char>
181 constexpr auto operator+ (RawStr<N1, Char> s1, CtString<N2, Char> s2) noexcept
182 {
183 return CtString<N1 - 1, Char> { s1 } + s2;
184 }
185
186 template<typename Char>
187 constexpr size_t StringBufSize (const Char *str) noexcept
188 {
189 size_t result = 0;
190 while (str [result++])
191 ;
192 return result - 1;
193 }
194
195 template<size_t N, typename Char>
197}
198
199namespace LC
200{
201 template<Util::CtString S>
202 constexpr auto operator""_ct () noexcept
203 {
204 return S;
205 }
206}
Q_DECL_IMPORT const QString Count
The new total event count (int).
QString ToString() noexcept
Definition ctstring.h:164
constexpr auto ReplaceAll(CtString< N2, Char > replacement) noexcept
Definition ctstring.h:140
const Char(&)[N] RawStr
Definition ctstring.h:21
QByteArray ToByteArray() noexcept
Definition ctstring.h:156
constexpr size_t StringBufSize(const Char *str) noexcept
Definition ctstring.h:187
constexpr auto operator+(RawStr< N1, Char > s1, CtString< N2, Char > s2) noexcept
Definition ctstring.h:181
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