2024-06-04 12:41:53 +00:00
|
|
|
// -*-mode:c++;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8-*-
|
|
|
|
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
|
2024-07-19 21:14:08 +00:00
|
|
|
#ifndef CTL_STRINGVIEW_H_
|
|
|
|
#define CTL_STRINGVIEW_H_
|
2024-06-19 05:00:59 +00:00
|
|
|
#include "utility.h"
|
2024-06-04 12:41:53 +00:00
|
|
|
|
|
|
|
namespace ctl {
|
|
|
|
|
|
|
|
struct string_view;
|
|
|
|
|
|
|
|
int
|
|
|
|
strcmp(const string_view, const string_view) noexcept;
|
|
|
|
|
|
|
|
struct string_view
|
|
|
|
{
|
|
|
|
const char* p;
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
using iterator = const char*;
|
|
|
|
using const_iterator = const char*;
|
|
|
|
static constexpr size_t npos = -1;
|
|
|
|
|
|
|
|
constexpr string_view() noexcept : p(nullptr), n(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr string_view(const char* s) noexcept
|
|
|
|
: p(s), n(s ? __builtin_strlen(s) : 0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-06-16 01:09:05 +00:00
|
|
|
constexpr string_view(const char* s, const size_t n) noexcept : p(s), n(n)
|
2024-06-04 12:41:53 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
inline constexpr ~string_view() noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-06-16 01:09:05 +00:00
|
|
|
bool operator==(string_view) const noexcept;
|
|
|
|
bool operator!=(string_view) const noexcept;
|
|
|
|
bool contains(string_view) const noexcept;
|
|
|
|
bool ends_with(string_view) const noexcept;
|
|
|
|
bool starts_with(string_view) const noexcept;
|
2024-06-04 12:41:53 +00:00
|
|
|
string_view substr(size_t = 0, size_t = npos) const noexcept;
|
|
|
|
size_t find(char, size_t = 0) const noexcept;
|
2024-06-16 01:09:05 +00:00
|
|
|
size_t find(string_view, size_t = 0) const noexcept;
|
2024-06-04 12:41:53 +00:00
|
|
|
|
2024-06-16 01:09:05 +00:00
|
|
|
constexpr string_view& operator=(const string_view s) noexcept
|
2024-06-04 12:41:53 +00:00
|
|
|
{
|
|
|
|
p = s.p;
|
|
|
|
n = s.n;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool empty() const noexcept
|
|
|
|
{
|
|
|
|
return !n;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr const char* data() const noexcept
|
|
|
|
{
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr size_t size() const noexcept
|
|
|
|
{
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr size_t length() const noexcept
|
|
|
|
{
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2024-06-16 01:09:05 +00:00
|
|
|
constexpr const char& operator[](const size_t i) const noexcept
|
2024-06-04 12:41:53 +00:00
|
|
|
{
|
|
|
|
if (i >= n)
|
|
|
|
__builtin_trap();
|
|
|
|
return p[i];
|
|
|
|
}
|
|
|
|
|
2024-06-16 01:09:05 +00:00
|
|
|
constexpr void remove_prefix(const size_t count)
|
2024-06-04 12:41:53 +00:00
|
|
|
{
|
|
|
|
if (count > n)
|
|
|
|
__builtin_trap();
|
|
|
|
p += count;
|
|
|
|
n -= count;
|
|
|
|
}
|
|
|
|
|
2024-06-16 01:09:05 +00:00
|
|
|
constexpr void remove_suffix(const size_t count)
|
2024-06-04 12:41:53 +00:00
|
|
|
{
|
|
|
|
if (count > n)
|
|
|
|
__builtin_trap();
|
|
|
|
n -= count;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr const char& front() const
|
|
|
|
{
|
|
|
|
if (!n)
|
|
|
|
__builtin_trap();
|
|
|
|
return p[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr const char& back() const
|
|
|
|
{
|
|
|
|
if (!n)
|
|
|
|
__builtin_trap();
|
|
|
|
return p[n - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr const_iterator begin() noexcept
|
|
|
|
{
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr const_iterator end() noexcept
|
|
|
|
{
|
|
|
|
return p + n;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr const_iterator cbegin() const noexcept
|
|
|
|
{
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr const_iterator cend() const noexcept
|
|
|
|
{
|
|
|
|
return p + n;
|
|
|
|
}
|
|
|
|
|
|
|
|
int compare(const string_view s) const noexcept
|
|
|
|
{
|
|
|
|
return strcmp(*this, s);
|
|
|
|
}
|
|
|
|
|
2024-06-16 01:09:05 +00:00
|
|
|
bool operator<(const string_view s) const noexcept
|
2024-06-04 12:41:53 +00:00
|
|
|
{
|
|
|
|
return compare(s) < 0;
|
|
|
|
}
|
|
|
|
|
2024-06-16 01:09:05 +00:00
|
|
|
bool operator<=(const string_view s) const noexcept
|
2024-06-04 12:41:53 +00:00
|
|
|
{
|
|
|
|
return compare(s) <= 0;
|
|
|
|
}
|
|
|
|
|
2024-06-16 01:09:05 +00:00
|
|
|
bool operator>(const string_view s) const noexcept
|
2024-06-04 12:41:53 +00:00
|
|
|
{
|
|
|
|
return compare(s) > 0;
|
|
|
|
}
|
|
|
|
|
2024-06-16 01:09:05 +00:00
|
|
|
bool operator>=(const string_view s) const noexcept
|
2024-06-04 12:41:53 +00:00
|
|
|
{
|
|
|
|
return compare(s) >= 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ctl
|
|
|
|
|
2024-07-19 21:14:08 +00:00
|
|
|
#endif // CTL_STRINGVIEW_H_
|