Commit 51b9a0d0 authored by Damien George's avatar Damien George
Browse files

py/objstr: Make string formatting 8-bit clean.

parent 1d350b8a
...@@ -851,7 +851,7 @@ mp_obj_t mp_obj_str_format(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwa ...@@ -851,7 +851,7 @@ mp_obj_t mp_obj_str_format(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwa
if (*str == '}') { if (*str == '}') {
str++; str++;
if (str < top && *str == '}') { if (str < top && *str == '}') {
vstr_add_char(&vstr, '}'); vstr_add_byte(&vstr, '}');
continue; continue;
} }
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
...@@ -862,13 +862,13 @@ mp_obj_t mp_obj_str_format(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwa ...@@ -862,13 +862,13 @@ mp_obj_t mp_obj_str_format(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwa
} }
} }
if (*str != '{') { if (*str != '{') {
vstr_add_char(&vstr, *str); vstr_add_byte(&vstr, *str);
continue; continue;
} }
str++; str++;
if (str < top && *str == '{') { if (str < top && *str == '{') {
vstr_add_char(&vstr, '{'); vstr_add_byte(&vstr, '{');
continue; continue;
} }
...@@ -881,7 +881,7 @@ mp_obj_t mp_obj_str_format(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwa ...@@ -881,7 +881,7 @@ mp_obj_t mp_obj_str_format(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwa
if (str < top && *str != '}' && *str != '!' && *str != ':') { if (str < top && *str != '}' && *str != '!' && *str != ':') {
field_name = vstr_new(); field_name = vstr_new();
while (str < top && *str != '}' && *str != '!' && *str != ':') { while (str < top && *str != '}' && *str != '!' && *str != ':') {
vstr_add_char(field_name, *str++); vstr_add_byte(field_name, *str++);
} }
} }
...@@ -911,7 +911,7 @@ mp_obj_t mp_obj_str_format(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwa ...@@ -911,7 +911,7 @@ mp_obj_t mp_obj_str_format(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwa
if (*str != '}') { if (*str != '}') {
format_spec = vstr_new(); format_spec = vstr_new();
while (str < top && *str != '}') { while (str < top && *str != '}') {
vstr_add_char(format_spec, *str++); vstr_add_byte(format_spec, *str++);
} }
} }
} }
...@@ -1290,14 +1290,14 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, mp_uint_t n_args, const mp_o ...@@ -1290,14 +1290,14 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, mp_uint_t n_args, const mp_o
for (const byte *top = str + len; str < top; str++) { for (const byte *top = str + len; str < top; str++) {
mp_obj_t arg = MP_OBJ_NULL; mp_obj_t arg = MP_OBJ_NULL;
if (*str != '%') { if (*str != '%') {
vstr_add_char(&vstr, *str); vstr_add_byte(&vstr, *str);
continue; continue;
} }
if (++str >= top) { if (++str >= top) {
break; break;
} }
if (*str == '%') { if (*str == '%') {
vstr_add_char(&vstr, '%'); vstr_add_byte(&vstr, '%');
continue; continue;
} }
......
# test handling of unicode chars in format strings
print('α'.format())
print('{α}'.format(α=1))
# test handling of unicode chars in string % formatting
print('α' % ())
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment