summaryrefslogtreecommitdiff
path: root/py/objstr.c
diff options
context:
space:
mode:
authorBenjamin Shockley <benjaminshockley@hotmail.com>2020-08-20 13:48:15 -0500
committerGitHub <noreply@github.com>2020-08-20 13:48:15 -0500
commit0084f0af24e4e219bc040c52ee723959423de6e2 (patch)
tree1aebdb362f08bf6417caa87836e3e4e739afc964 /py/objstr.c
parent9aebe2f1efc99871fcf283c304e3a8700050d736 (diff)
parent4d7b9cde33934a44c4c905a49d2f831339fc8bd2 (diff)
Merge pull request #2 from adafruit/master
Master
Diffstat (limited to 'py/objstr.c')
-rw-r--r--py/objstr.c64
1 files changed, 48 insertions, 16 deletions
diff --git a/py/objstr.c b/py/objstr.c
index 621f1b837..a60f507e9 100644
--- a/py/objstr.c
+++ b/py/objstr.c
@@ -31,6 +31,7 @@
#include "py/unicode.h"
#include "py/objstr.h"
#include "py/objlist.h"
+#include "py/objtype.h"
#include "py/runtime.h"
#include "py/stackctrl.h"
@@ -41,6 +42,12 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, size_t n_args, const mp_obj_
STATIC mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str, mp_obj_iter_buf_t *iter_buf);
STATIC NORETURN void bad_implicit_conversion(mp_obj_t self_in);
+const char nibble_to_hex_upper[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
+ 'A', 'B', 'C', 'D', 'E', 'F'};
+
+const char nibble_to_hex_lower[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
+ 'a', 'b', 'c', 'd', 'e', 'f'};
+
/******************************************************************************/
/* str */
@@ -132,14 +139,14 @@ STATIC void str_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t
}
}
-mp_obj_t mp_obj_str_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
+mp_obj_t mp_obj_str_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
#if MICROPY_CPYTHON_COMPAT
- if (n_kw != 0) {
+ if (kw_args != NULL && kw_args->used != 0) {
mp_arg_error_unimpl_kw();
}
#endif
- mp_arg_check_num(n_args, n_kw, 0, 3, false);
+ mp_arg_check_num(n_args, kw_args, 0, 3, false);
switch (n_args) {
case 0:
@@ -190,15 +197,15 @@ mp_obj_t mp_obj_str_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_
}
}
-STATIC mp_obj_t bytes_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
+STATIC mp_obj_t bytes_make_new(const mp_obj_type_t *type_in, size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
(void)type_in;
#if MICROPY_CPYTHON_COMPAT
- if (n_kw != 0) {
+ if (kw_args != NULL && kw_args->used != 0) {
mp_arg_error_unimpl_kw();
}
#else
- (void)n_kw;
+ (void)kw_args;
#endif
if (n_args == 0) {
@@ -220,10 +227,6 @@ STATIC mp_obj_t bytes_make_new(const mp_obj_type_t *type_in, size_t n_args, size
return MP_OBJ_FROM_PTR(o);
}
- if (n_args > 1) {
- goto wrong_args;
- }
-
if (MP_OBJ_IS_SMALL_INT(args[0])) {
mp_int_t len = MP_OBJ_SMALL_INT_VALUE(args[0]);
if (len < 0) {
@@ -235,6 +238,17 @@ STATIC mp_obj_t bytes_make_new(const mp_obj_type_t *type_in, size_t n_args, size
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}
+ if (n_args > 1) {
+ goto wrong_args;
+ }
+
+ // check if __bytes__ exists, and if so delegate to it
+ mp_obj_t dest[2];
+ mp_load_method_maybe(args[0], MP_QSTR___bytes__, dest);
+ if (dest[0] != MP_OBJ_NULL) {
+ return mp_call_method_n_kw(0, 0, dest);
+ }
+
// check if argument has the buffer protocol
mp_buffer_info_t bufinfo;
if (mp_get_buffer(args[0], &bufinfo, MP_BUFFER_READ)) {
@@ -332,8 +346,9 @@ mp_obj_t mp_obj_str_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_i
return mp_const_empty_bytes;
}
}
+ size_t new_len = mp_seq_multiply_len(lhs_len, n);
vstr_t vstr;
- vstr_init_len(&vstr, lhs_len * n);
+ vstr_init_len(&vstr, new_len);
mp_seq_multiply(lhs_data, sizeof(*lhs_data), lhs_len, n, vstr.buf);
return mp_obj_new_str_from_vstr(lhs_type, &vstr);
}
@@ -407,6 +422,15 @@ mp_obj_t mp_obj_str_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_i
#if !MICROPY_PY_BUILTINS_STR_UNICODE
// objstrunicode defines own version
+size_t str_offset_to_index(const mp_obj_type_t *type, const byte *self_data, size_t self_len,
+ size_t offset) {
+ if (offset > self_len) {
+ mp_raise_ValueError(translate("offset out of bounds"));
+ }
+
+ return offset;
+}
+
const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, size_t self_len,
mp_obj_t index, bool is_slice) {
size_t index_val = mp_get_index(type, self_len, index, is_slice);
@@ -455,7 +479,7 @@ STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
if (!MP_OBJ_IS_TYPE(arg, &mp_type_list) && !MP_OBJ_IS_TYPE(arg, &mp_type_tuple)) {
// arg is not a list nor a tuple, try to convert it to a list
// TODO: Try to optimize?
- arg = mp_type_list.make_new(&mp_type_list, 1, 0, &arg);
+ arg = mp_type_list.make_new(&mp_type_list, 1, &arg, NULL);
}
mp_obj_get_array(arg, &seq_len, &seq_items);
@@ -1328,7 +1352,7 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
terse_str_format_value_error();
} else {
mp_raise_ValueError_varg(
- translate("unknown format code '%c' for object of type 'float'"),
+ translate("unknown format code '%c' for object of type '%s'"),
type, mp_obj_get_type_str(arg));
}
}
@@ -1364,7 +1388,7 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
terse_str_format_value_error();
} else {
mp_raise_ValueError_varg(
- translate("unknown format code '%c' for object of type 'str'"),
+ translate("unknown format code '%c' for object of type '%s'"),
type, mp_obj_get_type_str(arg));
}
}
@@ -1875,7 +1899,7 @@ STATIC mp_obj_t bytes_decode(size_t n_args, const mp_obj_t *args) {
args = new_args;
n_args++;
}
- return mp_obj_str_make_new(&mp_type_str, n_args, 0, args);
+ return mp_obj_str_make_new(&mp_type_str, n_args, args, NULL);
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bytes_decode_obj, 1, 3, bytes_decode);
@@ -1888,7 +1912,7 @@ STATIC mp_obj_t str_encode(size_t n_args, const mp_obj_t *args) {
args = new_args;
n_args++;
}
- return bytes_make_new(NULL, n_args, 0, args);
+ return bytes_make_new(NULL, n_args, args, NULL);
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_encode_obj, 1, 3, str_encode);
#endif
@@ -2078,6 +2102,14 @@ mp_obj_t mp_obj_new_bytes(const byte* data, size_t len) {
return mp_obj_new_str_copy(&mp_type_bytes, data, len);
}
+mp_obj_t mp_obj_new_bytes_of_zeros(size_t len) {
+ vstr_t vstr;
+ vstr_init_len(&vstr, len);
+ memset(vstr.buf, 0, len);
+ return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
+}
+
+
bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2) {
if (MP_OBJ_IS_QSTR(s1) && MP_OBJ_IS_QSTR(s2)) {
return s1 == s2;