3#include "HashFunctions.h"
4#include "StringViewFormat.h"
6#include "Collections/SmallVector.h"
25 using traits_type = std::char_traits<char>;
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);
Provides a weakly referenced view over the contents of a string.
const char * iterator
Iterator type.
size_t strSize
Length of the string data pointed to.
constexpr char operator[](const size_t index) const noexcept
Get the character at position index in the string.
constexpr const_iterator cbegin() const noexcept
Const iterator to the beginning of the string.
constexpr const char * data() const noexcept
Get the sequence of characters referenced by the string view.
constexpr iterator begin() noexcept
Iterator to the beginning of the string.
constexpr const_iterator end() const noexcept
Const iterator to the end of the string.
constexpr size_t size() const noexcept
Get the size of the string.
constexpr size_t hash(size_t hashValue) const noexcept
Combine the hash code of the string with the initial hash value provided.
constexpr StringView(const std::string_view s) noexcept
Construct a String view of the given std::string_view object.
constexpr std::string_view to_string_view() const noexcept
Create a standard library string_view of the same view.
constexpr StringView() noexcept=default
Constructs an empty string view.
constexpr iterator end() noexcept
Iterator to the end of the string.
const char * const_iterator
Const iterator type.
constexpr StringView(const char(&data)[LENGTH]) noexcept
Construct a string view from the given string literal.
constexpr size_t length() const noexcept
Get the length of the string.
constexpr bool empty() const noexcept
Check if the string is empty.
char value_type
Value type.
static bool isWhiteSpace(char c)
Helper function that tests whether the given character is an ASCII whitespace character.
constexpr const_iterator cend() const noexcept
Const iterator to the end of the string.
constexpr StringView(const char *data, const size_t length) noexcept
Construct a string view of the given string data, with the given length.
constexpr const_iterator begin() const noexcept
Const iterator to the beginning of the string.
bool operator==(const StringView &other) const
Operator equal.
const char * str
Pointer to the string this view is over.
constexpr size_t hash() const noexcept
Get the hash code of the string.
Contains all Cogs related functionality.
constexpr size_t hash() noexcept
Simple getter function that returns the initial value for fnv1a hashing.
bool operator>=(const StringView &left, const StringView &right) noexcept
Lexicographically compare two string views.
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.
bool operator<(const StringView &left, const StringView &right) noexcept
Lexicographically compare two string views.
bool operator<=(const StringView &left, const StringView &right) noexcept
Lexicographically compare two string views.
bool operator>(const StringView &left, const StringView &right) noexcept
Lexicographically compare two string views.