compiler,runtime: implement string to []rune conversion
Commit message by aykevl
Этот коммит содержится в:
родитель
fa5855bff5
коммит
e9f6e51fc8
4 изменённых файлов: 35 добавлений и 1 удалений
|
@ -2458,8 +2458,10 @@ func (c *Compiler) parseConvert(typeFrom, typeTo types.Type, value llvm.Value, p
|
||||||
switch elemType.Kind() {
|
switch elemType.Kind() {
|
||||||
case types.Byte:
|
case types.Byte:
|
||||||
return c.createRuntimeCall("stringToBytes", []llvm.Value{value}, ""), nil
|
return c.createRuntimeCall("stringToBytes", []llvm.Value{value}, ""), nil
|
||||||
|
case types.Rune:
|
||||||
|
return c.createRuntimeCall("stringToRunes", []llvm.Value{value}, ""), nil
|
||||||
default:
|
default:
|
||||||
return llvm.Value{}, c.makeError(pos, "todo: convert from string: "+elemType.String())
|
panic("unexpected type in string to slice conversion")
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -89,6 +89,21 @@ func stringToBytes(x _string) (slice struct {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Convert a string to []rune slice.
|
||||||
|
func stringToRunes(s string) []rune {
|
||||||
|
var n = 0
|
||||||
|
for range s {
|
||||||
|
n++
|
||||||
|
}
|
||||||
|
var r = make([]rune, n)
|
||||||
|
n = 0
|
||||||
|
for _, e := range s {
|
||||||
|
r[n] = e
|
||||||
|
n++
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
// Create a string from a Unicode code point.
|
// Create a string from a Unicode code point.
|
||||||
func stringFromUnicode(x rune) _string {
|
func stringFromUnicode(x rune) _string {
|
||||||
array, length := encodeUTF8(x)
|
array, length := encodeUTF8(x)
|
||||||
|
|
8
testdata/rangestring.go → testdata/string.go
предоставленный
8
testdata/rangestring.go → testdata/string.go
предоставленный
|
@ -6,6 +6,14 @@ func testRangeString() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testStringToRunes() {
|
||||||
|
var s = "abcü¢€𐍈°x"
|
||||||
|
for i,c := range []rune(s) {
|
||||||
|
println(i, c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
testRangeString()
|
testRangeString()
|
||||||
|
testStringToRunes()
|
||||||
}
|
}
|
9
testdata/rangestring.txt → testdata/string.txt
предоставленный
9
testdata/rangestring.txt → testdata/string.txt
предоставленный
|
@ -7,3 +7,12 @@
|
||||||
10 66376
|
10 66376
|
||||||
14 176
|
14 176
|
||||||
16 120
|
16 120
|
||||||
|
0 97
|
||||||
|
1 98
|
||||||
|
2 99
|
||||||
|
3 252
|
||||||
|
4 162
|
||||||
|
5 8364
|
||||||
|
6 66376
|
||||||
|
7 176
|
||||||
|
8 120
|
Загрузка…
Создание таблицы
Сослаться в новой задаче