Commit 64c3fe05 authored by Ken Thompson's avatar Ken Thompson

string([]int) conversion

R=r
OCL=29466
CL=29466
parent 18890eeb
......@@ -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"
......
......@@ -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);
......
......@@ -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;
......
......@@ -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,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment