LeechCraft 0.6.70-17335-ge406ffdcaf
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 <QString>
14
15class QByteArray;
16
17namespace LC::Util
18{
19 template<size_t N, typename Char = char>
20 using RawStr = const Char (&) [N];
21
27 template<size_t N, typename Char = char>
28 struct CtString
29 {
30 using Char_t = Char;
31
34 constexpr static size_t Size = N;
35
36 Char Data_ [Size] {};
37
38 constexpr CtString () noexcept = default;
39
40 constexpr CtString (RawStr<N + 1, Char> s) noexcept
41 {
42 std::copy (s, s + Size, Data_);
43 }
44
45 constexpr auto operator<=> (const CtString&) const = default;
46
47 constexpr static auto FromUnsized (const Char *s) noexcept
48 {
49 CtString result {};
50 std::copy (s, s + Size, result.Data_);
51 return result;
52 }
53
54 template<size_t N2>
55 constexpr auto operator+ (const CtString<N2, Char>& s2) const noexcept
56 {
57 // TODO clang bug, use s2.Size otherwise
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 operator QStringView () const noexcept
104 requires std::is_same_v<Char, char16_t>
105 {
106 return QStringView { Data_, Size };
107 }
108
109 constexpr auto Data () const noexcept
110 {
111 return Data_;
112 }
113
114 template<typename NewChar>
115 constexpr CtString<N, NewChar> CastChars () const noexcept
116 {
118 std::copy (Data_, Data_ + N, result.Data_);
119 return result;
120 }
121 };
122
123 template<CtString Str>
124 QByteArray ToByteArray ()
125 {
126 constexpr static auto terminated = Str + '\0';
127 return QByteArray { QByteArrayData { nullptr, terminated.Data_, terminated.Size - 1 } };
128 }
129
130 template<CtString Str>
131 QString ToString ()
132 {
133 if constexpr (std::is_same_v<typename decltype (Str)::Char_t, char16_t>)
134 {
135 constexpr static auto terminated = Str + '\0';
136 // this const_cast is fine-ish, since Qt is doing the same in
137 // QtPrivate::qMakeStringPrivate()
138 return QString { QStringPrivate { nullptr, const_cast<char16_t*> (terminated.Data_), terminated.Size - 1 } };
139 }
140 else
141 return ToString<Str.template CastChars<char16_t> ()> ();
142 }
143
144 template<size_t N1, size_t N2, typename Char>
145 constexpr auto operator+ (RawStr<N1, Char> s1, CtString<N2, Char> s2) noexcept
146 {
147 return CtString<N1 - 1, Char> { s1 } + s2;
148 }
149
150 template<typename Char>
151 constexpr size_t StringBufSize (const Char *str) noexcept
152 {
153 size_t result = 0;
154 while (str [result++])
155 ;
156 return result - 1;
157 }
158
159 template<size_t N, typename Char>
161}
162
163namespace LC
164{
165 template<Util::CtString S>
166 constexpr auto operator""_ct () noexcept
167 {
168 return S;
169 }
170}
QByteArray ToByteArray()
Definition ctstring.h:124
QString ToString()
Definition ctstring.h:131
const Char(&)[N] RawStr
Definition ctstring.h:20
constexpr size_t StringBufSize(const Char *str) noexcept
Definition ctstring.h:151
CtString(RawStr< N, Char >) -> CtString< N - 1, Char >
constexpr auto operator+(RawStr< N1, Char > s1, CtString< N2, Char > s2) noexcept
Definition ctstring.h:145
Definition constants.h:15
static constexpr auto FromUnsized(const Char *s) noexcept
Definition ctstring.h:47
constexpr auto operator+(const CtString< N2, Char > &s2) const noexcept
Definition ctstring.h:55
constexpr auto Chop() const noexcept
Definition ctstring.h:88
constexpr auto Data() const noexcept
Definition ctstring.h:109
constexpr bool EndsWith(Char ch) const noexcept
Definition ctstring.h:80
Char Data_[Size]
Definition ctstring.h:36
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:34
constexpr CtString< N, NewChar > CastChars() const noexcept
Definition ctstring.h:115
constexpr auto operator<=>(const CtString &) const =default