|
| 1 | +#pragma once |
| 2 | +#ifndef _GHLIBCPP_STRING_VIEW |
| 3 | +#define _GHLIBCPP_STRING_VIEW |
| 4 | + |
| 5 | +#include "stddef.h" |
| 6 | + |
| 7 | +namespace std { |
| 8 | + |
| 9 | +template <typename CharT> class basic_string_view { |
| 10 | +public: |
| 11 | + typedef CharT value_type; |
| 12 | + typedef const CharT *pointer; |
| 13 | + typedef const CharT *const_pointer; |
| 14 | + typedef const CharT &reference; |
| 15 | + typedef const CharT &const_reference; |
| 16 | + typedef const CharT *const_iterator; |
| 17 | + typedef const_iterator iterator; |
| 18 | + typedef size_t size_type; |
| 19 | + typedef ptrdiff_t difference_type; |
| 20 | + |
| 21 | + // Constructors |
| 22 | + basic_string_view() noexcept; |
| 23 | + basic_string_view(const basic_string_view &) noexcept = default; |
| 24 | + basic_string_view(const CharT *s, size_type count); |
| 25 | + basic_string_view(const CharT *s); |
| 26 | + |
| 27 | + // Assignment |
| 28 | + basic_string_view &operator=(const basic_string_view &) noexcept = default; |
| 29 | + |
| 30 | + // Element access |
| 31 | + const_reference operator[](size_type pos) const; |
| 32 | + const_reference at(size_type pos) const; |
| 33 | + const_reference front() const; |
| 34 | + const_reference back() const; |
| 35 | + const_pointer data() const noexcept; |
| 36 | + |
| 37 | + // Capacity |
| 38 | + size_type size() const noexcept; |
| 39 | + size_type length() const noexcept; |
| 40 | + size_type max_size() const noexcept; |
| 41 | + bool empty() const noexcept; |
| 42 | + |
| 43 | + // Modifiers |
| 44 | + void remove_prefix(size_type n); |
| 45 | + void remove_suffix(size_type n); |
| 46 | + void swap(basic_string_view &v) noexcept; |
| 47 | + |
| 48 | + // String operations |
| 49 | + size_type copy(CharT *dest, size_type count, size_type pos = 0) const; |
| 50 | + basic_string_view substr(size_type pos = 0, size_type len = npos) const; |
| 51 | + |
| 52 | + // Comparison |
| 53 | + int compare(basic_string_view v) const noexcept; |
| 54 | + int compare(size_type pos1, size_type n1, basic_string_view v) const; |
| 55 | + int compare(const CharT *s) const; |
| 56 | + |
| 57 | + // Search |
| 58 | + size_type find(basic_string_view v, size_type pos = 0) const noexcept; |
| 59 | + size_type find(CharT c, size_type pos = 0) const noexcept; |
| 60 | + size_type find(const CharT *s, size_type pos, size_type n) const; |
| 61 | + size_type find(const CharT *s, size_type pos = 0) const; |
| 62 | + |
| 63 | + // Constants |
| 64 | + static const size_type npos = static_cast<size_type>(-1); |
| 65 | + |
| 66 | +private: |
| 67 | + const CharT *data_; |
| 68 | + size_type size_; |
| 69 | +}; |
| 70 | + |
| 71 | +// Type aliases |
| 72 | +typedef basic_string_view<char> string_view; |
| 73 | +typedef basic_string_view<wchar_t> wstring_view; |
| 74 | +typedef basic_string_view<char16_t> u16string_view; |
| 75 | +typedef basic_string_view<char32_t> u32string_view; |
| 76 | + |
| 77 | +} // namespace std |
| 78 | + |
| 79 | +#endif // _GHLIBCPP_STRING_VIEW |
0 commit comments