LeechCraft  0.6.70-18450-gabe19ee3b0
Modular cross-platform feature rich live environment.
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 
15 class QByteArray;
16 
17 namespace 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  {
117  CtString<N, NewChar> result;
118  std::copy (Data_, Data_ + N, result.Data_);
119  return result;
120  }
121 
122  template<Char Needle>
123  constexpr size_t Count () const noexcept
124  {
125  return std::count (Data_, Data_ + Size, Needle);
126  }
127  };
128 
129  template<CtString Source, auto Needle, size_t N2, typename Char>
130  constexpr auto ReplaceAll (CtString<N2, Char> replacement) noexcept
131  {
132  constexpr auto repCount = Source.template Count<static_cast<Char> (Needle)> ();
133  constexpr auto newSize = Source.Size + repCount * (N2 - 1);
134 
136  auto out = result.Data_;
137  for (auto in = Source.Data_; in < Source.Data_ + Source.Size; ++in)
138  if (*in == Needle)
139  out = std::copy (replacement.Data_, replacement.Data_ + N2, out);
140  else
141  *out++ = *in;
142  return result;
143  }
144 
145  template<CtString Str>
146  QByteArray ToByteArray () noexcept
147  {
148  constexpr static auto terminated = Str + '\0';
149  // this const_cast is fine-ish, since Qt is doing the same in QByteArrayLiteral()
150  return QByteArray { QByteArrayData { nullptr, const_cast<char*> (terminated.Data_), terminated.Size - 1 } };
151  }
152 
153  template<CtString Str>
154  QString ToString () noexcept
155  {
156  if constexpr (std::is_same_v<typename decltype (Str)::Char_t, char16_t>)
157  {
158  constexpr static auto terminated = Str + '\0';
159  // this const_cast is fine-ish, since Qt is doing the same in QtPrivate::qMakeStringPrivate()
160  return QString { QStringPrivate { nullptr, const_cast<char16_t*> (terminated.Data_), terminated.Size - 1 } };
161  }
162  else
163  return ToString<Str.template CastChars<char16_t> ()> ();
164  }
165 
166  template<size_t N1, size_t N2, typename Char>
168  {
169  return CtString<N1 - 1, Char> { s1 } + s2;
170  }
171 
172  template<typename Char>
173  constexpr size_t StringBufSize (const Char *str) noexcept
174  {
175  size_t result = 0;
176  while (str [result++])
177  ;
178  return result - 1;
179  }
180 
181  template<size_t N, typename Char>
182  CtString (RawStr<N, Char>) -> CtString<N - 1, Char>;
183 }
184 
185 namespace LC
186 {
187  template<Util::CtString S>
188  constexpr auto operator""_ct () noexcept
189  {
190  return S;
191  }
192 }
constexpr detail::AggregateType< detail::AggregateFunction::Count, Ptr > count
Definition: oral.h:1104
static constexpr size_t Size
Definition: ctstring.h:34
QByteArray ToByteArray() noexcept
Definition: ctstring.h:146
requires(Count<=Size) const expr auto Chop() const noexcept
Definition: ctstring.h:87
constexpr Char & operator[](size_t pos) noexcept
Definition: ctstring.h:93
STL namespace.
Q_DECL_IMPORT const QString Count
The new total event count (int).
Char Data_[Size]
Definition: ctstring.h:36
constexpr bool EndsWith(Char ch) const noexcept requires(Size > 0)
Definition: ctstring.h:80
constexpr size_t StringBufSize(const Char *str) noexcept
Definition: ctstring.h:173
CtString(RawStr< N, Char >) -> CtString< N - 1, Char >
const Char(&) [N] RawStr
Definition: ctstring.h:20
QString ToString() noexcept
Definition: ctstring.h:154
static constexpr auto FromUnsized(const Char *s) noexcept
Definition: ctstring.h:47
constexpr bool IsEmpty() const noexcept
Definition: ctstring.h:75
constexpr auto ReplaceAll(CtString< N2, Char > replacement) noexcept
Definition: ctstring.h:130
auto Tup2 &&tup2 noexcept
Definition: ctstringutils.h:68
constexpr auto operator+(RawStr< N1, Char > s1, CtString< N2, Char > s2) noexcept
Definition: ctstring.h:167
constexpr auto operator+(const CtString< N2, Char > &s2) const noexcept
Definition: ctstring.h:55
constexpr CtString() noexcept=default