WebCFace 3.1.1
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
49inline constexpr char field_separator = '.';
50inline constexpr std::string_view field_separator_sv = ".";
54inline constexpr char field_separator_alt = '/';
58inline constexpr std::string_view field_separator_alt_sv = "/";
59
70template <typename CharT>
71class TStringView : public std::basic_string_view<CharT> {
72 std::shared_ptr<const internal::SharedStringData> s_data;
73
74 static inline CharT empty_buf[1] = {0};
75
76 public:
77 TStringView() : std::basic_string_view<CharT>(empty_buf, 0) {}
78
79 TStringView(const CharT *data, std::size_t size,
80 std::shared_ptr<const internal::SharedStringData> s_data)
81 : std::basic_string_view<CharT>(data, size), s_data(std::move(s_data)) {
82 }
87 const CharT *c_str() const { return this->data(); }
88
89 operator const CharT *() const { return this->data(); }
90
91 template <typename T,
92 std::enable_if_t<
93 std::is_convertible_v<T, std::basic_string_view<CharT>>,
94 std::nullptr_t> = nullptr>
95 bool operator==(const T &other) const {
96 return static_cast<std::basic_string_view<CharT>>(*this) ==
97 std::basic_string_view<CharT>(other);
98 }
99 template <typename T,
100 std::enable_if_t<
101 std::is_convertible_v<T, std::basic_string_view<CharT>>,
102 std::nullptr_t> = nullptr>
103 bool operator!=(const T &other) const {
104 return static_cast<std::basic_string_view<CharT>>(*this) !=
105 std::basic_string_view<CharT>(other);
106 }
107 template <typename T,
108 std::enable_if_t<
109 std::is_convertible_v<T, std::basic_string_view<CharT>>,
110 std::nullptr_t> = nullptr>
111 bool operator<=(const T &other) const {
112 return static_cast<std::basic_string_view<CharT>>(*this) <=
113 std::basic_string_view<CharT>(other);
114 }
115 template <typename T,
116 std::enable_if_t<
117 std::is_convertible_v<T, std::basic_string_view<CharT>>,
118 std::nullptr_t> = nullptr>
119 bool operator>=(const T &other) const {
120 return static_cast<std::basic_string_view<CharT>>(*this) >=
121 std::basic_string_view<CharT>(other);
122 }
123 template <typename T,
124 std::enable_if_t<
125 std::is_convertible_v<T, std::basic_string_view<CharT>>,
126 std::nullptr_t> = nullptr>
127 bool operator<(const T &other) const {
128 return static_cast<std::basic_string_view<CharT>>(*this) <
129 std::basic_string_view<CharT>(other);
130 }
131 template <typename T,
132 std::enable_if_t<
133 std::is_convertible_v<T, std::basic_string_view<CharT>>,
134 std::nullptr_t> = nullptr>
135 bool operator>(const T &other) const {
136 return static_cast<std::basic_string_view<CharT>>(*this) >
137 std::basic_string_view<CharT>(other);
138 }
139};
140
143
171 std::shared_ptr<internal::SharedStringData> data;
172
173 public:
174 SharedString() : data() {}
175 SharedString(std::nullptr_t) : data() {}
176 explicit SharedString(std::shared_ptr<internal::SharedStringData> data);
177
178 static SharedString WEBCFACE_CALL fromU8String(std::string u8s);
179 static SharedString WEBCFACE_CALL fromU8StringStatic(std::string_view u8s);
180 static SharedString WEBCFACE_CALL encode(std::string s);
181 static SharedString WEBCFACE_CALL encodeStatic(std::string_view s);
182 static SharedString WEBCFACE_CALL encode(std::wstring ws);
183 static SharedString WEBCFACE_CALL encodeStatic(std::wstring_view ws);
184
185 std::string_view u8StringView() const;
186 StringView u8StringViewShare() const;
187 std::string_view decode() const;
188 StringView decodeShare() const;
189 std::wstring_view decodeW() const;
190 WStringView decodeShareW() const;
191
192 static const std::string &emptyStr();
193 static const std::wstring &emptyStrW();
194
195 bool empty() const;
196 bool startsWith(std::string_view str) const;
197 bool startsWith(char str) const;
198 SharedString substr(std::size_t pos,
199 std::size_t len = std::string::npos) const;
200 std::size_t find(char c, std::size_t pos = 0) const;
201
209 SharedString &normalizeSeparator();
210
211 bool operator==(const SharedString &other) const;
212 bool operator<=(const SharedString &other) const;
213 bool operator>=(const SharedString &other) const;
214 bool operator!=(const SharedString &other) const {
215 return !(*this == other);
216 }
217 bool operator<(const SharedString &other) const {
218 return !(*this >= other);
219 }
220 bool operator>(const SharedString &other) const {
221 return !(*this <= other);
222 }
223};
224
236 public:
238 // move
239 StringInitializer(std::string &&s)
240 : SharedString(SharedString::encode(std::move(s))) {}
241 StringInitializer(std::wstring &&s)
242 : SharedString(SharedString::encode(std::move(s))) {}
243 // copy
244 template <typename T,
245 typename std::enable_if_t<
246 std::conjunction_v<std::negation<std::is_void<T>>,
247 std::is_constructible<std::string, T>>,
248 std::nullptr_t> = nullptr>
250 : SharedString(SharedString::encode(std::string(s))) {}
251 template <typename T,
252 typename std::enable_if_t<
253 std::conjunction_v<
254 std::negation<std::is_void<T>>,
255 std::negation<std::is_constructible<std::string, T>>,
256 std::is_constructible<std::wstring, T>>,
257 std::nullptr_t> = nullptr>
259 : SharedString(SharedString::encode(std::wstring(s))) {}
260 // c-string ptr
261 template <typename CharT, std::size_t N,
262 typename std::enable_if_t<std::is_same_v<CharT, char> ||
263 std::is_same_v<CharT, wchar_t>,
264 std::nullptr_t> = nullptr>
265 StringInitializer(const CharT (&static_str)[N])
266 : SharedString(SharedString::encodeStatic(
267 std::basic_string_view<CharT>(static_str, N - 1))) {}
268};
269
278template <typename CharT, typename... Args>
279std::basic_string<CharT> strJoin(std::basic_string_view<CharT> first_str,
280 Args &&...args) {
281 std::basic_string<CharT> str;
282 str.reserve(first_str.size() +
283 (std::basic_string_view<CharT>(args).size() + ...));
284 str.append(first_str);
285 (str.append(std::basic_string_view<CharT>(args)), ...);
286 return str;
287}
288
u8stringとstringとwstringをshared_ptrで持ち共有する
Definition encoding.h:170
bool operator!=(const SharedString &other) const
Definition encoding.h:214
bool operator<(const SharedString &other) const
Definition encoding.h:217
SharedString()
Definition encoding.h:174
SharedString(std::nullptr_t)
Definition encoding.h:175
bool operator>(const SharedString &other) const
Definition encoding.h:220
SharedString のpublicなコンストラクタインタフェース (入力専用)
Definition encoding.h:235
StringInitializer(const CharT(&static_str)[N])
Definition encoding.h:265
StringInitializer(const T &s)
Definition encoding.h:249
StringInitializer(std::wstring &&s)
Definition encoding.h:241
StringInitializer()
Definition encoding.h:237
StringInitializer(std::string &&s)
Definition encoding.h:239
webcfaceで管理されている文字列を参照するstring_view
Definition encoding.h:71
bool operator==(const T &other) const
Definition encoding.h:95
bool operator!=(const T &other) const
Definition encoding.h:103
bool operator>(const T &other) const
Definition encoding.h:135
bool operator<=(const T &other) const
Definition encoding.h:111
TStringView(const CharT *data, std::size_t size, std::shared_ptr< const internal::SharedStringData > s_data)
Definition encoding.h:79
const CharT * c_str() const
null終端の文字列ポインタを返す
Definition encoding.h:87
bool operator<(const T &other) const
Definition encoding.h:127
TStringView()
Definition encoding.h:77
bool operator>=(const T &other) const
Definition encoding.h:119
constexpr std::string_view field_separator_alt_sv
Definition encoding.h:58
std::string toNarrow(std::wstring_view name_ref)
wstringをstringに変換する
Definition encoding.cc:344
std::basic_string< CharT > strJoin(std::basic_string_view< CharT > first_str, Args &&...args)
string_viewやconst char*同士を連結しstringを返す
Definition encoding.h:279
void usingUTF8(bool flag)
webcfaceが使用するエンコーディングを設定する
Definition encoding.cc:81
bool operator==(const T &other, const InputRef &ref)
Definition text.h:608
std::wstring toWide(std::string_view name_ref)
stringをwstringに変換する
Definition encoding.cc:429
constexpr char field_separator
Definition encoding.h:49
constexpr char field_separator_alt
Definition encoding.h:54
constexpr std::string_view field_separator_sv
Definition encoding.h:50
#define WEBCFACE_DLL
Definition webcface-config.h:69
#define WEBCFACE_CALL
Definition webcface-config.h:106
#define WEBCFACE_NS_END
Definition webcface-config.h:113
#define WEBCFACE_NS_BEGIN
Definition webcface-config.h:112