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:
Ayke van Laethem
2019-08-11 14:35:34 +02:00
committed by Ron Evans
parent fea56d4164
commit fd3309afa8
4 changed files with 33 additions and 1 deletions
+24
View File
@@ -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