mirror of
https://github.com/arendst/Tasmota.git
synced 2025-07-23 18:56:38 +00:00
Berry bytes add appendb64 (#23008)
* Berry Leds_panel minor fix * Fix * Berry add 'bytes().appendb64()' * fix lib
This commit is contained in:
parent
64ec55db4e
commit
d805cef66e
@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file.
|
||||
- Support for Lithuanian language translations by zzdovydas (#22971)
|
||||
- `MqttTLS` field in `Status 6` to indicate if the MQTT connection is encrypted (#22995)
|
||||
- Support for WiZ Smart Remote using `#define USE_WIZMOTE` and command `SetOption164 1`
|
||||
- Berry add `bytes().appendb64()` (#22767)
|
||||
|
||||
### Breaking Changed
|
||||
|
||||
|
@ -1376,6 +1376,43 @@ static int m_appendhex(bvm *vm)
|
||||
be_return_nil(vm); /* return self */
|
||||
}
|
||||
|
||||
static int m_appendb64(bvm *vm)
|
||||
{
|
||||
int argc = be_top(vm);
|
||||
buf_impl attr = m_read_attributes(vm, 1);
|
||||
check_ptr_modifiable(vm, &attr);
|
||||
if (attr.fixed) { be_raise(vm, BYTES_RESIZE_ERROR, BYTES_RESIZE_MESSAGE); }
|
||||
if (argc >= 2 && be_isbytes(vm, 2)) {
|
||||
buf_impl attr2 = m_read_attributes(vm, 2);
|
||||
check_ptr(vm, &attr2);
|
||||
int32_t idx = 0; /* start from index 0 */
|
||||
int32_t len = attr2.len; /* entire len */
|
||||
if (argc >= 3 && be_isint(vm, 3)) { /* read optional idx and len */
|
||||
idx = be_toint(vm, 3);
|
||||
if (idx < 0) { idx = attr2.len + idx; } /* if negative, count from end */
|
||||
if (idx < 0) { idx = 0; } /* guardrails */
|
||||
if (idx > attr2.len) { idx = attr2.len; }
|
||||
if (argc >= 4 && be_isint(vm, 4)) {
|
||||
len = be_toint(vm, 4);
|
||||
if (len < 0) { len = 0; }
|
||||
}
|
||||
if (idx + len >= attr2.len) { len = attr2.len - idx; }
|
||||
}
|
||||
if (len > 0) { /* only if there is something to encode */
|
||||
bytes_resize(vm, &attr, attr.len + encode_base64_length(len)); /* resize */
|
||||
|
||||
size_t converted = encode_base64(attr2.bufptr + idx, len, (unsigned char*)(attr.bufptr + attr.len));
|
||||
attr.len += converted;
|
||||
|
||||
m_write_attributes(vm, 1, &attr); /* update instance */
|
||||
}
|
||||
be_pushvalue(vm, 1);
|
||||
be_return(vm); /* return self */
|
||||
}
|
||||
be_raise(vm, "type_error", "operand must be bytes");
|
||||
be_return_nil(vm); /* return self */
|
||||
}
|
||||
|
||||
static int bytes_equal(bvm *vm, bbool iseq)
|
||||
{
|
||||
bbool ret;
|
||||
@ -1862,6 +1899,8 @@ void be_load_byteslib(bvm *vm)
|
||||
{ "reverse", m_reverse },
|
||||
{ "copy", m_copy },
|
||||
{ "append", m_connect },
|
||||
{ "appendhex", m_appendhex },
|
||||
{ "appendb64", m_appendb64 },
|
||||
{ "+", m_merge },
|
||||
{ "..", m_connect },
|
||||
{ "==", m_equal },
|
||||
@ -1916,6 +1955,7 @@ class be_class_bytes (scope: global, name: bytes) {
|
||||
copy, func(m_copy)
|
||||
append, func(m_connect)
|
||||
appendhex, func(m_appendhex)
|
||||
appendb64, func(m_appendb64)
|
||||
+, func(m_merge)
|
||||
.., func(m_connect)
|
||||
==, func(m_equal)
|
||||
|
@ -330,3 +330,22 @@ assert(bytes("02"))
|
||||
a = bytes("01020304")
|
||||
assert(a.get(1, 3) == 0x040302)
|
||||
assert(a.get(1, -3) == 0x020304)
|
||||
|
||||
# append base64
|
||||
b = bytes("AABBCC")
|
||||
c = bytes("001122")
|
||||
assert(bytes().fromstring(bytes("001122").tob64()) == bytes('41424569'))
|
||||
assert(b.appendb64(c) == bytes("AABBCC41424569"))
|
||||
assert(b.appendb64(bytes()) == bytes("AABBCC41424569"))
|
||||
|
||||
b = bytes("AABBCC")
|
||||
assert(bytes().fromstring(bytes("1122").tob64()) == bytes('4553493D'))
|
||||
assert(b.appendb64(c, 1) == bytes("AABBCC4553493D"))
|
||||
|
||||
b = bytes("AABBCC")
|
||||
assert(bytes().fromstring(bytes("22").tob64()) == bytes('49673D3D'))
|
||||
assert(b.appendb64(c, 2) == bytes("AABBCC49673D3D"))
|
||||
|
||||
b = bytes("AABBCC")
|
||||
assert(bytes().fromstring(bytes("11").tob64()) == bytes('45513D3D'))
|
||||
assert(b.appendb64(c, 1, 1) == bytes("AABBCC45513D3D"))
|
||||
|
Loading…
x
Reference in New Issue
Block a user