| 1 | #cxx . . $ `<file>/mlspp/lib/bytes/src/bytes.cpp`/include <bytes/bytes.h>cxx . . $ `<file>/mlspp/lib/bytes/include/bytes/bytes.h`/ |
| 2 | |
| 3 | #include <iomanip> |
| 4 | #include <iostream> |
| 5 | #include <sstream> |
| 6 | #include <stdexcept> |
| 7 | |
| 8 | namespace mlsppcxx . . $ mlspp/::bytes_nscxx . . $ mlspp/bytes_ns/ { |
| 9 | |
| 10 | bool |
| 11 | bytescxx . . $ mlspp/bytes_ns/bytes#::operatorcxx . . $ mlspp/bytes_ns/bytes#`operator==`(efd4f3c976ab92ce).==(const bytescxx . . $ mlspp/bytes_ns/bytes#& other) const |
| 12 | { |
| 13 | return *this ==cxx . . $ mlspp/bytes_ns/bytes#`operator==`(811ac1523642ab5e). other._datacxx . . $ mlspp/bytes_ns/bytes#_data.; |
| 14 | } |
| 15 | |
| 16 | bool |
| 17 | bytescxx . . $ mlspp/bytes_ns/bytes#::operatorcxx . . $ mlspp/bytes_ns/bytes#`operator!=`(efd4f3c976ab92ce).!=(const bytescxx . . $ mlspp/bytes_ns/bytes#& other) const |
| 18 | { |
| 19 | return !(*this ==cxx . . $ mlspp/bytes_ns/bytes#`operator==`(811ac1523642ab5e). other._datacxx . . $ mlspp/bytes_ns/bytes#_data.); |
| 20 | } |
| 21 | |
| 22 | bool |
| 23 | bytescxx . . $ mlspp/bytes_ns/bytes#::operatorcxx . . $ mlspp/bytes_ns/bytes#`operator==`(811ac1523642ab5e).==(const stdcxx . . $ std/::vectorcxx . . $ std/vector#<uint8_tcxx . . $ uint8_t#>& other) const |
| 24 | { |
| 25 | const size_t size = other.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).(); |
| 26 | if (_datacxx . . $ mlspp/bytes_ns/bytes#_data..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() != size) { |
| 27 | return false; |
| 28 | } |
| 29 | |
| 30 | unsigned char diff = 0; |
| 31 | for (size_t i = 0; i < size; ++i) { |
| 32 | // Not sure why the linter thinks `diff` is signed |
| 33 | // NOLINTNEXTLINE(hicpp-signed-bitwise) |
| 34 | diff |= (_datacxx . . $ mlspp/bytes_ns/bytes#_data..atcxx . . $ std/vector#at(b61e45a10b63f40a).(i) ^ other.atcxx . . $ std/vector#at(b61e45a10b63f40a).(i)); |
| 35 | } |
| 36 | return (diff == 0); |
| 37 | } |
| 38 | |
| 39 | bool |
| 40 | bytescxx . . $ mlspp/bytes_ns/bytes#::operatorcxx . . $ mlspp/bytes_ns/bytes#`operator!=`(811ac1523642ab5e).!=(const stdcxx . . $ std/::vectorcxx . . $ std/vector#<uint8_tcxx . . $ uint8_t#>& other) const |
| 41 | { |
| 42 | return !(*this ==cxx . . $ mlspp/bytes_ns/bytes#`operator==`(811ac1523642ab5e). other); |
| 43 | } |
| 44 | |
| 45 | bytescxx . . $ mlspp/bytes_ns/bytes#& |
| 46 | bytescxx . . $ mlspp/bytes_ns/bytes#::operatorcxx . . $ mlspp/bytes_ns/bytes#`operator+=`(89adb9997e4a49c).+=(const bytescxx . . $ mlspp/bytes_ns/bytes#& other) |
| 47 | { |
| 48 | // Not sure what the default argument is here |
| 49 | // NOLINTNEXTLINE(fuchsia-default-arguments) |
| 50 | _datacxx . . $ mlspp/bytes_ns/bytes#_data..insertcxx . . $ std/vector#insert(134ef7853f0b3ebe).(endcxx . . $ __gnu_cxx/__normal_iterator#__normal_iterator(459a7e7b9fbe7640).(), other.begincxx . . $ mlspp/bytes_ns/bytes#begin(2066806ed53596f0).(), other.endcxx . . $ mlspp/bytes_ns/bytes#end(2066806ed53596f0).()); |
| 51 | return *this; |
| 52 | } |
| 53 | |
| 54 | bytescxx . . $ mlspp/bytes_ns/bytes# |
| 55 | bytescxx . . $ mlspp/bytes_ns/bytes#::operatorcxx . . $ mlspp/bytes_ns/bytes#operator+(c5e1b85e16e94921).+(const bytescxx . . $ mlspp/bytes_ns/bytes#& rhs) const |
| 56 | { |
| 57 | bytescxx . . $ mlspp/bytes_ns/bytes# out = *cxx . . $ mlspp/bytes_ns/bytes#bytes(68ca607eebb5be3e).this; |
| 58 | out +=cxx . . $ mlspp/bytes_ns/bytes#`operator+=`(89adb9997e4a49c). rhs; |
| 59 | return outcxx . . $ mlspp/bytes_ns/bytes#bytes(628b76f52f2a67f).; |
| 60 | } |
| 61 | |
| 62 | bool |
| 63 | bytescxx . . $ mlspp/bytes_ns/bytes#::operatorcxx . . $ mlspp/bytes_ns/bytes#`operator<`(efd4f3c976ab92ce).<(const bytescxx . . $ mlspp/bytes_ns/bytes#& rhs) const |
| 64 | { |
| 65 | return _datacxx . . $ mlspp/bytes_ns/bytes#_data. <cxx . . $ std/`operator<`(a0a51a13f7e42509). rhs._datacxx . . $ mlspp/bytes_ns/bytes#_data.; |
| 66 | } |
| 67 | |
| 68 | bytescxx . . $ mlspp/bytes_ns/bytes# |
| 69 | bytescxx . . $ mlspp/bytes_ns/bytes#::operatorcxx . . $ mlspp/bytes_ns/bytes#`operator^`(c5e1b85e16e94921).^(const bytescxx . . $ mlspp/bytes_ns/bytes#& rhs) const |
| 70 | { |
| 71 | if (sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).() != rhs.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).()) { |
| 72 | throw stdcxx . . $ std/::invalid_argumentcxx . . $ std/invalid_argument#("XOR with unequal size"); |
| 73 | } |
| 74 | |
| 75 | bytescxx . . $ mlspp/bytes_ns/bytes# out = *cxx . . $ mlspp/bytes_ns/bytes#bytes(68ca607eebb5be3e).this; |
| 76 | for (size_t i = 0; i < sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).(); ++i) { |
| 77 | out.atcxx . . $ mlspp/bytes_ns/bytes#at(b19923e35e3d04d7).(i) ^= rhs.atcxx . . $ mlspp/bytes_ns/bytes#at(f7d94d2ba28cb144).(i); |
| 78 | } |
| 79 | return outcxx . . $ mlspp/bytes_ns/bytes#bytes(628b76f52f2a67f).; |
| 80 | } |
| 81 | |
| 82 | stdcxx . . $ std/::stringcxx . . $ std/string# |
| 83 | to_asciicxx . . $ mlspp/bytes_ns/to_ascii(b0627b6bf491e6e2).(const bytescxx . . $ mlspp/bytes_ns/bytes#& data) |
| 84 | { |
| 85 | return {cxx . . $ std/__cxx11/basic_string#basic_string(47be8e3519ed771f). data.begincxx . . $ mlspp/bytes_ns/bytes#begin(2066806ed53596f0).(), data.endcxx . . $ mlspp/bytes_ns/bytes#end(2066806ed53596f0).() }; |
| 86 | } |
| 87 | |
| 88 | bytescxx . . $ mlspp/bytes_ns/bytes# |
| 89 | from_asciicxx . . $ mlspp/bytes_ns/from_ascii(fc01d114001b7256).(const stdcxx . . $ std/::stringcxx . . $ std/string#& ascii) |
| 90 | { |
| 91 | return stdcxx . . $ mlspp/bytes_ns/bytes#bytes(dd8c33c7e39dbe02).::vectorcxx . . $ std/vector#<uint8_tcxx . . $ uint8_t#>(ascii.begincxx . . $ std/__cxx11/basic_string#begin(a479f6ffbc603b2b).(), ascii.endcxx . . $ std/__cxx11/basic_string#end(a479f6ffbc603b2b).()); |
| 92 | } |
| 93 | |
| 94 | stdcxx . . $ std/::stringcxx . . $ std/string# |
| 95 | to_hexcxx . . $ mlspp/bytes_ns/to_hex(b0627b6bf491e6e2).(const bytescxx . . $ mlspp/bytes_ns/bytes#& data) |
| 96 | { |
| 97 | stdcxx . . $ std/::stringstreamcxx . . $ std/stringstream# hexcxx . . $ std/__cxx11/basic_stringstream#basic_stringstream(322f8929844464d3).(stdcxx . . $ std/::ios_basecxx . . $ std/ios_base#::outcxx . . $ std/ios_base#out.); |
| 98 | hex.flagscxx . . $ std/ios_base#flags(79f645757f7c3f1c).(stdcxx . . $ std/::ioscxx . . $ std/ios#::hexcxx . . $ std/ios_base#hex.); |
| 99 | for (const auto& byte : data) { |
| 100 | hex <<cxx . . $ std/`operator<<`(fe9a269018418773). stdcxx . . $ std/::setwcxx . . $ std/setw(f06ce2e9220601fb).(2) <<cxx . . $ std/`operator<<`(47f1cf32aeb3f1a2). stdcxx . . $ std/::setfillcxx . . $ std/setfill(ade351e831a1374a).('0') <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). int(byte); |
| 101 | } |
| 102 | return hex.strcxx . . $ std/__cxx11/basic_stringstream#str(d33e1a6fd36255f7).(); |
| 103 | } |
| 104 | |
| 105 | bytescxx . . $ mlspp/bytes_ns/bytes# |
| 106 | from_hexcxx . . $ mlspp/bytes_ns/from_hex(fc01d114001b7256).(const stdcxx . . $ std/::stringcxx . . $ std/string#& hex) |
| 107 | { |
| 108 | if (hex.lengthcxx . . $ std/__cxx11/basic_string#length(6f8bd5c0ea8fb29a).() % 2 == 1) { |
| 109 | throw stdcxx . . $ std/::invalid_argumentcxx . . $ std/invalid_argument#("Odd-length hex string"); |
| 110 | } |
| 111 | |
| 112 | auto len = hex.lengthcxx . . $ std/__cxx11/basic_string#length(6f8bd5c0ea8fb29a).() / 2; |
| 113 | auto out = bytescxx . . $ mlspp/bytes_ns/bytes#(len); |
| 114 | for (size_t i = 0; i < len; i += 1) { |
| 115 | const stdcxx . . $ std/::stringcxx . . $ std/string# byte = hex.substrcxx . . $ std/__cxx11/basic_string#substr(149c655009c9bbd2).(2 * i, 2); |
| 116 | out.atcxx . . $ mlspp/bytes_ns/bytes#at(b19923e35e3d04d7).(i) = static_cast<uint8_tcxx . . $ uint8_t#>(strtol(byte.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(), nullptr, 16)); |
| 117 | } |
| 118 | |
| 119 | return outcxx . . $ mlspp/bytes_ns/bytes#bytes(628b76f52f2a67f).; |
| 120 | } |
| 121 | |
| 122 | stdcxx . . $ std/::ostreamcxx . . $ std/ostream#& |
| 123 | operatorcxx . . $ mlspp/bytes_ns/`operator<<`(ae8d2372d747597c).<<(stdcxx . . $ std/::ostreamcxx . . $ std/ostream#& out, const bytescxx . . $ mlspp/bytes_ns/bytes#& data) |
| 124 | { |
| 125 | // Adjust this threshold to make output more compact |
| 126 | const size_t threshold = 0xffff; |
| 127 | if (data.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).() < threshold) { |
| 128 | return out <<cxx . . $ std/`operator<<`(88df5bef833f7f83). to_hexcxx . . $ mlspp/bytes_ns/to_hex(b0627b6bf491e6e2).(data); |
| 129 | } |
| 130 | |
| 131 | return out <<cxx . . $ std/`operator<<`(88df5bef833f7f83). to_hexcxx . . $ mlspp/bytes_ns/to_hex(b0627b6bf491e6e2).(data.slicecxx . . $ mlspp/bytes_ns/bytes#slice(ef0b1008b63e00ff).(0, threshold)) <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "..."; |
| 132 | } |
| 133 | |
| 134 | bool |
| 135 | operatorcxx . . $ mlspp/bytes_ns/`operator==`(78f9e06a1dd233f0).==(const stdcxx . . $ std/::vectorcxx . . $ std/vector#<uint8_tcxx . . $ uint8_t#>& lhs, const bytes_nscxx . . $ mlspp/bytes_ns/::bytescxx . . $ mlspp/bytes_ns/bytes#& rhs) |
| 136 | { |
| 137 | return rhs ==cxx . . $ mlspp/bytes_ns/bytes#`operator==`(811ac1523642ab5e). lhs; |
| 138 | } |
| 139 | |
| 140 | bool |
| 141 | operatorcxx . . $ mlspp/bytes_ns/`operator!=`(78f9e06a1dd233f0).!=(const stdcxx . . $ std/::vectorcxx . . $ std/vector#<uint8_tcxx . . $ uint8_t#>& lhs, const bytes_nscxx . . $ mlspp/bytes_ns/::bytescxx . . $ mlspp/bytes_ns/bytes#& rhs) |
| 142 | { |
| 143 | return rhs !=cxx . . $ mlspp/bytes_ns/bytes#`operator!=`(811ac1523642ab5e). lhs; |
| 144 | } |
| 145 | |
| 146 | } // namespace mlspp::bytes_ns |