1
0
mirror of https://github.com/golang/go synced 2024-11-25 22:17:58 -07:00

string([]int) conversion

R=r
OCL=29466
CL=29466
This commit is contained in:
Ken Thompson 2009-05-27 15:38:02 -07:00
parent 18890eebbf
commit 64c3fe05bf
4 changed files with 43 additions and 1 deletions

View File

@ -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"

View File

@ -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);

View File

@ -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;

View File

@ -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<b.nel; i++) {
siz += runetochar(dum, a[i]);
}
s = gostringsize(siz);
siz = 0;
for(i=0; i<b.nel; i++) {
siz += runetochar(s.str+siz, a[i]);
}
FLUSH(&s);
}
enum
{
Runeself = 0x80,