Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
src: make ToLower/ToUpper input args more flexible
In particular, this enables passing `std::string_view` instead.
  • Loading branch information
addaleax committed Sep 28, 2025
commit a0c6a1e8df60a4b777c035dbcfc8bb538258c49f
24 changes: 16 additions & 8 deletions src/util-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,21 +194,29 @@ char ToLower(char c) {
return std::tolower(c, std::locale::classic());
}

std::string ToLower(const std::string& in) {
std::string out(in.size(), 0);
for (size_t i = 0; i < in.size(); ++i)
out[i] = ToLower(in[i]);
template <typename T>
std::string ToLower(const T& in) {
auto it = std::cbegin(in);
auto end = std::cend(in);
std::string out(std::distance(it, end), 0);
size_t i;
for (i = 0; it != end; ++it, ++i) out[i] = ToLower(*it);
DCHECK_EQ(i, out.size());
return out;
}

char ToUpper(char c) {
return std::toupper(c, std::locale::classic());
}

std::string ToUpper(const std::string& in) {
std::string out(in.size(), 0);
for (size_t i = 0; i < in.size(); ++i)
out[i] = ToUpper(in[i]);
template <typename T>
std::string ToUpper(const T& in) {
auto it = std::cbegin(in);
auto end = std::cend(in);
std::string out(std::distance(it, end), 0);
size_t i;
for (i = 0; it != end; ++it, ++i) out[i] = ToUpper(*it);
DCHECK_EQ(i, out.size());
return out;
}

Expand Down
6 changes: 4 additions & 2 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -366,11 +366,13 @@ inline v8::Local<v8::String> FIXED_ONE_BYTE_STRING(v8::Isolate* isolate,

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

// toupper() is locale-sensitive. Use ToUpper() instead.
inline char ToUpper(char c);
inline std::string ToUpper(const std::string& in);
template <typename T>
inline std::string ToUpper(const T& in);

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