Berry add bytes().fromhex()

This commit is contained in:
Stephan Hadinger 2022-05-24 21:52:37 +02:00
parent 6a5e194f4e
commit 208077c808
4 changed files with 1352 additions and 1308 deletions

View File

@ -487,6 +487,7 @@ extern const bcstring be_const_str_format;
extern const bcstring be_const_str_from_to;
extern const bcstring be_const_str_fromb64;
extern const bcstring be_const_str_frombytes;
extern const bcstring be_const_str_fromhex;
extern const bcstring be_const_str_fromptr;
extern const bcstring be_const_str_fromstring;
extern const bcstring be_const_str_full_state;

File diff suppressed because it is too large Load Diff

View File

@ -1,42 +1,43 @@
#include "be_constobj.h"
static be_define_const_map_slots(be_class_bytes_map) {
{ be_const_key(deinit, -1), be_const_func(m_deinit) },
{ be_const_key(get, 5), be_const_func(m_getu) },
{ be_const_key(fromb64, 18), be_const_func(m_fromb64) },
{ be_const_key(copy, -1), be_const_func(m_copy) },
{ be_const_key(_buffer, -1), be_const_func(m_buffer) },
{ be_const_key(getfloat, -1), be_const_func(m_getfloat) },
{ be_const_key(getbits, -1), be_const_closure(getbits_closure) },
{ be_const_key(tob64, -1), be_const_func(m_tob64) },
{ be_const_key(item, -1), be_const_func(m_item) },
{ be_const_key(_X21_X3D, -1), be_const_func(m_nequal) },
{ be_const_key(clear, 28), be_const_func(m_clear) },
{ be_const_key(size, 4), be_const_func(m_size) },
{ be_const_key(fromstring, -1), be_const_func(m_fromstring) },
{ be_const_key(_X2Elen, 21), be_const_var(0) },
{ be_const_key(seti, -1), be_const_func(m_set) },
{ be_const_key(_X2B, 9), be_const_func(m_merge) },
{ be_const_key(_X2Esize, 13), be_const_var(1) },
{ be_const_key(setfloat, 6), be_const_func(m_setfloat) },
{ be_const_key(_X2Ep, -1), be_const_var(2) },
{ be_const_key(set, -1), be_const_func(m_set) },
{ be_const_key(asstring, 0), be_const_func(m_asstring) },
{ be_const_key(add, 24), be_const_func(m_add) },
{ be_const_key(setitem, -1), be_const_func(m_setitem) },
{ be_const_key(_change_buffer, -1), be_const_func(m_change_buffer) },
{ be_const_key(_X3D_X3D, 15), be_const_func(m_equal) },
{ be_const_key(resize, 2), be_const_func(m_resize) },
{ be_const_key(_X2Esize, 9), be_const_var(0) },
{ be_const_key(resize, -1), be_const_func(m_resize) },
{ be_const_key(clear, -1), be_const_func(m_clear) },
{ be_const_key(_X21_X3D, 14), be_const_func(m_nequal) },
{ be_const_key(copy, 11), be_const_func(m_copy) },
{ be_const_key(tostring, -1), be_const_func(m_tostring) },
{ be_const_key(setbits, -1), be_const_closure(setbits_closure) },
{ be_const_key(item, -1), be_const_func(m_item) },
{ be_const_key(setbits, 29), be_const_closure(setbits_closure) },
{ be_const_key(deinit, 12), be_const_func(m_deinit) },
{ be_const_key(asstring, -1), be_const_func(m_asstring) },
{ be_const_key(_X2B, -1), be_const_func(m_merge) },
{ be_const_key(setitem, -1), be_const_func(m_setitem) },
{ be_const_key(fromstring, 31), be_const_func(m_fromstring) },
{ be_const_key(_X2E_X2E, -1), be_const_func(m_connect) },
{ be_const_key(init, -1), be_const_func(m_init) },
{ be_const_key(geti, -1), be_const_func(m_geti) },
{ be_const_key(set, -1), be_const_func(m_set) },
{ be_const_key(_X3D_X3D, -1), be_const_func(m_equal) },
{ be_const_key(getbits, 18), be_const_closure(getbits_closure) },
{ be_const_key(fromhex, -1), be_const_func(m_fromhex) },
{ be_const_key(getfloat, 27), be_const_func(m_getfloat) },
{ be_const_key(init, 16), be_const_func(m_init) },
{ be_const_key(add, -1), be_const_func(m_add) },
{ be_const_key(_change_buffer, -1), be_const_func(m_change_buffer) },
{ be_const_key(_X2Elen, -1), be_const_var(1) },
{ be_const_key(get, 25), be_const_func(m_getu) },
{ be_const_key(_buffer, 22), be_const_func(m_buffer) },
{ be_const_key(fromb64, -1), be_const_func(m_fromb64) },
{ be_const_key(geti, 17), be_const_func(m_geti) },
{ be_const_key(_X2Ep, -1), be_const_var(2) },
{ be_const_key(size, -1), be_const_func(m_size) },
{ be_const_key(setfloat, -1), be_const_func(m_setfloat) },
{ be_const_key(seti, -1), be_const_func(m_set) },
{ be_const_key(tob64, -1), be_const_func(m_tob64) },
};
static be_define_const_map(
be_class_bytes_map,
31
32
);
BE_EXPORT_VARIABLE be_define_const_class(

View File

@ -1184,6 +1184,45 @@ static int m_fromb64(bvm *vm)
be_return_nil(vm);
}
/*
* Converts hex to bytes()
*
* `bytes().fromhexx() -> bytes()`
*/
static int m_fromhex(bvm *vm)
{
int argc = be_top(vm);
if (argc >= 2 && be_isstring(vm, 2)) {
int32_t from = 0; // skip x chars
if (argc >= 3 && be_isint(vm, 3)) {
from = be_toint(vm, 3);
}
const char *s = be_tostring(vm, 2);
size_t s_len = strlen(s);
if (from < 0) { from = 0; }
if (from > s_len) { from = s_len; }
int32_t bin_len = (s_len - from) / 2;
buf_impl attr = m_read_attributes(vm, 1);
check_ptr(vm, &attr);
if (attr.fixed && attr.len != bin_len) {
be_raise(vm, BYTES_RESIZE_ERROR, BYTES_RESIZE_MESSAGE);
}
bytes_resize(vm, &attr, bin_len); /* resize if needed */
if (bin_len > attr.size) { /* avoid overflow */
be_raise(vm, "memory_error", "cannot allocate buffer");
}
attr.len = 0;
buf_add_hex(&attr, s + from, s_len - from);
be_pop(vm, 1); /* remove arg to leave instance */
m_write_attributes(vm, 1, &attr); /* update instance */
be_pop(vm, be_top(vm) - 1); /* leave instance on stack */
be_return(vm);
}
be_raise(vm, "type_error", "operand must be a string");
be_return_nil(vm);
}
/*
* Advanced API
@ -1538,6 +1577,7 @@ class be_class_bytes (scope: global, name: bytes) {
fromstring, func(m_fromstring)
tob64, func(m_tob64)
fromb64, func(m_fromb64)
fromhex, func(m_fromhex)
add, func(m_add)
get, func(m_getu)
geti, func(m_geti)