aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorscriptonist <[email protected]>2019-06-11 21:56:28 +0530
committerAyke van Laethem <[email protected]>2019-06-19 01:17:21 +0200
commite9f6e51fc8bda1824adc403494fb79ce710a006c (patch)
tree2cbd87977fcabf508a27e310835573c743984481
parentfa5855bff5142632b889e4d58cc0386141436b03 (diff)
downloadtinygo-e9f6e51fc8bda1824adc403494fb79ce710a006c.tar.gz
tinygo-e9f6e51fc8bda1824adc403494fb79ce710a006c.zip
compiler,runtime: implement string to []rune conversion
Commit message by aykevl
-rw-r--r--compiler/compiler.go4
-rw-r--r--src/runtime/string.go15
-rw-r--r--testdata/string.go (renamed from testdata/rangestring.go)8
-rw-r--r--testdata/string.txt (renamed from testdata/rangestring.txt)9
4 files changed, 35 insertions, 1 deletions
diff --git a/compiler/compiler.go b/compiler/compiler.go
index 93c3f496a..00e008fbe 100644
--- a/compiler/compiler.go
+++ b/compiler/compiler.go
@@ -2458,8 +2458,10 @@ func (c *Compiler) parseConvert(typeFrom, typeTo types.Type, value llvm.Value, p
switch elemType.Kind() {
case types.Byte:
return c.createRuntimeCall("stringToBytes", []llvm.Value{value}, ""), nil
+ case types.Rune:
+ return c.createRuntimeCall("stringToRunes", []llvm.Value{value}, ""), nil
default:
- return llvm.Value{}, c.makeError(pos, "todo: convert from string: "+elemType.String())
+ panic("unexpected type in string to slice conversion")
}
default:
diff --git a/src/runtime/string.go b/src/runtime/string.go
index 780c97f4b..6d9c38753 100644
--- a/src/runtime/string.go
+++ b/src/runtime/string.go
@@ -89,6 +89,21 @@ func stringToBytes(x _string) (slice struct {
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.
func stringFromUnicode(x rune) _string {
array, length := encodeUTF8(x)
diff --git a/testdata/rangestring.go b/testdata/string.go
index 32401c2cd..f0aecd07e 100644
--- a/testdata/rangestring.go
+++ b/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() {
testRangeString()
+ testStringToRunes()
}
diff --git a/testdata/rangestring.txt b/testdata/string.txt
index e3318b416..b5dca1981 100644
--- a/testdata/rangestring.txt
+++ b/testdata/string.txt
@@ -7,3 +7,12 @@
10 66376
14 176
16 120
+0 97
+1 98
+2 99
+3 252
+4 162
+5 8364
+6 66376
+7 176
+8 120