YSTest: tstring_view.hpp 源文件

YSTest PreAlpha_b800_20170810 The YSLib Test Project

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

- 文件列表
- 文件成员

tstring_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
28 #ifndef YB_INC_ystdex_tstring_view_hpp_
29 #define YB_INC_ystdex_tstring_view_hpp_ 1
30
31 #include "string_view.hpp" // for std::char_traits, basic_string_view,
32 // totally_ordered, std::basic_string, std::allocator, std::out_of_range,
33 // ntctsncmp;
34
35 namespace ystdex
36 {
37
45 template<typename _tChar, class _tTraits = std::char_traits<_tChar>,
46 char32_t _vTerm = 0>
47 class basic_tstring_view;
48
49 template<typename _tChar, class _tTraits>
50 class basic_tstring_view<_tChar, _tTraits, 0> : private
51 basic_string_view<_tChar, _tTraits>, yimpl(private
52 totally_ordered<basic_tstring_view<_tChar, _tTraits, 0>>)
53 {
54 public:
55 using traits_type = _tTraits;
56 using value_type = _tChar;
57 using pointer = _tChar*;
58 using const_pointer = const _tChar*;
59 using reference = _tChar&;
60 using const_reference = const _tChar&;
61 using const_iterator = yimpl(const _tChar*);
62 using iterator = const_iterator;
63 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
64 using reverse_iterator = const_reverse_iterator;
65 using size_type = size_t;
66 using difference_type = ptrdiff_t;
67 using base = basic_string_view<_tChar, _tTraits>;
68 using base::npos;
69 static yconstexpr value_type term = {};
70
71 yconstfn
72 basic_tstring_view() = default;
73 yconstfn YB_NONNULL(2)
74 basic_tstring_view(const _tChar* str)
75 : base(str)
76 {}
77 yconstfn YB_NONNULL(2)
78 basic_tstring_view(const _tChar* str, size_type len)
79 : base(verify(str, len) ? str
80 : (throw std::invalid_argument("Invalid string found."), str), len)
81 {}
83 basic_tstring_view(base sv)
84 : basic_tstring_view(sv.data(), sv.length())
85 {}
86 template<class _tAlloc>
87 basic_tstring_view(const std::basic_string<_tChar, _tTraits, _tAlloc>& str)
88 ynothrow
89 : basic_tstring_view(str.data())
90 {}
91 yconstfn
92 basic_tstring_view(const basic_tstring_view&) ynothrow = default;
93
94 basic_tstring_view&
95 operator=(const basic_tstring_view&) ynothrow = default;
96
97 using base::begin;
98
99 using base::end;
100
101 using base::cbegin;
102
103 using base::cend;
104
105 using base::rbegin;
106
107 using base::rend;
108
109 using base::crbegin;
110
111 using base::crend;
112
113 using base::size;
114
115 using base::length;
116
117 using base::max_size;
118
119 using base::empty;
120
121 using base::operator[];
122
123 using base::at;
124
125 using base::front;
126
127 using base::back;
128
129 using base::data;
130
131 using base::remove_prefix;
132
133 yconstfn_relaxed void
134 swap(basic_tstring_view& s) ynothrow
135 {
136 base::swap(static_cast<base&>(s));
137 }
138
139 template<class _tAlloc = std::allocator<_tChar>>
140 explicit
141 operator std::basic_string<_tChar, _tTraits, _tAlloc>() const
142 {
143 return std::basic_string<_tChar, _tTraits, _tAlloc>(
144 static_cast<const base&>(*this));
145 }
146
147 operator base() const ynothrow
148 {
149 return *this;
150 }
151
152 using base::to_string;
153
154 using base::copy;
155
156 yconstfn basic_tstring_view
157 substr(size_type pos = 0) const
158 {
159 return pos <= size()
160 ? basic_tstring_view(data() + pos, size_type(size() - pos))
161 : throw std::out_of_range("tstring_view::substr");
162 }
163
164 yconstfn int
165 compare(basic_tstring_view s) const ynothrow
166 {
167 return compare_remained(s, ystdex::ntctscmp(data(), s.data(),
168 std::min<size_type>(size(), s.size())));
169 }
170 yconstfn int
171 compare(size_type pos1, size_type n1, basic_tstring_view s) const
172 {
173 return substr(pos1, n1).compare(s);
174 }
175 yconstfn int
176 compare(size_type pos1, size_type n1, basic_tstring_view s, size_type pos2,
177 size_type n2) const
178 {
179 return substr(pos1, n1).compare(s.substr(pos2, n2));
180 }
181 yconstfn int
182 compare(const _tChar* s) const
183 {
184 return compare(basic_tstring_view(s));
185 }
186 yconstfn int
187 compare(size_type pos1, size_type n1, const _tChar* s) const
188 {
189 return substr(pos1, n1).compare(basic_tstring_view(s));
190 }
191 yconstfn int
192 compare(size_type pos1, size_type n1, const _tChar* s, size_type n2) const
193 {
194 return substr(pos1, n1).compare(basic_tstring_view(s, n2));
195 }
196
197 private:
198 yconstfn int
199 compare_remained(basic_tstring_view s, int ret) const ynothrow
200 {
201 return ret == 0 ? (size() == s.size() ? 0
202 : (size() < s.size() ? -1 : 1)) : ret;
203 }
204
205 public:
206 using base::find;
207 using base::rfind;
208 using base::find_first_of;
209 using base::find_last_of;
210 using base::find_first_not_of;
211 using base::find_last_not_of;
212
214
215 static YB_NONNULL(1) bool
216 verify(const _tChar* str, size_type len) ynothrowv
217 {
218 yconstraint(str);
219 return traits_type::length(str) == len;
220 }
221
222 static YB_NONNULL(1) bool
223 verify_term(const _tChar* str, size_type len) ynothrowv
224 {
225 yconstraint(str);
226 return str[len] == term;
227 }
229 };
230
236 template<typename _tChar, class _tTraits>
237 yconstfn bool
238 operator==(basic_tstring_view<_tChar, _tTraits> x,
239 basic_tstring_view<_tChar, _tTraits> y) ynothrow
240 {
241 return x.compare(y) == 0;
242 }
243 template<typename _tChar, class _tTraits>
244 yconstfn bool
245 operator==(basic_tstring_view<_tChar, _tTraits> x,
246 yimpl(decay_t<basic_tstring_view<_tChar, _tTraits>>) y) ynothrow
247 {
248 return x.compare(y) == 0;
249 }
250 template<typename _tChar, class _tTraits>
251 yconstfn bool
252 operator==(yimpl(decay_t<basic_tstring_view<_tChar, _tTraits>>) x,
253 basic_tstring_view<_tChar, _tTraits> y) ynothrow
254 {
255 return x.compare(y) == 0;
256 }
257
258 template<typename _tChar, class _tTraits>
259 yconstfn bool
260 operator<(basic_tstring_view<_tChar, _tTraits> x,
261 basic_tstring_view<_tChar, _tTraits> y) ynothrow
262 {
263 return x.compare(y) < 0;
264 }
265 template<typename _tChar, class _tTraits>
266 yconstfn bool
267 operator<(basic_tstring_view<_tChar, _tTraits> x,
268 yimpl(decay_t<basic_tstring_view<_tChar, _tTraits>>) y) ynothrow
269 {
270 return x.compare(y) < 0;
271 }
272 template<typename _tChar, class _tTraits>
273 yconstfn bool
274 operator<(yimpl(decay_t<basic_tstring_view<_tChar, _tTraits>>) x,
275 basic_tstring_view<_tChar, _tTraits> y) ynothrow
276 {
277 return x.compare(y) < 0;
278 }
279
280 template<typename _tChar, class _tTraits>
281 std::basic_ostream<_tChar, _tTraits>&
282 operator<<(std::basic_ostream<_tChar, _tTraits>& os,
283 basic_tstring_view<_tChar, _tTraits> str)
284 {
285 // XXX: Better implementation?
286 return os << str.to_string();
287 }
289
290
291 using tstring_view = basic_tstring_view<char>;
292 using u16tstring_view = basic_tstring_view<char16_t>;
293 using u32tstring_view = basic_tstring_view<char32_t>;
294 using wtstring_view = basic_tstring_view<wchar_t>;
296
297 } // namespace ystdex;
298
299 #endif
300
ystdex::basic_tstring_view< _tChar, _tTraits, 0 >::basic_tstring_view
basic_tstring_view(base sv)
Definition: tstring_view.hpp:83
ystdex::basic_tstring_view< _tChar, _tTraits, 0 >::compare
int compare(size_type pos1, size_type n1, const _tChar *s) const
Definition: tstring_view.hpp:187
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::to_string
std::string to_string(const std::thread::id &)
转换 thread::id 为字符串表示。
ystdex::basic_string_view::const_reference
const _tChar & const_reference
Definition: string_view.hpp:58
ystdex::basic_tstring_view< _tChar, _tTraits, 0 >::basic_tstring_view
basic_tstring_view(const std::basic_string< _tChar, _tTraits, _tAlloc > &str)
Definition: tstring_view.hpp:87
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::cpp2014::rbegin
auto rbegin(_tRange &c) -> decltype(c.rbegin())
Definition: range.hpp:94
ystdex::ntctscmp
int ntctscmp(const _tChar *s1, const _tChar *s2)
按字典序比较简单 NTCTS 。
Definition: cstring.h:154
platform::bool
std::FILE *fp bool
Definition: FileIO.h:1280
yimpl
#define yimpl(...)
实现标签。
Definition: ydef.h:262
ystdex::cpp2014::crend
auto crend(const _tRange &c) -> decltype(ystdex::rend(c))
Definition: range.hpp:150
yconstfn_relaxed
#define yconstfn_relaxed
指定编译时没有 C++11 限制和隐式成员 const 的常量函数。
Definition: ydef.h:692
ystdex::cpp2014::cend
auto cend(const _tRange &c) -> decltype(std::end(c))
Definition: range.hpp:87
ystdex::basic_string_view::reference
_tChar & reference
Definition: string_view.hpp:57
yconstexpr
#define yconstexpr
指定编译时常量表达式。
Definition: ydef.h:679
ystdex::basic_tstring_view< _tChar, _tTraits, 0 >::compare
int compare(size_type pos1, size_type n1, basic_tstring_view s) const
Definition: tstring_view.hpp:171
ystdex::data
auto data(_tRange &c) -> decltype(c.data())
Definition: range.hpp:280
ystdex::cpp2011::end
_type * end(_type(&&array)[_vN])
Definition: range.hpp:54
ystdex::basic_string_view::traits_type
_tTraits traits_type
Definition: string_view.hpp:53
ystdex::cpp2014::rend
auto rend(_tRange &c) -> decltype(c.rend())
Definition: range.hpp:119
ystdex::basic_string_view::reverse_iterator
const_reverse_iterator reverse_iterator
Definition: string_view.hpp:62
ystdex::path_category::empty
ystdex::basic_string_view::difference_type
ptrdiff_t difference_type
Definition: string_view.hpp:64
ystdex::basic_tstring_view
指定结束字符的只读字符串视图。
Definition: tstring_view.hpp:47
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
CHRLib::s
_tDst const _tSrc * s
Definition: CharacterProcessing.h:140
ystdex::cpp2014::cbegin
auto cbegin(const _tRange &c) -> decltype(std::begin(c))
Definition: range.hpp:79
ystdex
特性测试宏。
Definition: ydef.h:822
ystdex::operator==
bool operator==(nullptr_t lhs, const _type &rhs)
Definition: ydef.h:895
platform_ex::Windows::Invalid
Definition: MinGW32.h:471
string_view.hpp
只读字符串视图。
ystdex::basic_tstring_view< _tChar, _tTraits, 0 >::compare
int compare(size_type pos1, size_type n1, basic_tstring_view s, size_type pos2, size_type n2) const
Definition: tstring_view.hpp:176
ystdex::fseq::front
identity< _type > front(empty_base< _type, _types... >)
Definition: variadic.hpp:74
ystdex::cpp2014::crbegin
auto crbegin(const _tRange &c) -> decltype(ystdex::rbegin(c))
Definition: range.hpp:143
ystdex::basic_string_view::size_type
size_t size_type
Definition: string_view.hpp:63
ynothrowv
#define ynothrowv
YSLib 无异常抛出保证验证：有条件地使用无异常抛出规范。
Definition: ydef.h:786
ystdex::basic_tstring_view< _tChar, _tTraits, 0 >::compare_remained
int compare_remained(basic_tstring_view s, int ret) const
Definition: tstring_view.hpp:199
ystdex::swap
void swap(container_adaptor< _tSeqCon > &x, container_adaptor< _tSeqCon > &y)
Definition: container.hpp:179
ystdex::basic_string_view::iterator
const_iterator iterator
Definition: string_view.hpp:60
std
Definition: memory.hpp:1183
ystdex::basic_tstring_view< _tChar, _tTraits, 0 >::value_type
_tChar value_type
Definition: tstring_view.hpp:56
ystdex::basic_tstring_view< _tChar, _tTraits, 0 >::compare
int compare(const _tChar *s) const
Definition: tstring_view.hpp:182
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::cpp2011::begin
_type * begin(_type(&&array)[_vN])
Definition: range.hpp:46
ystdex::basic_tstring_view< _tChar, _tTraits, 0 >::compare
int compare(basic_tstring_view s) const
Definition: tstring_view.hpp:165
ystdex::size
auto size(const _tRange &c) -> decltype(c.size())
类容器访问。
Definition: range.hpp:233
ystdex::basic_string_view::pointer
_tChar * pointer
Definition: string_view.hpp:55
yconstraint
#define yconstraint(_expr)
约束：接口语义。
Definition: cassert.h:88
ystdex::basic_tstring_view< _tChar, _tTraits, 0 >::compare
int compare(size_type pos1, size_type n1, const _tChar *s, size_type n2) const
Definition: tstring_view.hpp:192
ystdex::basic_string_view::const_reverse_iterator
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: string_view.hpp:61
ystdex::basic_tstring_view< _tChar, _tTraits, 0 >::substr
basic_tstring_view substr(size_type pos=0) const
Definition: tstring_view.hpp:157