Luna/libluna/include/luna/StringView.h

73 lines
1.2 KiB
C
Raw Normal View History

2023-03-29 15:43:10 +00:00
#pragma once
#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
{
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;
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;
Result<usize> to_uint() const;
2023-04-07 13:14:46 +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 };
};
inline StringView operator""_sv(const char* cstring)
{
return StringView { cstring };
}
inline StringView operator""_sv(const char* cstring, usize length)
{
return StringView { cstring, length };
}