-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jm_nstrings.spin2
216 lines (159 loc) · 7.66 KB
/
jm_nstrings.spin2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
'' =================================================================================================
''
'' File....... jm_nstrings.spin2
'' Purpose.... Convert numbers to strings
'' Authors.... Jon McPhalen
'' -- Copyright (c) 2020 Jon McPhalen
'' -- see below for terms of use
'' E-mail..... jon.mcphalen@gmail.com
'' Started....
'' Updated.... 15 MAR 2020
''
'' =================================================================================================
con
NBUF_SIZE = 33
PBUF_SIZE = 81
var
byte nbuf[NBUF_SIZE] ' number conversions
byte pbuf[PBUF_SIZE] ' padded strings
PUB null()
'' This is not a top level object
PUB fmt_number(value, radix, digits, width, pad) : p_str
'' Return pointer to string of value converted to number in padded field
'' -- value is converted using radix
'' -- digits is max number of digits to use
'' -- width is width of final fields (max)
'' -- pad is character used to pad final field (if needed)
case radix
10 : p_str := padstr(itoa(value, 10, digits), width, pad)
99 : p_str := padstr(dpdec(value, digits), width, pad)
16 : p_str := padstr(itoa(value, 16, digits), width, pad)
08 : p_str := padstr(itoa(value, 8, digits), width, pad)
04 : p_str := padstr(itoa(value, 4, digits), width, pad)
02 : p_str := padstr(itoa(value, 2, digits), width, pad)
other : p_str := string("?")
PUB dec(value, digits) : p_str | sign, len
'' Convert decimal value to string
'' -- digits is 0 (auto size) to 10
p_str := itoa(value, 10, digits)
PUB dpdec(value, dp) : p_str | len, byte scratch[12]
'' Convert value to string with decimal point
'' -- dp is digits after decimal point
'' -- returns pointer to updated fp string
'' -- modifies original string
'' -- return pointer to converted string
' p_str := dec(value, 0) ' convert to string
p_str := itoa(value, 10, 0)
if (dp <= 0) ' abort if no decimal point
return p_str
len := strsize(p_str) ' digits
bytefill(@scratch, 0, 12) ' clear scratch buffer
if (value < 0) ' ignore "-" if present
++p_str
--len
if (len < (dp+1)) ' insert 0s?
bytemove(@scratch, p_str, len) ' move digits to scratch buffer
bytefill(p_str, "0", dp+2-len) ' pad string with 0s
bytemove(p_str+dp+2-len, @scratch, len+1) ' move digits back
byte[p_str+1] := "." ' insert dpoint
else
bytemove(@scratch, p_str+len-dp, dp) ' move decimal part to buffer
byte[p_str+len-dp] := "." ' insert dpoint
bytemove(p_str+len-dp+1, @scratch, dp+1) ' move decimal part back
if (value < 0) ' fix pointer for negative #s
--p_str
PUB itoa(value, radix, digits) : p_str | sign, len, d
'' Convert to value string
'' -- supports radix 10, 2, 4, 8, and 16
'' -- digits is 0 (auto size) to limit for long using radix
bytefill(@nbuf, 0, NBUF_SIZE) ' clear buffer
p_str := @nbuf ' point to it
case radix ' fix digits
02 : digits := 0 #> digits <# 32
04 : digits := 0 #> digits <# 16
08 : digits := 0 #> digits <# 11
10 : digits := 0 #> digits <# 10
16 : digits := 0 #> digits <# 8
other :
byte[p_str] := 0
return
if ((radix == 10) && (value < 0)) ' deal with negative decimals
if (value == negx)
sign := 2
value := posx
else
sign := 1
value := -value
else
sign := 0
len := 0
repeat
d := value +// radix ' get digit (1s column)
byte[p_str++] := (d < 10) ? d + "0" : d - 10 + "A" ' convert to ASCII
value +/= radix ' remove digit
if (digits) ' length limited?
if (++len == digits) ' check size
quit
else
if (value == 0) ' done?
quit
if (sign)
byte[p_str++] := "-" ' add sign if needed
if (sign == 2)
nbuf[0] := "8" ' fix negx if needed
byte[p_str++] := 0 ' terminate string
return revstr(@nbuf) ' fix order (reverse)
PUB revstr(p_str) : result | first, len, last
result := first := p_str ' start
len := strsize(p_str) ' length
last := first + len - 1 ' end
repeat (len >> 1) ' reverse them
byte[first++], byte[last--] := byte[last], byte[first]
PUB padstr(p_str, width, padchar) : p_pad | len
'' Pad string with padchar character
'' -- positive width uses left pad, negative field width uses right pad
'' -- truncate if string len > width
'' -- input string is not modified
'' -- returns pointer to padded string
bytefill(@pbuf, 0, PBUF_SIZE) ' clear padded buffer
len := strsize(p_str) ' get length of input
width := -PBUF_SIZE+1 #> width <# PBUF_SIZE-1 ' constrain to buffer size
if (width > 0) ' right-justify in padded field
if (width > len)
bytefill(@pbuf, padchar, width-len)
bytemove(@pbuf+width-len, p_str, len)
p_pad := @pbuf
else
bytemove(@pbuf, p_str+len-width, width) ' truncate to right-most characters
p_pad := @pbuf
elseif (width < 0) ' left-justify in padded field
width := -width
if (width > len)
bytemove(@pbuf, p_str, len)
bytefill(@pbuf+len, padchar, width-len)
p_pad := @pbuf
else
bytemove(@pbuf, p_str, width) ' truncate to leftmost characters
p_pad := @pbuf
else
p_pad := p_str
CON { license }
{{
MIT License
Copyright (c) 2020 Jon McPhalen
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
}}