From: Adam C. Emerson Date: Fri, 20 Mar 2026 17:54:21 +0000 (-0400) Subject: rgw: Add `{lower,upper,camel}case_dash_transform` X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=966f5c6529224f88f3816e56b42956b28e3865c8;p=ceph.git rgw: Add `{lower,upper,camel}case_dash_transform` To convert case and separators. Signed-off-by: Adam C. Emerson --- diff --git a/src/rgw/rgw_common.cc b/src/rgw/rgw_common.cc index faf80be64bc1..6c1629c3f9e7 100644 --- a/src/rgw/rgw_common.cc +++ b/src/rgw/rgw_common.cc @@ -2263,65 +2263,6 @@ bool match_policy(const std::string& pattern, const std::string& input, } } -/* - * make attrs look-like-this - * converts underscores to dashes - */ -string lowercase_dash_http_attr(const string& orig, bool bidirection) -{ - const char *s = orig.c_str(); - char buf[orig.size() + 1]; - buf[orig.size()] = '\0'; - - for (size_t i = 0; i < orig.size(); ++i, ++s) { - switch (*s) { - case '_': - buf[i] = '-'; - break; - case '-': - if (bidirection) - buf[i] = '_'; - else - buf[i] = tolower(*s); - break; - default: - buf[i] = tolower(*s); - } - } - return string(buf); -} - -/* - * make attrs Look-Like-This - * converts underscores to dashes - */ -string camelcase_dash_http_attr(const string& orig, bool convert2dash) -{ - const char *s = orig.c_str(); - char buf[orig.size() + 1]; - buf[orig.size()] = '\0'; - - bool last_sep = true; - - for (size_t i = 0; i < orig.size(); ++i, ++s) { - switch (*s) { - case '_': - case '-': - buf[i] = convert2dash ? '-' : *s; - last_sep = true; - break; - default: - if (last_sep) { - buf[i] = toupper(*s); - } else { - buf[i] = tolower(*s); - } - last_sep = false; - } - } - return string(buf); -} - RGWBucketInfo::RGWBucketInfo() { } diff --git a/src/rgw/rgw_common.h b/src/rgw/rgw_common.h index 8f95f4ef6716..e86dede3594c 100644 --- a/src/rgw/rgw_common.h +++ b/src/rgw/rgw_common.h @@ -18,11 +18,13 @@ #include #include +#include +#include #include -#include #include #include + #include #include @@ -1965,8 +1967,179 @@ static constexpr uint32_t MATCH_POLICY_ARN = 0x02; extern bool match_policy(const std::string& pattern, const std::string& input, uint32_t flag); -extern std::string camelcase_dash_http_attr(const std::string& orig, bool convert2dash = true); -extern std::string lowercase_dash_http_attr(const std::string& orig, bool bidirection = false); +/* + * Converts to lowercase with upper after a separator + * + * `THIS_KIND_OF_STRING` to `This_Kind_Of_String` and possibly + * `This-Kind-Of-String` if `convert2dash` is true. Not actually CamelCase + * as generally understood. + * + * \param[in] in Range to transform + * \param[out] out Output iterator + * \param[in] convert2dash Transform '_' to '-' + * + * \return A structure of input and output iterators, as with std::transform. + */ +inline auto +camelcase_dash_transform( + std::ranges::input_range auto&& in, + std::output_iterator auto out, + bool convert2dash = true) +{ + bool last_sep = true; + return std::ranges::transform( + std::forward(in), out, [&last_sep, convert2dash](char c) -> char { + switch (c) { + case '_': + case '-': + last_sep = true; + return convert2dash ? '-' : c; + default: + c = last_sep ? toupper(static_cast(c)) : tolower(static_cast(c)); + last_sep = false; + return c; + } + }); +} + +/* + * Converts to lowercase with upper after a separator + * + * `THIS_KIND_OF_STRING` to `This_Kind_Of_String` and possibly + * `This-Kind-Of-String` if `convert2dash` is true. Not actually CamelCase + * as generally understood. + * + * \param[in] in Range to transform + * \param[in] convert2dash Transform '_' to '-' + * + * \return Transformed string + */ +inline std::string +camelcase_dash_http_attr(const std::string& in, bool convert2dash = true) +{ + std::string out; + out.reserve(in.size()); + camelcase_dash_transform(in, std::back_inserter(out), convert2dash); + return out; +} + +/* + * Converts to lowercase with upper after a separator + * + * `THIS_KIND_OF_STRING` to `This_Kind_Of_String` and possibly + * `This-Kind-Of-String` if `convert2dash` is true. Not actually CamelCase + * as generally understood. + * + * \param[in] in Range to transform + * \param[in] convert2dash Transform '_' to '-' + * + * \return Transformed string move-constructed from 'in'. + */ +inline std::string +camelcase_dash_http_attr(std::string&& in, bool convert2dash = true) +{ + camelcase_dash_transform(in, in.begin(), convert2dash); + return std::move(in); +} + +/* + * Converts uppercase to lowercase and underscores to dashes + * + * `THIS_KIND_OF_STRING` to `this-kind-of-string` + * + * \param[in] in Range to transform + * \param[out] out Output iterator + * \param[in] bidirectional Transform '-' to '_' + * + * \return A structure of input and output iterators, as with std::transform. + */ +inline auto +lowercase_dash_transform( + std::ranges::input_range auto&& in, + std::output_iterator auto out, + bool bidirectional = false) +{ + return std::ranges::transform( + std::forward(in), out, [bidirectional](char c) -> char { + switch (c) { + case '_': + return '-'; + case '-': + return bidirectional ? '_' : '-'; + default: + return tolower(static_cast(c)); + } + }); +} + +/* + * Converts uppercase to lowercase and underscores to dashes + * + * `THIS_KIND_OF_STRING` to `this-kind-of-string` + * + * \param[in] in String to transform + * \param[in] bidirectional Transform '-' to '_' + * + * \return Transformed string + */ +inline std::string +lowercase_dash_http_attr(const std::string& in, bool bidirectional = false) +{ + std::string out; + out.reserve(in.size()); + lowercase_dash_transform(in, std::back_inserter(out), bidirectional); + return out; +} + +/* + * Converts uppercase to lowercase and underscores to dashes + * + * `THIS_KIND_OF_STRING` to `this-kind-of-string` + * + * \param[in] in String to transform + * \param[in] bidirectional Transform '-' to '_' + * + * \return Transformed string move-constructed from 'in'. + */ +inline std::string +lowercase_dash_http_attr(std::string&& in, bool bidirectional = false) +{ + lowercase_dash_transform(in, in.begin(), bidirectional); + return std::move(in); +} + +/* + * Converts lower to upper and dashes to underscores + * + * 'this-kind-of-string' to 'THIS_KIND_OF_STRING' + * + * \param[in] in Range to transform + * \param[out] out Output iterator + * \param[in] bidirectional Transform '_' to '-' + * + * \return A structure of input and output iterators, as with std::transform. + */ +inline auto +uppercase_dash_transform( + std::ranges::input_range auto&& in, + std::output_iterator auto out, + bool bidirectional = false) +{ + return std::ranges::transform( + std::forward(in), out, [bidirectional](char c) -> char { + switch (c) { + case '-': + return '_'; + case '_': + if (bidirectional) + return '-'; + else + return toupper(static_cast(c)); + default: + return toupper(static_cast(c)); + } + }); +} void rgw_setup_saved_curl_handles(); void rgw_release_all_curl_handles();