Cogs.Core
StringView.h
1#pragma once
2
3#include "HashFunctions.h"
4#include "StringViewFormat.h"
5
6#include "Collections/SmallVector.h"
7
8#include <string>
9#include <cstdio>
10#include <vector>
11
12namespace Cogs
13{
23 class COGSFOUNDATION_API StringView
24 {
25 using traits_type = std::char_traits<char>;
26 public:
28 using iterator = const char *;
29
31 using const_iterator = const char *;
32
34 using value_type = char;
35
38
40 static constexpr size_t npos = static_cast<size_t>(-1);
41
43 static constexpr size_t NoPosition = npos;
44
46 constexpr StringView() noexcept = default;
47
54 constexpr StringView(const char * data) noexcept
55 : str(data), strSize(data ? traits_type::length(data) : 0)
56 {
57 }
58
64 template<size_t LENGTH>
65 constexpr StringView(const char (&data)[LENGTH]) noexcept : str(data), strSize(data ? traits_type::length(data) : 0) {
66 }
67
74 constexpr StringView(const char * data, const size_t length) noexcept : str(data), strSize(length) {}
75
80 constexpr StringView(const std::string_view s) noexcept : str(s.data()), strSize(s.length()) {}
81
87 StringView(const std::string & s) noexcept;
88
97 bool operator==(const StringView & other) const { return strSize == other.strSize && compareMemory(str, other.str, other.strSize) == 0; }
98
100 constexpr iterator begin() noexcept { return str; }
101
103 constexpr iterator end() noexcept { return str + strSize; }
104
106 constexpr const_iterator begin() const noexcept { return str; }
107
109 constexpr const_iterator end() const noexcept { return str + strSize; }
110
112 constexpr const_iterator cbegin() const noexcept { return str; }
113
115 constexpr const_iterator cend() const noexcept { return str + strSize; }
116
122 constexpr bool empty() const noexcept { return strSize == 0; }
123
129 constexpr explicit operator bool() const noexcept { return strSize != 0; }
130
137 explicit operator std::string() const { return to_string(); }
138
145 constexpr operator std::string_view() const noexcept { return to_string_view(); }
146
153 std::string to_string() const;
154
161 constexpr std::string_view to_string_view() const noexcept { return std::string_view(str, strSize); }
162
171 constexpr const char * data() const noexcept { return str; }
172
178 constexpr size_t size() const noexcept { return strSize; }
179
185 constexpr size_t length() const noexcept { return strSize; }
186
192 constexpr char operator[](const size_t index) const noexcept { return data()[index]; }
193
199 [[nodiscard]]
200 constexpr size_t hash() const noexcept { return Cogs::hash(*this); }
201
208 [[nodiscard]]
209 constexpr size_t hash(size_t hashValue) const noexcept { return Cogs::hash(*this, hashValue); }
210
219 [[nodiscard]]
220 size_t hashLowercase(size_t hashValue = Cogs::hash()) const noexcept;
221
229 size_t find(const StringView & other, size_t offset = 0) const noexcept;
230
234 size_t find_first_of(char character, size_t pos = 0) const noexcept;
235
239 size_t find_last_of(const StringView characters, size_t pos = NoPosition) const noexcept;
240
244 size_t find_last_of(const char character, size_t pos = NoPosition) const noexcept;
245
253 int compare(const StringView & other) const noexcept;
254
258 constexpr StringView substr(size_t offset, size_t count = NoPosition) const noexcept
259 {
260 if (offset >= size()) return StringView();
261
262 if (count == NoPosition) {
263 return StringView(data() + offset, size() - offset);
264 }
265
266 return StringView(data() + offset, count < (size() - offset) ? count : size() - offset);
267 }
268
272 Vector split(const StringView& delimiters) const;
273
277 Vector splitAll(const StringView& delimiters) const;
278
282 StringView& trimStart() noexcept;
283
287 StringView& trimEnd() noexcept;
288
292 StringView& trim() noexcept;
293
294 private:
298 int compareMemory(const char * a, const char * b, size_t length) const { return ::memcmp(a, b, length); }
299
301 const char * str = nullptr;
302
304 size_t strSize = 0;
305
311 static bool isWhiteSpace(char c) {
312 return ((c == '\t') || (c == '\n') || (c == '\r') || (c == ' '));
313 }
314 };
315
319 inline bool operator<(const StringView & left, const StringView & right) noexcept
320 {
321 return left.compare(right) < 0;
322 }
323
327 inline bool operator>(const StringView & left, const StringView & right) noexcept
328 {
329 return right < left;
330 }
331
335 inline bool operator<=(const StringView & left, const StringView & right) noexcept
336 {
337 return (!(right < left));
338 }
339
343 inline bool operator>=(const StringView & left, const StringView & right) noexcept
344 {
345 return (!(left < right));
346 }
347
351 inline bool operator==(const char * left, const StringView & right) noexcept
352 {
353 return StringView(left) == right;
354 }
355
359 inline bool operator==(const StringView & left, const char * right) noexcept
360 {
361 return left == StringView(right);
362 }
363
367 inline bool operator==(const std::string & left, const StringView & right) noexcept
368 {
369 return StringView(left) == right;
370 }
371
375 inline bool operator==(const std::string_view& left, const StringView& right) noexcept
376 {
377 return left == std::string_view(right);
378 }
379}
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
const char * iterator
Iterator type.
Definition: StringView.h:28
size_t strSize
Length of the string data pointed to.
Definition: StringView.h:304
constexpr char operator[](const size_t index) const noexcept
Get the character at position index in the string.
Definition: StringView.h:192
constexpr const_iterator cbegin() const noexcept
Const iterator to the beginning of the string.
Definition: StringView.h:112
constexpr const char * data() const noexcept
Get the sequence of characters referenced by the string view.
Definition: StringView.h:171
constexpr iterator begin() noexcept
Iterator to the beginning of the string.
Definition: StringView.h:100
constexpr const_iterator end() const noexcept
Const iterator to the end of the string.
Definition: StringView.h:109
constexpr size_t size() const noexcept
Get the size of the string.
Definition: StringView.h:178
constexpr size_t hash(size_t hashValue) const noexcept
Combine the hash code of the string with the initial hash value provided.
Definition: StringView.h:209
constexpr StringView(const std::string_view s) noexcept
Construct a String view of the given std::string_view object.
Definition: StringView.h:80
constexpr std::string_view to_string_view() const noexcept
Create a standard library string_view of the same view.
Definition: StringView.h:161
constexpr StringView() noexcept=default
Constructs an empty string view.
constexpr iterator end() noexcept
Iterator to the end of the string.
Definition: StringView.h:103
const char * const_iterator
Const iterator type.
Definition: StringView.h:31
constexpr StringView(const char(&data)[LENGTH]) noexcept
Construct a string view from the given string literal.
Definition: StringView.h:65
constexpr size_t length() const noexcept
Get the length of the string.
Definition: StringView.h:185
constexpr bool empty() const noexcept
Check if the string is empty.
Definition: StringView.h:122
char value_type
Value type.
Definition: StringView.h:34
static bool isWhiteSpace(char c)
Helper function that tests whether the given character is an ASCII whitespace character.
Definition: StringView.h:311
constexpr const_iterator cend() const noexcept
Const iterator to the end of the string.
Definition: StringView.h:115
constexpr StringView(const char *data, const size_t length) noexcept
Construct a string view of the given string data, with the given length.
Definition: StringView.h:74
constexpr const_iterator begin() const noexcept
Const iterator to the beginning of the string.
Definition: StringView.h:106
bool operator==(const StringView &other) const
Operator equal.
Definition: StringView.h:97
const char * str
Pointer to the string this view is over.
Definition: StringView.h:301
constexpr size_t hash() const noexcept
Get the hash code of the string.
Definition: StringView.h:200
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
constexpr size_t hash() noexcept
Simple getter function that returns the initial value for fnv1a hashing.
Definition: HashFunctions.h:62
bool operator>=(const StringView &left, const StringView &right) noexcept
Lexicographically compare two string views.
Definition: StringView.h:343
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.
bool operator==(const char *left, const StringView &right) noexcept
Char ptr equality operator.
Definition: StringView.h:351
bool operator<(const StringView &left, const StringView &right) noexcept
Lexicographically compare two string views.
Definition: StringView.h:319
bool operator<=(const StringView &left, const StringView &right) noexcept
Lexicographically compare two string views.
Definition: StringView.h:335
bool operator>(const StringView &left, const StringView &right) noexcept
Lexicographically compare two string views.
Definition: StringView.h:327