From: Ken Thompson Date: Wed, 27 May 2009 22:38:02 +0000 (-0700) Subject: string([]int) conversion X-Git-Tag: weekly.2009-11-06~1526 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=64c3fe05bf16ea9107be5a7e4257db86addb6d08;p=gostls13.git string([]int) conversion R=r OCL=29466 CL=29466 --- diff --git a/src/cmd/gc/builtin.c.boot b/src/cmd/gc/builtin.c.boot index 97574f7103..6ee50e035e 100644 --- a/src/cmd/gc/builtin.c.boot +++ b/src/cmd/gc/builtin.c.boot @@ -20,6 +20,7 @@ char *sysimport = "func sys.indexstring (? string, ? int) (? uint8)\n" "func sys.intstring (? int64) (? string)\n" "func sys.arraystring (? []uint8) (? string)\n" + "func sys.arraystringi (? []int) (? string)\n" "func sys.stringiter (? string, ? int) (? int)\n" "func sys.stringiter2 (? string, ? int) (retk int, retv int)\n" "func sys.ifaceI2E (iface any) (ret any)\n" diff --git a/src/cmd/gc/sys.go b/src/cmd/gc/sys.go index 270ca04da4..3a278d9ff9 100644 --- a/src/cmd/gc/sys.go +++ b/src/cmd/gc/sys.go @@ -29,6 +29,7 @@ func slicestring(string, int, int) string; func indexstring(string, int) byte; func intstring(int64) string; func arraystring([]byte) string; +func arraystringi([]int) string; func stringiter(string, int) int; func stringiter2(string, int) (retk int, retv int); diff --git a/src/cmd/gc/walk.c b/src/cmd/gc/walk.c index e47be30ff4..96cd400541 100644 --- a/src/cmd/gc/walk.c +++ b/src/cmd/gc/walk.c @@ -1293,6 +1293,7 @@ walkconv(Node *n) indir(n, stringop(n, Erv)); return; } + // can convert []byte and *[10]byte if((isptr[et] && isfixedarray(l->type->type) && istype(l->type->type->type, TUINT8)) || (isslice(l->type) && istype(l->type->type, TUINT8))) { @@ -1300,6 +1301,14 @@ walkconv(Node *n) indir(n, stringop(n, Erv)); return; } + + // can convert []int and *[10]int + if((isptr[et] && isfixedarray(l->type->type) && istype(l->type->type->type, TINT)) + || (isslice(l->type) && istype(l->type->type, TINT))) { + n->op = OARRAY; + indir(n, stringop(n, Erv)); + return; + } } // convert dynamic to static generated by ONEW/OMAKE @@ -2450,8 +2459,17 @@ stringop(Node *n, int top) break; case OARRAY: - // arraystring([]byte) string; r = n->left; + if(r->type != T && r->type->type != T) { + if(istype(r->type->type, TINT) || istype(r->type->type->type, TINT)) { + // arraystring([]byte) string; + on = syslook("arraystringi", 0); + r = nod(OCALL, on, r); + break; + } + } + + // arraystring([]byte) string; on = syslook("arraystring", 0); r = nod(OCALL, on, r); break; diff --git a/src/runtime/string.c b/src/runtime/string.c index d7393ef6ed..04cf06bc3c 100644 --- a/src/runtime/string.c +++ b/src/runtime/string.c @@ -181,6 +181,28 @@ sys·arraystring(Array b, String s) FLUSH(&s); } +void +sys·arraystringi(Array b, String s) +{ + int32 siz, i; + int32 *a; + byte dum[8]; + + a = (int32*)b.array; + siz = 0; + for(i=0; i