Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/core/StarStringView.hpp
blob: 14123422981d7bece1375ea0aad169576f2904eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#pragma once

#include "StarString.hpp"

namespace Star {

STAR_CLASS(StringView);
STAR_CLASS(String);

// This is a StringView version of Star::String
// I literally just copy-pasted it all from there
class StringView {
public:
  typedef String::Char Char;

  typedef U8ToU32Iterator<std::string_view::const_iterator> const_iterator;
  typedef Char value_type;
  typedef value_type const& const_reference;

  using CaseSensitivity = String::CaseSensitivity;

  StringView();
  StringView(StringView const& s);
  StringView(StringView&& s) noexcept;
  StringView(String const& s);

  // These assume utf8 input
  StringView(char const* s);
  StringView(char const* s, size_t n);
  StringView(std::string_view const& s);
  StringView(std::string_view&& s) noexcept;
  StringView(std::string const& s);

  StringView(Char const* s);
  StringView(Char const* s, size_t n);

  // const& to internal utf8 data
  std::string_view const& utf8() const;
  std::string_view takeUtf8();
  ByteArray utf8Bytes() const;
  // Pointer to internal utf8 data, null-terminated.
  char const* utf8Ptr() const;
  size_t utf8Size() const;

  const_iterator begin() const;
  const_iterator end() const;

  size_t size() const;
  size_t length() const;

  bool empty() const;

  Char operator[](size_t index) const;
  // Throws StringException if i out of range.
  Char at(size_t i) const;

  bool endsWith(StringView end, CaseSensitivity cs = CaseSensitivity::CaseSensitive) const;
  bool endsWith(Char end, CaseSensitivity cs = CaseSensitivity::CaseSensitive) const;
  bool beginsWith(StringView beg, CaseSensitivity cs = CaseSensitivity::CaseSensitive) const;
  bool beginsWith(Char beg, CaseSensitivity cs = CaseSensitivity::CaseSensitive) const;

  typedef function<void(StringView, size_t, size_t)> SplitCallback;
  void forEachSplitAnyView(StringView pattern, SplitCallback) const;
  void forEachSplitView(StringView pattern, SplitCallback) const;

  bool hasChar(Char c) const;
  // Identical to hasChar, except, if string is empty, tests if c is
  // whitespace.
  bool hasCharOrWhitespace(Char c) const;

  size_t find(Char c, size_t beg = 0, CaseSensitivity cs = CaseSensitivity::CaseSensitive) const;
  size_t find(StringView s, size_t beg = 0, CaseSensitivity cs = CaseSensitivity::CaseSensitive) const;
  size_t findLast(Char c, CaseSensitivity cs = CaseSensitivity::CaseSensitive) const;
  size_t findLast(StringView s, CaseSensitivity cs = CaseSensitivity::CaseSensitive) const;

  // If pattern is empty, finds first whitespace
  size_t findFirstOf(StringView chars = "", size_t beg = 0) const;

  // If pattern is empty, finds first non-whitespace
  size_t findFirstNotOf(StringView chars = "", size_t beg = 0) const;

  // finds the the start of the next 'boundary' in a string.  used for quickly
  // scanning a string
  size_t findNextBoundary(size_t index, bool backwards = false) const;

  bool contains(StringView s, CaseSensitivity cs = CaseSensitivity::CaseSensitive) const;

  int compare(StringView s, CaseSensitivity cs = CaseSensitivity::CaseSensitive) const;
  bool equals(StringView s, CaseSensitivity cs = CaseSensitivity::CaseSensitive) const;
  // Synonym for equals(s, String::CaseInsensitive)
  bool equalsIgnoreCase(StringView s) const;

  StringView substr(size_t position, size_t n = NPos) const;

  StringView& operator=(StringView s);

  friend bool operator==(StringView s1, const char* s2);
  friend bool operator==(StringView s1, std::string const& s2);
  friend bool operator==(StringView s1, String const& s2);
  friend bool operator==(StringView s1, StringView s2);
  friend bool operator!=(StringView s1, StringView s2);
  friend bool operator<(StringView s1, StringView s2);

  friend std::ostream& operator<<(std::ostream& os, StringView const& s);

private:
  int compare(size_t selfOffset,
      size_t selfLen,
      StringView other,
      size_t otherOffset,
      size_t otherLen,
      CaseSensitivity cs) const;

  std::string_view m_view;
};

}

template <> struct fmt::formatter<Star::StringView> : formatter<std::string_view> {
  fmt::appender format(Star::StringView const& s, format_context& ctx) const;
};