mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-08-05 09:20:29 +00:00
ctl string const/value tweaks cont'd
At function definition (as opposed to prototype), we add const to values in parameters by default, unless we're going to mutate them.
This commit is contained in:
parent
c09c5121d8
commit
0e30ed0e1f
4 changed files with 33 additions and 29 deletions
|
@ -54,12 +54,12 @@ string::string(const string_view s) noexcept : string()
|
||||||
append(s.p, s.n);
|
append(s.p, s.n);
|
||||||
}
|
}
|
||||||
|
|
||||||
string::string(size_t size, char ch) noexcept : string()
|
string::string(const size_t size, const char ch) noexcept : string()
|
||||||
{
|
{
|
||||||
resize(size, ch);
|
resize(size, ch);
|
||||||
}
|
}
|
||||||
|
|
||||||
string::string(const char* s, size_t size) noexcept : string()
|
string::string(const char* s, const size_t size) noexcept : string()
|
||||||
{
|
{
|
||||||
append(s, size);
|
append(s, size);
|
||||||
}
|
}
|
||||||
|
@ -101,7 +101,7 @@ string::reserve(size_t c2) noexcept
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
string::resize(size_t n2, char ch) noexcept
|
string::resize(const size_t n2, const char ch) noexcept
|
||||||
{
|
{
|
||||||
size_t c2;
|
size_t c2;
|
||||||
if (ckd_add(&c2, n2, 1))
|
if (ckd_add(&c2, n2, 1))
|
||||||
|
@ -118,7 +118,7 @@ string::resize(size_t n2, char ch) noexcept
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
string::append(char ch) noexcept
|
string::append(const char ch) noexcept
|
||||||
{
|
{
|
||||||
size_t n2;
|
size_t n2;
|
||||||
if (ckd_add(&n2, size(), 2))
|
if (ckd_add(&n2, size(), 2))
|
||||||
|
@ -139,7 +139,7 @@ string::append(char ch) noexcept
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
string::grow(size_t size) noexcept
|
string::grow(const size_t size) noexcept
|
||||||
{
|
{
|
||||||
size_t need;
|
size_t need;
|
||||||
if (ckd_add(&need, this->size(), size))
|
if (ckd_add(&need, this->size(), size))
|
||||||
|
@ -158,7 +158,7 @@ string::grow(size_t size) noexcept
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
string::append(char ch, size_t size) noexcept
|
string::append(const char ch, const size_t size) noexcept
|
||||||
{
|
{
|
||||||
grow(size);
|
grow(size);
|
||||||
if (size)
|
if (size)
|
||||||
|
@ -172,7 +172,7 @@ string::append(char ch, size_t size) noexcept
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
string::append(const void* data, size_t size) noexcept
|
string::append(const void* data, const size_t size) noexcept
|
||||||
{
|
{
|
||||||
grow(size);
|
grow(size);
|
||||||
if (size)
|
if (size)
|
||||||
|
@ -254,7 +254,7 @@ string::starts_with(const string_view s) const noexcept
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t
|
size_t
|
||||||
string::find(char ch, size_t pos) const noexcept
|
string::find(const char ch, const size_t pos) const noexcept
|
||||||
{
|
{
|
||||||
char* q;
|
char* q;
|
||||||
if ((q = (char*)memchr(data(), ch, size())))
|
if ((q = (char*)memchr(data(), ch, size())))
|
||||||
|
@ -263,7 +263,7 @@ string::find(char ch, size_t pos) const noexcept
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t
|
size_t
|
||||||
string::find(const string_view s, size_t pos) const noexcept
|
string::find(const string_view s, const size_t pos) const noexcept
|
||||||
{
|
{
|
||||||
char* q;
|
char* q;
|
||||||
if (pos > size())
|
if (pos > size())
|
||||||
|
@ -274,7 +274,7 @@ string::find(const string_view s, size_t pos) const noexcept
|
||||||
}
|
}
|
||||||
|
|
||||||
string
|
string
|
||||||
string::substr(size_t pos, size_t count) const noexcept
|
string::substr(const size_t pos, size_t count) const noexcept
|
||||||
{
|
{
|
||||||
size_t last;
|
size_t last;
|
||||||
if (pos > size())
|
if (pos > size())
|
||||||
|
@ -289,7 +289,9 @@ string::substr(size_t pos, size_t count) const noexcept
|
||||||
}
|
}
|
||||||
|
|
||||||
string&
|
string&
|
||||||
string::replace(size_t pos, size_t count, const string_view s) noexcept
|
string::replace(const size_t pos,
|
||||||
|
const size_t count,
|
||||||
|
const string_view s) noexcept
|
||||||
{
|
{
|
||||||
size_t last;
|
size_t last;
|
||||||
if (ckd_add(&last, pos, count))
|
if (ckd_add(&last, pos, count))
|
||||||
|
@ -319,7 +321,7 @@ string::replace(size_t pos, size_t count, const string_view s) noexcept
|
||||||
}
|
}
|
||||||
|
|
||||||
string&
|
string&
|
||||||
string::insert(size_t i, const string_view s) noexcept
|
string::insert(const size_t i, const string_view s) noexcept
|
||||||
{
|
{
|
||||||
if (i > size())
|
if (i > size())
|
||||||
__builtin_trap();
|
__builtin_trap();
|
||||||
|
@ -343,7 +345,7 @@ string::insert(size_t i, const string_view s) noexcept
|
||||||
}
|
}
|
||||||
|
|
||||||
string&
|
string&
|
||||||
string::erase(size_t pos, size_t count) noexcept
|
string::erase(const size_t pos, size_t count) noexcept
|
||||||
{
|
{
|
||||||
if (pos > size())
|
if (pos > size())
|
||||||
__builtin_trap();
|
__builtin_trap();
|
||||||
|
|
12
ctl/string.h
12
ctl/string.h
|
@ -201,14 +201,14 @@ class string
|
||||||
return data()[i];
|
return data()[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
const char& operator[](size_t i) const noexcept
|
const char& operator[](const size_t i) const noexcept
|
||||||
{
|
{
|
||||||
if (i >= size())
|
if (i >= size())
|
||||||
__builtin_trap();
|
__builtin_trap();
|
||||||
return data()[i];
|
return data()[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
void push_back(char ch) noexcept
|
void push_back(const char ch) noexcept
|
||||||
{
|
{
|
||||||
append(ch);
|
append(ch);
|
||||||
}
|
}
|
||||||
|
@ -237,7 +237,7 @@ class string
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
string& operator+=(char x) noexcept
|
string& operator+=(const char x) noexcept
|
||||||
{
|
{
|
||||||
append(x);
|
append(x);
|
||||||
return *this;
|
return *this;
|
||||||
|
@ -285,14 +285,16 @@ class string
|
||||||
return *(blob + __::sso_max) & 0x80;
|
return *(blob + __::sso_max) & 0x80;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void set_small_size(size_t size) noexcept
|
inline void set_small_size(const size_t size) noexcept
|
||||||
{
|
{
|
||||||
if (size > __::sso_max)
|
if (size > __::sso_max)
|
||||||
__builtin_trap();
|
__builtin_trap();
|
||||||
*(blob + __::sso_max) = (__::sso_max - size);
|
*(blob + __::sso_max) = (__::sso_max - size);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void set_big_string(char* p, size_t n, size_t c2) noexcept
|
inline void set_big_string(char* const p,
|
||||||
|
const size_t n,
|
||||||
|
const size_t c2) noexcept
|
||||||
{
|
{
|
||||||
if (c2 > __::big_mask)
|
if (c2 > __::big_mask)
|
||||||
__builtin_trap();
|
__builtin_trap();
|
||||||
|
|
|
@ -26,7 +26,7 @@
|
||||||
namespace ctl {
|
namespace ctl {
|
||||||
|
|
||||||
size_t
|
size_t
|
||||||
string_view::find(char ch, size_t pos) const noexcept
|
string_view::find(const char ch, const size_t pos) const noexcept
|
||||||
{
|
{
|
||||||
char* q;
|
char* q;
|
||||||
if (n && (q = (char*)memchr(p, ch, n)))
|
if (n && (q = (char*)memchr(p, ch, n)))
|
||||||
|
@ -35,7 +35,7 @@ string_view::find(char ch, size_t pos) const noexcept
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t
|
size_t
|
||||||
string_view::find(const string_view s, size_t pos) const noexcept
|
string_view::find(const string_view s, const size_t pos) const noexcept
|
||||||
{
|
{
|
||||||
char* q;
|
char* q;
|
||||||
if (pos > n)
|
if (pos > n)
|
||||||
|
@ -46,7 +46,7 @@ string_view::find(const string_view s, size_t pos) const noexcept
|
||||||
}
|
}
|
||||||
|
|
||||||
string_view
|
string_view
|
||||||
string_view::substr(size_t pos, size_t count) const noexcept
|
string_view::substr(const size_t pos, size_t count) const noexcept
|
||||||
{
|
{
|
||||||
size_t last;
|
size_t last;
|
||||||
if (pos > n)
|
if (pos > n)
|
||||||
|
|
|
@ -28,7 +28,7 @@ struct string_view
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr string_view(const char* s, size_t n) noexcept : p(s), n(n)
|
constexpr string_view(const char* s, const size_t n) noexcept : p(s), n(n)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,14 +72,14 @@ struct string_view
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr const char& operator[](size_t i) const noexcept
|
constexpr const char& operator[](const size_t i) const noexcept
|
||||||
{
|
{
|
||||||
if (i >= n)
|
if (i >= n)
|
||||||
__builtin_trap();
|
__builtin_trap();
|
||||||
return p[i];
|
return p[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr void remove_prefix(size_t count)
|
constexpr void remove_prefix(const size_t count)
|
||||||
{
|
{
|
||||||
if (count > n)
|
if (count > n)
|
||||||
__builtin_trap();
|
__builtin_trap();
|
||||||
|
@ -87,7 +87,7 @@ struct string_view
|
||||||
n -= count;
|
n -= count;
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr void remove_suffix(size_t count)
|
constexpr void remove_suffix(const size_t count)
|
||||||
{
|
{
|
||||||
if (count > n)
|
if (count > n)
|
||||||
__builtin_trap();
|
__builtin_trap();
|
||||||
|
@ -133,22 +133,22 @@ struct string_view
|
||||||
return strcmp(*this, s);
|
return strcmp(*this, s);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator<(const string_view& s) const noexcept
|
bool operator<(const string_view s) const noexcept
|
||||||
{
|
{
|
||||||
return compare(s) < 0;
|
return compare(s) < 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator<=(const string_view& s) const noexcept
|
bool operator<=(const string_view s) const noexcept
|
||||||
{
|
{
|
||||||
return compare(s) <= 0;
|
return compare(s) <= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator>(const string_view& s) const noexcept
|
bool operator>(const string_view s) const noexcept
|
||||||
{
|
{
|
||||||
return compare(s) > 0;
|
return compare(s) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator>=(const string_view& s) const noexcept
|
bool operator>=(const string_view s) const noexcept
|
||||||
{
|
{
|
||||||
return compare(s) >= 0;
|
return compare(s) >= 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue