autofree: builtin fixes

pull/6912/head
Alexander Medvednikov 2020-11-21 22:33:31 +01:00
parent 8cef4e0433
commit 9a51f4e3c2
2 changed files with 13 additions and 11 deletions

View File

@ -16,32 +16,34 @@ pub fn utf32_to_str(code u32) string {
pub fn utf32_to_str_no_malloc(code u32, buf voidptr) string {
icode := int(code) // Prevents doing casts everywhere
mut res := ''
unsafe {
mut buffer := byteptr(buf)
if icode <= 127 { /* 0x7F */
buffer[0] = byte(icode)
return tos(buffer, 1)
res = tos(buffer, 1)
}
if icode <= 2047 { /* 0x7FF */
buffer[0] = 192 | byte(icode>>6) /* 0xC0 - 110xxxxx */
buffer[1] = 128 | byte(icode & 63) /* 0x80 - 0x3F - 10xxxxxx */
return tos(buffer, 2)
res = tos(buffer, 2)
}
if icode <= 65535 { /* 0xFFFF */
buffer[0] = 224 | byte(icode>>12)/* 0xE0 - 1110xxxx */
buffer[1] = 128 | (byte(icode>>6) & 63) /* 0x80 - 0x3F - 10xxxxxx */
buffer[2] = 128 | byte(icode & 63) /* 0x80 - 0x3F - 10xxxxxx */
return tos(buffer, 3)
res = tos(buffer, 3)
}
if icode <= 1114111/* 0x10FFFF */ {
buffer[0] = 240 | byte(icode>>18) /* 0xF0 - 11110xxx */
buffer[1] = 128 | (byte(icode>>12) & 63) /* 0x80 - 0x3F - 10xxxxxx */
buffer[2] = 128 | (byte(icode>>6) & 63) /* 0x80 - 0x3F - 10xxxxxx */
buffer[3] = 128 | byte(icode & 63) /* 0x80 - 0x3F - 10xxxxxx */
return tos(buffer, 4)
res = tos(buffer, 4)
}
}
return ''
res.is_lit = 1 // let autofree know this string doesn't have to be freed
return res
}
// Convert utf8 to utf32
@ -181,10 +183,10 @@ fn utf8_str_visible_length(s string) int {
// diacritical marks supplement 0xe1b780 - 0xe1b880
// diacritical marks for symbols 0xe28390 - 0xe28480
// half marks 0xefb8a0 - 0xefb8b0
if (r >= 0xe1aab0 && r < 0xe1ac80)
|| (r >= 0xe1b780 && r < 0xe1b880)
|| (r >= 0xe28390 && r < 0xe28480)
|| (r >= 0xefb8a0 && r < 0xefb8b0) {
if (r >= 0xe1aab0 && r < 0xe1ac80)
|| (r >= 0xe1b780 && r < 0xe1b880)
|| (r >= 0xe28390 && r < 0xe28480)
|| (r >= 0xefb8a0 && r < 0xefb8b0) {
l--
}
}

View File

@ -1551,8 +1551,8 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
else {}
}
// Free the old value assigned to this string var (only if it's `str = [new value]`)
mut af := g.pref.autofree && assign_stmt.op == .assign && assign_stmt.left_types.len == 1 &&
assign_stmt.left_types[0] == table.string_type && assign_stmt.left[0] is ast.Ident
mut af := g.pref.autofree && !g.is_builtin_mod && assign_stmt.op == .assign && assign_stmt.left_types.len ==
1 && assign_stmt.left_types[0] == table.string_type && assign_stmt.left[0] is ast.Ident
mut sref_name := ''
if af {
ident := assign_stmt.left[0] as ast.Ident