LeechCraft  0.6.70-16373-g319c272718
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 
14 class QByteArray;
15 
16 namespace LC::Util
17 {
18  template<size_t N, typename Char = char>
19  using RawStr = const Char (&) [N];
20 
26  template<size_t N, typename Char = char>
27  struct CtString
28  {
29  using Char_t = Char;
30 
33  constexpr static size_t Size = N;
34 
35  Char Data_ [Size] {};
36 
37  constexpr CtString () noexcept = default;
38 
39  constexpr CtString (RawStr<N + 1, Char> s) noexcept
40  {
41  std::copy (s, s + Size, Data_);
42  }
43 
44  constexpr auto operator<=> (const CtString&) const = default;
45 
46  constexpr static auto FromUnsized (const Char *s) noexcept
47  {
48  CtString result {};
49  std::copy (s, s + Size, result.Data_);
50  return result;
51  }
52 
53  template<size_t N2>
54  constexpr auto operator+ (const CtString<N2, Char>& s2) const noexcept
55  {
56  // TODO clang bug, use s2.Size otherwise
58  std::copy (Data_, Data_ + Size, result.Data_);
59  std::copy (s2.Data_, s2.Data_ + s2.Size, result.Data_ + Size);
60  return result;
61  }
62 
63  template<size_t N2>
64  constexpr auto operator+ (RawStr<N2, Char> s2) const noexcept
65  {
66  return *this + CtString<N2 - 1, Char> { s2 };
67  }
68 
69  constexpr auto operator+ (Char ch) const noexcept
70  {
71  return *this + CtString<1, Char> { { ch } };
72  }
73 
74  constexpr bool IsEmpty () const noexcept
75  {
76  return !Size;
77  }
78 
79  constexpr bool EndsWith (Char ch) const noexcept
80  requires (Size > 0)
81  {
82  return Data_ [Size - 1] == ch;
83  }
84 
85  template<size_t Count>
86  requires (Count <= Size)
87  [[nodiscard]] constexpr auto Chop () const noexcept
88  {
90  }
91 
92  constexpr Char& operator[] (size_t pos) noexcept
93  {
94  return Data_ [pos];
95  }
96 
97  constexpr Char operator[] (size_t pos) const noexcept
98  {
99  return Data_ [pos];
100  }
101 
102  constexpr operator QStringView () const noexcept
103  requires std::is_same_v<Char, char16_t>
104  {
105  return QStringView { Data_, Size };
106  }
107 
108  constexpr auto Data () const noexcept
109  {
110  return Data_;
111  }
112  };
113 
114  template<CtString Str>
115  QByteArray ToByteArray ()
116  {
117  static constexpr auto literal = []<size_t... Idxes> (std::index_sequence<Idxes...>)
118  {
119  return QStaticByteArrayData<Str.Size>
120  {
121  Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER (Str.Size),
122  { Str.Data_ [Idxes]..., 0 }
123  };
124  } (std::make_index_sequence<Str.Size> {});
125  QByteArrayDataPtr holder { literal.data_ptr () };
126  return QByteArray { holder };
127  }
128 
129  template<CtString Str>
130  QString ToString ()
131  {
132  static constexpr auto literal = []<size_t... Idxes> (std::index_sequence<Idxes...>)
133  {
134  return QStaticStringData<Str.Size>
135  {
136  Q_STATIC_STRING_DATA_HEADER_INITIALIZER (Str.Size),
137  { Str.Data_ [Idxes]..., 0 }
138  };
139  } (std::make_index_sequence<Str.Size> {});
140  QStringDataPtr holder { literal.data_ptr () };
141  return QString { holder };
142  }
143 
144  template<size_t N1, size_t N2, typename Char>
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>
160  CtString (RawStr<N, Char>) -> CtString<N - 1, Char>;
161 }
162 
163 namespace LC
164 {
165  template<Util::CtString S>
166  constexpr auto operator""_ct () noexcept
167  {
168  return S;
169  }
170 }
static constexpr size_t Size
Definition: ctstring.h:33
requires(Count<=Size) const expr auto Chop() const noexcept
Definition: ctstring.h:86
constexpr Char & operator[](size_t pos) noexcept
Definition: ctstring.h:92
STL namespace.
Char Data_[Size]
Definition: ctstring.h:35
constexpr bool EndsWith(Char ch) const noexcept requires(Size > 0)
Definition: ctstring.h:79
constexpr size_t StringBufSize(const Char *str) noexcept
Definition: ctstring.h:151
CtString(RawStr< N, Char >) -> CtString< N - 1, Char >
QString ToString()
Definition: ctstring.h:130
const Char(&) [N] RawStr
Definition: ctstring.h:19
static constexpr auto FromUnsized(const Char *s) noexcept
Definition: ctstring.h:46
QByteArray ToByteArray()
Definition: ctstring.h:115
constexpr bool IsEmpty() const noexcept
Definition: ctstring.h:74
auto Tup2 &&tup2 noexcept
Definition: ctstringutils.h:41
constexpr auto operator+(RawStr< N1, Char > s1, CtString< N2, Char > s2) noexcept
Definition: ctstring.h:145
constexpr auto operator+(const CtString< N2, Char > &s2) const noexcept
Definition: ctstring.h:54
constexpr CtString() noexcept=default