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 { pub fn utf32_to_str_no_malloc(code u32, buf voidptr) string {
icode := int(code) // Prevents doing casts everywhere icode := int(code) // Prevents doing casts everywhere
mut res := ''
unsafe { unsafe {
mut buffer := byteptr(buf) mut buffer := byteptr(buf)
if icode <= 127 { /* 0x7F */ if icode <= 127 { /* 0x7F */
buffer[0] = byte(icode) buffer[0] = byte(icode)
return tos(buffer, 1) res = tos(buffer, 1)
} }
if icode <= 2047 { /* 0x7FF */ if icode <= 2047 { /* 0x7FF */
buffer[0] = 192 | byte(icode>>6) /* 0xC0 - 110xxxxx */ buffer[0] = 192 | byte(icode>>6) /* 0xC0 - 110xxxxx */
buffer[1] = 128 | byte(icode & 63) /* 0x80 - 0x3F - 10xxxxxx */ buffer[1] = 128 | byte(icode & 63) /* 0x80 - 0x3F - 10xxxxxx */
return tos(buffer, 2) res = tos(buffer, 2)
} }
if icode <= 65535 { /* 0xFFFF */ if icode <= 65535 { /* 0xFFFF */
buffer[0] = 224 | byte(icode>>12)/* 0xE0 - 1110xxxx */ buffer[0] = 224 | byte(icode>>12)/* 0xE0 - 1110xxxx */
buffer[1] = 128 | (byte(icode>>6) & 63) /* 0x80 - 0x3F - 10xxxxxx */ buffer[1] = 128 | (byte(icode>>6) & 63) /* 0x80 - 0x3F - 10xxxxxx */
buffer[2] = 128 | byte(icode & 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 */ { if icode <= 1114111/* 0x10FFFF */ {
buffer[0] = 240 | byte(icode>>18) /* 0xF0 - 11110xxx */ buffer[0] = 240 | byte(icode>>18) /* 0xF0 - 11110xxx */
buffer[1] = 128 | (byte(icode>>12) & 63) /* 0x80 - 0x3F - 10xxxxxx */ buffer[1] = 128 | (byte(icode>>12) & 63) /* 0x80 - 0x3F - 10xxxxxx */
buffer[2] = 128 | (byte(icode>>6) & 63) /* 0x80 - 0x3F - 10xxxxxx */ buffer[2] = 128 | (byte(icode>>6) & 63) /* 0x80 - 0x3F - 10xxxxxx */
buffer[3] = 128 | byte(icode & 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 // Convert utf8 to utf32

View File

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