forked from grandideastudio/jtagulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jm_strings.spin
executable file
·603 lines (428 loc) · 16.7 KB
/
jm_strings.spin
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
'' =================================================================================================
''
'' File....... jm_strings.spin
'' Purpose.... Miscellaneous string methods
'' Author..... Jon "JonnyMac" McPhalen
'' Copyright (c) 2011-2015 Jon McPhalen
'' -- see below for terms of use
'' E-mail.....
'' Started....
'' Updated.... 25 APR 2015
''
'' =================================================================================================
'' Modified by Joe Grand for JTAGulator, commented out unused methods to save space and fix to is_hex
{pub ucstr(p_str)
'' Converts z-string at pntr to upper case
repeat strsize(p_str)
byte[p_str] := upper(byte[p_str])
p_str++
}
pub upper(c)
'' Convert c to uppercase
'' -- does not modify non-alphas
if ((c => "a") and (c =< "z"))
c -= 32
return c
{pub lcstr(p_str)
'' Converts z-string at pntr to lower case
repeat strsize(p_str)
byte[p_str] := lower(byte[p_str])
p_str++
}
{pub lower(c)
'' Convert c to lowercase
'' -- does not modify non-alphas
if ((c => "A") and (c =< "Z"))
c += 32
return c
}
{pub is_alpha(c)
'' Returns true if c is alpha character
if ((c => "a") and (c =< "z"))
return true
elseif ((c => "A") and (c =< "Z"))
return true
else
return false
}
pub is_digit(c)
'' Returns true if c is digit character
if ((c => "0") and (c =< "9"))
return true
else
return false
{pub is_alphanum(c)
'' Returns true if c is alpha/numeric character
if ((c => "a") and (c =< "z"))
return true
elseif ((c => "A") and (c =< "Z"))
return true
elseif ((c => "0") and (c =< "9"))
return true
else
return false
}
{pub is_space(c)
'' Returns true if c a standard whitespace character
return (lookdown(c : $20, $09, $0A, $0B, $0C, $0D) > 0)
}
{pub is_decimal(p_str) | c
'' Returns true if string is decimal format
'' -- separator okay
repeat strsize(p_str)
c := byte[p_str++]
ifnot (is_digit(c))
ifnot (c == "_")
return false
return true
}
{pub is_binary(p_str) | c
'' Returns true if string is binary format
'' -- excludes format indicator
'' -- separator okay
repeat strsize(p_str)
c := byte[p_str++]
ifnot ((c => "0") and (c =< "1"))
ifnot (c == "_")
return false
return true
}
pub is_hex(p_str) | c
'' Returns true if string is hexadecimal format
'' -- excludes format indicator
repeat strsize(p_str)
c := upper(byte[p_str++])
ifnot (is_digit(c))
ifnot ((c => "A") and (c =< "F"))
ifnot (c == "_")
return false
return true
{pub is_number(p_str)
'' Returns true if string is number in known format (dec, bin, or hex)
if (byte[p_str] == "$") ' hex indicator?
return is_hex(p_str+1)
if (byte[p_str] == "%") ' binary indicator?
return is_binary(p_str+1)
if lookdown(byte[p_str] : "+-", "0".."9") ' sign or digit?
return is_decimal(p_str+1)
return false
}
{pub strncmp(p_str1, p_str2, n) | match
'' Compares n characters of str2 with str1
'' -- p_str1 and p_str2 are pointers to strings (or byte arrays)
'' -- 0 if str1 == str2, 1 if str1 > str2, -1 if str1 < str2
match := 0
if (n > 0)
repeat n
if (byte[p_str1] > byte[p_str2])
++match
quit
elseif (byte[p_str1] < byte[p_str2])
--match
quit
else
++p_str1
++p_str2
return match
}
{pub instr(p_str1, p_str2) | len1, len2, pos, idx
'' Returns position of str2 in str1
'' -- p_str1 is pointer to z-string to search
'' -- p_str2 is pointer to z-string to look for in str1
'' -- if str2 not in str1 returns -1
len1 := strsize(p_str1)
len2 := strsize(p_str2)
pos := -1
idx := 0
if (len1 >= len2)
repeat (len1 - len2 + 1)
if (byte[p_str1] == 0)
quit
else
if (strncmp(p_str1++, p_str2, len2) == 0)
pos := idx
quit
else
++idx
return pos
}
{pub first(c, p_str) | pos ' changed from cinstr(p_str, c)
'' Returns first position of character c in string at p_str
'' -- returns -1 if not found
pos := 0
repeat strsize(p_str) ' loop through string chars
if (byte[p_str][pos] == c) ' if match
return pos ' return position
pos += 1
return -1 ' return not found
}
{pub last(c, p_str) | pos
'' Returns last position of character c in string at p_str
'' -- returns -1 if not found
pos := strsize(p_str)
repeat strsize(p_str) ' loop through string chars
if (byte[p_str][pos] == c) ' if match
return pos ' return position
pos -= 1
return -1 ' return not found
}
{pub fields(p_str, sepchar) | len, fc
'' Returns number of fields in string
'' -- fields are separated by sepchar
len := strsize(p_str) ' get length of string
if (len == 0) ' if no length
fc := 0 ' no fields
else
fc := 1 ' at least 1 for any valid string
repeat len ' iterate through string
if (byte[p_str++] == sepchar) ' if separator found
++fc ' increment field count
return fc
}
{pub field_pntr(p_str, n, sepchar) | c
'' Returns pointer to field following nth appearance of sepchar
'' -- p_str is pointer to source string
'' -- n is the field number
'' -- sepchar is the field seperating character
repeat while (n > 0)
c := byte[p_str++]
if (c == sepchar)
n -= 1
elseif (c == 0) ' end of string
return -1 ' report sepchar not found
return p_str
}
{pub copy_field(p_dest, p_src, sepchar, cmax) | c
'' Copies field starting at p_src to p_dest
'' -- stops at sepchar or 0
'' -- cmax is maximum length of string
repeat cmax
c := byte[p_src++] ' get character from source
if (c == sepchar) ' if separator
c := 0 ' convert to 0
byte[p_dest++] := c ' update destination
if (c == 0)
quit
if (c <> 0) ' if field too long
byte[p_dest] := 0 ' truncate
}
{pub replace(p_str, tc, nc) | c
'' Replaces target character (tc) in string at p_str witn new character (nc)
repeat strsize(p_str)
if (byte[p_str] == tc)
byte[p_str] := nc
++p_str
}
{pub left(p_dest, p_src, n)
'' Copies left n characters from src string to dest string
'' -- p_dest and p_src are pointers to string buffers
n <#= strsize(p_src)
bytemove(p_dest, p_src, n)
byte[p_dest][n] := 0 ' terminate new p_dest
}
{pub right(p_dest, p_src, n) | sl
'' Copies right n characters from src string to dest string
'' -- p_dest and p_src are pointers to string buffers
sl := strsize(p_src) ' length of source
bytemove(p_dest, p_src+(sl-n), n+1) ' include terminating 0
}
{pub mid(p_dest, p_src, start, n)
'' Copies middle n characters from src string to dest string
'' -- p_dest and p_src are pointers to string buffers
'' -- start is zero indexed
p_src += start ' bump start address
n <#= strsize(p_src) ' keep size legal
bytemove(p_dest, p_src, n)
byte[p_dest][n] := 0 ' terminate updated dest string
}
{pub ltrim(p_str) : idx
'' Trims leading whitespace(s) from string at p_str
repeat
ifnot (is_space(byte[p_str][idx]))
quit
else
++idx
if (idx > 0) ' if spaces
bytemove(p_str, p_str+idx, strsize(p_str)-idx+1) ' move sub-string + 0 left
}
{pub rtrim(p_str) | idx, end
'' Trims trailing whitespaces from string at p_str
idx := end := strsize(p_str) - 1 ' get index of last char in string
repeat
ifnot (is_space(byte[p_str][idx]))
quit
else
--idx
if (idx < end) ' if spaces at end
byte[p_str][idx+1] := 0 ' truncate
}
{pub trim(p_str)
'' Trims leading and trailing spaces from string at p_str
rtrim(p_str)
ltrim(p_str)
}
{pub index(p_str, p_list, count) | idx
'' Returns index of string (at p_str) in list of strings (at p_list)
'' -- count is number of strings in list
idx := 0
repeat count
if (strcomp(p_str, p_list)) ' if match
return idx ' return index
else
++idx ' next index
p_list += strsize(p_list) + 1 ' skip over last string
return -1 ' no match in list
}
{pub pntr(idx, p_list)
'' Returns to pointer to idx'th string in list (at p_list)
'' -- string may be variable length
repeat idx
p_list += strsize(p_list) + 1 ' skip current string
return p_list
}
con
{ --------------------- }
{ Numeric conversions }
{ --------------------- }
{pub asc2val(p_str) | c
'' Returns value of numeric string
'' -- p_str is pointer to string
'' -- binary (%) and hex ($) must be indicated
repeat
c := byte[p_str]
case c
" ": ' skip leading space(s)
p_str++
"+", "-", "0".."9": ' found decimal value
return asc2dec(p_str, 11)
"%": ' found binary value
return bin2dec(p_str, 32)
"$": ' found hex value
return hex2dec(p_str, 8)
other: ' abort on bad character
return 0
}
{pub asc2dec(p_str, n) | c, value, sign
'' Returns signed value from decimal string
'' -- p_str is pointer to decimal string
'' -- n is maximum number of digits to process
if (n < 1) ' if bogus, bail out
return 0
value := 0 ' initialize value
sign := 1 ' assume positive
' trim leading chars (spaces / sign)
repeat
c := byte[p_str]
case c
" ": ' skip leading space(s)
p_str++
"0".."9": ' found #s, extract value
quit
"+":
p_str++ ' skip sign, extract value
quit
"-":
sign := -1 ' value is negative
p_str++ ' skip sign, extract value
quit
other: ' abort on bad character
return 0
' extract numeric character
' -- can contain comma and underscore separators
n <#= 10 ' limit to 10 digits
repeat while (n > 0)
c := byte[p_str++]
case c
"0".."9": ' digit?
value := (value * 10) + (c - "0") ' update value
n--
"_":
{ ignore }
other:
quit
return sign * value
}
{pub bin2dec(p_str, n) | c, value
'' Returns value from {indicated} binary string
'' -- p_str is pointer to binary string
'' -- n is maximum number of digits to process
if (n < 1) ' if bogus, bail out
return 0
repeat
c := byte[p_str]
case c
" ": ' skip leading space(s)
p_str++
"%": ' found indicator
p_str++ ' move to value
quit
"0".."1": ' found value
quit
other: ' abort on bad character
return 0
value := 0
n <#= 32 ' limit chars in value
repeat while (n)
c := byte[p_str++] ' get next character
case c
"0".."1": ' binary digit?
value := (value << 1) | (c - "0") ' update value
--n ' dec digits count
"_":
' skip
other:
quit
return value
}
pub hex2dec(p_str, n) | c, value
'' Returns value from {indicated} hex string
'' -- p_str is pointer to binary string
'' -- n is maximum number of digits to process
if (n < 1) ' if bogus, bail out
return 0
repeat
c := upper(byte[p_str])
case c
" ": ' skip leading space(s)
p_str++
"$": ' found indicator
p_str++ ' move to value
quit
"0".."9", "A".."F": ' found value
quit
other: ' abort on bad character
return 0
value := 0
n <#= 8 ' limit field width
repeat while (n)
c := upper(byte[p_str++])
case c
"0".."9": ' digit?
value := (value << 4) | (c - "0") ' update value
--n ' dec digits count
"A".."F": ' hex digit?
value := (value << 4) | (c - "A" + 10)
--n
"_":
{ skip }
other:
quit
return value
dat { license }
{{
Terms of Use: MIT License
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 NON-INFRINGEMENT. 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.
}}