🛰️航天仿真算法库 SpaceAST 0.0.1
载入中...
搜索中...
未找到
StringView.hpp
浏览该文件的文档.
1
20
21#pragma once
22
23#include "AstGlobal.h"
24#include <algorithm>
25#include <cstring>
26#include <ostream>
27#include <string>
28#include <iterator>
29
30AST_NAMESPACE_BEGIN
31
42template<typename _Char>
44{
45public:
46 // 类型定义
47 typedef _Char value_type;
48 typedef const _Char* pointer;
49 typedef const _Char& reference;
50 typedef const _Char& const_reference;
51 typedef const _Char* const_iterator;
52 typedef const _Char* iterator;
53 typedef typename std::reverse_iterator<const_iterator> const_reverse_iterator;
54 typedef typename std::reverse_iterator<iterator> reverse_iterator;
55 typedef size_t size_type;
56 typedef ptrdiff_t difference_type;
57
58 static const size_type npos = static_cast<size_type>(-1);
59
60private:
61 const _Char* m_data;
62 size_type m_size;
63
64public:
65 // 构造函数
66 constexpr StringViewBasic() noexcept
67 : m_data(nullptr), m_size(0)
68 {}
69
70 constexpr StringViewBasic(const _Char* str) noexcept
71 : m_data(str), m_size(str ? traits_type::length(str) : 0)
72 {}
73
74 constexpr StringViewBasic(const _Char* str, size_type len) noexcept
75 : m_data(str), m_size(len)
76 {}
77
78 template<typename _Allocator>
79 StringViewBasic(const std::basic_string<_Char, std::char_traits<_Char>, _Allocator>& str) noexcept
80 : m_data(str.data()), m_size(str.size())
81 {}
82
83 // 拷贝构造和赋值
84 constexpr StringViewBasic(const StringViewBasic& other) noexcept = default;
85
86 StringViewBasic& operator=(const StringViewBasic& other) noexcept = default;
87
88 // 容量相关
89 constexpr size_type size() const noexcept
90 {
91 return m_size;
92 }
93 constexpr size_type length() const noexcept
94 {
95 return m_size;
96 }
97 constexpr bool empty() const noexcept
98 {
99 return m_size == 0;
100 }
101
102 // 元素访问
103 constexpr const _Char& operator[](size_type pos) const noexcept
104 {
105 return m_data[pos];
106 }
107
108 constexpr const _Char& front() const noexcept
109 {
110 return m_data[0];
111 }
112
113 constexpr const _Char& back() const noexcept
114 {
115 return m_data[m_size - 1];
116 }
117
118 constexpr const _Char* data() const noexcept
119 {
120 return m_data;
121 }
122
123 // 迭代器
124 constexpr const_iterator begin() const noexcept
125 {
126 return m_data;
127 }
128 constexpr const_iterator end() const noexcept
129 {
130 return m_data + m_size;
131 }
132 constexpr const_iterator cbegin() const noexcept
133 {
134 return m_data;
135 }
136 constexpr const_iterator cend() const noexcept
137 {
138 return m_data + m_size;
139 }
140
141 const_reverse_iterator rbegin() const noexcept
142 {
143 return const_reverse_iterator(end());
144 }
145 const_reverse_iterator rend() const noexcept
146 {
147 return const_reverse_iterator(begin());
148 }
149 const_reverse_iterator crbegin() const noexcept
150 {
151 return const_reverse_iterator(end());
152 }
153 const_reverse_iterator crend() const noexcept
154 {
155 return const_reverse_iterator(begin());
156 }
157
158 // 修改操作(不修改底层数据,只修改视图)
159 void remove_prefix(size_type n) noexcept
160 {
161 if (n > m_size) n = m_size;
162 m_data += n;
163 m_size -= n;
164 }
165
166 void remove_suffix(size_type n) noexcept
167 {
168 if (n > m_size) n = m_size;
169 m_size -= n;
170 }
171
172 void swap(StringViewBasic& other) noexcept
173 {
174 std::swap(m_data, other.m_data);
175 std::swap(m_size, other.m_size);
176 }
177
178 // 子串操作
179 StringViewBasic substr(size_type pos = 0, size_type count = npos) const
180 {
181 if (pos > m_size) {
182 // 抛出异常或返回空视图
183 return StringViewBasic();
184 }
185 size_type rlen = (std::min)(count, m_size - pos);
186 return StringViewBasic(m_data + pos, rlen);
187 }
188
189 // 比较操作
190 int compare(StringViewBasic other) const noexcept
191 {
192 size_type len = (std::min)(m_size, other.m_size);
193 int result = traits_type::compare(m_data, other.m_data, len);
194 if (result != 0) return result;
195 if (m_size < other.m_size) return -1;
196 if (m_size > other.m_size) return 1;
197 return 0;
198 }
199
200 bool operator==(StringViewBasic other) const noexcept
201 {
202 return m_size == other.m_size &&
203 (m_data == other.m_data ||
204 traits_type::compare(m_data, other.m_data, m_size) == 0);
205 }
206
207 bool operator!=(StringViewBasic other) const noexcept
208 {
209 return !(*this == other);
210 }
211
212 bool operator<(StringViewBasic other) const noexcept
213 {
214 return compare(other) < 0;
215 }
216
217 bool operator<=(StringViewBasic other) const noexcept
218 {
219 return compare(other) <= 0;
220 }
221
222 bool operator>(StringViewBasic other) const noexcept
223 {
224 return compare(other) > 0;
225 }
226
227 bool operator>=(StringViewBasic other) const noexcept
228 {
229 return compare(other) >= 0;
230 }
231
232 // 查找操作
233 size_type find(_Char ch, size_type pos = 0) const noexcept
234 {
235 if (pos >= m_size) return npos;
236 const _Char* result = traits_type::find(m_data + pos, m_size - pos, ch);
237 return result ? static_cast<size_type>(result - m_data) : npos;
238 }
239
240 size_type find(StringViewBasic str, size_type pos = 0) const noexcept
241 {
242 if (pos > m_size || str.size() + pos> m_size)
243 return npos;
244
245 const _Char* start = m_data + pos;
246 const _Char* end = m_data + m_size - str.size() + 1;
247
248 for (const _Char* p = start; p < end; ++p) {
249 if (traits_type::compare(p, str.data(), str.size()) == 0) {
250 return static_cast<size_type>(p - m_data);
251 }
252 }
253
254 return npos;
255 }
256
257 size_type rfind(_Char ch, size_type pos = npos) const noexcept
258 {
259 if (m_size == 0) return npos;
260 if (pos >= m_size) pos = m_size - 1;
261
262 for (size_type i = pos + 1; i > 0; --i) {
263 if (m_data[i - 1] == ch) {
264 return i - 1;
265 }
266 }
267
268 return npos;
269 }
270
271 // 查找第一个出现的指定字符集合中的字符
272 size_type find_first_of(_Char ch, size_type pos = 0) const noexcept
273 {
274 return find(ch, pos);
275 }
276
277 size_type find_first_of(StringViewBasic str, size_type pos = 0) const noexcept
278 {
279 if (pos >= m_size || str.empty()) return npos;
280
281 for (size_type i = pos; i < m_size; ++i) {
282 for (size_type j = 0; j < str.size(); ++j) {
283 if (m_data[i] == str[j]) {
284 return i;
285 }
286 }
287 }
288
289 return npos;
290 }
291
292 // 查找最后一个出现的指定字符集合中的字符
293 size_type find_last_of(_Char ch, size_type pos = npos) const noexcept
294 {
295 return rfind(ch, pos);
296 }
297
298 size_type find_last_of(StringViewBasic str, size_type pos = npos) const noexcept
299 {
300 if (m_size == 0 || str.empty()) return npos;
301 if (pos >= m_size) pos = m_size - 1;
302
303 for (size_type i = pos + 1; i > 0; --i) {
304 for (size_type j = 0; j < str.size(); ++j) {
305 if (m_data[i - 1] == str[j]) {
306 return i - 1;
307 }
308 }
309 }
310
311 return npos;
312 }
313
314 // 检查是否以指定前缀/后缀开头/结尾
315 bool starts_with(StringViewBasic str) const noexcept
316 {
317 return m_size >= str.size() &&
318 traits_type::compare(m_data, str.data(), str.size()) == 0;
319 }
320
321 bool starts_with(_Char ch) const noexcept
322 {
323 return !empty() && front() == ch;
324 }
325
326 bool ends_with(StringViewBasic str) const noexcept
327 {
328 return m_size >= str.size() &&
329 traits_type::compare(m_data + m_size - str.size(), str.data(), str.size()) == 0;
330 }
331
332 bool ends_with(_Char ch) const noexcept
333 {
334 return !empty() && back() == ch;
335 }
336
337 // 转换为字符串
338 // std::basic_string<_Char> to_string() const noexcept
339 // {
340 // return std::basic_string<_Char>(m_data, m_size);
341 // }
342
343 // 显式转换到 std::string
344 explicit
345 operator std::basic_string<_Char>() const noexcept
346 {
347 return std::basic_string<_Char>(m_data, m_size);
348 }
349private:
350 typedef std::char_traits<_Char> traits_type;
351};
352
353
354// 注意:如果ast工程不启用命名空间编译,而同时又使用了using namespace std; 则会造成冲突
355// using namespace std在大型工程里是不好的做法
356// 但如果已经这样做了,则需要启用ast命名空间编译,且不要直接using namespace ast
357
358#if defined AST_ENABLE_OVERRIDE_STDLIB // 覆盖标准库函数
359
360// 特化常用的字符类型
361typedef StringViewBasic<char> string_view;
362typedef StringViewBasic<wchar_t> wstring_view;
363typedef StringViewBasic<char16_t> u16string_view;
364typedef StringViewBasic<char32_t> u32string_view;
365
366#endif
367
369// typedef std::string_view StringView;
373
374
375// 流输出操作符
376template<typename _Char>
377std::basic_ostream<_Char>& operator<<(std::basic_ostream<_Char>& os, const StringViewBasic<_Char>& sv)
378{
379 if (sv.size() > 0) {
380 os.write(sv.data(), static_cast<std::streamsize>(sv.size()));
381 }
382 return os;
383}
384
385// 字面量操作符(C++11不支持用户定义字面量用于模板,这里只提供char版本)
386inline StringView operator""_sv(const char* str, size_t len) noexcept
387{
388 return StringView(str, len);
389}
390
391inline WStringView operator""_sv(const wchar_t* str, size_t len) noexcept
392{
393 return WStringView(str, len);
394}
395
396
400AST_NAMESPACE_END
401
402
403// 哈希支持
404namespace std
405{
406 template<typename _Char>
407 struct hash<_AST StringViewBasic<_Char>>
408 {
409 size_t operator()(const _AST StringViewBasic<_Char>& sv) const noexcept
410 {
411 // 简单的FNV-1a哈希算法
412 size_t result = 2166136261U;
413 for (auto ch : sv) {
414 result = (result ^ static_cast<size_t>(ch)) * 16777619U;
415 }
416 return result;
417 }
418 };
419}
轻量级的string_view实现
定义 StringView.hpp:44