mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-17 03:03:27 +00:00
compiler,runtime: implement string to []rune conversion
Commit message by aykevl
This commit is contained in:
committed by
Ayke van Laethem
parent
fa5855bff5
commit
e9f6e51fc8
@@ -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)
|
||||||
|
|||||||
@@ -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()
|
||||||
}
|
}
|
||||||
@@ -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
|
||||||
Reference in New Issue
Block a user