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{
49 class COGSFOUNDATION_API StringView
50 {
51 using traits_type = std::char_traits<char>;
52 public:
54 using iterator = const char *;
55
57 using const_iterator = const char *;
58
60 using value_type = char;
61
64
66 static constexpr size_t npos = static_cast<size_t>(-1);
67
69 static constexpr size_t NoPosition = npos;
70
72 constexpr StringView() noexcept = default;
73
80 constexpr StringView(const char * data) noexcept
81 : str(data), strSize(data ? traits_type::length(data) : 0)
82 {
83 }
84
90 template<size_t LENGTH>
91 constexpr StringView(const char (&data)[LENGTH]) noexcept : str(data), strSize(data ? traits_type::length(data) : 0) {
92 }
93
100 constexpr StringView(const char * data, const size_t length) noexcept : str(data), strSize(length) {}
101
106 constexpr StringView(const std::string_view s) noexcept : str(s.data()), strSize(s.length()) {}
107
113 StringView(const std::string & s) noexcept;
114
123 bool operator==(const StringView & other) const { return strSize == other.strSize && compareMemory(str, other.str, other.strSize) == 0; }
124
126 constexpr iterator begin() noexcept { return str; }
127
129 constexpr iterator end() noexcept { return str + strSize; }
130
132 constexpr const_iterator begin() const noexcept { return str; }
133
135 constexpr const_iterator end() const noexcept { return str + strSize; }
136
138 constexpr const_iterator cbegin() const noexcept { return str; }
139
141 constexpr const_iterator cend() const noexcept { return str + strSize; }
142
148 constexpr bool empty() const noexcept { return strSize == 0; }
149
155 constexpr explicit operator bool() const noexcept { return strSize != 0; }
156
163 explicit operator std::string() const { return to_string(); }
164
171 constexpr operator std::string_view() const noexcept { return to_string_view(); }
172
179 std::string to_string() const;
180
187 constexpr std::string_view to_string_view() const noexcept { return std::string_view(str, strSize); }
188
197 constexpr const char * data() const noexcept { return str; }
198
204 constexpr size_t size() const noexcept { return strSize; }
205
211 constexpr size_t length() const noexcept { return strSize; }
212
218 constexpr char operator[](const size_t index) const noexcept { return data()[index]; }
219
225 [[nodiscard]]
226 constexpr size_t hash() const noexcept { return Cogs::hash(*this); }
227
234 [[nodiscard]]
235 constexpr size_t hash(size_t hashValue) const noexcept { return Cogs::hash(*this, hashValue); }
236
245 [[nodiscard]]
246 size_t hashLowercase(size_t hashValue = Cogs::hash()) const noexcept;
247
255 size_t find(const StringView & other, size_t offset = 0) const noexcept;
256
260 size_t find_first_of(char character, size_t pos = 0) const noexcept;
261
265 size_t find_last_of(const StringView characters, size_t pos = NoPosition) const noexcept;
266
270 size_t find_last_of(const char character, size_t pos = NoPosition) const noexcept;
271
279 int compare(const StringView & other) const noexcept;
280
284 constexpr StringView substr(size_t offset, size_t count = NoPosition) const noexcept
285 {
286 if (offset >= size()) return StringView();
287
288 if (count == NoPosition) {
289 return StringView(data() + offset, size() - offset);
290 }
291
292 return StringView(data() + offset, count < (size() - offset) ? count : size() - offset);
293 }
294
298 Vector split(const StringView& delimiters) const;
299
303 Vector splitAll(const StringView& delimiters) const;
304
308 StringView& trimStart() noexcept;
309
313 StringView& trimEnd() noexcept;
314
318 StringView& trim() noexcept;
319
320 private:
324 int compareMemory(const char * a, const char * b, size_t length) const { return ::memcmp(a, b, length); }
325
327 const char * str = nullptr;
328
330 size_t strSize = 0;
331
337 static bool isWhiteSpace(char c) {
338 return ((c == '\t') || (c == '\n') || (c == '\r') || (c == ' '));
339 }
340 };
341
345 inline bool operator<(const StringView & left, const StringView & right) noexcept
346 {
347 return left.compare(right) < 0;
348 }
349
353 inline bool operator>(const StringView & left, const StringView & right) noexcept
354 {
355 return right < left;
356 }
357
361 inline bool operator<=(const StringView & left, const StringView & right) noexcept
362 {
363 return (!(right < left));
364 }
365
369 inline bool operator>=(const StringView & left, const StringView & right) noexcept
370 {
371 return (!(left < right));
372 }
373
377 inline bool operator==(const char * left, const StringView & right) noexcept
378 {
379 return StringView(left) == right;
380 }
381
385 inline bool operator==(const StringView & left, const char * right) noexcept
386 {
387 return left == StringView(right);
388 }
389
393 inline bool operator==(const std::string & left, const StringView & right) noexcept
394 {
395 return StringView(left) == right;
396 }
397
401 inline bool operator==(const std::string_view& left, const StringView& right) noexcept
402 {
403 return left == std::string_view(right);
404 }
405}
Vector with inline storage for up to Size elements, avoiding heap allocation for small collections.
Definition: SmallVector.h:460
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:50
const char * iterator
Iterator type.
Definition: StringView.h:54
size_t strSize
Length of the string data pointed to.
Definition: StringView.h:330
constexpr char operator[](const size_t index) const noexcept
Get the character at position index in the string.
Definition: StringView.h:218
constexpr const_iterator cbegin() const noexcept
Const iterator to the beginning of the string.
Definition: StringView.h:138
constexpr const char * data() const noexcept
Get the sequence of characters referenced by the string view.
Definition: StringView.h:197
constexpr iterator begin() noexcept
Iterator to the beginning of the string.
Definition: StringView.h:126
constexpr const_iterator end() const noexcept
Const iterator to the end of the string.
Definition: StringView.h:135
constexpr size_t size() const noexcept
Get the size of the string.
Definition: StringView.h:204
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:235
constexpr StringView(const std::string_view s) noexcept
Construct a String view of the given std::string_view object.
Definition: StringView.h:106
constexpr std::string_view to_string_view() const noexcept
Create a standard library string_view of the same view.
Definition: StringView.h:187
constexpr StringView() noexcept=default
Constructs an empty string view.
constexpr iterator end() noexcept
Iterator to the end of the string.
Definition: StringView.h:129
const char * const_iterator
Const iterator type.
Definition: StringView.h:57
constexpr StringView(const char(&data)[LENGTH]) noexcept
Construct a string view from the given string literal.
Definition: StringView.h:91
constexpr size_t length() const noexcept
Get the length of the string.
Definition: StringView.h:211
constexpr bool empty() const noexcept
Check if the string is empty.
Definition: StringView.h:148
char value_type
Value type.
Definition: StringView.h:60
static bool isWhiteSpace(char c)
Helper function that tests whether the given character is an ASCII whitespace character.
Definition: StringView.h:337
constexpr const_iterator cend() const noexcept
Const iterator to the end of the string.
Definition: StringView.h:141
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:100
constexpr const_iterator begin() const noexcept
Const iterator to the beginning of the string.
Definition: StringView.h:132
bool operator==(const StringView &other) const
Operator equal.
Definition: StringView.h:123
const char * str
Pointer to the string this view is over.
Definition: StringView.h:327
constexpr size_t hash() const noexcept
Get the hash code of the string.
Definition: StringView.h:226
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:369
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:377
bool operator<(const StringView &left, const StringView &right) noexcept
Lexicographically compare two string views.
Definition: StringView.h:345
bool operator<=(const StringView &left, const StringView &right) noexcept
Lexicographically compare two string views.
Definition: StringView.h:361
bool operator>(const StringView &left, const StringView &right) noexcept
Lexicographically compare two string views.
Definition: StringView.h:353