ref: d0909089b28d28ea54c6451d94434fda8b0c1780
parent: 72d08816abaa2935e8055d9cdfd73b59df5f9bd3
author: cinap_lenrek <[email protected]>
date: Sun Oct 17 14:45:05 EDT 2021
libaml: implement ToString instruction
--- a/sys/src/libaml/aml.c
+++ b/sys/src/libaml/aml.c
@@ -152,7 +152,7 @@
Oif, Oelse, Owhile, Obreak, Oret, Ocall,
Ostore, Oderef, Osize, Oref, Ocref, Ocat,
Oacq, Osignal, Orel, Ostall, Osleep, Oload, Ounload,
- Otodec, Otohex, Otoint,
+ Otodec, Otohex, Otoint, Otostr,
};
static Op optab[];
@@ -2012,6 +2012,27 @@
return r;
}
+static void*
+evaltostr(void)
+{
+ char *buf, *r, *e;
+ int len;
+
+ buf = FP->arg[0];
+ if(buf == nil || (TAG(buf) != 'b' && TAG(buf) != 's'))
+ buf = copy('b', buf);
+ len = ival(FP->arg[1]);
+ if(~len == 0 || len > SIZE(buf))
+ len = SIZE(buf);
+ if(len > 0 && (e = memchr(buf, 0, len)) != nil)
+ len = e - buf;
+ r = mk('s', len+1);
+ r[len] = '\0';
+ memmove(r, buf, len);
+ store(r, FP->arg[2]);
+ return r;
+}
+
static Op optab[] = {
[Obad] "", "", evalbad,
[Onop] "Noop", "", evalnop,
@@ -2110,6 +2131,7 @@
[Otodec] "ToDecimalString", "*@", evalconv,
[Otohex] "ToHexString", "*@", evalconv,
[Otoint] "ToInteger", "*@", evalconv,
+ [Otostr] "ToString", "*i@", evaltostr,
};
static uchar octab1[] = {
@@ -2132,7 +2154,7 @@
/* 80 */ Onot, Olbit, Orbit, Oderef, Obad, Omod, Obad, Osize,
/* 88 */ Oindex, Omatch, Ocfld4, Ocfld2, Ocfld1, Ocfld0, Obad, Ocfld8,
/* 90 */ Oland, Olor, Olnot, Oleq, Olgt, Ollt, Obad, Otodec,
-/* 98 */ Otohex, Otoint, Obad, Obad, Obad, Obad, Obad, Obad,
+/* 98 */ Otohex, Otoint, Obad, Obad, Otostr, Obad, Obad, Obad,
/* A0 */ Oif, Oelse, Owhile, Onop, Oret, Obreak, Obad, Obad,
/* A8 */ Obad, Obad, Obad, Obad, Obad, Obad, Obad, Obad,
/* B0 */ Obad, Obad, Obad, Obad, Obad, Obad, Obad, Obad,