diff options
| author | Russ Cox <rsc@golang.org> | 2010-02-25 15:11:07 -0800 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2010-02-25 15:11:07 -0800 |
| commit | 3910161307f79fd821148652fb2a77872e7efd52 (patch) | |
| tree | ed1675a302b1008c7a13de6f750f25e73a3b8d9a /src/pkg | |
| parent | b86c0b0c4a69aaca1bd748fb2969f90cb2a28310 (diff) | |
| download | go-3910161307f79fd821148652fb2a77872e7efd52.tar.xz | |
gc: implement []int(string) and []byte(string)
R=ken2
CC=golang-dev
https://golang.org/cl/224060
Diffstat (limited to 'src/pkg')
| -rw-r--r-- | src/pkg/runtime/string.cgo | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/pkg/runtime/string.cgo b/src/pkg/runtime/string.cgo index 4c85766c2f..2cb518c6f8 100644 --- a/src/pkg/runtime/string.cgo +++ b/src/pkg/runtime/string.cgo @@ -4,6 +4,7 @@ package runtime #include "runtime.h" +#include "malloc.h" String emptystring; @@ -210,6 +211,12 @@ func slicebytetostring(b Slice) (s String) { mcpy(s.str, b.array, s.len); } +func stringtoslicebyte(s String) (b Slice) { + b.array = mallocgc(s.len, RefNoPointers, 1, 1); + b.len = s.len; + b.cap = s.len; + mcpy(b.array, s.str, s.len); +} func sliceinttostring(b Slice) (s String) { int32 siz1, siz2, i; @@ -233,6 +240,30 @@ func sliceinttostring(b Slice) (s String) { s.len = siz2; } +func stringtosliceint(s String) (b Slice) { + int32 n; + int32 dum, *r; + uint8 *p, *ep; + + // two passes. + // unlike sliceinttostring, no race because strings are immutable. + p = s.str; + ep = s.str+s.len; + n = 0; + while(p < ep) { + p += charntorune(&dum, p, ep-p); + n++; + } + + b.array = mallocgc(n*sizeof(r[0]), RefNoPointers, 1, 1); + b.len = n; + b.cap = n; + p = s.str; + r = (int32*)b.array; + while(p < ep) + p += charntorune(r++, p, ep-p); +} + enum { Runeself = 0x80, |
