]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/seastar/include/seastar/util/print_safe.hh
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / seastar / include / seastar / util / print_safe.hh
index ffcdbe1900f3858f7bd0da81ae87217241626cf6..d7da26cf1cb798c1980a69f0095e8e4d320b4c00 100644 (file)
 
 #pragma once
 
+#include <seastar/util/concepts.hh>
+#include <cassert>
+#include <cstring>
+#if __cplusplus > 201703L
+#include <concepts>
+#endif
 #include <stdio.h>
+#include <unistd.h>
 
 namespace seastar {
 
@@ -57,28 +64,37 @@ void print_safe(const char *str) noexcept {
     print_safe(str, strlen(str));
 }
 
+// Fills a buffer with a hexadecimal representation of an integer
+// and returns a pointer to the first character.
+// For example, convert_hex_safe(buf, 4, uint16_t(12)) fills the buffer with "   c".
+template<typename Integral, char Padding = ' '>
+SEASTAR_CONCEPT( requires std::integral<Integral> )
+char* convert_hex_safe(char *buf, size_t bufsz, Integral n) noexcept {
+    const char *digits = "0123456789abcdef";
+    memset(buf, Padding, bufsz);
+    auto* p = buf + bufsz;
+    do {
+        assert(p > buf);
+        *--p = digits[n & 0xf];
+        n >>= 4;
+    } while (n);
+    return p;
+}
+
 // Fills a buffer with a zero-padded hexadecimal representation of an integer.
 // For example, convert_zero_padded_hex_safe(buf, 4, uint16_t(12)) fills the buffer with "000c".
 template<typename Integral>
-SEASTAR_CONCEPT( requires std::is_integral_v<Integral> )
+SEASTAR_CONCEPT( requires std::integral<Integral> )
 void convert_zero_padded_hex_safe(char *buf, size_t bufsz, Integral n) noexcept {
-    const char *digits = "0123456789abcdef";
-    memset(buf, '0', bufsz);
-    unsigned i = bufsz;
-    while (n) {
-        assert(i > 0);
-        buf[--i] = digits[n & 0xf];
-        n >>= 4;
-    }
+    convert_hex_safe<'0'>(buf, bufsz, n);
 }
 
 // Prints zero-padded hexadecimal representation of an integer to stderr.
 // For example, print_zero_padded_hex_safe(uint16_t(12)) prints "000c".
 // Async-signal safe.
 template<typename Integral>
+SEASTAR_CONCEPT ( requires std::signed_integral<Integral> )
 void print_zero_padded_hex_safe(Integral n) noexcept {
-    static_assert(std::is_integral<Integral>::value && !std::is_signed<Integral>::value, "Requires unsigned integrals");
-
     char buf[sizeof(n) * 2];
     convert_zero_padded_hex_safe(buf, sizeof(buf), n);
     print_safe(buf, sizeof(buf));
@@ -88,10 +104,8 @@ void print_zero_padded_hex_safe(Integral n) noexcept {
 // The argument bufsz is the maximum size of the buffer.
 // For example, print_decimal_safe(buf, 16, 12) prints "12".
 template<typename Integral>
-SEASTAR_CONCEPT( requires std::is_integral_v<Integral> )
+SEASTAR_CONCEPT( requires std::unsigned_integral<Integral> )
 size_t convert_decimal_safe(char *buf, size_t bufsz, Integral n) noexcept {
-    static_assert(std::is_integral<Integral>::value && !std::is_signed<Integral>::value, "Requires unsigned integrals");
-
     char tmp[sizeof(n) * 3];
     unsigned i = bufsz;
     do {