mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-01 18:22:27 +00:00
ctl string const/value tweaks (#1218)
The mangled name of a C++ function will typically not vary by const-ness of a by-value parameter; in other words, there is no meaning to a const- qualified by-value parameter in a function prototype. However, the const keyword _does_ matter at function _definition_ time, like it does with a variable declared in the body. So for prototypes, we strip out const for by-value parameters; but for definitions, we leave them alone. 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 also changes a couple of const string_view& to be simply by- value string_view. A string_view is only two words; it rarely ever makes sense to pass one by reference if it’s not going to be mutated.
This commit is contained in:
parent
ddfaf3fee0
commit
e38a6e7996
4 changed files with 51 additions and 48 deletions
|
@ -54,12 +54,12 @@ string::string(const string_view s) noexcept : string()
|
|||
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);
|
||||
}
|
||||
|
||||
string::string(const char* s, size_t size) noexcept : string()
|
||||
string::string(const char* s, const size_t size) noexcept : string()
|
||||
{
|
||||
append(s, size);
|
||||
}
|
||||
|
@ -101,7 +101,7 @@ string::reserve(size_t c2) noexcept
|
|||
}
|
||||
|
||||
void
|
||||
string::resize(size_t n2, char ch) noexcept
|
||||
string::resize(const size_t n2, const char ch) noexcept
|
||||
{
|
||||
size_t c2;
|
||||
if (ckd_add(&c2, n2, 1))
|
||||
|
@ -118,7 +118,7 @@ string::resize(size_t n2, char ch) noexcept
|
|||
}
|
||||
|
||||
void
|
||||
string::append(char ch) noexcept
|
||||
string::append(const char ch) noexcept
|
||||
{
|
||||
size_t n2;
|
||||
if (ckd_add(&n2, size(), 2))
|
||||
|
@ -139,7 +139,7 @@ string::append(char ch) noexcept
|
|||
}
|
||||
|
||||
void
|
||||
string::grow(size_t size) noexcept
|
||||
string::grow(const size_t size) noexcept
|
||||
{
|
||||
size_t need;
|
||||
if (ckd_add(&need, this->size(), size))
|
||||
|
@ -158,7 +158,7 @@ string::grow(size_t size) noexcept
|
|||
}
|
||||
|
||||
void
|
||||
string::append(char ch, size_t size) noexcept
|
||||
string::append(const char ch, const size_t size) noexcept
|
||||
{
|
||||
grow(size);
|
||||
if (size)
|
||||
|
@ -172,7 +172,7 @@ string::append(char ch, size_t size) noexcept
|
|||
}
|
||||
|
||||
void
|
||||
string::append(const void* data, size_t size) noexcept
|
||||
string::append(const void* data, const size_t size) noexcept
|
||||
{
|
||||
grow(size);
|
||||
if (size)
|
||||
|
@ -254,7 +254,7 @@ string::starts_with(const string_view s) const noexcept
|
|||
}
|
||||
|
||||
size_t
|
||||
string::find(char ch, size_t pos) const noexcept
|
||||
string::find(const char ch, const size_t pos) const noexcept
|
||||
{
|
||||
char* q;
|
||||
if ((q = (char*)memchr(data(), ch, size())))
|
||||
|
@ -263,7 +263,7 @@ string::find(char ch, size_t pos) const noexcept
|
|||
}
|
||||
|
||||
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;
|
||||
if (pos > size())
|
||||
|
@ -274,7 +274,7 @@ string::find(const string_view s, size_t pos) const noexcept
|
|||
}
|
||||
|
||||
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;
|
||||
if (pos > size())
|
||||
|
@ -289,7 +289,9 @@ string::substr(size_t pos, size_t count) const noexcept
|
|||
}
|
||||
|
||||
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;
|
||||
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::insert(size_t i, const string_view s) noexcept
|
||||
string::insert(const size_t i, const string_view s) noexcept
|
||||
{
|
||||
if (i > size())
|
||||
__builtin_trap();
|
||||
|
@ -343,7 +345,7 @@ string::insert(size_t i, const string_view s) noexcept
|
|||
}
|
||||
|
||||
string&
|
||||
string::erase(size_t pos, size_t count) noexcept
|
||||
string::erase(const size_t pos, size_t count) noexcept
|
||||
{
|
||||
if (pos > size())
|
||||
__builtin_trap();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue