Skip to content

Commit 289b2dd

Browse files
jonfosterdpgeorge
authored andcommitted
py/objstr: Add new mp_obj_new_str_from_cstr() helper function.
There were lots of places where this pattern was duplicated, to convert a standard C string to a MicroPython string: x = mp_obj_new_str(s, strlen(s)); This commit provides a simpler method that removes this code duplication: x = mp_obj_new_str_from_cstr(s); This gives clearer, and probably smaller, code. Signed-off-by: Jon Foster <[email protected]>
1 parent f36a565 commit 289b2dd

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

py/obj.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,7 @@ mp_obj_t mp_obj_new_int_from_str_len(const char **str, size_t len, bool neg, uns
971971
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)
972972
mp_obj_t mp_obj_new_int_from_ull(unsigned long long val); // this must return a multi-precision integer object (or raise an overflow exception)
973973
mp_obj_t mp_obj_new_str(const char *data, size_t len); // will check utf-8 (raises UnicodeError)
974+
mp_obj_t mp_obj_new_str_from_cstr(const char *str); // // accepts null-terminated string, will check utf-8 (raises UnicodeError)
974975
mp_obj_t mp_obj_new_str_via_qstr(const char *data, size_t len); // input data must be valid utf-8
975976
mp_obj_t mp_obj_new_str_from_vstr(vstr_t *vstr); // will check utf-8 (raises UnicodeError)
976977
#if MICROPY_PY_BUILTINS_STR_UNICODE && MICROPY_PY_BUILTINS_STR_UNICODE_CHECK

py/objstr.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2308,6 +2308,10 @@ mp_obj_t mp_obj_new_str(const char *data, size_t len) {
23082308
}
23092309
}
23102310

2311+
mp_obj_t mp_obj_new_str_from_cstr(const char *str) {
2312+
return mp_obj_new_str(str, strlen(str));
2313+
}
2314+
23112315
mp_obj_t mp_obj_str_intern(mp_obj_t str) {
23122316
GET_STR_DATA_LEN(str, data, len);
23132317
return mp_obj_new_str_via_qstr((const char *)data, len);

0 commit comments

Comments
 (0)