Skip to content

Commit

Permalink
improve consistency and behavior regarding explicity mutability
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm committed May 24, 2024
1 parent cc72c2e commit 72803ca
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
5 changes: 3 additions & 2 deletions vlib/builtin/rune.v
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,16 @@ pub fn (c rune) repeat(count int) string {
return c.str()
}
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)
}

// bytes converts a rune to an array of bytes
@[manualfree]
pub fn (c rune) bytes() []u8 {
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
}

Expand Down
8 changes: 4 additions & 4 deletions vlib/builtin/utf8.v
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn utf8_char_len(b u8) int {
pub fn utf32_to_str(code u32) string {
unsafe {
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 {
// the buffer was not used at all
free(buffer)
Expand All @@ -22,9 +22,9 @@ pub fn utf32_to_str(code u32) string {
}

@[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 {
len := utf32_decode_to_buffer(code, buf)
len := utf32_decode_to_buffer(code, mut buf)
if len == 0 {
return ''
}
Expand All @@ -34,7 +34,7 @@ pub fn utf32_to_str_no_malloc(code u32, buf &u8) string {
}

@[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 {
icode := int(code) // Prevents doing casts everywhere
mut buffer := &u8(buf)
Expand Down
4 changes: 2 additions & 2 deletions vlib/strings/builder.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn (mut b Builder) write_ptr(ptr &u8, len int) {
@[manualfree]
pub fn (mut b Builder) write_rune(r rune) {
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 {
return
}
Expand All @@ -51,7 +51,7 @@ pub fn (mut b Builder) write_rune(r rune) {
pub fn (mut b Builder) write_runes(runes []rune) {
mut buffer := [5]u8{}
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 {
continue
}
Expand Down

0 comments on commit 72803ca

Please sign in to comment.