szaszm commented on code in PR #1543:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1543#discussion_r1158428649
##########
libminifi/include/utils/StringUtils.h:
##########
@@ -88,7 +87,15 @@ class StringUtils {
*/
static std::optional<bool> toBool(const std::string& input);
- static std::string toLower(std::string_view str);
+ static inline std::string toLower(std::string_view str) {
+ const auto tolower = [](auto c) { return std::tolower(static_cast<unsigned
char>(c)); };
+ return str | ranges::views::transform(tolower) | ranges::to<std::string>();
+ }
+
+ static inline std::string toUpper(std::string_view str) {
+ const auto toupper = [](auto c) { return std::toupper(static_cast<unsigned
char>(c)); };
+ return str | ranges::views::transform(toupper) | ranges::to<std::string>();
+ }
Review Comment:
The dumber version of taking a `std::string` by value, and doing in-place
transform would be more efficient, because it allows NRVO, and better
optimization
demonstration: https://godbolt.org/z/q5qWqGbnx
above version: 3 allocations, 1 copy
std::string version: 1 allocation (of the passed-in string)
We need 1 allocation either way, to construct the result, but by taking a
std::string by value, we push this responsibility to the caller, who may be
able to avoid it by reusing an existing std::string object. If the string is
moved in from an existing object, the resulting assembly is basically
equivalent to passing in a pointer to the string, and doing an in-place
transform, with no extra allocation, but the interface keeps the
value-semantics, and falls back to copy when necessary.
```suggestion
static inline std::string toLower(std::string str) {
const auto tolower = [](auto c) { return
std::tolower(static_cast<unsigned char>(c)); };
std::transform(std::begin(str), std::end(str), std::begin(str), tolower);
return str;
}
static inline std::string toUpper(std::string str) {
const auto toupper = [](auto c) { return
std::toupper(static_cast<unsigned char>(c)); };
std::transform(std::begin(str), std::end(str), std::begin(str), toupper);
return str;
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]