builtin, strings: improve consistency and behavior regarding explicit mutability (#21566)

This commit is contained in:
Turiiya 2024-07-10 14:26:51 +02:00 committed by GitHub
parent 04ff511e27
commit 2a368cf53c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 10 additions and 9 deletions

View File

@ -51,7 +51,7 @@ pub fn (c rune) repeat(count int) string {
return c.str() return c.str()
} }
mut buffer := [5]u8{} mut buffer := [5]u8{}
res := unsafe { utf32_to_str_no_malloc(u32(c), &buffer[0]) } res := unsafe { utf32_to_str_no_malloc(u32(c), mut &buffer[0]) }
return res.repeat(count) return res.repeat(count)
} }
@ -59,7 +59,8 @@ pub fn (c rune) repeat(count int) string {
@[manualfree] @[manualfree]
pub fn (c rune) bytes() []u8 { pub fn (c rune) bytes() []u8 {
mut res := []u8{cap: 5} mut res := []u8{cap: 5}
res.len = unsafe { utf32_decode_to_buffer(u32(c), &u8(res.data)) } mut buf := &u8(res.data)
res.len = unsafe { utf32_decode_to_buffer(u32(c), mut buf) }
return res return res
} }

View File

@ -12,7 +12,7 @@ pub fn utf8_char_len(b u8) int {
pub fn utf32_to_str(code u32) string { pub fn utf32_to_str(code u32) string {
unsafe { unsafe {
mut buffer := malloc_noscan(5) mut buffer := malloc_noscan(5)
res := utf32_to_str_no_malloc(code, buffer) res := utf32_to_str_no_malloc(code, mut buffer)
if res.len == 0 { if res.len == 0 {
// the buffer was not used at all // the buffer was not used at all
free(buffer) free(buffer)
@ -22,9 +22,9 @@ pub fn utf32_to_str(code u32) string {
} }
@[manualfree; unsafe] @[manualfree; unsafe]
pub fn utf32_to_str_no_malloc(code u32, buf &u8) string { pub fn utf32_to_str_no_malloc(code u32, mut buf &u8) string {
unsafe { unsafe {
len := utf32_decode_to_buffer(code, buf) len := utf32_decode_to_buffer(code, mut buf)
if len == 0 { if len == 0 {
return '' return ''
} }
@ -34,7 +34,7 @@ pub fn utf32_to_str_no_malloc(code u32, buf &u8) string {
} }
@[manualfree; unsafe] @[manualfree; unsafe]
pub fn utf32_decode_to_buffer(code u32, buf &u8) int { pub fn utf32_decode_to_buffer(code u32, mut buf &u8) int {
unsafe { unsafe {
icode := int(code) // Prevents doing casts everywhere icode := int(code) // Prevents doing casts everywhere
mut buffer := &u8(buf) mut buffer := &u8(buf)

View File

@ -40,7 +40,7 @@ pub fn (mut b Builder) write_ptr(ptr &u8, len int) {
@[manualfree] @[manualfree]
pub fn (mut b Builder) write_rune(r rune) { pub fn (mut b Builder) write_rune(r rune) {
mut buffer := [5]u8{} mut buffer := [5]u8{}
res := unsafe { utf32_to_str_no_malloc(u32(r), &buffer[0]) } res := unsafe { utf32_to_str_no_malloc(u32(r), mut &buffer[0]) }
if res.len == 0 { if res.len == 0 {
return return
} }
@ -51,7 +51,7 @@ pub fn (mut b Builder) write_rune(r rune) {
pub fn (mut b Builder) write_runes(runes []rune) { pub fn (mut b Builder) write_runes(runes []rune) {
mut buffer := [5]u8{} mut buffer := [5]u8{}
for r in runes { for r in runes {
res := unsafe { utf32_to_str_no_malloc(u32(r), &buffer[0]) } res := unsafe { utf32_to_str_no_malloc(u32(r), mut &buffer[0]) }
if res.len == 0 { if res.len == 0 {
continue continue
} }

View File

@ -1,7 +1,7 @@
fn main() { fn main() {
unsafe { unsafe {
mut buffer := malloc_noscan(5) mut buffer := malloc_noscan(5)
s := utf32_to_str_no_malloc(77, buffer) s := utf32_to_str_no_malloc(77, mut buffer)
println(s) println(s)
} }
} }