Skip to content

Commit 6e386c0

Browse files
addaleaxaduh95
authored andcommitted
src: make ToLower/ToUpper input args more flexible
In particular, this enables passing `std::string_view` instead. PR-URL: #60052 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]>
1 parent 9fae03c commit 6e386c0

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

src/util-inl.h

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -193,21 +193,29 @@ char ToLower(char c) {
193193
return std::tolower(c, std::locale::classic());
194194
}
195195

196-
std::string ToLower(const std::string& in) {
197-
std::string out(in.size(), 0);
198-
for (size_t i = 0; i < in.size(); ++i)
199-
out[i] = ToLower(in[i]);
196+
template <typename T>
197+
std::string ToLower(const T& in) {
198+
auto it = std::cbegin(in);
199+
auto end = std::cend(in);
200+
std::string out(std::distance(it, end), 0);
201+
size_t i;
202+
for (i = 0; it != end; ++it, ++i) out[i] = ToLower(*it);
203+
DCHECK_EQ(i, out.size());
200204
return out;
201205
}
202206

203207
char ToUpper(char c) {
204208
return std::toupper(c, std::locale::classic());
205209
}
206210

207-
std::string ToUpper(const std::string& in) {
208-
std::string out(in.size(), 0);
209-
for (size_t i = 0; i < in.size(); ++i)
210-
out[i] = ToUpper(in[i]);
211+
template <typename T>
212+
std::string ToUpper(const T& in) {
213+
auto it = std::cbegin(in);
214+
auto end = std::cend(in);
215+
std::string out(std::distance(it, end), 0);
216+
size_t i;
217+
for (i = 0; it != end; ++it, ++i) out[i] = ToUpper(*it);
218+
DCHECK_EQ(i, out.size());
211219
return out;
212220
}
213221

src/util.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,13 @@ inline v8::Local<v8::String> FIXED_ONE_BYTE_STRING(v8::Isolate* isolate,
365365

366366
// tolower() is locale-sensitive. Use ToLower() instead.
367367
inline char ToLower(char c);
368-
inline std::string ToLower(const std::string& in);
368+
template <typename T>
369+
inline std::string ToLower(const T& in);
369370

370371
// toupper() is locale-sensitive. Use ToUpper() instead.
371372
inline char ToUpper(char c);
372-
inline std::string ToUpper(const std::string& in);
373+
template <typename T>
374+
inline std::string ToUpper(const T& in);
373375

374376
// strcasecmp() is locale-sensitive. Use StringEqualNoCase() instead.
375377
inline bool StringEqualNoCase(const char* a, const char* b);

0 commit comments

Comments
 (0)