Skip to content

Commit 1b5abfc

Browse files
committed
py/objstr: Implement str.center().
Disabled by default, enabled in unix port. Need for this method easily pops up when working with text UI/reporting, and coding workalike manually again and again counter-productive.
1 parent 2c573f0 commit 1b5abfc

5 files changed

Lines changed: 29 additions & 0 deletions

File tree

py/mpconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,11 @@ typedef double mp_float_t;
589589
#define MICROPY_PY_BUILTINS_STR_UNICODE (0)
590590
#endif
591591

592+
// Whether str.center() method provided
593+
#ifndef MICROPY_PY_BUILTINS_STR_CENTER
594+
#define MICROPY_PY_BUILTINS_STR_CENTER (0)
595+
#endif
596+
592597
// Whether str.splitlines() method provided
593598
#ifndef MICROPY_PY_BUILTINS_STR_SPLITLINES
594599
#define MICROPY_PY_BUILTINS_STR_SPLITLINES (0)

py/objstr.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,23 @@ STATIC mp_obj_t str_rstrip(size_t n_args, const mp_obj_t *args) {
817817
return str_uni_strip(RSTRIP, n_args, args);
818818
}
819819

820+
#if MICROPY_PY_BUILTINS_STR_CENTER
821+
STATIC mp_obj_t str_center(mp_obj_t str_in, mp_obj_t width_in) {
822+
GET_STR_DATA_LEN(str_in, str, str_len);
823+
int width = mp_obj_get_int(width_in);
824+
if (str_len >= width) {
825+
return str_in;
826+
}
827+
828+
vstr_t vstr;
829+
vstr_init_len(&vstr, width);
830+
memset(vstr.buf, ' ', width);
831+
int left = (width - str_len) / 2;
832+
memcpy(vstr.buf + left, str, str_len);
833+
return mp_obj_new_str_from_vstr(mp_obj_get_type(str_in), &vstr);
834+
}
835+
#endif
836+
820837
// Takes an int arg, but only parses unsigned numbers, and only changes
821838
// *num if at least one digit was parsed.
822839
STATIC const char *str_to_int(const char *str, const char *top, int *num) {
@@ -1846,6 +1863,9 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_split_obj, 1, 3, mp_obj_str_split);
18461863
#if MICROPY_PY_BUILTINS_STR_SPLITLINES
18471864
MP_DEFINE_CONST_FUN_OBJ_KW(str_splitlines_obj, 1, str_splitlines);
18481865
#endif
1866+
#if MICROPY_PY_BUILTINS_STR_CENTER
1867+
MP_DEFINE_CONST_FUN_OBJ_2(str_center_obj, str_center);
1868+
#endif
18491869
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_rsplit_obj, 1, 3, str_rsplit);
18501870
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_startswith_obj, 2, 3, str_startswith);
18511871
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_endswith_obj, 2, 3, str_endswith);
@@ -1897,6 +1917,7 @@ STATIC const mp_rom_map_elem_t str8_locals_dict_table[] = {
18971917
{ MP_ROM_QSTR(MP_QSTR_count), MP_ROM_PTR(&str_count_obj) },
18981918
{ MP_ROM_QSTR(MP_QSTR_partition), MP_ROM_PTR(&str_partition_obj) },
18991919
{ MP_ROM_QSTR(MP_QSTR_rpartition), MP_ROM_PTR(&str_rpartition_obj) },
1920+
{ MP_ROM_QSTR(MP_QSTR_center), MP_ROM_PTR(&str_center_obj) },
19001921
{ MP_ROM_QSTR(MP_QSTR_lower), MP_ROM_PTR(&str_lower_obj) },
19011922
{ MP_ROM_QSTR(MP_QSTR_upper), MP_ROM_PTR(&str_upper_obj) },
19021923
{ MP_ROM_QSTR(MP_QSTR_isspace), MP_ROM_PTR(&str_isspace_obj) },

py/objstr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ MP_DECLARE_CONST_FUN_OBJ(str_replace_obj);
9292
MP_DECLARE_CONST_FUN_OBJ(str_count_obj);
9393
MP_DECLARE_CONST_FUN_OBJ(str_partition_obj);
9494
MP_DECLARE_CONST_FUN_OBJ(str_rpartition_obj);
95+
MP_DECLARE_CONST_FUN_OBJ(str_center_obj);
9596
MP_DECLARE_CONST_FUN_OBJ(str_lower_obj);
9697
MP_DECLARE_CONST_FUN_OBJ(str_upper_obj);
9798
MP_DECLARE_CONST_FUN_OBJ(str_isspace_obj);

py/objstrunicode.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ STATIC const mp_rom_map_elem_t struni_locals_dict_table[] = {
238238
{ MP_ROM_QSTR(MP_QSTR_count), MP_ROM_PTR(&str_count_obj) },
239239
{ MP_ROM_QSTR(MP_QSTR_partition), MP_ROM_PTR(&str_partition_obj) },
240240
{ MP_ROM_QSTR(MP_QSTR_rpartition), MP_ROM_PTR(&str_rpartition_obj) },
241+
{ MP_ROM_QSTR(MP_QSTR_center), MP_ROM_PTR(&str_center_obj) },
241242
{ MP_ROM_QSTR(MP_QSTR_lower), MP_ROM_PTR(&str_lower_obj) },
242243
{ MP_ROM_QSTR(MP_QSTR_upper), MP_ROM_PTR(&str_upper_obj) },
243244
{ MP_ROM_QSTR(MP_QSTR_isspace), MP_ROM_PTR(&str_isspace_obj) },

unix/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
#define MICROPY_PY_FUNCTION_ATTRS (1)
7272
#define MICROPY_PY_DESCRIPTORS (1)
7373
#define MICROPY_PY_BUILTINS_STR_UNICODE (1)
74+
#define MICROPY_PY_BUILTINS_STR_CENTER (1)
7475
#define MICROPY_PY_BUILTINS_STR_SPLITLINES (1)
7576
#define MICROPY_PY_BUILTINS_MEMORYVIEW (1)
7677
#define MICROPY_PY_BUILTINS_FROZENSET (1)

0 commit comments

Comments
 (0)