Berry bytes add 3 (#23200)

* Berry bytes add with 3 bytes

* add changelog
This commit is contained in:
s-hadinger 2025-03-27 22:55:42 +01:00 committed by GitHub
parent a47e6f1496
commit b77b622fbe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 29 additions and 1 deletions

View File

@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file.
## [14.5.0.3]
### Added
- Extend command `GPIO` with different display options and allowing updating of module GPIO's in one go
- Berry `bytes.add()` now accepts 3-bytes values
### Breaking Changed

View File

@ -258,6 +258,26 @@ static size_t buf_add2_be(buf_impl* attr, const uint16_t data) // append 16 bits
return attr->len;
}
static size_t buf_add3_le(buf_impl* attr, const uint32_t data) // append 32 bits value
{
if (attr->len < attr->size - 2) { // do we have room for 4 bytes
attr->bufptr[attr->len++] = data;
attr->bufptr[attr->len++] = data >> 8;
attr->bufptr[attr->len++] = data >> 16;
}
return attr->len;
}
size_t buf_add3_be(buf_impl* attr, const uint32_t data) // append 32 bits value
{
if (attr->len < attr->size - 2) { // do we have room for 4 bytes
attr->bufptr[attr->len++] = data >> 16;
attr->bufptr[attr->len++] = data >> 8;
attr->bufptr[attr->len++] = data;
}
return attr->len;
}
static size_t buf_add4_le(buf_impl* attr, const uint32_t data) // append 32 bits value
{
if (attr->len < attr->size - 3) { // do we have room for 4 bytes
@ -838,10 +858,12 @@ static int m_add(bvm *vm)
case -1: /* fallback below */
case 1: buf_add1(&attr, v); break;
case 2: buf_add2_le(&attr, v); break;
case 3: buf_add3_le(&attr, v); break;
case 4: buf_add4_le(&attr, v); break;
case -2: buf_add2_be(&attr, v); break;
case -3: buf_add3_be(&attr, v); break;
case -4: buf_add4_be(&attr, v); break;
default: be_raise(vm, "type_error", "size must be -4, -2, -1, 0, 1, 2 or 4.");
default: be_raise(vm, "type_error", "size must be between -4 and 4.");
}
be_pop(vm, argc - 1);
m_write_attributes(vm, 1, &attr); /* update attributes */

View File

@ -53,6 +53,11 @@ b.add(0x12345678, -2)
assert(str(b) == "bytes('2278785678563412785678')")
b.add(0x12345678, -4)
assert(str(b) == "bytes('227878567856341278567812345678')")
b.add(0xAABBCC, 3)
assert(str(b) == "bytes('227878567856341278567812345678CCBBAA')")
b.add(0x998877, -3)
assert(str(b) == "bytes('227878567856341278567812345678CCBBAA998877')")
#- get -#
b=bytes("000102030405")