From c09c5121d862c45719924b25bc5f38d120b7e2ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C5=8Dshin?= Date: Sat, 15 Jun 2024 14:12:54 -0700 Subject: [PATCH] ctl string const/value tweaks 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. 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. --- ctl/string.cc | 2 +- ctl/string.h | 23 +++++++++++------------ ctl/string_view.h | 14 +++++++------- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/ctl/string.cc b/ctl/string.cc index 503ade6a8..62301bcfd 100644 --- a/ctl/string.cc +++ b/ctl/string.cc @@ -289,7 +289,7 @@ 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(size_t pos, size_t count, const string_view s) noexcept { size_t last; if (ckd_add(&last, pos, count)) diff --git a/ctl/string.h b/ctl/string.h index 450bb44b2..e5a848cc4 100644 --- a/ctl/string.h +++ b/ctl/string.h @@ -8,8 +8,7 @@ namespace ctl { class string; -string -strcat(const string_view, const string_view) noexcept __wur; +string strcat(string_view, string_view) noexcept __wur; namespace __ { @@ -50,7 +49,7 @@ class string static constexpr size_t npos = -1; ~string() /* noexcept */; - string(const string_view) noexcept; + string(string_view) noexcept; string(const char*) noexcept; string(const string&) noexcept; string(const char*, size_t) noexcept; @@ -67,17 +66,17 @@ class string void append(char, size_t) noexcept; void append(unsigned long) noexcept; void append(const void*, size_t) noexcept; - string& insert(size_t, const string_view) noexcept; + string& insert(size_t, 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; + string& replace(size_t, size_t, string_view) 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; 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; string() noexcept { @@ -330,7 +329,7 @@ class string return reinterpret_cast(blob); } - friend string strcat(const string_view, const string_view); + friend string strcat(string_view, string_view); alignas(union { __::big_string a; diff --git a/ctl/string_view.h b/ctl/string_view.h index 0fe526baf..87e409065 100644 --- a/ctl/string_view.h +++ b/ctl/string_view.h @@ -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;