Luna/libluna/include/luna/StringView.h
apio e6645ed607
StringView+apps: Add a _sv literal suffix to not confuse function overloads
C++ was being naughty and implicitly casting our fallback const char* to a boolean.
2023-03-29 22:10:01 +02:00

65 lines
1.0 KiB
C++

#pragma once
#include <luna/Result.h>
class String;
class StringView
{
typedef const char* Iterator;
public:
StringView(const char* c_str);
StringView(const char* c_str, usize length);
StringView();
StringView(const StringView&);
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;
Iterator begin() const
{
return m_string;
}
Iterator end() const
{
return m_string + m_length;
}
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 };
}