MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
string_view.hpp
1#ifndef MSTL_CORE_STRING_STRING_VIEW_HPP__
2#define MSTL_CORE_STRING_STRING_VIEW_HPP__
3#include "basic_string_view.hpp"
5
6using string_view = basic_string_view<char>;
7using bstring_view = basic_string_view<byte_t>;
8using wstring_view = basic_string_view<wchar_t>;
9#ifdef MSTL_STANDARD_20__
10using u8string_view = basic_string_view<char8_t>;
11#endif // MSTL_STANDARD_20__
12using u16string_view = basic_string_view<char16_t>;
13using u32string_view = basic_string_view<char32_t>;
14
15
17MSTL_NODISCARD constexpr string_view operator ""_sv(const char* str, size_t len) noexcept {
18 return {str, len};
19}
20MSTL_NODISCARD constexpr wstring_view operator ""_sv(const wchar_t* str, size_t len) noexcept {
21 return {str, len};
22}
23#ifdef MSTL_STANDARD_20__
24MSTL_NODISCARD constexpr u8string_view operator ""_sv(const char8_t* str, size_t len) noexcept {
25 return {str, len};
26}
27#endif // MSTL_STANDARD_20__
28MSTL_NODISCARD constexpr u16string_view operator ""_sv(const char16_t* str, size_t len) noexcept {
29 return {str, len};
30}
31MSTL_NODISCARD constexpr u32string_view operator ""_sv(const char32_t* str, size_t len) noexcept {
32 return {str, len};
33}
35
36
37template <typename CharT>
38constexpr bool getline(const basic_string_view<CharT> data, size_t& pos,
39 basic_string_view<CharT>& str, CharT delim = static_cast<CharT>('\n')) {
40
41 if (pos >= data.size()) {
42 str = basic_string_view<CharT>();
43 return false;
44 }
45
46 size_t start = pos;
47 size_t end = pos;
48 while (end < data.size() && data[end] != delim) {
49 ++end;
50 }
51 str = data.substr(start, end - start);
52 pos = (end < data.size()) ? end + 1 : end;
53
54 return true;
55}
56
57template <typename CharT, typename Pred>
58constexpr bool getline(const basic_string_view<CharT> data, size_t& pos,
59 basic_string_view<CharT>& str, Pred split = [](const CharT ch) {
60 return ch == static_cast<CharT>('\n');
61 }) {
62
63 if (pos >= data.size()) {
64 str = basic_string_view<CharT>();
65 return false;
66 }
67
68 size_t start = pos;
69 size_t end = pos;
70 while (end < data.size() && !split(data[end])) {
71 ++end;
72 }
73 str = data.substr(start, end - start);
74 pos = (end < data.size()) ? end + 1 : end;
75
76 return true;
77}
78
80#endif // MSTL_CORE_STRING_STRING_VIEW_HPP__
#define MSTL_END_LITERALS__
结束literals命名空间
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
#define MSTL_BEGIN_LITERALS__
开始literals命名空间(内联)
MSTL_NODISCARD MSTL_ALWAYS_INLINE constexpr decltype(auto) end(Container &cont) noexcept(noexcept(cont.end()))
获取容器的结束迭代器
MSTL_NODISCARD MSTL_ALWAYS_INLINE constexpr decltype(auto) data(Container &cont) noexcept(noexcept(cont.data()))
获取容器的底层数据指针