77
88#include "misc.h"
99#include "mpconfig.h"
10- #include "mpqstr .h"
10+ #include "qstr .h"
1111#include "lexer.h"
1212#include "parse.h"
1313#include "scope.h"
@@ -273,8 +273,8 @@ static bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
273273}
274274
275275static void cpython_c_print_quoted_str (vstr_t * vstr , qstr qstr , bool bytes ) {
276- const char * str = qstr_str ( qstr ) ;
277- int len = strlen ( str );
276+ uint len ;
277+ const byte * str = qstr_data ( qstr , & len );
278278 bool has_single_quote = false;
279279 bool has_double_quote = false;
280280 for (int i = 0 ; i < len ; i ++ ) {
@@ -1169,22 +1169,20 @@ void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q1, qstr *q2) {
11691169 int n = MP_PARSE_NODE_STRUCT_NUM_NODES (pns );
11701170 int len = n - 1 ;
11711171 for (int i = 0 ; i < n ; i ++ ) {
1172- len += strlen ( qstr_str ( MP_PARSE_NODE_LEAF_ARG (pns -> nodes [i ]) ));
1172+ len += qstr_len ( MP_PARSE_NODE_LEAF_ARG (pns -> nodes [i ]));
11731173 }
1174- char * str = m_new (char , len + 1 );
1175- char * str_dest = str ;
1176- str [0 ] = 0 ;
1174+ byte * q_ptr ;
1175+ byte * str_dest = qstr_build_start (len , & q_ptr );
11771176 for (int i = 0 ; i < n ; i ++ ) {
11781177 if (i > 0 ) {
11791178 * str_dest ++ = '.' ;
11801179 }
1181- const char * str_src = qstr_str ( MP_PARSE_NODE_LEAF_ARG ( pns -> nodes [ i ])) ;
1182- size_t str_src_len = strlen ( str_src );
1180+ uint str_src_len ;
1181+ const byte * str_src = qstr_data ( MP_PARSE_NODE_LEAF_ARG ( pns -> nodes [ i ]), & str_src_len );
11831182 memcpy (str_dest , str_src , str_src_len );
11841183 str_dest += str_src_len ;
11851184 }
1186- * str_dest = '\0' ;
1187- * q2 = qstr_from_str_take (str , len + 1 );
1185+ * q2 = qstr_build_end (q_ptr );
11881186 EMIT (import_name , * q2 );
11891187 if (is_as ) {
11901188 for (int i = 1 ; i < n ; i ++ ) {
@@ -1221,7 +1219,7 @@ void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
12211219#if MICROPY_EMIT_CPYTHON
12221220 EMIT (load_const_verbatim_str , "('*',)" );
12231221#else
1224- EMIT (load_const_str , qstr_from_str_static ("*" ), false);
1222+ EMIT (load_const_str , QSTR_FROM_STR_STATIC ("*" ), false);
12251223 EMIT (build_tuple , 1 );
12261224#endif
12271225
@@ -1248,7 +1246,9 @@ void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
12481246 vstr_printf (vstr , ", " );
12491247 }
12501248 vstr_printf (vstr , "'" );
1251- vstr_printf (vstr , qstr_str (id2 ));
1249+ uint len ;
1250+ const byte * str = qstr_data (id2 , & len );
1251+ vstr_add_strn (vstr , (const char * )str , len );
12521252 vstr_printf (vstr , "'" );
12531253 }
12541254 if (n == 1 ) {
@@ -2128,24 +2128,21 @@ void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
21282128 printf ("SyntaxError: cannot mix bytes and nonbytes literals\n" );
21292129 return ;
21302130 }
2131- const char * str = qstr_str (MP_PARSE_NODE_LEAF_ARG (pns -> nodes [i ]));
2132- n_bytes += strlen (str );
2131+ n_bytes += qstr_len (MP_PARSE_NODE_LEAF_ARG (pns -> nodes [i ]));
21332132 }
21342133
2135- // allocate memory for concatenated string/bytes
2136- char * cat_str = m_new (char , n_bytes + 1 );
2137-
21382134 // concatenate string/bytes
2139- char * s_dest = cat_str ;
2135+ byte * q_ptr ;
2136+ byte * s_dest = qstr_build_start (n_bytes , & q_ptr );
21402137 for (int i = 0 ; i < n ; i ++ ) {
2141- const char * s = qstr_str ( MP_PARSE_NODE_LEAF_ARG ( pns -> nodes [ i ])) ;
2142- size_t s_len = strlen ( s );
2138+ uint s_len ;
2139+ const byte * s = qstr_data ( MP_PARSE_NODE_LEAF_ARG ( pns -> nodes [ i ]), & s_len );
21432140 memcpy (s_dest , s , s_len );
21442141 s_dest += s_len ;
21452142 }
2146- * s_dest = '\0' ;
2143+ qstr q = qstr_build_end ( q_ptr ) ;
21472144
2148- EMIT (load_const_str , qstr_from_str_take ( cat_str , n_bytes + 1 ) , string_kind == MP_PARSE_NODE_BYTES );
2145+ EMIT (load_const_str , q , string_kind == MP_PARSE_NODE_BYTES );
21492146}
21502147
21512148// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
@@ -2767,7 +2764,7 @@ void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
27672764 assert (MP_PARSE_NODE_IS_STRUCT_KIND (pns -> nodes [1 ], PN_comp_for ));
27682765 mp_parse_node_struct_t * pns_comp_for = (mp_parse_node_struct_t * )pns -> nodes [1 ];
27692766
2770- qstr qstr_arg = qstr_from_str_static (".0" );
2767+ qstr qstr_arg = QSTR_FROM_STR_STATIC (".0" );
27712768 if (comp -> pass == PASS_1 ) {
27722769 bool added ;
27732770 id_info_t * id_info = scope_find_or_add_id (comp -> scope_cur , qstr_arg , & added );
0 commit comments