Skip to content

Commit 698ec21

Browse files
committed
Make mp_obj_str_get_data return char* instead of byte*.
Can't decide which is better for string type, char or byte pointer. Changing to char removes a few casts. Really need to do proper unicode.
1 parent 2317708 commit 698ec21

12 files changed

Lines changed: 23 additions & 21 deletions

File tree

py/builtin.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,11 @@ MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_next_obj, mp_builtin_next);
288288

289289
static mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
290290
uint len;
291-
const byte *str = mp_obj_str_get_data(o_in, &len);
291+
const char *str = mp_obj_str_get_data(o_in, &len);
292292
if (len == 1) {
293-
return mp_obj_new_int(str[0]);
293+
// don't sign extend when converting to ord
294+
// TODO unicode
295+
return mp_obj_new_int(((const byte*)str)[0]);
294296
} else {
295297
nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "ord() expected a character, but string of length %d found", len));
296298
}

py/builtinevex.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121

2222
static mp_obj_t parse_compile_execute(mp_obj_t o_in, mp_parse_input_kind_t parse_input_kind) {
2323
uint str_len;
24-
const byte *str = mp_obj_str_get_data(o_in, &str_len);
24+
const char *str = mp_obj_str_get_data(o_in, &str_len);
2525

2626
// create the lexer
27-
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_string_gt_, (const char*)str, str_len, 0);
27+
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_string_gt_, str, str_len, 0);
2828
qstr source_name = mp_lexer_source_name(lex);
2929

3030
// parse the string

py/builtinimport.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ mp_import_stat_t find_file(const char *file_str, uint file_len, vstr_t *dest) {
5454
for (int i = 0; i < path_num; i++) {
5555
vstr_reset(dest);
5656
uint p_len;
57-
const byte *p = mp_obj_str_get_data(path_items[i], &p_len);
57+
const char *p = mp_obj_str_get_data(path_items[i], &p_len);
5858
if (p_len > 0) {
59-
vstr_add_strn(dest, (const char*)p, p_len);
59+
vstr_add_strn(dest, p, p_len);
6060
vstr_add_char(dest, PATH_SEP_CHAR);
6161
}
6262
vstr_add_strn(dest, file_str, file_len);

py/obj.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ uint mp_obj_str_get_hash(mp_obj_t self_in);
290290
uint mp_obj_str_get_len(mp_obj_t self_in);
291291
qstr mp_obj_str_get_qstr(mp_obj_t self_in); // use this if you will anyway convert the string to a qstr
292292
const char *mp_obj_str_get_str(mp_obj_t self_in); // use this only if you need the string to be null terminated
293-
const byte *mp_obj_str_get_data(mp_obj_t self_in, uint *len);
293+
const char *mp_obj_str_get_data(mp_obj_t self_in, uint *len);
294294
void mp_str_print_quoted(void (*print)(void *env, const char *fmt, ...), void *env, const byte *str_data, uint str_len);
295295

296296
// bytes

py/objarray.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ static mp_obj_t array_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const m
174174
}
175175
// TODO check args
176176
uint l;
177-
const byte *typecode = mp_obj_str_get_data(args[0], &l);
177+
const char *typecode = mp_obj_str_get_data(args[0], &l);
178178
if (n_args == 1) {
179179
return array_new(*typecode, 0);
180180
}

py/objint.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ static mp_obj_t int_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_
2323
if (MP_OBJ_IS_STR(args[0])) {
2424
// a string, parse it
2525
uint l;
26-
const byte *s = mp_obj_str_get_data(args[0], &l);
27-
return MP_OBJ_NEW_SMALL_INT(strtonum((const char*)s, 0));
26+
const char *s = mp_obj_str_get_data(args[0], &l);
27+
return MP_OBJ_NEW_SMALL_INT(strtonum(s, 0));
2828
} else {
2929
return MP_OBJ_NEW_SMALL_INT(mp_obj_get_int(args[0]));
3030
}
@@ -34,8 +34,8 @@ static mp_obj_t int_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_
3434
// should be a string, parse it
3535
// TODO proper error checking of argument types
3636
uint l;
37-
const byte *s = mp_obj_str_get_data(args[0], &l);
38-
return MP_OBJ_NEW_SMALL_INT(strtonum((const char*)s, mp_obj_get_int(args[1])));
37+
const char *s = mp_obj_str_get_data(args[0], &l);
38+
return MP_OBJ_NEW_SMALL_INT(strtonum(s, mp_obj_get_int(args[1])));
3939
}
4040

4141
default:

py/objstr.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,11 +635,11 @@ const char *mp_obj_str_get_str(mp_obj_t self_in) {
635635
}
636636
}
637637

638-
const byte *mp_obj_str_get_data(mp_obj_t self_in, uint *len) {
638+
const char *mp_obj_str_get_data(mp_obj_t self_in, uint *len) {
639639
if (MP_OBJ_IS_STR(self_in)) {
640640
GET_STR_DATA_LEN(self_in, s, l);
641641
*len = l;
642-
return s;
642+
return (const char*)s;
643643
} else {
644644
bad_implicit_conversion(self_in);
645645
}

py/stream.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static mp_obj_t stream_write(mp_obj_t self_in, mp_obj_t arg) {
4343
}
4444

4545
uint sz;
46-
const byte *buf = mp_obj_str_get_data(arg, &sz);
46+
const char *buf = mp_obj_str_get_data(arg, &sz);
4747
int error;
4848
machine_int_t out_sz = o->type->stream_p.write(self_in, buf, sz, &error);
4949
if (out_sz == -1) {

stm/file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ mp_obj_t file_obj_read(mp_obj_t self_in, mp_obj_t arg) {
2929
mp_obj_t file_obj_write(mp_obj_t self_in, mp_obj_t arg) {
3030
pyb_file_obj_t *self = self_in;
3131
uint l;
32-
const byte *s = mp_obj_str_get_data(arg, &l);
32+
const char *s = mp_obj_str_get_data(arg, &l);
3333
UINT n_out;
3434
FRESULT res = f_write(&self->fp, s, l, &n_out);
3535
if (res != FR_OK) {

stm/lcd.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ mp_obj_t lcd_pix_show(void) {
202202

203203
mp_obj_t lcd_print(mp_obj_t text) {
204204
uint len;
205-
const byte *data = mp_obj_str_get_data(text, &len);
206-
lcd_print_strn((const char*)data, len);
205+
const char *data = mp_obj_str_get_data(text, &len);
206+
lcd_print_strn(data, len);
207207
return mp_const_none;
208208
}
209209

0 commit comments

Comments
 (0)