Debian Codegraph @

terps/tads/tads3/test/data/packarr.t

raw
1 #include <tads.h>
2 #include <bignum.h>
3 #include <strbuf.h>
4 #include <bytearr.h>
5
6
7 main(args)
8 {
9 testNew('A4/(A10)', 'One', 'Two', 'Three', 'Four', 'Five');
10 testNew('C/l', [0x12345678, 0x11aabbcc]);
11 testNew('A10 X5 A3', 'aaaaaaaaaa', 'BBBBB');
12 testNew('b*', 'byte array test!');
13
14 local arr = new ByteArray(1000);
15 local idx = 1;
16 idx += arr.packBytes(idx, 'C/l', 1, 2, 3, 4, 5);
17 idx += arr.packBytes(idx, 'C/(a0)', 'one', 'two', 'three', 'four', 'five');
18 idx += arr.packBytes(idx, '[A5 C]3!', ['abc', 1], ['def', 2], ['ghijkl']);
19 "pack in place\n";
20 ".. pack hex (full array) = <<arr.unpackBytes(1, 'H*')[1]>>\n";
21 ".. pack hex (up to idx = <<idx-1>>) = <<
22 arr.unpackBytes(1, 'H!' + (idx-1))[1]>>\n";
23
24 idx = 1;
25 local lst = arr.unpackBytes(idx, 'C/l @?');
26 ".. unpack 1 = <<sayList(lst)>>\n";
27
28 idx += lst[lst.length()];
29 lst = arr.unpackBytes(idx, 'C/(a0) @?');
30 ".. unpack 2 = <<sayList(lst)>>\n";
31
32 idx += lst[lst.length()];
33 lst = arr.unpackBytes(idx, '[A5 C]3!');
34 ".. unpack 3 = <<sayList(lst)>>\n";
35 }
36
37 testNew(fmt, [args])
38 {
39 local arr = ByteArray.packBytes(fmt, args...);
40 "format = <<fmt.htmlify()>>, args = <<sayList(args)>>\n";
41 ".. pack hex = <<arr.unpackBytes(1, 'H*')[1]>>\n";
42 ".. unpack = <<sayList(arr.unpackBytes(1, fmt))>>\b";
43 }
44
45 sayList(lst)
46 {
47 "[";
48
49 local idx = 0;
50 foreach (local val in lst)
51 {
52 if (idx++ > 0)
53 ", ";
54
55 switch (dataType(val))
56 {
57 case TypeList:
58 sayList(val);
59 break;
60
61 case TypeSString:
62 "'<<quoteStr(val)>>'";
63 break;
64
65 case TypeInt:
66 "<<val>> (0x<<toString(val, 16)>>)";
67 break;
68
69 case TypeObject:
70 if (val.ofKind(BigNumber))
71 {
72 "<<val>> (bignum)";
73 if (val.getWhole() == val && val.getScale() <= 20)
74 " (0x<<bignumToHex(val)>>)";
75 }
76 else if (val.ofKind(ByteArray))
77 {
78 "'<<quoteStr(toString(val))>>' (ByteArray)";
79 }
80 else
81 "(object)";
82 break;
83
84 default:
85 "<<val>>";
86 break;
87 }
88 }
89
90 "]";
91 }
92
93 quoteStr(str)
94 {
95 return rexReplace('[\'\\\n\t\b]', str, new function(m)
96 {
97 switch (m)
98 {
99 case '\'': return '\\\'';
100 case '\\': return '\\\\';
101 case '\n': return '\\n';
102 case '\r': return '\\r';
103 case '\t': return '\\t';
104 case '\b': return '\\b';
105 case '\^': return '\\^';
106 case '\v': return '\\v';
107 case '\ ': return '\\\ ';
108 default:
109 m = toString(m.toUnicode(1), 16);
110 return m.length() < 2 ? '0x0<<m>>' : '0x<<m>>';
111 }
112 });
113 }
114
115 bignumToHex(val)
116 {
117 local s = new StringBuffer(30);
118 local neg = (val < 0);
119 if (neg)
120 val = -val;
121
122 while (val > 0)
123 {
124 local q = val.divideBy(16);
125 s.append(toString(toInteger(q[2]), 16));
126 val = q[1];
127 }
128
129 if (s.length() == 0)
130 s.append('0');
131 if (neg)
132 s.append('-');
133
134 for (local i = 1, local j = s.length() ; i < j ; ++i, --j)
135 {
136 local tmp = s[i];
137 s[i] = s[j];
138 s[j] = tmp;
139 }
140
141 return toString(s);
142 }