Differences From
Artifact [f661c3d77e]:
145 145 buf = buf + 1
146 146 end
147 147 end
148 148
149 149 terra m.b32str(a: lib.mem.ptr(uint64))
150 150
151 151 end
152 +
153 +terra m.decstr(val: intptr, buf: &int8): rawstring
154 +-- works backwards to avoid copies. log10(2^64) ≈ 19.2 and we
155 +-- need a byte for NUL so buf MUST point to THE END OF a buffer
156 +-- at least 21 bytes long
157 + @buf = 0
158 + if val > 0 then while val > 0 do
159 + buf = buf - 1
160 + var dgt = val % 10
161 + val = val / 10
162 + @buf = 0x30 + dgt
163 + end else
164 + buf = buf - 1
165 + @buf = 0x30
166 + end
167 + return buf
168 +end
169 +
170 +terra m.decstr_friendly(val: intptr, buf: &int8): rawstring
171 +-- as above except needs size-28 buffers, on account of all the commas
172 + @buf = 0
173 + var dgtct: uint8 = 0
174 + if val > 0 then while val > 0 do
175 + buf = buf - 1
176 + var dgt = val % 10
177 + val = val / 10
178 + @buf = 0x30 + dgt
179 + if dgtct == 2 and val > 0 then
180 + buf = buf - 1 @buf = @','
181 + dgtct = 0
182 + else dgtct = dgtct + 1 end
183 + end else
184 + buf = buf - 1
185 + @buf = 0x30
186 + end
187 + return buf
188 +end
152 189
153 190 return m