Skip to content

Commit e38a6e7

Browse files
committed
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.
1 parent ddfaf3f commit e38a6e7

File tree

4 files changed

+51
-48
lines changed

4 files changed

+51
-48
lines changed

ctl/string.cc

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ string::string(const string_view s) noexcept : string()
5454
append(s.p, s.n);
5555
}
5656

57-
string::string(size_t size, char ch) noexcept : string()
57+
string::string(const size_t size, const char ch) noexcept : string()
5858
{
5959
resize(size, ch);
6060
}
6161

62-
string::string(const char* s, size_t size) noexcept : string()
62+
string::string(const char* s, const size_t size) noexcept : string()
6363
{
6464
append(s, size);
6565
}
@@ -101,7 +101,7 @@ string::reserve(size_t c2) noexcept
101101
}
102102

103103
void
104-
string::resize(size_t n2, char ch) noexcept
104+
string::resize(const size_t n2, const char ch) noexcept
105105
{
106106
size_t c2;
107107
if (ckd_add(&c2, n2, 1))
@@ -118,7 +118,7 @@ string::resize(size_t n2, char ch) noexcept
118118
}
119119

120120
void
121-
string::append(char ch) noexcept
121+
string::append(const char ch) noexcept
122122
{
123123
size_t n2;
124124
if (ckd_add(&n2, size(), 2))
@@ -139,7 +139,7 @@ string::append(char ch) noexcept
139139
}
140140

141141
void
142-
string::grow(size_t size) noexcept
142+
string::grow(const size_t size) noexcept
143143
{
144144
size_t need;
145145
if (ckd_add(&need, this->size(), size))
@@ -158,7 +158,7 @@ string::grow(size_t size) noexcept
158158
}
159159

160160
void
161-
string::append(char ch, size_t size) noexcept
161+
string::append(const char ch, const size_t size) noexcept
162162
{
163163
grow(size);
164164
if (size)
@@ -172,7 +172,7 @@ string::append(char ch, size_t size) noexcept
172172
}
173173

174174
void
175-
string::append(const void* data, size_t size) noexcept
175+
string::append(const void* data, const size_t size) noexcept
176176
{
177177
grow(size);
178178
if (size)
@@ -254,7 +254,7 @@ string::starts_with(const string_view s) const noexcept
254254
}
255255

256256
size_t
257-
string::find(char ch, size_t pos) const noexcept
257+
string::find(const char ch, const size_t pos) const noexcept
258258
{
259259
char* q;
260260
if ((q = (char*)memchr(data(), ch, size())))
@@ -263,7 +263,7 @@ string::find(char ch, size_t pos) const noexcept
263263
}
264264

265265
size_t
266-
string::find(const string_view s, size_t pos) const noexcept
266+
string::find(const string_view s, const size_t pos) const noexcept
267267
{
268268
char* q;
269269
if (pos > size())
@@ -274,7 +274,7 @@ string::find(const string_view s, size_t pos) const noexcept
274274
}
275275

276276
string
277-
string::substr(size_t pos, size_t count) const noexcept
277+
string::substr(const size_t pos, size_t count) const noexcept
278278
{
279279
size_t last;
280280
if (pos > size())
@@ -289,7 +289,9 @@ string::substr(size_t pos, size_t count) const noexcept
289289
}
290290

291291
string&
292-
string::replace(size_t pos, size_t count, const string_view& s) noexcept
292+
string::replace(const size_t pos,
293+
const size_t count,
294+
const string_view s) noexcept
293295
{
294296
size_t last;
295297
if (ckd_add(&last, pos, count))
@@ -319,7 +321,7 @@ string::replace(size_t pos, size_t count, const string_view& s) noexcept
319321
}
320322

321323
string&
322-
string::insert(size_t i, const string_view s) noexcept
324+
string::insert(const size_t i, const string_view s) noexcept
323325
{
324326
if (i > size())
325327
__builtin_trap();
@@ -343,7 +345,7 @@ string::insert(size_t i, const string_view s) noexcept
343345
}
344346

345347
string&
346-
string::erase(size_t pos, size_t count) noexcept
348+
string::erase(const size_t pos, size_t count) noexcept
347349
{
348350
if (pos > size())
349351
__builtin_trap();

ctl/string.h

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ namespace ctl {
88

99
class string;
1010

11-
string
12-
strcat(const string_view, const string_view) noexcept __wur;
11+
string strcat(string_view, string_view) noexcept __wur;
1312

1413
namespace __ {
1514

@@ -50,7 +49,7 @@ class string
5049
static constexpr size_t npos = -1;
5150

5251
~string() /* noexcept */;
53-
string(const string_view) noexcept;
52+
string(string_view) noexcept;
5453
string(const char*) noexcept;
5554
string(const string&) noexcept;
5655
string(const char*, size_t) noexcept;
@@ -67,17 +66,17 @@ class string
6766
void append(char, size_t) noexcept;
6867
void append(unsigned long) noexcept;
6968
void append(const void*, size_t) noexcept;
70-
string& insert(size_t, const string_view) noexcept;
69+
string& insert(size_t, string_view) noexcept;
7170
string& erase(size_t = 0, size_t = npos) noexcept;
7271
string substr(size_t = 0, size_t = npos) const noexcept;
73-
string& replace(size_t, size_t, const string_view&) noexcept;
74-
bool operator==(const string_view) const noexcept;
75-
bool operator!=(const string_view) const noexcept;
76-
bool contains(const string_view) const noexcept;
77-
bool ends_with(const string_view) const noexcept;
78-
bool starts_with(const string_view) const noexcept;
72+
string& replace(size_t, size_t, string_view) noexcept;
73+
bool operator==(string_view) const noexcept;
74+
bool operator!=(string_view) const noexcept;
75+
bool contains(string_view) const noexcept;
76+
bool ends_with(string_view) const noexcept;
77+
bool starts_with(string_view) const noexcept;
7978
size_t find(char, size_t = 0) const noexcept;
80-
size_t find(const string_view, size_t = 0) const noexcept;
79+
size_t find(string_view, size_t = 0) const noexcept;
8180

8281
string() noexcept
8382
{
@@ -202,14 +201,14 @@ class string
202201
return data()[i];
203202
}
204203

205-
const char& operator[](size_t i) const noexcept
204+
const char& operator[](const size_t i) const noexcept
206205
{
207206
if (i >= size())
208207
__builtin_trap();
209208
return data()[i];
210209
}
211210

212-
void push_back(char ch) noexcept
211+
void push_back(const char ch) noexcept
213212
{
214213
append(ch);
215214
}
@@ -238,7 +237,7 @@ class string
238237
return *this;
239238
}
240239

241-
string& operator+=(char x) noexcept
240+
string& operator+=(const char x) noexcept
242241
{
243242
append(x);
244243
return *this;
@@ -286,14 +285,16 @@ class string
286285
return *(blob + __::sso_max) & 0x80;
287286
}
288287

289-
inline void set_small_size(size_t size) noexcept
288+
inline void set_small_size(const size_t size) noexcept
290289
{
291290
if (size > __::sso_max)
292291
__builtin_trap();
293292
*(blob + __::sso_max) = (__::sso_max - size);
294293
}
295294

296-
inline void set_big_string(char* p, size_t n, size_t c2) noexcept
295+
inline void set_big_string(char* const p,
296+
const size_t n,
297+
const size_t c2) noexcept
297298
{
298299
if (c2 > __::big_mask)
299300
__builtin_trap();
@@ -330,7 +331,7 @@ class string
330331
return reinterpret_cast<const __::big_string*>(blob);
331332
}
332333

333-
friend string strcat(const string_view, const string_view);
334+
friend string strcat(string_view, string_view);
334335

335336
alignas(union {
336337
__::big_string a;

ctl/string_view.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
namespace ctl {
2727

2828
size_t
29-
string_view::find(char ch, size_t pos) const noexcept
29+
string_view::find(const char ch, const size_t pos) const noexcept
3030
{
3131
char* q;
3232
if (n && (q = (char*)memchr(p, ch, n)))
@@ -35,7 +35,7 @@ string_view::find(char ch, size_t pos) const noexcept
3535
}
3636

3737
size_t
38-
string_view::find(const string_view s, size_t pos) const noexcept
38+
string_view::find(const string_view s, const size_t pos) const noexcept
3939
{
4040
char* q;
4141
if (pos > n)
@@ -46,7 +46,7 @@ string_view::find(const string_view s, size_t pos) const noexcept
4646
}
4747

4848
string_view
49-
string_view::substr(size_t pos, size_t count) const noexcept
49+
string_view::substr(const size_t pos, size_t count) const noexcept
5050
{
5151
size_t last;
5252
if (pos > n)

ctl/string_view.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,24 @@ struct string_view
2828
{
2929
}
3030

31-
constexpr string_view(const char* s, size_t n) noexcept : p(s), n(n)
31+
constexpr string_view(const char* s, const size_t n) noexcept : p(s), n(n)
3232
{
3333
}
3434

3535
inline constexpr ~string_view() noexcept
3636
{
3737
}
3838

39-
bool operator==(const string_view) const noexcept;
40-
bool operator!=(const string_view) const noexcept;
41-
bool contains(const string_view) const noexcept;
42-
bool ends_with(const string_view) const noexcept;
43-
bool starts_with(const string_view) const noexcept;
39+
bool operator==(string_view) const noexcept;
40+
bool operator!=(string_view) const noexcept;
41+
bool contains(string_view) const noexcept;
42+
bool ends_with(string_view) const noexcept;
43+
bool starts_with(string_view) const noexcept;
4444
string_view substr(size_t = 0, size_t = npos) const noexcept;
4545
size_t find(char, size_t = 0) const noexcept;
46-
size_t find(const string_view, size_t = 0) const noexcept;
46+
size_t find(string_view, size_t = 0) const noexcept;
4747

48-
constexpr string_view& operator=(const string_view& s) noexcept
48+
constexpr string_view& operator=(const string_view s) noexcept
4949
{
5050
p = s.p;
5151
n = s.n;
@@ -72,22 +72,22 @@ struct string_view
7272
return n;
7373
}
7474

75-
constexpr const char& operator[](size_t i) const noexcept
75+
constexpr const char& operator[](const size_t i) const noexcept
7676
{
7777
if (i >= n)
7878
__builtin_trap();
7979
return p[i];
8080
}
8181

82-
constexpr void remove_prefix(size_t count)
82+
constexpr void remove_prefix(const size_t count)
8383
{
8484
if (count > n)
8585
__builtin_trap();
8686
p += count;
8787
n -= count;
8888
}
8989

90-
constexpr void remove_suffix(size_t count)
90+
constexpr void remove_suffix(const size_t count)
9191
{
9292
if (count > n)
9393
__builtin_trap();
@@ -133,22 +133,22 @@ struct string_view
133133
return strcmp(*this, s);
134134
}
135135

136-
bool operator<(const string_view& s) const noexcept
136+
bool operator<(const string_view s) const noexcept
137137
{
138138
return compare(s) < 0;
139139
}
140140

141-
bool operator<=(const string_view& s) const noexcept
141+
bool operator<=(const string_view s) const noexcept
142142
{
143143
return compare(s) <= 0;
144144
}
145145

146-
bool operator>(const string_view& s) const noexcept
146+
bool operator>(const string_view s) const noexcept
147147
{
148148
return compare(s) > 0;
149149
}
150150

151-
bool operator>=(const string_view& s) const noexcept
151+
bool operator>=(const string_view s) const noexcept
152152
{
153153
return compare(s) >= 0;
154154
}

0 commit comments

Comments
 (0)