-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitwise.cpp
More file actions
79 lines (72 loc) · 3.17 KB
/
Copy pathbitwise.cpp
File metadata and controls
79 lines (72 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <covscript/cni.hpp>
#include <covscript/dll.hpp>
#include <cstdint>
#include <bitset>
using bitset_t = std::bitset<std::numeric_limits<std::uint64_t>::digits>;
CNI_ROOT_NAMESPACE
{
bitset_t from_string(const std::string &data)
{
if (data.length() < 3 || (data[0] != '0' || (data[1] != 'x' && data[1] != 'X')))
throw cs::lang_error("Wrong literal: expected '0x' prefix.");
if (data.length() > 18)
throw cs::lang_error("Wrong literal: hex digits > 16.");
std::uint64_t hex = 0;
auto current = data.c_str() + 2;
auto end = data.c_str() + data.length();
while (current < end && ((*current >= '0' && *current <= '9') || (*current >= 'a' && *current <= 'f') ||
(*current >= 'A' && *current <= 'F'))) {
hex = hex * 16 + (*current & 15U) + (*current >= 'A' ? 9 : 0);
++current;
}
if (current != end)
throw cs::lang_error("Wrong literal.");
return bitset_t(hex);
}
CNI_CONST_V(hex_literal, from_string)
CNI_TYPE_EXT(bitset, bitset_t, bitset_t())
{
CNI_CONST_V(test, &bitset_t::test)
CNI_CONST_V(all, &bitset_t::all)
CNI_CONST_V(any, &bitset_t::any)
CNI_CONST_V(none, &bitset_t::none)
CNI_CONST_V(count, &bitset_t::count)
CNI_CONST_V(set_all, [](bitset_t &val) { return val.set(); })
CNI_CONST_V(set, [](bitset_t &val, std::size_t pos) { return val.set(pos); })
CNI_CONST_V(reset_all, [](bitset_t &val) { return val.reset(); })
CNI_CONST_V(reset, [](bitset_t &val, std::size_t pos) { return val.reset(pos); })
CNI_CONST_V(flip_all, [](bitset_t &val) { return val.flip(); })
CNI_CONST_V(flip, [](bitset_t &val, std::size_t pos) { return val.flip(pos); })
CNI_CONST_V(logic_and, [](const bitset_t &lhs, const bitset_t &rhs) { return lhs & rhs; })
CNI_CONST_V(logic_or, [](const bitset_t &lhs, const bitset_t &rhs) { return lhs | rhs; })
CNI_CONST_V(logic_xor, [](const bitset_t &lhs, const bitset_t &rhs) { return lhs ^ rhs; })
CNI_CONST_V(logic_not, [](const bitset_t &lhs) { return ~lhs; })
CNI_CONST_V(shift_left, [](const bitset_t &val, std::size_t pos) { return val << pos; })
CNI_CONST_V(shift_right, [](const bitset_t &val, std::size_t pos) { return val >> pos; })
CNI_CONST_V(to_hash, [](const bitset_t &val) { return cs::var(val.to_ullong()); })
CNI_CONST_V(to_number, [](const bitset_t &val) -> cs::number { return val.to_ulong(); })
CNI_CONST_V(to_string, [](const bitset_t &val) { return val.to_string(); })
CNI_CONST_V(from_number, [](cs::number val) { return bitset_t(static_cast<std::uint64_t>(val)); })
CNI_CONST(from_string)
CNI_CONST_V(to_hex, [](const bitset_t &val) {
std::uint64_t n = val.to_ullong();
if (n == 0)
return cs::string("0x0");
char buf[20];
std::snprintf(buf, sizeof(buf), "0x%llX", static_cast<unsigned long long>(n));
return cs::string(buf);
})
CNI_CONST_V(set_value, [](bitset_t &val, std::size_t pos, bool value) { return val.set(pos, value); })
CNI_CONST_V(shift_left_safe, [](const bitset_t &val, std::size_t pos) {
if (pos >= 64)
return bitset_t(0);
return val << pos;
})
CNI_CONST_V(shift_right_safe, [](const bitset_t &val, std::size_t pos) {
if (pos >= 64)
return bitset_t(0);
return val >> pos;
})
}
}
CNI_ENABLE_TYPE_EXT_V(bitset, bitset_t, cs::bitset)