Commit d84e6482 authored by Giuseppe Ottaviano's avatar Giuseppe Ottaviano Committed by Facebook Github Bot

Add convenience std::string overload for toLowerAscii

Summary:
`toLowerAscii` is surprisingly hard to use with `std::string` because (until C++17) `data()` returns a `const char*`. In addition, it is not widely known that `s[0]` is legal even if the string is empty. This can lead to a variety of suboptimal code, from explicitly checking emptiness, to casting away the `const` (which causes UB).

Let's just have a simple overload for it.

Reviewed By: pixelb, philippv, yfeldblum

Differential Revision: D5801970

fbshipit-source-id: 407e2e9e66147a0662a5e09c75921255adff0702
parent cb471a45
...@@ -626,6 +626,11 @@ inline void toLowerAscii(MutableStringPiece str) { ...@@ -626,6 +626,11 @@ inline void toLowerAscii(MutableStringPiece str) {
toLowerAscii(str.begin(), str.size()); toLowerAscii(str.begin(), str.size());
} }
inline void toLowerAscii(std::string& str) {
// str[0] is legal also if the string is empty.
toLowerAscii(&str[0], str.size());
}
template < template <
class Iterator = const char*, class Iterator = const char*,
class Base = folly::Range<boost::u8_to_u32_iterator<Iterator>>> class Base = folly::Range<boost::u8_to_u32_iterator<Iterator>>>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment