Commit 8ccf92c9 authored by Dominik Charousset's avatar Dominik Charousset

Support lowercase output in append_hex

parent fdce05dd
......@@ -56,7 +56,6 @@ set(CAF_CORE_SOURCES
src/deserializer.cpp
src/detail/abstract_worker.cpp
src/detail/abstract_worker_hub.cpp
src/detail/append_hex.cpp
src/detail/append_percent_encoded.cpp
src/detail/behavior_impl.cpp
src/detail/behavior_stack.cpp
......
......@@ -21,25 +21,41 @@
#include <string>
#include <type_traits>
#include "caf/byte.hpp"
#include "caf/detail/core_export.hpp"
#include "caf/detail/type_traits.hpp"
namespace caf::detail {
CAF_CORE_EXPORT void
append_hex(std::string& result, const uint8_t* xs, size_t n);
template <class T>
enable_if_t<has_data_member<T>::value>
append_hex(std::string& result, const T& x) {
return append_hex(result, reinterpret_cast<const uint8_t*>(x.data()),
x.size());
enum class hex_format {
uppercase,
lowercase,
};
template <hex_format format = hex_format::uppercase>
void append_hex(std::string& result, const void* vptr, size_t n) {
if (n == 0) {
result += "00";
return;
}
auto xs = reinterpret_cast<const uint8_t*>(vptr);
const char* tbl;
if constexpr (format == hex_format::uppercase)
tbl = "0123456789ABCDEF";
else
tbl = "0123456789abcdef";
char buf[3] = {0, 0, 0};
for (size_t i = 0; i < n; ++i) {
auto c = xs[i];
buf[0] = tbl[c >> 4];
buf[1] = tbl[c & 0x0F];
result += buf;
}
}
template <class T>
enable_if_t<std::is_integral<T>::value>
append_hex(std::string& result, const T& x) {
return append_hex(result, reinterpret_cast<const uint8_t*>(&x), sizeof(T));
template <hex_format format = hex_format::uppercase, class T = int>
void append_hex(std::string& result, const T& x) {
append_hex<format>(result, &x, sizeof(T));
}
} // namespace caf::detail
......@@ -18,7 +18,6 @@
#pragma once
#include "caf/detail/append_hex.hpp"
#include "caf/detail/core_export.hpp"
#include "caf/fwd.hpp"
......@@ -26,7 +25,7 @@ namespace caf::detail {
// Escapes all reserved characters according to RFC 3986 in `x` and
// adds the encoded string to `str`.
CAF_CORE_EXPORT void
append_percent_encoded(std::string& str, string_view x, bool is_path = false);
CAF_CORE_EXPORT void append_percent_encoded(std::string& str, string_view x,
bool is_path = false);
} // namespace caf::detail
......@@ -21,6 +21,7 @@
#include <limits>
#include "caf/config.hpp"
#include "caf/detail/parser/ascii_to_int.hpp"
#include "caf/detail/type_traits.hpp"
......
......@@ -267,8 +267,7 @@ bool operator<(const T* x, const intrusive_ptr<T>& y) {
template <class T>
std::string to_string(const intrusive_ptr<T>& x) {
std::string result;
auto v = reinterpret_cast<uintptr_t>(x.get());
detail::append_hex(result, reinterpret_cast<uint8_t*>(&v), sizeof(v));
detail::append_hex(result, reinterpret_cast<uintptr_t>(x.get()));
return result;
}
......
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2018 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#include "caf/detail/append_hex.hpp"
namespace caf::detail {
void append_hex(std::string& result, const uint8_t* xs, size_t n) {
if (n == 0) {
result += "00";
return;
}
auto tbl = "0123456789ABCDEF";
char buf[3] = {0, 0, 0};
for (size_t i = 0; i < n; ++i) {
auto c = xs[i];
buf[0] = tbl[c >> 4];
buf[1] = tbl[c & 0x0F];
result += buf;
}
}
} // namespace caf::detail
......@@ -20,7 +20,6 @@
#include <cstring>
#include "caf/detail/append_hex.hpp"
#include "caf/detail/network_order.hpp"
#include "caf/detail/parser/read_ipv6_address.hpp"
#include "caf/error.hpp"
......
......@@ -29,6 +29,7 @@
#include "caf/binary_serializer.hpp"
#include "caf/config.hpp"
#include "caf/deserializer.hpp"
#include "caf/detail/append_hex.hpp"
#include "caf/detail/get_mac_addresses.hpp"
#include "caf/detail/get_process_id.hpp"
#include "caf/detail/get_root_uuid.hpp"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment