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.
This commit is contained in:
Damien George 2014-02-08 18:17:23 +00:00
parent 23177088d2
commit 698ec21e46
12 changed files with 23 additions and 21 deletions

View File

@ -288,9 +288,11 @@ MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_next_obj, mp_builtin_next);
static mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
uint len;
const byte *str = mp_obj_str_get_data(o_in, &len);
const char *str = mp_obj_str_get_data(o_in, &len);
if (len == 1) {
return mp_obj_new_int(str[0]);
// don't sign extend when converting to ord
// TODO unicode
return mp_obj_new_int(((const byte*)str)[0]);
} else {
nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "ord() expected a character, but string of length %d found", len));
}

View File

@ -21,10 +21,10 @@
static mp_obj_t parse_compile_execute(mp_obj_t o_in, mp_parse_input_kind_t parse_input_kind) {
uint str_len;
const byte *str = mp_obj_str_get_data(o_in, &str_len);
const char *str = mp_obj_str_get_data(o_in, &str_len);
// create the lexer
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_string_gt_, (const char*)str, str_len, 0);
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_string_gt_, str, str_len, 0);
qstr source_name = mp_lexer_source_name(lex);
// parse the string

View File

@ -54,9 +54,9 @@ mp_import_stat_t find_file(const char *file_str, uint file_len, vstr_t *dest) {
for (int i = 0; i < path_num; i++) {
vstr_reset(dest);
uint p_len;
const byte *p = mp_obj_str_get_data(path_items[i], &p_len);
const char *p = mp_obj_str_get_data(path_items[i], &p_len);
if (p_len > 0) {
vstr_add_strn(dest, (const char*)p, p_len);
vstr_add_strn(dest, p, p_len);
vstr_add_char(dest, PATH_SEP_CHAR);
}
vstr_add_strn(dest, file_str, file_len);

View File

@ -290,7 +290,7 @@ uint mp_obj_str_get_hash(mp_obj_t self_in);
uint mp_obj_str_get_len(mp_obj_t self_in);
qstr mp_obj_str_get_qstr(mp_obj_t self_in); // use this if you will anyway convert the string to a qstr
const char *mp_obj_str_get_str(mp_obj_t self_in); // use this only if you need the string to be null terminated
const byte *mp_obj_str_get_data(mp_obj_t self_in, uint *len);
const char *mp_obj_str_get_data(mp_obj_t self_in, uint *len);
void mp_str_print_quoted(void (*print)(void *env, const char *fmt, ...), void *env, const byte *str_data, uint str_len);
// bytes

View File

@ -174,7 +174,7 @@ static mp_obj_t array_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const m
}
// TODO check args
uint l;
const byte *typecode = mp_obj_str_get_data(args[0], &l);
const char *typecode = mp_obj_str_get_data(args[0], &l);
if (n_args == 1) {
return array_new(*typecode, 0);
}

View File

@ -23,8 +23,8 @@ static mp_obj_t int_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_
if (MP_OBJ_IS_STR(args[0])) {
// a string, parse it
uint l;
const byte *s = mp_obj_str_get_data(args[0], &l);
return MP_OBJ_NEW_SMALL_INT(strtonum((const char*)s, 0));
const char *s = mp_obj_str_get_data(args[0], &l);
return MP_OBJ_NEW_SMALL_INT(strtonum(s, 0));
} else {
return MP_OBJ_NEW_SMALL_INT(mp_obj_get_int(args[0]));
}
@ -34,8 +34,8 @@ static mp_obj_t int_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_
// should be a string, parse it
// TODO proper error checking of argument types
uint l;
const byte *s = mp_obj_str_get_data(args[0], &l);
return MP_OBJ_NEW_SMALL_INT(strtonum((const char*)s, mp_obj_get_int(args[1])));
const char *s = mp_obj_str_get_data(args[0], &l);
return MP_OBJ_NEW_SMALL_INT(strtonum(s, mp_obj_get_int(args[1])));
}
default:

View File

@ -635,11 +635,11 @@ const char *mp_obj_str_get_str(mp_obj_t self_in) {
}
}
const byte *mp_obj_str_get_data(mp_obj_t self_in, uint *len) {
const char *mp_obj_str_get_data(mp_obj_t self_in, uint *len) {
if (MP_OBJ_IS_STR(self_in)) {
GET_STR_DATA_LEN(self_in, s, l);
*len = l;
return s;
return (const char*)s;
} else {
bad_implicit_conversion(self_in);
}

View File

@ -43,7 +43,7 @@ static mp_obj_t stream_write(mp_obj_t self_in, mp_obj_t arg) {
}
uint sz;
const byte *buf = mp_obj_str_get_data(arg, &sz);
const char *buf = mp_obj_str_get_data(arg, &sz);
int error;
machine_int_t out_sz = o->type->stream_p.write(self_in, buf, sz, &error);
if (out_sz == -1) {

View File

@ -29,7 +29,7 @@ mp_obj_t file_obj_read(mp_obj_t self_in, mp_obj_t arg) {
mp_obj_t file_obj_write(mp_obj_t self_in, mp_obj_t arg) {
pyb_file_obj_t *self = self_in;
uint l;
const byte *s = mp_obj_str_get_data(arg, &l);
const char *s = mp_obj_str_get_data(arg, &l);
UINT n_out;
FRESULT res = f_write(&self->fp, s, l, &n_out);
if (res != FR_OK) {

View File

@ -202,8 +202,8 @@ mp_obj_t lcd_pix_show(void) {
mp_obj_t lcd_print(mp_obj_t text) {
uint len;
const byte *data = mp_obj_str_get_data(text, &len);
lcd_print_strn((const char*)data, len);
const char *data = mp_obj_str_get_data(text, &len);
lcd_print_strn(data, len);
return mp_const_none;
}

View File

@ -159,7 +159,7 @@ void usart_tx_str(pyb_usart_t usart_id, const char *str) {
}
}
void usart_tx_bytes(pyb_usart_t usart_id, const byte *data, uint len) {
void usart_tx_bytes(pyb_usart_t usart_id, const char *data, uint len) {
for (; len > 0; data++, len--) {
usart_tx_char(usart_id, *data);
}
@ -216,7 +216,7 @@ static mp_obj_t usart_obj_tx_str(mp_obj_t self_in, mp_obj_t s) {
if (self->is_enabled) {
if (MP_OBJ_IS_STR(s)) {
uint len;
const byte *data = mp_obj_str_get_data(s, &len);
const char *data = mp_obj_str_get_data(s, &len);
usart_tx_bytes(self->usart_id, data, len);
}
}

View File

@ -154,7 +154,7 @@ static mp_obj_t socket_send(uint n_args, const mp_obj_t *args) {
}
uint sz;
const byte *buf = mp_obj_str_get_data(args[1], &sz);
const char *buf = mp_obj_str_get_data(args[1], &sz);
int out_sz = send(self->fd, buf, sz, flags);
RAISE_ERRNO(out_sz, errno);