mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-28 15:28:30 +00:00
Refactor and improve CTL and other code
This commit is contained in:
parent
1d8f37a2f0
commit
9906f299bb
25 changed files with 5768 additions and 5350 deletions
188
ctl/string.h
188
ctl/string.h
|
@ -2,111 +2,16 @@
|
|||
// vi: set et ft=c++ ts=4 sts=4 sw=4 fenc=utf-8 :vi
|
||||
#ifndef COSMOPOLITAN_CTL_STRING_H_
|
||||
#define COSMOPOLITAN_CTL_STRING_H_
|
||||
#include "string_view.h"
|
||||
|
||||
struct String;
|
||||
namespace ctl {
|
||||
|
||||
struct StringView
|
||||
{
|
||||
const char* p;
|
||||
size_t n;
|
||||
struct string;
|
||||
|
||||
static constexpr size_t npos = -1;
|
||||
string
|
||||
strcat(const string_view, const string_view) noexcept __wur;
|
||||
|
||||
constexpr StringView(const char* s) noexcept
|
||||
: p(s), n(s ? __builtin_strlen(s) : 0)
|
||||
{
|
||||
}
|
||||
|
||||
constexpr StringView(const char* s, size_t n) noexcept : p(s), n(n)
|
||||
{
|
||||
}
|
||||
|
||||
inline constexpr ~StringView() noexcept
|
||||
{
|
||||
}
|
||||
|
||||
int compare(const StringView) const noexcept;
|
||||
bool operator==(const StringView) const noexcept;
|
||||
bool operator!=(const StringView) const noexcept;
|
||||
bool contains(const StringView) const noexcept;
|
||||
String operator+(const StringView) const noexcept;
|
||||
bool ends_with(const StringView) const noexcept;
|
||||
bool starts_with(const StringView) const noexcept;
|
||||
StringView substr(size_t = 0, size_t = npos) const noexcept;
|
||||
size_t find(char, size_t = 0) const noexcept;
|
||||
size_t find(const StringView, size_t = 0) const noexcept;
|
||||
|
||||
constexpr StringView& operator=(const StringView& s) noexcept
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
constexpr const char& operator[](size_t i) const noexcept
|
||||
{
|
||||
if (i >= n)
|
||||
__builtin_trap();
|
||||
return p[i];
|
||||
}
|
||||
|
||||
constexpr void remove_prefix(size_t count)
|
||||
{
|
||||
if (count > n)
|
||||
__builtin_trap();
|
||||
p += count;
|
||||
n -= count;
|
||||
}
|
||||
|
||||
constexpr void remove_suffix(size_t count)
|
||||
{
|
||||
if (count > n)
|
||||
__builtin_trap();
|
||||
n -= count;
|
||||
}
|
||||
|
||||
bool operator<(const StringView& s) const noexcept
|
||||
{
|
||||
return compare(s) < 0;
|
||||
}
|
||||
|
||||
bool operator<=(const StringView& s) const noexcept
|
||||
{
|
||||
return compare(s) <= 0;
|
||||
}
|
||||
|
||||
bool operator>(const StringView& s) const noexcept
|
||||
{
|
||||
return compare(s) > 0;
|
||||
}
|
||||
|
||||
bool operator>=(const StringView& s) const noexcept
|
||||
{
|
||||
return compare(s) >= 0;
|
||||
}
|
||||
};
|
||||
|
||||
struct String
|
||||
struct string
|
||||
{
|
||||
char* p = nullptr;
|
||||
size_t n = 0;
|
||||
|
@ -116,14 +21,14 @@ struct String
|
|||
using const_iterator = const char*;
|
||||
static constexpr size_t npos = -1;
|
||||
|
||||
~String() noexcept;
|
||||
String() = default;
|
||||
String(const StringView) noexcept;
|
||||
String(const char*) noexcept;
|
||||
String(const String&) noexcept;
|
||||
String(const char*, size_t) noexcept;
|
||||
explicit String(size_t, char = 0) noexcept;
|
||||
String& operator=(String&&) noexcept;
|
||||
~string() noexcept;
|
||||
string() = default;
|
||||
string(const string_view) noexcept;
|
||||
string(const char*) noexcept;
|
||||
string(const string&) noexcept;
|
||||
string(const char*, size_t) noexcept;
|
||||
explicit string(size_t, char = 0) noexcept;
|
||||
string& operator=(string&&) noexcept;
|
||||
const char* c_str() const noexcept;
|
||||
|
||||
void pop_back() noexcept;
|
||||
|
@ -134,21 +39,19 @@ struct String
|
|||
void append(char, size_t) noexcept;
|
||||
void append(unsigned long) noexcept;
|
||||
void append(const void*, size_t) noexcept;
|
||||
String& insert(size_t, const StringView) noexcept;
|
||||
String& erase(size_t = 0, size_t = npos) noexcept;
|
||||
String operator+(const StringView) const noexcept;
|
||||
String substr(size_t = 0, size_t = npos) const noexcept;
|
||||
String& replace(size_t, size_t, const StringView&) noexcept;
|
||||
bool operator==(const StringView) const noexcept;
|
||||
bool operator!=(const StringView) const noexcept;
|
||||
bool contains(const StringView) const noexcept;
|
||||
bool ends_with(const StringView) const noexcept;
|
||||
bool starts_with(const StringView) const noexcept;
|
||||
int compare(const StringView) const noexcept;
|
||||
string& insert(size_t, const string_view) noexcept;
|
||||
string& erase(size_t = 0, size_t = npos) noexcept;
|
||||
string substr(size_t = 0, size_t = npos) const noexcept;
|
||||
string& replace(size_t, size_t, const string_view&) noexcept;
|
||||
bool operator==(const string_view) const noexcept;
|
||||
bool operator!=(const string_view) const noexcept;
|
||||
bool contains(const string_view) const noexcept;
|
||||
bool ends_with(const string_view) const noexcept;
|
||||
bool starts_with(const string_view) const noexcept;
|
||||
size_t find(char, size_t = 0) const noexcept;
|
||||
size_t find(const StringView, size_t = 0) const noexcept;
|
||||
size_t find(const string_view, size_t = 0) const noexcept;
|
||||
|
||||
String(String&& s) noexcept : p(s.p), n(s.n), c(s.c)
|
||||
string(string&& s) noexcept : p(s.p), n(s.n), c(s.c)
|
||||
{
|
||||
s.p = nullptr;
|
||||
s.n = 0;
|
||||
|
@ -252,69 +155,82 @@ struct String
|
|||
append(ch);
|
||||
}
|
||||
|
||||
void append(const StringView s) noexcept
|
||||
void append(const string_view s) noexcept
|
||||
{
|
||||
append(s.p, s.n);
|
||||
}
|
||||
|
||||
inline constexpr operator StringView() const noexcept
|
||||
inline constexpr operator string_view() const noexcept
|
||||
{
|
||||
return StringView(p, n);
|
||||
return string_view(p, n);
|
||||
}
|
||||
|
||||
String& operator=(const char* s) noexcept
|
||||
string& operator=(const char* s) noexcept
|
||||
{
|
||||
clear();
|
||||
append(s);
|
||||
return *this;
|
||||
}
|
||||
|
||||
String& operator=(const StringView s) noexcept
|
||||
string& operator=(const string_view s) noexcept
|
||||
{
|
||||
clear();
|
||||
append(s);
|
||||
return *this;
|
||||
}
|
||||
|
||||
String& operator+=(char x) noexcept
|
||||
string& operator+=(char x) noexcept
|
||||
{
|
||||
append(x);
|
||||
return *this;
|
||||
}
|
||||
|
||||
String& operator+=(const StringView s) noexcept
|
||||
string& operator+=(const string_view s) noexcept
|
||||
{
|
||||
append(s);
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool operator<(const StringView s) const noexcept
|
||||
string operator+(const string_view s) const noexcept
|
||||
{
|
||||
return strcat(*this, s);
|
||||
}
|
||||
|
||||
int compare(const string_view s) const noexcept
|
||||
{
|
||||
return strcmp(*this, s);
|
||||
}
|
||||
|
||||
bool operator<(const string_view s) const noexcept
|
||||
{
|
||||
return compare(s) < 0;
|
||||
}
|
||||
|
||||
bool operator<=(const StringView s) const noexcept
|
||||
bool operator<=(const string_view s) const noexcept
|
||||
{
|
||||
return compare(s) <= 0;
|
||||
}
|
||||
|
||||
bool operator>(const StringView s) const noexcept
|
||||
bool operator>(const string_view s) const noexcept
|
||||
{
|
||||
return compare(s) > 0;
|
||||
}
|
||||
|
||||
bool operator>=(const StringView s) const noexcept
|
||||
bool operator>=(const string_view s) const noexcept
|
||||
{
|
||||
return compare(s) >= 0;
|
||||
}
|
||||
};
|
||||
|
||||
#pragma GCC diagnostic ignored "-Wliteral-suffix"
|
||||
} // namespace ctl
|
||||
|
||||
inline String
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wliteral-suffix"
|
||||
inline ctl::string
|
||||
operator"" s(const char* s, size_t n)
|
||||
{
|
||||
return String(s, n);
|
||||
return ctl::string(s, n);
|
||||
}
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
#endif // COSMOPOLITAN_CTL_STRING_H_
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue