From: Giuseppe Ottaviano Date: Sun, 10 Sep 2017 16:59:56 +0000 (-0700) Subject: Add convenience std::string overload for toLowerAscii X-Git-Tag: v2017.09.11.00^0 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=d84e6482135417bafed3dea69c29b8d8b8c39970;p=folly.git 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 --- diff --git a/folly/String.h b/folly/String.h index 53b54f42..4262a9d1 100644 --- a/folly/String.h +++ b/folly/String.h @@ -626,6 +626,11 @@ inline void toLowerAscii(MutableStringPiece str) { 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 < class Iterator = const char*, class Base = folly::Range>>