40 static constexpr size_t npos =
static_cast<size_t>(-1);
43 static constexpr size_t NoPosition = npos;
55 : str(data), strSize(data ?
traits_type::length(data) : 0)
64 template<
size_t LENGTH>
65 constexpr StringView(
const char (&data)[LENGTH]) noexcept : str(data), strSize(data ? traits_type::length(data) : 0) {
74 constexpr StringView(
const char * data,
const size_t length) noexcept : str(data), strSize(length) {}
80 constexpr StringView(
const std::string_view s) noexcept : str(s.data()), strSize(s.length()) {}
122 constexpr bool empty() const noexcept {
return strSize == 0; }
129 constexpr explicit operator bool() const noexcept {
return strSize != 0; }
137 explicit operator std::string()
const {
return to_string(); }
145 constexpr operator std::string_view() const noexcept {
return to_string_view(); }
153 std::string to_string()
const;
161 constexpr std::string_view
to_string_view() const noexcept {
return std::string_view(str, strSize); }
171 constexpr const char *
data() const noexcept {
return str; }
178 constexpr size_t size() const noexcept {
return strSize; }
185 constexpr size_t length() const noexcept {
return strSize; }
192 constexpr char operator[](
const size_t index)
const noexcept {
return data()[index]; }
209 constexpr size_t hash(
size_t hashValue)
const noexcept {
return Cogs::hash(*
this, hashValue); }
229 size_t find(const
StringView & other,
size_t offset = 0) const noexcept;
234 size_t find_first_of(
char character,
size_t pos = 0) const noexcept;
239 size_t find_last_of(const
StringView characters,
size_t pos = NoPosition) const noexcept;
244 size_t find_last_of(const
char character,
size_t pos = NoPosition) const noexcept;
253 int compare(const
StringView & other) const noexcept;
258 constexpr
StringView substr(
size_t offset,
size_t count = NoPosition) const noexcept
262 if (count == NoPosition) {
263 return StringView(data() + offset, size() - offset);
266 return StringView(data() + offset, count < (size() - offset) ? count : size() - offset);
272 Vector split(
const StringView& delimiters)
const;
277 Vector splitAll(
const StringView& delimiters)
const;
298 int compareMemory(const
char * a, const
char * b,
size_t length)
const { return ::memcmp(a, b, length); }
301 const char * str =
nullptr;
312 return ((c ==
'\t') || (c ==
'\n') || (c ==
'\r') || (c ==
' '));
321 return left.compare(right) < 0;
337 return (!(right < left));
345 return (!(left < right));
377 return left == std::string_view(right);
#define COGSFOUNDATION_API
Definition: FoundationBase.h:31
Definition: SmallVector.h:431
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
std::char_traits< char > traits_type
Definition: StringView.h:25
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
Main Cogs namespace.
Definition: MortonCode.h:5
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.
Definition: HashFunctions.cpp:57
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