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:
Steven Dee (Jōshin) 2024-06-15 18:09:05 -07:00
parent ddfaf3fee0
commit e38a6e7996
No known key found for this signature in database
4 changed files with 51 additions and 48 deletions

View file

@ -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)
{
}
@ -36,16 +36,16 @@ struct string_view
{
}
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;
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;
string_view substr(size_t = 0, size_t = npos) const noexcept;
size_t find(char, size_t = 0) const noexcept;
size_t find(const string_view, size_t = 0) const noexcept;
size_t find(string_view, size_t = 0) const noexcept;
constexpr string_view& operator=(const string_view& s) noexcept
constexpr string_view& operator=(const string_view s) noexcept
{
p = s.p;
n = s.n;
@ -72,14 +72,14 @@ struct string_view
return n;
}
constexpr const char& operator[](size_t i) const noexcept
constexpr const char& operator[](const size_t i) const noexcept
{
if (i >= n)
__builtin_trap();
return p[i];
}
constexpr void remove_prefix(size_t count)
constexpr void remove_prefix(const size_t count)
{
if (count > n)
__builtin_trap();
@ -87,7 +87,7 @@ struct string_view
n -= count;
}
constexpr void remove_suffix(size_t count)
constexpr void remove_suffix(const size_t count)
{
if (count > n)
__builtin_trap();
@ -133,22 +133,22 @@ struct string_view
return strcmp(*this, s);
}
bool operator<(const string_view& s) const noexcept
bool operator<(const string_view s) const noexcept
{
return compare(s) < 0;
}
bool operator<=(const string_view& s) const noexcept
bool operator<=(const string_view s) const noexcept
{
return compare(s) <= 0;
}
bool operator>(const string_view& s) const noexcept
bool operator>(const string_view s) const noexcept
{
return compare(s) > 0;
}
bool operator>=(const string_view& s) const noexcept
bool operator>=(const string_view s) const noexcept
{
return compare(s) >= 0;
}