2023-03-29 15:43:10 +00:00
|
|
|
#pragma once
|
2023-07-21 18:44:01 +00:00
|
|
|
#include <luna/Hash.h>
|
2023-03-29 15:43:10 +00:00
|
|
|
#include <luna/Result.h>
|
2023-04-07 13:14:46 +00:00
|
|
|
#include <luna/Vector.h>
|
2023-03-29 15:43:10 +00:00
|
|
|
|
|
|
|
class String;
|
|
|
|
|
|
|
|
class StringView
|
|
|
|
{
|
2023-03-29 17:25:11 +00:00
|
|
|
typedef const char* Iterator;
|
|
|
|
|
2023-03-29 15:43:10 +00:00
|
|
|
public:
|
|
|
|
StringView(const char* c_str);
|
|
|
|
StringView(const char* c_str, usize length);
|
|
|
|
|
|
|
|
StringView();
|
|
|
|
|
|
|
|
StringView(const StringView&);
|
|
|
|
|
2023-04-11 20:45:13 +00:00
|
|
|
StringView& operator=(const StringView&);
|
|
|
|
|
2023-03-29 15:43:10 +00:00
|
|
|
Result<String> to_string();
|
|
|
|
|
|
|
|
const char* chars() const
|
|
|
|
{
|
|
|
|
return m_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
usize length() const
|
|
|
|
{
|
|
|
|
return m_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_empty() const
|
|
|
|
{
|
|
|
|
return m_length == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char& operator[](usize) const;
|
|
|
|
|
2023-03-29 17:25:11 +00:00
|
|
|
bool operator==(const char* other) const;
|
|
|
|
bool operator==(StringView other) const;
|
|
|
|
|
2023-04-07 13:14:46 +00:00
|
|
|
Result<Vector<String>> split(StringView delim) const;
|
2023-04-20 18:08:34 +00:00
|
|
|
Result<Vector<String>> split_once(char delim) const;
|
2023-06-17 18:58:54 +00:00
|
|
|
Result<Vector<StringView>> split_view(char delim) const;
|
|
|
|
|
|
|
|
bool contains(char v) const;
|
2023-04-20 18:08:34 +00:00
|
|
|
|
2023-05-02 08:49:12 +00:00
|
|
|
static StringView from_fixed_size_cstring(const char* string, usize max);
|
|
|
|
|
2023-04-20 18:08:34 +00:00
|
|
|
Result<usize> to_uint() const;
|
2023-04-07 13:14:46 +00:00
|
|
|
|
2023-03-29 17:25:11 +00:00
|
|
|
Iterator begin() const
|
|
|
|
{
|
|
|
|
return m_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
Iterator end() const
|
|
|
|
{
|
|
|
|
return m_string + m_length;
|
|
|
|
}
|
|
|
|
|
2023-03-29 15:43:10 +00:00
|
|
|
private:
|
|
|
|
const char* m_string { nullptr };
|
|
|
|
|
|
|
|
usize m_length { 0 };
|
|
|
|
};
|
2023-03-29 20:10:01 +00:00
|
|
|
|
|
|
|
inline StringView operator""_sv(const char* cstring)
|
|
|
|
{
|
|
|
|
return StringView { cstring };
|
|
|
|
}
|
|
|
|
|
|
|
|
inline StringView operator""_sv(const char* cstring, usize length)
|
|
|
|
{
|
|
|
|
return StringView { cstring, length };
|
|
|
|
}
|
2023-07-21 18:44:01 +00:00
|
|
|
|
|
|
|
template <> u64 hash(const StringView& value, u64 salt);
|