Skip to content

Commit aaa7198

Browse files
authored
Merge pull request #3161 from tannewt/bytearray_find
Add find variants to bytearray
2 parents 8e90c19 + 8ff2846 commit aaa7198

File tree

3 files changed

+78
-5
lines changed

3 files changed

+78
-5
lines changed

locale/circuitpython.pot

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
# SPDX-FileCopyrightText: 2014 MicroPython & CircuitPython contributors (https://github.com/adafruit/circuitpython/graphs/contributors)
1+
# SOME DESCRIPTIVE TITLE.
2+
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
3+
# This file is distributed under the same license as the PACKAGE package.
4+
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
25
#
3-
# SPDX-License-Identifier: MIT
4-
6+
#, fuzzy
57
msgid ""
68
msgstr ""
79
"Project-Id-Version: PACKAGE VERSION\n"
810
"Report-Msgid-Bugs-To: \n"
9-
"POT-Creation-Date: 2020-07-07 14:38-0500\n"
11+
"POT-Creation-Date: 2020-07-17 18:03-0700\n"
1012
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1113
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1214
"Language-Team: LANGUAGE <[email protected]>\n"
@@ -103,6 +105,10 @@ msgstr ""
103105
msgid "'%q' argument required"
104106
msgstr ""
105107

108+
#: py/objarray.c
109+
msgid "'%q' object is not bytes-like"
110+
msgstr ""
111+
106112
#: py/emitinlinethumb.c py/emitinlinextensa.c
107113
#, c-format
108114
msgid "'%s' expects a label"
@@ -3098,7 +3104,7 @@ msgstr ""
30983104
msgid "struct: no fields"
30993105
msgstr ""
31003106

3101-
#: py/objstr.c
3107+
#: py/objarray.c py/objstr.c
31023108
msgid "substring not found"
31033109
msgstr ""
31043110

ports/atmel-samd/boards/circuitbrains_basic_m0/mpconfigboard.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ EXTERNAL_FLASH_DEVICES = "W25Q32JV_IQ"
1212
LONGINT_IMPL = MPZ
1313

1414
CIRCUITPY_BITBANGIO = 0
15+
CIRCUITPY_COUNTIO = 0
1516
CIRCUITPY_FREQUENCYIO = 0
1617
CIRCUITPY_I2CPERIPHERAL = 0
1718
CIRCUITPY_VECTORIO = 0

py/objarray.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,66 @@ STATIC mp_obj_t array_extend(mp_obj_t self_in, mp_obj_t arg_in) {
400400
STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_extend_obj, array_extend);
401401
#endif
402402

403+
#if MICROPY_PY_BUILTINS_BYTEARRAY && MICROPY_CPYTHON_COMPAT
404+
STATIC mp_obj_t buffer_finder(size_t n_args, const mp_obj_t *args, int direction, bool is_index) {
405+
mp_check_self(MP_OBJ_IS_TYPE(args[0], &mp_type_bytearray));
406+
const mp_obj_type_t *self_type = mp_obj_get_type(args[0]);
407+
408+
mp_buffer_info_t haystack_bufinfo;
409+
mp_get_buffer_raise(args[0], &haystack_bufinfo, MP_BUFFER_READ);
410+
411+
mp_buffer_info_t needle_bufinfo;
412+
mp_get_buffer_raise(args[1], &needle_bufinfo, MP_BUFFER_READ);
413+
414+
if (mp_binary_get_size('@', needle_bufinfo.typecode, NULL) != 1) {
415+
mp_raise_TypeError(translate("a bytes-like object is required"));
416+
}
417+
418+
const byte *start = haystack_bufinfo.buf;
419+
const byte *end = ((const byte*)haystack_bufinfo.buf) + haystack_bufinfo.len;
420+
if (n_args >= 3 && args[2] != mp_const_none) {
421+
start += mp_get_index(self_type, haystack_bufinfo.len, args[2], true);
422+
}
423+
if (n_args >= 4 && args[3] != mp_const_none) {
424+
end = ((const byte*)haystack_bufinfo.buf) + mp_get_index(self_type, haystack_bufinfo.len, args[3], true);
425+
}
426+
427+
const byte *p = NULL;
428+
if (end >= start) {
429+
p = find_subbytes(start, end - start, needle_bufinfo.buf, needle_bufinfo.len, direction);
430+
}
431+
432+
if (p == NULL) {
433+
if (is_index) {
434+
mp_raise_ValueError(translate("substring not found"));
435+
} else {
436+
return MP_OBJ_NEW_SMALL_INT(-1);
437+
}
438+
}
439+
return MP_OBJ_NEW_SMALL_INT(p - (const byte*) haystack_bufinfo.buf);
440+
}
441+
442+
STATIC mp_obj_t buffer_find(size_t n_args, const mp_obj_t *args) {
443+
return buffer_finder(n_args, args, 1, false);
444+
}
445+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(buffer_find_obj, 2, 4, buffer_find);
446+
447+
STATIC mp_obj_t buffer_rfind(size_t n_args, const mp_obj_t *args) {
448+
return buffer_finder(n_args, args, -1, false);
449+
}
450+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(buffer_rfind_obj, 2, 4, buffer_rfind);
451+
452+
STATIC mp_obj_t buffer_index(size_t n_args, const mp_obj_t *args) {
453+
return buffer_finder(n_args, args, 1, true);
454+
}
455+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(buffer_index_obj, 2, 4, buffer_index);
456+
457+
STATIC mp_obj_t buffer_rindex(size_t n_args, const mp_obj_t *args) {
458+
return buffer_finder(n_args, args, -1, true);
459+
}
460+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(buffer_rindex_obj, 2, 4, buffer_rindex);
461+
#endif
462+
403463
STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value) {
404464
if (value == MP_OBJ_NULL) {
405465
// delete item
@@ -580,7 +640,13 @@ STATIC MP_DEFINE_CONST_DICT(array_locals_dict, array_locals_dict_table);
580640
STATIC const mp_rom_map_elem_t bytearray_locals_dict_table[] = {
581641
{ MP_ROM_QSTR(MP_QSTR_append), MP_ROM_PTR(&array_append_obj) },
582642
{ MP_ROM_QSTR(MP_QSTR_extend), MP_ROM_PTR(&array_extend_obj) },
643+
583644
#if MICROPY_CPYTHON_COMPAT
645+
{ MP_ROM_QSTR(MP_QSTR_find), MP_ROM_PTR(&buffer_find_obj) },
646+
{ MP_ROM_QSTR(MP_QSTR_rfind), MP_ROM_PTR(&buffer_rfind_obj) },
647+
{ MP_ROM_QSTR(MP_QSTR_index), MP_ROM_PTR(&buffer_index_obj) },
648+
{ MP_ROM_QSTR(MP_QSTR_rindex), MP_ROM_PTR(&buffer_rindex_obj) },
649+
584650
{ MP_ROM_QSTR(MP_QSTR_decode), MP_ROM_PTR(&array_decode_obj) },
585651
#endif
586652
};

0 commit comments

Comments
 (0)