mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
add bytes.__add__
This commit is contained in:
parent
142e4fd45f
commit
d81a1c5415
@ -1073,6 +1073,15 @@ void init_builtins(VM* _vm) {
|
|||||||
return VAR(self[i]);
|
return VAR(self[i]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
_vm->bind__add__(VM::tp_bytes, [](VM* vm, PyObject* _0, PyObject* _1) {
|
||||||
|
const Bytes& a = _CAST(Bytes&, _0);
|
||||||
|
const Bytes& b = CAST(Bytes&, _1);
|
||||||
|
unsigned char *buffer = new unsigned char[a.size() + b.size()];
|
||||||
|
memcpy(buffer, a.data(), a.size());
|
||||||
|
memcpy(buffer + a.size(), b.data(), b.size());
|
||||||
|
return VAR(Bytes(buffer, a.size() + b.size()));
|
||||||
|
});
|
||||||
|
|
||||||
_vm->bind__hash__(VM::tp_bytes, [](VM* vm, PyObject* _0) {
|
_vm->bind__hash__(VM::tp_bytes, [](VM* vm, PyObject* _0) {
|
||||||
const Bytes& self = _CAST(Bytes&, _0);
|
const Bytes& self = _CAST(Bytes&, _0);
|
||||||
std::string_view view((char*)self.data(), self.size());
|
std::string_view view((char*)self.data(), self.size());
|
||||||
|
@ -1,6 +1,12 @@
|
|||||||
a = '12345'
|
a = '12345'
|
||||||
assert a.encode() == b'12345'
|
assert a.encode() == b'12345'
|
||||||
|
|
||||||
|
# test add
|
||||||
|
assert b'123' + b'456' == b'123456'
|
||||||
|
assert b'' + b'123' == b'123'
|
||||||
|
assert b'123' + b'' == b'123'
|
||||||
|
assert b'' + b'' == b''
|
||||||
|
|
||||||
assert b'\xff\xee' != b'1234'
|
assert b'\xff\xee' != b'1234'
|
||||||
assert b'\xff\xee' == b'\xff\xee'
|
assert b'\xff\xee' == b'\xff\xee'
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user