Commit ce577346 authored by Dominik Charousset's avatar Dominik Charousset Committed by Dominik Charousset

Add to_lowercase convenience function for atoms

parent be020998
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include <type_traits> #include <type_traits>
#include "caf/detail/atom_val.hpp" #include "caf/detail/atom_val.hpp"
#include "caf/fwd.hpp"
namespace caf { namespace caf {
...@@ -36,6 +37,9 @@ enum class atom_value : uint64_t { ...@@ -36,6 +37,9 @@ enum class atom_value : uint64_t {
/// @relates atom_value /// @relates atom_value
std::string to_string(const atom_value& what); std::string to_string(const atom_value& what);
/// @relates atom_value
atom_value to_lowercase(atom_value x);
atom_value atom_from_string(const std::string& x); atom_value atom_from_string(const std::string& x);
/// Creates an atom from given string literal. /// Creates an atom from given string literal.
......
...@@ -18,34 +18,57 @@ ...@@ -18,34 +18,57 @@
#include "caf/atom.hpp" #include "caf/atom.hpp"
#include <array>
#include <cstring> #include <cstring>
#include "caf/string_view.hpp"
namespace caf { namespace caf {
atom_value atom_from_string(const std::string& x) { namespace {
if (x.size() > 10)
return atom("");
char buf[11];
memcpy(buf, x.c_str(), x.size());
buf[x.size()] = '\0';
return atom(buf);
}
std::string to_string(const atom_value& what) { /// A buffer for decoding atom values.
using atom_value_buf = std::array<char, 11>;
size_t decode(atom_value_buf& buf, atom_value what) {
size_t pos = 0;
auto x = static_cast<uint64_t>(what); auto x = static_cast<uint64_t>(what);
std::string result; // Don't read characters before we found the leading 0xF.
result.reserve(11);
// don't read characters before we found the leading 0xF
// first four bits set?
bool read_chars = ((x & 0xF000000000000000) >> 60) == 0xF; bool read_chars = ((x & 0xF000000000000000) >> 60) == 0xF;
uint64_t mask = 0x0FC0000000000000; uint64_t mask = 0x0FC0000000000000;
for (int bitshift = 54; bitshift >= 0; bitshift -= 6, mask >>= 6) { for (int bitshift = 54; bitshift >= 0; bitshift -= 6, mask >>= 6) {
if (read_chars) if (read_chars)
result += detail::decoding_table[(x & mask) >> bitshift]; buf[pos++] = detail::decoding_table[(x & mask) >> bitshift];
else if (((x & mask) >> bitshift) == 0xF) else if (((x & mask) >> bitshift) == 0xF)
read_chars = true; read_chars = true;
} }
return result; buf[pos] = '\0';
return pos;
}
} // namespace <anonymous>
atom_value to_lowercase(atom_value x) {
atom_value_buf buf;
decode(buf, x);
for (auto ch = buf.data(); *ch != '\0'; ++ch)
*ch = static_cast<char>(tolower(*ch));
return static_cast<atom_value>(detail::atom_val(buf.data()));
}
atom_value atom_from_string(const std::string& x) {
if (x.size() > 10)
return atom("");
char buf[11];
memcpy(buf, x.c_str(), x.size());
buf[x.size()] = '\0';
return atom(buf);
}
std::string to_string(const atom_value& x) {
atom_value_buf str;
auto len = decode(str, x);
return std::string(str.begin(), str.begin() + len);
} }
} // namespace caf } // namespace caf
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