1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: use STR_TMP with -autofree

This commit is contained in:
Alexander Medvednikov 2020-05-25 12:46:18 +02:00
parent fe249cd1f8
commit 53a10eb312
2 changed files with 17 additions and 12 deletions

View File

@ -2669,7 +2669,11 @@ fn (g Gen) sort_structs(typesa []table.TypeSymbol) []table.TypeSymbol {
} }
fn (mut g Gen) string_inter_literal(node ast.StringInterLiteral) { fn (mut g Gen) string_inter_literal(node ast.StringInterLiteral) {
if g.pref.autofree {
g.write('_STR_TMP("')
} else {
g.write('_STR("') g.write('_STR("')
}
// Build the string with % // Build the string with %
mut fieldwidths := []int{} mut fieldwidths := []int{}
mut specs := []byte{} mut specs := []byte{}
@ -2679,7 +2683,9 @@ fn (mut g Gen) string_inter_literal(node ast.StringInterLiteral) {
if i >= node.exprs.len { if i >= node.exprs.len {
if escaped_val.len > 0 { if escaped_val.len > 0 {
end_string = true end_string = true
if !g.pref.autofree {
g.write('\\000') g.write('\\000')
}
g.write(escaped_val) g.write(escaped_val)
} }
continue continue
@ -2722,13 +2728,12 @@ fn (mut g Gen) string_inter_literal(node ast.StringInterLiteral) {
fields := fmt.split('.') fields := fmt.split('.')
// validate format // validate format
// only floats should have precision specifier // only floats should have precision specifier
if fields.len > 2 || if fields.len > 2 || fields.len == 2 && !(node.expr_types[i].is_float()) || node.expr_types[i].is_signed() &&
fields.len == 2 && !(node.expr_types[i].is_float()) || fspec !in [`d`, `c`, `x`, `X`, `o`] || node.expr_types[i].is_unsigned() && fspec !in [`u`, `x`,
node.expr_types[i].is_signed() && fspec !in [`d`, `c`, `x`, `X`, `o`] || `X`, `o`, `c`] || node.expr_types[i].is_any_int() && fspec !in [`d`, `c`, `x`, `X`,
node.expr_types[i].is_unsigned() && fspec !in [`u`, `x`, `X`, `o`, `c`] || `o`, `u`,
node.expr_types[i].is_any_int() && fspec !in [`d`, `c`, `x`, `X`, `o`, `u`, `x`, `X`, `o`] || `x`, `X`, `o`] || node.expr_types[i].is_float() && fspec !in [`E`, `F`, `G`, `e`,
node.expr_types[i].is_float() && fspec !in [`E`, `F`, `G`, `e`, `f`, `g`] || `f`, `g`] || node.expr_types[i].is_pointer() && fspec !in [`p`, `x`, `X`] {
node.expr_types[i].is_pointer() && fspec !in [`p`, `x`, `X`] {
verror('illegal format specifier ${fspec:c} for type ${g.table.get_type_name(node.expr_types[i])}') verror('illegal format specifier ${fspec:c} for type ${g.table.get_type_name(node.expr_types[i])}')
} }
// make sure that format paramters are valid numbers // make sure that format paramters are valid numbers
@ -2786,7 +2791,7 @@ fn (mut g Gen) string_inter_literal(node ast.StringInterLiteral) {
// TODO: better check this case // TODO: better check this case
g.write('${fmt}"PRId32"') g.write('${fmt}"PRId32"')
} }
if i < node.exprs.len - 1 { if i < node.exprs.len - 1 && !g.pref.autofree {
g.write('\\000') g.write('\\000')
} }
} }

View File

@ -91,7 +91,7 @@ string _STR(const char *fmt, int nfmts, ...) {
string _STR_TMP(const char *fmt, ...) { string _STR_TMP(const char *fmt, ...) {
va_list argptr; va_list argptr;
va_start(argptr, fmt); va_start(argptr, fmt);
//size_t len = vsnprintf(0, 0, fmt, argptr) + 1; size_t len = vsnprintf(0, 0, fmt, argptr) + 1;
va_end(argptr); va_end(argptr);
va_start(argptr, fmt); va_start(argptr, fmt);
vsprintf((char *)g_str_buf, fmt, argptr); vsprintf((char *)g_str_buf, fmt, argptr);
@ -100,7 +100,7 @@ string _STR_TMP(const char *fmt, ...) {
//puts('_STR_TMP:'); //puts('_STR_TMP:');
//puts(g_str_buf); //puts(g_str_buf);
#endif #endif
return tos2(g_str_buf); return tos(g_str_buf, len);
} // endof _STR_TMP } // endof _STR_TMP
") ")