"func @\"\".concatstrings (? *[32]byte, ? []string) (? string)\n"
"func @\"\".cmpstring (? string, ? string) (? int)\n"
"func @\"\".eqstring (? string, ? string) (? bool)\n"
- "func @\"\".intstring (? int64) (? string)\n"
+ "func @\"\".intstring (? *[4]byte, ? int64) (? string)\n"
"func @\"\".slicebytetostring (? *[32]byte, ? []byte) (? string)\n"
"func @\"\".slicebytetostringtmp (? []byte) (? string)\n"
"func @\"\".slicerunetostring (? []rune) (? string)\n"
break;
case OARRAYBYTESTR:
+ case ORUNESTR:
n->escloopdepth = e->loopdepth;
n->esc = EscNone; // until proven otherwise
e->noesc = list(e->noesc, n);
case ONEW:
case OCLOSURE:
case OCALLPART:
+ case ORUNESTR:
escflows(e, dst, src);
break;
case ONEW:
case OCLOSURE:
case OCALLPART:
+ case ORUNESTR:
if(leaks) {
src->esc = EscHeap;
if(debug['m'])
func cmpstring(string, string) int
func eqstring(string, string) bool
-func intstring(int64) string
+func intstring(*[4]byte, int64) string
func slicebytetostring(*[32]byte, []byte) string
func slicebytetostringtmp([]byte) string
func slicerunetostring([]rune) string
goto ret;
case ORUNESTR:
- // sys_intstring(v)
- n = mkcall("intstring", n->type, init,
- conv(n->left, types[TINT64]));
+ a = nodnil();
+ if(n->esc == EscNone) {
+ t = aindex(nodintconst(4), types[TUINT8]);
+ var = temp(t);
+ a = nod(OADDR, var, N);
+ }
+ // intstring(*[4]byte, rune)
+ n = mkcall("intstring", n->type, init, a, conv(n->left, types[TINT64]));
goto ret;
case OARRAYBYTESTR:
len int
}
-func intstring(v int64) string {
- s, b := rawstring(4)
+func intstring(buf *[4]byte, v int64) string {
+ var s string
+ var b []byte
+ if buf != nil {
+ b = buf[:]
+ s = slicebytetostringtmp(b)
+ } else {
+ s, b = rawstring(4)
+ }
n := runetochar(b, rune(v))
return s[:n]
}
t.Fatalf("want: '%v', got '%v'", want, s)
}
}
+
+func TestIntString(t *testing.T) {
+ // Non-escaping result of intstring.
+ s := ""
+ for i := 0; i < 4; i++ {
+ s += string(i+'0') + string(i+'0'+1)
+ }
+ if want := "01122334"; s != want {
+ t.Fatalf("want '%v', got '%v'", want, s)
+ }
+
+ // Escaping result of intstring.
+ var a [4]string
+ for i := 0; i < 4; i++ {
+ a[i] = string(i + '0')
+ }
+ s = a[0] + a[1] + a[2] + a[3]
+ if want := "0123"; s != want {
+ t.Fatalf("want '%v', got '%v'", want, s)
+ }
+}
+
+func TestIntStringAllocs(t *testing.T) {
+ unknown := '0'
+ n := testing.AllocsPerRun(1000, func() {
+ s1 := string(unknown)
+ s2 := string(unknown + 1)
+ if s1 == s2 {
+ t.Fatalf("bad")
+ }
+ })
+ if n != 0 {
+ t.Fatalf("want 0 allocs, got %v", n)
+ }
+}
s2 := s[0:1]
sink = s2
}
+
+func intstring0() bool {
+ // string does not escape
+ x := '0'
+ s := string(x) // ERROR "string\(x\) does not escape"
+ return s == "0"
+}
+
+func intstring1() string {
+ // string does not escape, but the buffer does
+ x := '0'
+ s := string(x) // ERROR "string\(x\) escapes to heap"
+ return s
+}
+
+func intstring2() {
+ // string escapes to heap
+ x := '0'
+ s := string(x) // ERROR "string\(x\) escapes to heap" "moved to heap: s"
+ sink = &s // ERROR "&s escapes to heap"
+}
s2 := s[0:1]
sink = s2
}
+
+func intstring0() bool {
+ // string does not escape
+ x := '0'
+ s := string(x) // ERROR "string\(x\) does not escape"
+ return s == "0"
+}
+
+func intstring1() string {
+ // string does not escape, but the buffer does
+ x := '0'
+ s := string(x) // ERROR "string\(x\) escapes to heap"
+ return s
+}
+
+func intstring2() {
+ // string escapes to heap
+ x := '0'
+ s := string(x) // ERROR "string\(x\) escapes to heap" "moved to heap: s"
+ sink = &s // ERROR "&s escapes to heap"
+}