WebCFace 2.9.0
Web-based Communication Framework & Dashboard-like UI
Loading...
Searching...
No Matches
encoding.h
Go to the documentation of this file.
1#pragma once
2#include <string>
3#include <string_view>
4#include <memory>
5#ifdef WEBCFACE_MESON
6#include "webcface-config.h"
7#else
8#include "webcface/common/webcface-config.h"
9#endif
10
12namespace internal {
13struct SharedStringData;
14} // namespace internal
15
37
42WEBCFACE_DLL std::wstring WEBCFACE_CALL toWide(std::string_view name_ref);
47WEBCFACE_DLL std::string WEBCFACE_CALL toNarrow(std::wstring_view name_ref);
48
59template <typename CharT>
60class TStringView : public std::basic_string_view<CharT> {
61 std::shared_ptr<const internal::SharedStringData> s_data;
62
63 static inline CharT empty_buf[1] = {0};
64
65 public:
66 TStringView() : std::basic_string_view<CharT>(empty_buf, 0) {}
67
68 TStringView(const CharT *data, std::size_t size,
69 std::shared_ptr<const internal::SharedStringData> s_data)
70 : std::basic_string_view<CharT>(data, size), s_data(std::move(s_data)) {
71 }
76 const CharT *c_str() const { return this->data(); }
77
78 operator const CharT *() const { return this->data(); }
79
80 template <typename T,
81 std::enable_if_t<
82 std::is_convertible_v<T, std::basic_string_view<CharT>>,
83 std::nullptr_t> = nullptr>
84 bool operator==(const T &other) const {
85 return static_cast<std::basic_string_view<CharT>>(*this) ==
86 std::basic_string_view<CharT>(other);
87 }
88 template <typename T,
89 std::enable_if_t<
90 std::is_convertible_v<T, std::basic_string_view<CharT>>,
91 std::nullptr_t> = nullptr>
92 bool operator!=(const T &other) const {
93 return static_cast<std::basic_string_view<CharT>>(*this) !=
94 std::basic_string_view<CharT>(other);
95 }
96 template <typename T,
97 std::enable_if_t<
98 std::is_convertible_v<T, std::basic_string_view<CharT>>,
99 std::nullptr_t> = nullptr>
100 bool operator<=(const T &other) const {
101 return static_cast<std::basic_string_view<CharT>>(*this) <=
102 std::basic_string_view<CharT>(other);
103 }
104 template <typename T,
105 std::enable_if_t<
106 std::is_convertible_v<T, std::basic_string_view<CharT>>,
107 std::nullptr_t> = nullptr>
108 bool operator>=(const T &other) const {
109 return static_cast<std::basic_string_view<CharT>>(*this) >=
110 std::basic_string_view<CharT>(other);
111 }
112 template <typename T,
113 std::enable_if_t<
114 std::is_convertible_v<T, std::basic_string_view<CharT>>,
115 std::nullptr_t> = nullptr>
116 bool operator<(const T &other) const {
117 return static_cast<std::basic_string_view<CharT>>(*this) <
118 std::basic_string_view<CharT>(other);
119 }
120 template <typename T,
121 std::enable_if_t<
122 std::is_convertible_v<T, std::basic_string_view<CharT>>,
123 std::nullptr_t> = nullptr>
124 bool operator>(const T &other) const {
125 return static_cast<std::basic_string_view<CharT>>(*this) >
126 std::basic_string_view<CharT>(other);
127 }
128};
129
132
160 std::shared_ptr<internal::SharedStringData> data;
161
162 public:
163 SharedString() : data() {}
164 SharedString(std::nullptr_t) : data() {}
165 explicit SharedString(std::shared_ptr<internal::SharedStringData> data);
166
167 static SharedString WEBCFACE_CALL fromU8String(std::string u8s);
168 static SharedString WEBCFACE_CALL fromU8StringStatic(std::string_view u8s);
169 static SharedString WEBCFACE_CALL encode(std::string s);
170 static SharedString WEBCFACE_CALL encodeStatic(std::string_view s);
171 static SharedString WEBCFACE_CALL encode(std::wstring ws);
172 static SharedString WEBCFACE_CALL encodeStatic(std::wstring_view ws);
173
174 std::string_view u8StringView() const;
175 StringView u8StringViewShare() const;
176 std::string_view decode() const;
177 StringView decodeShare() const;
178 std::wstring_view decodeW() const;
179 WStringView decodeShareW() const;
180
181 static const std::string &emptyStr();
182 static const std::wstring &emptyStrW();
183
184 bool empty() const;
185 bool startsWith(std::string_view str) const;
186 bool startsWith(char str) const;
187 SharedString substr(std::size_t pos,
188 std::size_t len = std::string::npos) const;
189 std::size_t find(char c, std::size_t pos = 0) const;
190
191 bool operator==(const SharedString &other) const;
192 bool operator<=(const SharedString &other) const;
193 bool operator>=(const SharedString &other) const;
194 bool operator!=(const SharedString &other) const {
195 return !(*this == other);
196 }
197 bool operator<(const SharedString &other) const {
198 return !(*this >= other);
199 }
200 bool operator>(const SharedString &other) const {
201 return !(*this <= other);
202 }
203};
204
216 public:
218 // move
219 StringInitializer(std::string &&s)
220 : SharedString(SharedString::encode(std::move(s))) {}
221 StringInitializer(std::wstring &&s)
222 : SharedString(SharedString::encode(std::move(s))) {}
223 // copy
224 template <typename T,
225 typename std::enable_if_t<
226 std::conjunction_v<std::negation<std::is_void<T>>,
227 std::is_constructible<std::string, T>>,
228 std::nullptr_t> = nullptr>
230 : SharedString(SharedString::encode(std::string(s))) {}
231 template <typename T,
232 typename std::enable_if_t<
233 std::conjunction_v<
234 std::negation<std::is_void<T>>,
235 std::negation<std::is_constructible<std::string, T>>,
236 std::is_constructible<std::wstring, T>>,
237 std::nullptr_t> = nullptr>
239 : SharedString(SharedString::encode(std::wstring(s))) {}
240 // c-string ptr
241 template <std::size_t N>
242 StringInitializer(const char (&static_str)[N])
243 : SharedString(
244 SharedString::encodeStatic(std::string_view(static_str, N - 1))) {
245 }
246 template <std::size_t N>
247 StringInitializer(const wchar_t (&static_str)[N])
248 : SharedString(SharedString::encodeStatic(
249 std::wstring_view(static_str, N - 1))) {}
250};
251
260template <typename CharT, typename... Args>
261std::basic_string<CharT> strJoin(std::basic_string_view<CharT> first_str,
262 Args &&...args) {
263 std::basic_string<CharT> str;
264 str.reserve(first_str.size() +
265 (std::basic_string_view<CharT>(args).size() + ...));
266 str.append(first_str);
267 (str.append(std::basic_string_view<CharT>(args)), ...);
268 return str;
269}
270
271namespace [[deprecated("symbols in webcface::encoding namespace are "
272 "now directly in webcface namespace")]] encoding {
273inline bool usingUTF8() { return webcface::usingUTF8(); }
274inline void usingUTF8(bool flag) { webcface::usingUTF8(flag); }
275inline std::wstring toWide(std::string_view name_ref) {
276 return webcface::toWide(name_ref);
277}
278inline std::string toNarrow(std::wstring_view name) {
279 return webcface::toNarrow(name);
280}
281
283
284} // namespace encoding
285
u8stringとstringとwstringをshared_ptrで持ち共有する
Definition encoding.h:159
bool operator!=(const SharedString &other) const
Definition encoding.h:194
bool operator<(const SharedString &other) const
Definition encoding.h:197
SharedString()
Definition encoding.h:163
SharedString(std::nullptr_t)
Definition encoding.h:164
bool operator>(const SharedString &other) const
Definition encoding.h:200
SharedString のpublicなコンストラクタインタフェース (入力専用)
Definition encoding.h:215
StringInitializer(const wchar_t(&static_str)[N])
Definition encoding.h:247
StringInitializer(const char(&static_str)[N])
Definition encoding.h:242
StringInitializer(const T &s)
Definition encoding.h:229
StringInitializer(std::wstring &&s)
Definition encoding.h:221
StringInitializer()
Definition encoding.h:217
StringInitializer(std::string &&s)
Definition encoding.h:219
webcfaceで管理されている文字列を参照するstring_view
Definition encoding.h:60
bool operator==(const T &other) const
Definition encoding.h:84
bool operator!=(const T &other) const
Definition encoding.h:92
bool operator>(const T &other) const
Definition encoding.h:124
bool operator<=(const T &other) const
Definition encoding.h:100
TStringView(const CharT *data, std::size_t size, std::shared_ptr< const internal::SharedStringData > s_data)
Definition encoding.h:68
const CharT * c_str() const
null終端の文字列ポインタを返す
Definition encoding.h:76
bool operator<(const T &other) const
Definition encoding.h:116
TStringView()
Definition encoding.h:66
bool operator>=(const T &other) const
Definition encoding.h:108
std::string toNarrow(std::wstring_view name_ref)
wstringをstringに変換する
Definition encoding.cc:256
std::basic_string< CharT > strJoin(std::basic_string_view< CharT > first_str, Args &&...args)
string_viewやconst char*同士を連結しstringを返す
Definition encoding.h:261
void usingUTF8(bool flag)
webcfaceが使用するエンコーディングを設定する
Definition encoding.cc:51
bool operator==(const T &other, const InputRef &ref)
Definition text.h:641
std::wstring toWide(std::string_view name_ref)
stringをwstringに変換する
Definition encoding.cc:341
bool usingUTF8()
webcfaceが使用するエンコーディングを取得する
Definition encoding.cc:52
#define WEBCFACE_DLL
Definition webcface-config.h:69
#define WEBCFACE_CALL
Definition webcface-config.h:106
#define WEBCFACE_NS_END
Definition webcface-config.h:118
#define WEBCFACE_NS_BEGIN
Definition webcface-config.h:117