YSTest: string_view.hpp 源文件

YSTest PreAlpha_b800_20170810 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

string_view.hpp
浏览该文件的文档.
1 /*
2 © 2015-2016 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
33 #ifndef YB_INC_ystdex_string_view_hpp_
34 #define YB_INC_ystdex_string_view_hpp_ 1
35
36 #include "string.hpp" // for std::char_traits, std::basic_string,
37 // std::allocator, 'str_find*', 'str_rfind*', std::basic_ostream;
38 #include "operators.hpp" // for totally_ordered;
39 #include <limits> // for std::numeric_limits;
40 #include <stdexcept> // for std::out_of_range;
41 #include "hash.hpp" // for ystdex::hash_range;
42
43 namespace ystdex
44 {
45
47
48 template<typename _tChar, class _tTraits = std::char_traits<_tChar>>
49 class basic_string_view
50 : yimpl(private totally_ordered<basic_string_view<_tChar, _tTraits>>)
51 {
52 public:
53 using traits_type = _tTraits;
54 using value_type = _tChar;
55 using pointer = _tChar*;
56 using const_pointer = const _tChar*;
57 using reference = _tChar&;
58 using const_reference = const _tChar&;
59 using const_iterator = yimpl(const _tChar*);
60 using iterator = const_iterator;
61 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
62 using reverse_iterator = const_reverse_iterator;
63 using size_type = size_t;
64 using difference_type = ptrdiff_t;
65 static yconstexpr size_type npos = size_type(-1);
66
67 private:
68 const_pointer data_;
69 size_type size_;
70
71 public:
72 yconstfn
73 basic_string_view() ynothrow
74 : data_(), size_()
75 {}
76 yconstfn YB_NONNULL(2)
77 basic_string_view(const _tChar* str)
78 : data_(str), size_(traits_type::length(str))
79 {}
80 yconstfn YB_NONNULL(2)
81 basic_string_view(const _tChar* str, size_type len)
82 : data_(str), size_(len)
83 {}
84 template<class _tAlloc>
85 basic_string_view(const std::basic_string<_tChar, _tTraits, _tAlloc>& str)
86 ynothrow
87 : data_(str.data()), size_(str.size())
88 {}
89 yconstfn
90 basic_string_view(const basic_string_view&) ynothrow = default;
91
92 basic_string_view&
93 operator=(const basic_string_view&) ynothrow = default;
94
95 yconstfn const_iterator
96 begin() const ynothrow
97 {
98 return data_;
99 }
100
101 yconstfn const_iterator
102 end() const ynothrow
103 {
104 return data_ + size_;
105 }
106
107 yconstfn const_iterator
108 cbegin() const ynothrow
109 {
110 return data_;
111 }
112
113 yconstfn const_iterator
114 cend() const ynothrow
115 {
116 return data_ + size_;
117 }
118
119 const_reverse_iterator
120 rbegin() const ynothrow
121 {
122 return const_reverse_iterator(end());
123 }
124
125 const_reverse_iterator
126 rend() const ynothrow
127 {
128 return const_reverse_iterator(begin());
129 }
130
131 const_reverse_iterator
132 crbegin() const ynothrow
133 {
134 return const_reverse_iterator(cend());
135 }
136
137 const_reverse_iterator
138 crend() const ynothrow
139 {
140 return const_reverse_iterator(cbegin());
141 }
142
143 yconstfn size_type
144 size() const ynothrow
145 {
146 return size_;
147 }
148
149 yconstfn size_type
150 length() const ynothrow
151 {
152 return size_;
153 }
154
155 yconstfn size_type
156 max_size() const ynothrow
157 {
158 // NOTE: This is same to libc++ but not libstdc++.
159 return std::numeric_limits<size_type>::max();
160 }
161
162 yconstfn bool
163 empty() const ynothrow
164 {
165 return size_ == 0;
166 }
167
168 yconstfn const_reference
169 operator[](size_type pos) const
170 {
171 return yconstraint(pos < size_), data_[pos];
172 }
173
174 yconstfn const_reference
175 at(size_type pos) const
176 {
177 return pos < size_ ? data_[pos]
178 : (throw std::out_of_range("string_view::at"), data_[0]);
179 }
180
181 yconstfn const_reference
182 front() const
183 {
184 return yconstraint(0 < size_), data_[0];
185 }
186
187 yconstfn const_reference
188 back() const
189 {
190 return yconstraint(0 < size_), data_[size_ - 1];
191 }
192
193 yconstfn const_pointer
194 data() const ynothrow
195 {
196 return data_;
197 }
198
199 yconstfn_relaxed void
200 remove_prefix(size_type n) ynothrowv
201 {
202 yconstraint(n <= size_), yunseq(data_ += n, size_ -= n);
203 }
204
205 yconstfn_relaxed void
206 remove_suffix(size_type n) ynothrowv
207 {
208 yconstraint(n <= size_), size_ -= n;
209 }
210
211 yconstfn_relaxed void
212 swap(basic_string_view& s) ynothrow
213 {
214 std::swap(data_, s.data_),
215 std::swap(size_, s.size_);
216 }
217
219
220 template<class _tAlloc>
221 explicit
222 operator std::basic_string<_tChar, _tTraits, _tAlloc>() const
223 {
224 yconstraint(data_);
225 return {data_, size_};
226 }
227
228 template<class _tAlloc = std::allocator<_tChar>>
229 std::basic_string<_tChar, _tTraits, _tAlloc>
230 to_string(const _tAlloc& a = _tAlloc()) const
231 {
232 yconstraint(data_);
233 return {data_, size_, a};
234 }
236
237 YB_NONNULL(2) size_type
238 copy(_tChar* s, size_type n, size_type pos = 0) const
239 {
240 yconstraint(s);
241 if(pos <= size_)
242 {
243 const auto rlen(std::min<size_type>(n, size_ - pos));
244
245 std::copy_n(data_ + pos, rlen, s);
246 return rlen;
247 }
248 throw std::out_of_range("string_view::copy");
249 }
250
251 yconstfn basic_string_view
252 substr(size_type pos = 0, size_type n = npos) const
253 {
254 return pos <= size_ ? basic_string_view(data_ + pos,
255 std::min(n, size_type(size_ - pos)))
256 : throw std::out_of_range("string_view::substr");
257 }
258
259 yconstfn int
260 compare(basic_string_view s) const ynothrow
261 {
262 return compare_remained(s, _tTraits::compare(data(), s.data(),
263 std::min<size_type>(size(), s.size())));
264 }
265 yconstfn int
266 compare(size_type pos1, size_type n1, basic_string_view s) const
267 {
268 return substr(pos1, n1).compare(s);
269 }
270 yconstfn int
271 compare(size_type pos1, size_type n1, basic_string_view s, size_type pos2,
272 size_type n2) const
273 {
274 return substr(pos1, n1).compare(s.substr(pos2, n2));
275 }
276 yconstfn YB_NONNULL(2) int
277 compare(const _tChar* s) const
278 {
279 return compare(basic_string_view(s));
280 }
281 yconstfn YB_NONNULL(4) int
282 compare(size_type pos1, size_type n1, const _tChar* s) const
283 {
284 return substr(pos1, n1).compare(basic_string_view(s));
285 }
286 yconstfn YB_NONNULL(4) int
287 compare(size_type pos1, size_type n1, const _tChar* s, size_type n2) const
288 {
289 return substr(pos1, n1).compare(basic_string_view(s, n2));
290 }
291
292 private:
293 yconstfn int
294 compare_remained(basic_string_view s, int ret) const ynothrow
295 {
296 return ret == 0 ? (size() == s.size() ? 0
297 : (size() < s.size() ? -1 : 1)) : ret;
298 }
299
300 public:
301 #define YB_Impl_StringView_search_fn_head(_n, _attr, _spec, ...) \
302 yconstfn_relaxed _attr size_type \
303 _n(__VA_ARGS__) const _spec \
304 {
305 #define YB_Impl_StringView_search1(_n, _arg) \
306 YB_Impl_StringView_search_fn_head(_n, , ynothrow, basic_string_view s, \
307 size_type pos = _arg) \
308 return ystdex::str_##_n<value_type, size_type, traits_type, \
309 npos>(data_, size_, s.data_, pos, s.size_); \
310 }
311 #define YB_Impl_StringView_search34(_n, _arg) \
312 YB_Impl_StringView_search_fn_head(_n, YB_NONNULL(2), , const _tChar* s, \
313 size_type pos, size_type n) \
314 return yconstraint(s), _n(basic_string_view(s, n), pos); \
315 } \
316 YB_Impl_StringView_search_fn_head(_n, YB_NONNULL(2), , const _tChar* s, \
317 size_type pos = _arg) \
318 return _n(s, pos, traits_type::length(s)); \
319 }
320 #define YB_Impl_StringView_search_mf(_n, _arg) \
321 YB_Impl_StringView_search1(_n, _arg) \
322 YB_Impl_StringView_search_fn_head(_n, , ynothrow, _tChar c, \
323 size_type pos = _arg) \
324 return ystdex::str_##_n<value_type, size_type, traits_type, \
325 npos>(data_, size_, c, pos); \
326 } \
327 YB_Impl_StringView_search34(_n, _arg)
328 #define YB_Impl_StringView_search_mf2(_n, _arg, _n2) \
329 YB_Impl_StringView_search1(_n, _arg) \
330 YB_Impl_StringView_search_fn_head(_n, , ynothrow, _tChar c, \
331 size_type pos = _arg) \
332 return _n2(c, pos); \
333 } \
334 YB_Impl_StringView_search34(_n, _arg)
335
336 YB_Impl_StringView_search_mf(find, 0)
337
338 YB_Impl_StringView_search_mf(rfind, npos)
339
340 YB_Impl_StringView_search_mf2(find_first_of, 0, find)
341
342 YB_Impl_StringView_search_mf2(find_last_of, npos, rfind)
343
344 YB_Impl_StringView_search_mf(find_first_not_of, 0)
345
346 YB_Impl_StringView_search_mf(find_last_not_of, npos)
347
348 #undef YB_Impl_StringView_search_mf
349 #undef YB_Impl_StringView_search_mf2
350 #undef YB_Impl_StringView_search1
351 #undef YB_Impl_StringView_search34
352 #undef YB_Impl_StringView_search_fn_head
353 };
354
356
357 template<typename _tChar, class _tTraits>
358 yconstfn bool
359 operator==(basic_string_view<_tChar, _tTraits> x,
360 basic_string_view<_tChar, _tTraits> y) ynothrow
361 {
362 return x.compare(y) == 0;
363 }
364 template<typename _tChar, class _tTraits>
365 yconstfn bool
366 operator==(basic_string_view<_tChar, _tTraits> x,
367 yimpl(decay_t<basic_string_view<_tChar, _tTraits>>) y) ynothrow
368 {
369 return x.compare(y) == 0;
370 }
372 template<typename _tChar, class _tTraits>
373 yconstfn bool
374 operator==(yimpl(decay_t<basic_string_view<_tChar, _tTraits>>) x,
375 basic_string_view<_tChar, _tTraits> y) ynothrow
376 {
377 return x.compare(y) == 0;
378 }
379
380 template<typename _tChar, class _tTraits>
381 yconstfn bool
382 operator<(basic_string_view<_tChar, _tTraits> x,
383 basic_string_view<_tChar, _tTraits> y) ynothrow
384 {
385 return x.compare(y) < 0;
386 }
388 template<typename _tChar, class _tTraits>
389 yconstfn bool
390 operator<(basic_string_view<_tChar, _tTraits> x,
391 yimpl(decay_t<basic_string_view<_tChar, _tTraits>>) y) ynothrow
392 {
393 return x.compare(y) < 0;
394 }
396 template<typename _tChar, class _tTraits>
397 yconstfn bool
398 operator<(yimpl(decay_t<basic_string_view<_tChar, _tTraits>>) x,
399 basic_string_view<_tChar, _tTraits> y) ynothrow
400 {
401 return x.compare(y) < 0;
402 }
403
404 template<typename _tChar, class _tTraits>
405 std::basic_ostream<_tChar, _tTraits>&
406 operator<<(std::basic_ostream<_tChar, _tTraits>& os,
407 basic_string_view<_tChar, _tTraits> str)
408 {
409 // XXX: Better implementation?
410 return os << str.to_string();
411 }
413
414
415 using string_view = basic_string_view<char>;
416 using u16string_view = basic_string_view<char16_t>;
417 using u32string_view = basic_string_view<char32_t>;
418 using wstring_view = basic_string_view<wchar_t>;
420
421 } // namespace ystdex;
422
423
424 namespace std
425 {
426
433 template<typename>
434 struct hash;
435
436 template<typename _tChar, class _tTraits>
437 struct hash<ystdex::basic_string_view<_tChar, _tTraits>>
438 {
439 size_t
440 operator()(const ystdex::string_view& k) const yimpl(ynothrow)
441 {
442 return ystdex::hash_range(k.data(), k.data() + k.size());
443 }
444 };
446
447 } // namespace std;
448
449 #endif
450
ystdex::basic_string_view::end
const_iterator end() const
Definition: string_view.hpp:102
ystdex::basic_string_view::copy
size_type copy(_tChar *s, size_type n, size_type pos=0) const
Definition: string_view.hpp:238
std::hash< ystdex::basic_string_view< _tChar, _tTraits > >::operator()
size_t operator()(const ystdex::string_view &k) const
Definition: string_view.hpp:440
ystdex::basic_string_view
Definition: string_view.hpp:49
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:680
ystdex::basic_string_view::const_pointer
const _tChar * const_pointer
Definition: string_view.hpp:56
ystdex::basic_string_view::compare
int compare(basic_string_view s) const
Definition: string_view.hpp:260
ystdex::basic_string_view::npos
static size_type npos
Definition: string_view.hpp:65
ystdex::basic_string_view::operator[]
const_reference operator[](size_type pos) const
Definition: string_view.hpp:169
ystdex::basic_string_view::const_reference
const _tChar & const_reference
Definition: string_view.hpp:58
hash.hpp
散列接口。
ystdex::basic_string_view::compare
int compare(size_type pos1, size_type n1, basic_string_view s, size_type pos2, size_type n2) const
Definition: string_view.hpp:271
ystdex::basic_string_view::find_first_not_of
size_type find_first_not_of(basic_string_view s, size_type pos=0) const
Definition: string_view.hpp:344
ystdex::basic_string_view::rend
const_reverse_iterator rend() const
Definition: string_view.hpp:126
YSLib::UI::y
SPos SPos y
Definition: YWidget.cpp:44
NPL::x
const TermNode &term ynothrow const TermNode &term ynothrow const TermNode &term ynothrow TermNode &term const TermNode &term ValueNode &node const ValueNode &node const _type & x
Definition: SContext.h:94
platform::size_t
YF_API size_t
Definition: FileIO.h:1353
ystdex::basic_string_view::to_string
std::basic_string< _tChar, _tTraits, _tAlloc > to_string(const _tAlloc &a=_tAlloc()) const
Definition: string_view.hpp:230
ystdex::basic_string_view::data_
const_pointer data_
Definition: string_view.hpp:68
YB_Impl_StringView_search_mf2
#define YB_Impl_StringView_search_mf2(_n, _arg, _n2)
Definition: string_view.hpp:328
ystdex::basic_string_view::remove_prefix
void remove_prefix(size_type n)
Definition: string_view.hpp:200
yimpl
#define yimpl(...)
实现标签。
Definition: ydef.h:262
yconstfn_relaxed
#define yconstfn_relaxed
指定编译时没有 C++11 限制和隐式成员 const 的常量函数。
Definition: ydef.h:692
ystdex::basic_string_view::crbegin
const_reverse_iterator crbegin() const
Definition: string_view.hpp:132
ystdex::basic_string_view::basic_string_view
basic_string_view()
Definition: string_view.hpp:73
ystdex::basic_string_view::reference
_tChar & reference
Definition: string_view.hpp:57
yconstexpr
#define yconstexpr
指定编译时常量表达式。
Definition: ydef.h:679
ystdex::max
const _type & max(const _type &a, const _type &b, _fComp comp=less< _type >())
取较大的元素。
Definition: algorithm.hpp:549
ystdex::basic_string_view::traits_type
_tTraits traits_type
Definition: string_view.hpp:53
yunseq
#define yunseq
无序列依赖表达式组求值。
Definition: ydef.h:1022
ystdex::basic_string_view::compare
int compare(size_type pos1, size_type n1, basic_string_view s) const
Definition: string_view.hpp:266
ystdex::basic_string_view::find_last_of
size_type find_last_of(basic_string_view s, size_type pos=npos) const
Definition: string_view.hpp:342
ystdex::basic_string_view::reverse_iterator
const_reverse_iterator reverse_iterator
Definition: string_view.hpp:62
ystdex::basic_string_view::length
size_type length() const
Definition: string_view.hpp:150
ystdex::basic_string_view::difference_type
ptrdiff_t difference_type
Definition: string_view.hpp:64
YSLib::a
YB_PURE const _type * a
Definition: YCoreUtilities.h:167
ystdex::basic_string_view::begin
const_iterator begin() const
Definition: string_view.hpp:96
ystdex::basic_string_view::crend
const_reverse_iterator crend() const
Definition: string_view.hpp:138
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:766
ystdex::cpp2014::decay_t
typename decay< _type >::type decay_t
Definition: type_traits.hpp:340
ystdex::basic_string_view::back
const_reference back() const
Definition: string_view.hpp:188
CHRLib::s
_tDst const _tSrc * s
Definition: CharacterProcessing.h:140
ystdex
特性测试宏。
Definition: ydef.h:822
ystdex::basic_string_view::size
size_type size() const
Definition: string_view.hpp:144
std::hash
ystdex::observer_ptr 散列支持。
Definition: memory.hpp:1193
ystdex::operator==
bool operator==(nullptr_t lhs, const _type &rhs)
Definition: ydef.h:895
ystdex::basic_string_view::max_size
size_type max_size() const
Definition: string_view.hpp:156
ystdex::min
const _type & min(const _type &a, const _type &b, _fComp comp=less< _type >())
取较小的元素。
Definition: algorithm.hpp:527
YB_Impl_StringView_search_mf
#define YB_Impl_StringView_search_mf(_n, _arg)
Definition: string_view.hpp:320
ystdex::basic_string_view::value_type
_tChar value_type
Definition: string_view.hpp:54
ystdex::basic_string_view::empty
bool empty() const
Definition: string_view.hpp:163
ystdex::basic_string_view::size_type
size_t size_type
Definition: string_view.hpp:63
operators.hpp
重载操作符。
ynothrowv
#define ynothrowv
YSLib 无异常抛出保证验证：有条件地使用无异常抛出规范。
Definition: ydef.h:786
string.hpp
ISO C++ 标准字符串扩展。
ystdex::swap
void swap(container_adaptor< _tSeqCon > &x, container_adaptor< _tSeqCon > &y)
Definition: container.hpp:179
ystdex::basic_string_view::size_
size_type size_
Definition: string_view.hpp:69
ystdex::basic_string_view::iterator
const_iterator iterator
Definition: string_view.hpp:60
ystdex::basic_string_view::find_last_not_of
size_type find_last_not_of(basic_string_view s, size_type pos=npos) const
Definition: string_view.hpp:346
std
Definition: memory.hpp:1183
ystdex::basic_string_view::cend
const_iterator cend() const
Definition: string_view.hpp:114
ystdex::basic_string_view::front
const_reference front() const
Definition: string_view.hpp:182
ystdex::basic_string_view::rfind
size_type rfind(basic_string_view s, size_type pos=npos) const
Definition: string_view.hpp:338
ystdex::basic_string_view::rbegin
const_reverse_iterator rbegin() const
Definition: string_view.hpp:120
YSLib::Text::str
const char const char * str
Definition: TextFile.h:81
ystdex::basic_string_view::const_iterator
const _tChar * const_iterator
Definition: string_view.hpp:59
ystdex::basic_string_view::find
size_type find(basic_string_view s, size_type pos=0) const
Definition: string_view.hpp:336
ystdex::basic_string_view::remove_suffix
void remove_suffix(size_type n)
Definition: string_view.hpp:206
ystdex::basic_string_view::swap
void swap(basic_string_view &s)
Definition: string_view.hpp:212
ystdex::basic_string_view::basic_string_view
basic_string_view(const std::basic_string< _tChar, _tTraits, _tAlloc > &str)
Definition: string_view.hpp:85
ystdex::hash_range
size_t hash_range(size_t seed, _tIn first, _tIn last)
重复对范围计算散列。
Definition: hash.hpp:90
ystdex::basic_string_view::data
const_pointer data() const
Definition: string_view.hpp:194
ystdex::basic_string_view::compare_remained
int compare_remained(basic_string_view s, int ret) const
Definition: string_view.hpp:294
ystdex::basic_string_view::pointer
_tChar * pointer
Definition: string_view.hpp:55
YSLib::IO::int
YF_API int
Definition: File.h:51
yconstraint
#define yconstraint(_expr)
约束：接口语义。
Definition: cassert.h:88
ystdex::basic_string_view::const_reverse_iterator
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: string_view.hpp:61
ystdex::basic_string_view::find_first_of
size_type find_first_of(basic_string_view s, size_type pos=0) const
Definition: string_view.hpp:340
ystdex::basic_string_view::at
const_reference at(size_type pos) const
Definition: string_view.hpp:175
ystdex::basic_string_view::cbegin
const_iterator cbegin() const
Definition: string_view.hpp:108
ystdex::basic_string_view::substr
basic_string_view substr(size_type pos=0, size_type n=npos) const
Definition: string_view.hpp:252