Skip to content
Snippets Groups Projects
Commit 0b7e29c0 authored by Paul Sokolovsky's avatar Paul Sokolovsky
Browse files

Factor out quoted string print function for reuse (mp_str_print_quoted()).

parent 8e991e06
No related branches found
No related tags found
No related merge requests found
...@@ -291,6 +291,7 @@ uint mp_obj_str_get_len(mp_obj_t self_in); ...@@ -291,6 +291,7 @@ 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 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 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 byte *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 // bytes
extern const mp_obj_type_t bytes_type; extern const mp_obj_type_t bytes_type;
......
...@@ -34,12 +34,7 @@ static mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str); ...@@ -34,12 +34,7 @@ static mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str);
/******************************************************************************/ /******************************************************************************/
/* str */ /* str */
void str_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { void mp_str_print_quoted(void (*print)(void *env, const char *fmt, ...), void *env, const byte *str_data, uint str_len) {
GET_STR_DATA_LEN(self_in, str_data, str_len);
bool is_bytes = MP_OBJ_IS_TYPE(self_in, &bytes_type);
if (kind == PRINT_STR && !is_bytes) {
print(env, "%.*s", str_len, str_data);
} else {
// this escapes characters, but it will be very slow to print (calling print many times) // this escapes characters, but it will be very slow to print (calling print many times)
bool has_single_quote = false; bool has_single_quote = false;
bool has_double_quote = false; bool has_double_quote = false;
...@@ -50,9 +45,6 @@ void str_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj ...@@ -50,9 +45,6 @@ void str_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj
has_double_quote = true; has_double_quote = true;
} }
} }
if (is_bytes) {
print(env, "b");
}
int quote_char = '\''; int quote_char = '\'';
if (has_single_quote && !has_double_quote) { if (has_single_quote && !has_double_quote) {
quote_char = '"'; quote_char = '"';
...@@ -74,6 +66,18 @@ void str_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj ...@@ -74,6 +66,18 @@ void str_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj
} }
print(env, "%c", quote_char); print(env, "%c", quote_char);
} }
static void str_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
GET_STR_DATA_LEN(self_in, str_data, str_len);
bool is_bytes = MP_OBJ_IS_TYPE(self_in, &bytes_type);
if (kind == PRINT_STR && !is_bytes) {
print(env, "%.*s", str_len, str_data);
} else {
if (is_bytes) {
print(env, "b");
}
mp_str_print_quoted(print, env, str_data, str_len);
}
} }
// like strstr but with specified length and allows \0 bytes // like strstr but with specified length and allows \0 bytes
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment