]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
rgw: Add `{lower,upper,camel}case_dash_transform`
authorAdam C. Emerson <aemerson@redhat.com>
Fri, 20 Mar 2026 17:54:21 +0000 (13:54 -0400)
committerAdam C. Emerson <aemerson@redhat.com>
Thu, 26 Mar 2026 04:07:20 +0000 (00:07 -0400)
To convert case and separators.

Signed-off-by: Adam C. Emerson <aemerson@redhat.com>
src/rgw/rgw_common.cc
src/rgw/rgw_common.h

index faf80be64bc149ca056cb8376602301d45d52abe..6c1629c3f9e75448f2ce26cf754a8b30214d98a7 100644 (file)
@@ -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()
 {
 }
index 8f95f4ef6716971ba1228754ca6569231817c09b..e86dede3594c3121e2b85d5e7de365e3f3f8da8e 100644 (file)
 
 #include <array>
 #include <cstdint>
+#include <iterator>
+#include <ranges>
 #include <string_view>
-#include <atomic>
 #include <unordered_map>
 
 #include <fmt/format.h>
+
 #include <boost/container/flat_map.hpp>
 #include <boost/container/flat_set.hpp>
 
@@ -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<char> auto out,
+    bool convert2dash = true)
+{
+  bool last_sep = true;
+  return std::ranges::transform(
+      std::forward<decltype(in)>(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<unsigned char>(c)) : tolower(static_cast<unsigned char>(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<char> auto out,
+    bool bidirectional = false)
+{
+  return std::ranges::transform(
+      std::forward<decltype(in)>(in), out, [bidirectional](char c) -> char {
+        switch (c) {
+        case '_':
+          return '-';
+        case '-':
+          return bidirectional ? '_' : '-';
+        default:
+          return tolower(static_cast<unsigned char>(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<char> auto out,
+    bool bidirectional = false)
+{
+  return std::ranges::transform(
+      std::forward<decltype(in)>(in), out, [bidirectional](char c) -> char {
+        switch (c) {
+        case '-':
+          return '_';
+        case '_':
+          if (bidirectional)
+            return '-';
+          else
+            return toupper(static_cast<unsigned char>(c));
+        default:
+          return toupper(static_cast<unsigned char>(c));
+        }
+      });
+}
 
 void rgw_setup_saved_curl_handles();
 void rgw_release_all_curl_handles();