Skip to content

Commit 2617eeb

Browse files
committed
Change const byte* to const char* where sensible.
This removes need for some casts (at least, more than it adds need for new casts!).
1 parent f88fc7b commit 2617eeb

File tree

12 files changed

+25
-31
lines changed

12 files changed

+25
-31
lines changed

py/builtin.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_callable_obj, mp_builtin_callable);
172172
STATIC mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
173173
int ord = mp_obj_get_int(o_in);
174174
if (0 <= ord && ord <= 0x10ffff) {
175-
byte str[1] = {ord};
175+
char str[1] = {ord};
176176
return mp_obj_new_str(str, 1, true);
177177
} else {
178178
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "chr() arg not in range(0x110000)"));
@@ -391,7 +391,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_print_obj, 0, mp_builtin_print);
391391
STATIC mp_obj_t mp_builtin_repr(mp_obj_t o_in) {
392392
vstr_t *vstr = vstr_new();
393393
mp_obj_print_helper((void (*)(void *env, const char *fmt, ...))vstr_printf, vstr, o_in, PRINT_REPR);
394-
mp_obj_t s = mp_obj_new_str((byte*)vstr->buf, vstr->len, false);
394+
mp_obj_t s = mp_obj_new_str(vstr->buf, vstr->len, false);
395395
vstr_free(vstr);
396396
return s;
397397
}

py/builtinimport.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ mp_obj_t mp_builtin___import__(uint n_args, mp_obj_t *args) {
318318
DEBUG_printf("%s is dir\n", vstr_str(&path));
319319
// https://docs.python.org/3.3/reference/import.html
320320
// "Specifically, any module that contains a __path__ attribute is considered a package."
321-
mp_store_attr(module_obj, MP_QSTR___path__, mp_obj_new_str((byte*)vstr_str(&path), vstr_len(&path), false));
321+
mp_store_attr(module_obj, MP_QSTR___path__, mp_obj_new_str(vstr_str(&path), vstr_len(&path), false));
322322
vstr_add_char(&path, PATH_SEP_CHAR);
323323
vstr_add_str(&path, "__init__.py");
324324
if (mp_import_stat(vstr_str(&path)) != MP_IMPORT_STAT_FILE) {

py/obj.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ mp_obj_t mp_obj_new_int(machine_int_t value);
373373
mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value);
374374
mp_obj_t mp_obj_new_int_from_qstr(qstr qst);
375375
mp_obj_t mp_obj_new_int_from_ll(long long val); // this must return a multi-precision integer object (or raise an overflow exception)
376-
mp_obj_t mp_obj_new_str(const byte* data, uint len, bool make_qstr_if_not_already);
376+
mp_obj_t mp_obj_new_str(const char* data, uint len, bool make_qstr_if_not_already);
377377
mp_obj_t mp_obj_new_bytes(const byte* data, uint len);
378378
#if MICROPY_ENABLE_FLOAT
379379
mp_obj_t mp_obj_new_float(mp_float_t val);

py/objexcept.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char
283283
va_start(ap, fmt);
284284
vstr_vprintf(vstr, fmt, ap);
285285
va_end(ap);
286-
o->args->items[0] = mp_obj_new_str((byte*)vstr->buf, vstr->len, false);
286+
o->args->items[0] = mp_obj_new_str(vstr->buf, vstr->len, false);
287287
vstr_free(vstr);
288288
}
289289
}

py/objstr.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ STATIC mp_obj_t str_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_
129129
{
130130
vstr_t *vstr = vstr_new();
131131
mp_obj_print_helper((void (*)(void*, const char*, ...))vstr_printf, vstr, args[0], PRINT_STR);
132-
mp_obj_t s = mp_obj_new_str((byte*)vstr->buf, vstr->len, false);
132+
mp_obj_t s = mp_obj_new_str(vstr->buf, vstr->len, false);
133133
vstr_free(vstr);
134134
return s;
135135
}
@@ -363,7 +363,7 @@ STATIC mp_obj_t str_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
363363
if (type == &mp_type_bytes) {
364364
return MP_OBJ_NEW_SMALL_INT((mp_small_int_t)self_data[index_val]);
365365
} else {
366-
return mp_obj_new_str(self_data + index_val, 1, true);
366+
return mp_obj_new_str((char*)self_data + index_val, 1, true);
367367
}
368368
} else {
369369
return MP_OBJ_NULL; // op not supported
@@ -873,7 +873,7 @@ mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) {
873873
}
874874
vstr_t *arg_vstr = vstr_new();
875875
mp_obj_print_helper((void (*)(void*, const char*, ...))vstr_printf, arg_vstr, arg, print_kind);
876-
arg = mp_obj_new_str((const byte *)vstr_str(arg_vstr), vstr_len(arg_vstr), false);
876+
arg = mp_obj_new_str(vstr_str(arg_vstr), vstr_len(arg_vstr), false);
877877
vstr_free(arg_vstr);
878878
}
879879

@@ -1116,7 +1116,7 @@ mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) {
11161116
}
11171117
}
11181118

1119-
mp_obj_t s = mp_obj_new_str((byte*)vstr->buf, vstr->len, false);
1119+
mp_obj_t s = mp_obj_new_str(vstr->buf, vstr->len, false);
11201120
vstr_free(vstr);
11211121
return s;
11221122
}
@@ -1287,7 +1287,7 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, uint n_args, const mp_obj_t
12871287
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "not all arguments converted during string formatting"));
12881288
}
12891289

1290-
mp_obj_t s = mp_obj_new_str((byte*)vstr->buf, vstr->len, false);
1290+
mp_obj_t s = mp_obj_new_str(vstr->buf, vstr->len, false);
12911291
vstr_free(vstr);
12921292
return s;
12931293
}
@@ -1655,17 +1655,17 @@ mp_obj_t str_new(const mp_obj_type_t *type, const byte* data, uint len) {
16551655
return o;
16561656
}
16571657

1658-
mp_obj_t mp_obj_new_str(const byte* data, uint len, bool make_qstr_if_not_already) {
1658+
mp_obj_t mp_obj_new_str(const char* data, uint len, bool make_qstr_if_not_already) {
16591659
qstr q = qstr_find_strn(data, len);
16601660
if (q != MP_QSTR_NULL) {
16611661
// qstr with this data already exists
16621662
return MP_OBJ_NEW_QSTR(q);
16631663
} else if (make_qstr_if_not_already) {
16641664
// no existing qstr, make a new one
1665-
return MP_OBJ_NEW_QSTR(qstr_from_strn((const char*)data, len));
1665+
return MP_OBJ_NEW_QSTR(qstr_from_strn(data, len));
16661666
} else {
16671667
// no existing qstr, don't make one
1668-
return str_new(&mp_type_str, data, len);
1668+
return str_new(&mp_type_str, (const byte*)data, len);
16691669
}
16701670
}
16711671

@@ -1768,7 +1768,7 @@ STATIC mp_obj_t str_it_iternext(mp_obj_t self_in) {
17681768
mp_obj_str_it_t *self = self_in;
17691769
GET_STR_DATA_LEN(self->str, str, len);
17701770
if (self->cur < len) {
1771-
mp_obj_t o_out = mp_obj_new_str(str + self->cur, 1, true);
1771+
mp_obj_t o_out = mp_obj_new_str((const char*)str + self->cur, 1, true);
17721772
self->cur += 1;
17731773
return o_out;
17741774
} else {

py/parse.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ STATIC void push_result_token(parser_t *parser, const mp_lexer_t *lex) {
349349
qst = qstr_from_strn(tok->str, tok->len);
350350
} else {
351351
// check if this string is already interned
352-
qst = qstr_find_strn((const byte*)tok->str, tok->len);
352+
qst = qstr_find_strn(tok->str, tok->len);
353353
}
354354
if (qst != MP_QSTR_NULL) {
355355
// qstr exists, make a leaf node

py/qstr.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ STATIC qstr qstr_add(const byte *q_ptr) {
130130
return last_pool->total_prev_len + last_pool->len - 1;
131131
}
132132

133-
qstr qstr_find_strn(const byte *str, uint str_len) {
133+
qstr qstr_find_strn(const char *str, uint str_len) {
134134
// work out hash of str
135135
machine_uint_t str_hash = qstr_compute_hash((const byte*)str, str_len);
136136

@@ -152,7 +152,7 @@ qstr qstr_from_str(const char *str) {
152152
}
153153

154154
qstr qstr_from_strn(const char *str, uint len) {
155-
qstr q = qstr_find_strn((const byte*)str, len);
155+
qstr q = qstr_find_strn(str, len);
156156
if (q == 0) {
157157
machine_uint_t hash = qstr_compute_hash((const byte*)str, len);
158158
byte *q_ptr = m_new(byte, 4 + len + 1);
@@ -167,12 +167,6 @@ qstr qstr_from_strn(const char *str, uint len) {
167167
return q;
168168
}
169169

170-
qstr qstr_from_strn_take(char *str, uint alloc_len, uint len) {
171-
qstr q = qstr_from_strn(str, len);
172-
m_del(char, str, alloc_len);
173-
return q;
174-
}
175-
176170
byte *qstr_build_start(uint len, byte **q_ptr) {
177171
assert(len <= 65535);
178172
*q_ptr = m_new(byte, 4 + len + 1);
@@ -182,7 +176,7 @@ byte *qstr_build_start(uint len, byte **q_ptr) {
182176
}
183177

184178
qstr qstr_build_end(byte *q_ptr) {
185-
qstr q = qstr_find_strn(Q_GET_DATA(q_ptr), Q_GET_LENGTH(q_ptr));
179+
qstr q = qstr_find_strn((const char*)Q_GET_DATA(q_ptr), Q_GET_LENGTH(q_ptr));
186180
if (q == 0) {
187181
machine_uint_t len = Q_GET_LENGTH(q_ptr);
188182
machine_uint_t hash = qstr_compute_hash(Q_GET_DATA(q_ptr), len);

py/qstr.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,11 @@ typedef machine_uint_t qstr;
4646
void qstr_init(void);
4747

4848
machine_uint_t qstr_compute_hash(const byte *data, uint len);
49-
qstr qstr_find_strn(const byte *str, uint str_len); // returns MP_QSTR_NULL if not found
49+
qstr qstr_find_strn(const char *str, uint str_len); // returns MP_QSTR_NULL if not found
5050

5151
qstr qstr_from_str(const char *str);
5252
qstr qstr_from_strn(const char *str, uint len);
5353
//qstr qstr_from_str_static(const char *str);
54-
qstr qstr_from_strn_take(char *str, uint alloc_len, uint len);
5554
//qstr qstr_from_strn_copy(const char *str, int len);
5655

5756
byte* qstr_build_start(uint len, byte **q_ptr);

stmhal/input.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ STATIC mp_obj_t mp_builtin_input(uint n_args, const mp_obj_t *args) {
4444
if (line.len == 0 && ret == VCP_CHAR_CTRL_D) {
4545
nlr_raise(mp_obj_new_exception(&mp_type_EOFError));
4646
}
47-
mp_obj_t o = mp_obj_new_str((const byte*)line.buf, line.len, false);
47+
mp_obj_t o = mp_obj_new_str(line.buf, line.len, false);
4848
vstr_clear(&line);
4949
return o;
5050
}

unix/input.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ STATIC mp_obj_t mp_builtin_input(uint n_args, const mp_obj_t *args) {
7777
if (line == NULL) {
7878
nlr_raise(mp_obj_new_exception(&mp_type_EOFError));
7979
}
80-
mp_obj_t o = mp_obj_new_str((const byte*)line, strlen(line), false);
80+
mp_obj_t o = mp_obj_new_str(line, strlen(line), false);
8181
free(line);
8282
return o;
8383
}

0 commit comments

Comments
 (0)