mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-03 10:37:46 +00:00
compiler,runtime: implement []rune to string conversion
This is used by a few packages in the standard library, at least compress/gzip and regexp/syntax.
This commit is contained in:
committed by
Ron Evans
parent
fea56d4164
commit
fd3309afa8
@@ -89,6 +89,30 @@ func stringToBytes(x _string) (slice struct {
|
||||
return
|
||||
}
|
||||
|
||||
// Convert a []rune slice to a string.
|
||||
func stringFromRunes(runeSlice []rune) (s _string) {
|
||||
// Count the number of characters that will be in the string.
|
||||
for _, r := range runeSlice {
|
||||
_, numBytes := encodeUTF8(r)
|
||||
s.length += numBytes
|
||||
}
|
||||
|
||||
// Allocate memory for the string.
|
||||
s.ptr = (*byte)(alloc(s.length))
|
||||
|
||||
// Encode runes to UTF-8 and store the resulting bytes in the string.
|
||||
index := uintptr(0)
|
||||
for _, r := range runeSlice {
|
||||
array, numBytes := encodeUTF8(r)
|
||||
for _, c := range array[:numBytes] {
|
||||
*(*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(s.ptr)) + index)) = c
|
||||
index++
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Convert a string to []rune slice.
|
||||
func stringToRunes(s string) []rune {
|
||||
var n = 0
|
||||
|
||||
Reference in New Issue
Block a user