Cogs.Core
StringView.cpp
1#include "StringView.h"
2
3#include <algorithm>
4
5Cogs::StringView::StringView(const std::string & s) noexcept
6 : str(s.c_str()), strSize(s.length())
7{}
8
9std::string Cogs::StringView::to_string() const {
10 return std::string(str, strSize);
11}
12
13size_t Cogs::StringView::hashLowercase(size_t hashValue) const noexcept
14{
15 return Cogs::hashLowercase(*this, hashValue);
16}
17
18size_t Cogs::StringView::find(const StringView & other, size_t offset) const noexcept
19{
20 if (other.size() > size() + offset) return NoPosition;
21
22 auto found = std::search(begin()+offset, end(), other.begin(), other.end());
23
24 if (found != end()) {
25 return std::distance(begin(), found);
26 }
27
28 return NoPosition;
29}
30
31size_t Cogs::StringView::find_last_of(const StringView characters, size_t pos) const noexcept
32{
33 size_t N = length();
34 if (pos < N) N = pos + 1;
35
36 size_t k = NoPosition;
37 for (size_t i = 0; i < N; i++) {
38 auto ch = data()[i];
39 for (size_t j = 0; j < characters.size(); j++) {
40 if (characters[j] == ch) { k = i; }
41 }
42 }
43 return k;
44}
45
46size_t Cogs::StringView::find_first_of(char character, size_t pos) const noexcept
47{
48 for (size_t len = length(); pos < len; ++pos) {
49 if(data()[pos] == character) {
50 return pos;
51 }
52 }
53 return NoPosition;
54}
55
56size_t Cogs::StringView::find_last_of(const char character, size_t pos) const noexcept
57{
58 size_t N = length();
59 if (pos < N) N = pos + 1;
60
61 size_t k = NoPosition;
62 for (size_t i = 0; i < N; i++) {
63 if(data()[i] == character) { k = i; }
64 }
65 return k;
66}
67
68int Cogs::StringView::compare(const StringView & other) const noexcept
69{
70 const size_t lSize = size();
71 const size_t rSize = other.size();
72 const int result = compareMemory(str, other.str, std::min(lSize, rSize));
73 if (result != 0) return result;
74 if (lSize < rSize) return -1;
75 if (lSize > rSize) return 1;
76 return 0;
77}
78
80{
81 Vector tokens;
82 const char * read = data();
83 const char * token = nullptr;
84
85 for (const char * const end = read + size(); read != end; ++read) {
86 const bool isSplitter = delimiters.find_first_of(*read) != NoPosition;
87
88 if (!isSplitter && !token) {
89 token = read;
90 }
91 else if (isSplitter && token) {
92 tokens.push_back(StringView(token, read - token));
93 token = nullptr;
94 }
95 }
96 if (token) {
97 tokens.push_back(StringView(token, read - token));
98 }
99 return tokens;
100}
101
103{
104 Vector tokens;
105 const char * read = data();
106 const char * token = read;
107
108 for (const char * const end = read + size(); read != end; ++read) {
109 if (delimiters.find_first_of(*read) != NoPosition) {
110 tokens.push_back(StringView(token, read - token));
111 token = read + 1;
112 }
113 }
114 tokens.push_back(StringView(token, read - token));
115 return tokens;
116}
117
119{
120 while (strSize && isWhiteSpace(*str)) {
121 str++;
122 strSize--;
123 }
124 return *this;
125}
126
128{
129 while (strSize && isWhiteSpace(str[strSize - 1])) {
130 strSize--;
131 }
132 return *this;
133}
134
136{
137 return trimStart().trimEnd();
138}
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
size_t find_first_of(char character, size_t pos=0) const noexcept
Find the first occurance of the given character from the specified starting position.
Definition: StringView.cpp:46
size_t strSize
Length of the string data pointed to.
Definition: StringView.h:304
StringView & trimStart() noexcept
Remove any whitespace from the start of this StringView's contents.
Definition: StringView.cpp:118
size_t find_last_of(const StringView characters, size_t pos=NoPosition) const noexcept
See std::basic_string::find_last_of.
Definition: StringView.cpp:31
constexpr StringView() noexcept=default
Constructs an empty string view.
StringView & trim() noexcept
Remove any whitespace from the start and end of this StringView's contents.
Definition: StringView.cpp:135
int compare(const StringView &other) const noexcept
Lexicographically compares the content of this string view with the given other.
Definition: StringView.cpp:68
size_t hashLowercase(size_t hashValue=Cogs::hash()) const noexcept
Get the hash code of the string converted to lowercase.
Definition: StringView.cpp:13
StringView & trimEnd() noexcept
Remove any whitespace from the end of this StringView's contents.
Definition: StringView.cpp:127
Vector splitAll(const StringView &delimiters) const
Split this string into parts, empty parts will be retained.
Definition: StringView.cpp:102
Vector split(const StringView &delimiters) const
Split this string into parts.
Definition: StringView.cpp:79
size_t find(const StringView &other, size_t offset=0) const noexcept
Find the given string segment inside the string.
Definition: StringView.cpp:18
const char * str
Pointer to the string this view is over.
Definition: StringView.h:301
std::string to_string() const
String conversion method.
Definition: StringView.cpp:9
COGSFOUNDATION_API size_t hashLowercase(std::string_view str, size_t hashValue=Cogs::hash()) noexcept
Get the hash code of the string converted to lowercase.