-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddress.scm
77 lines (62 loc) · 1.98 KB
/
address.scm
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
;;; Copyright 2017 by Christian Jaeger <[email protected]>
;;; This file is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License (GPL) as published
;;; by the Free Software Foundation, either version 2 of the License, or
;;; (at your option) any later version.
(require easy
jclass
test
(test-lib-1 %try))
(export maybe-parse-common-hex-string
(method natural0.common-hex-string)
(jclass address32)
maybe-address32*
address32*)
;; "0x4802_A000"
(def (maybe-parse-common-hex-string s)
(let redo ((l (string.list s)))
(case (first l)
((#\0) (redo (rest l)))
((#\x) (string->number (list->string (filter (lambda (c)
(not (char=? c #\_)))
(rest l)))
16))
(else #f))))
(TEST
> (number->string (maybe-parse-common-hex-string "0x4802_A000") 16)
"4802a000"
> (number->string (maybe-parse-common-hex-string "0x4802_a_000") 16)
"4802a000"
> (number->string (maybe-parse-common-hex-string "x4802_a_000") 16)
"4802a000"
)
(def. (natural0.common-hex-string v #!optional maybe-bits)
(string-append "0x"
(let ((s (number->string v 16)))
(if maybe-bits
(let* ((len (string-length s))
(formatlen (arithmetic-shift (+ maybe-bits 3) -2))
(fill-len (- formatlen len)))
(assert (>= fill-len 0))
(string-append (make-string fill-len #\0) s))
s))))
(jclass (address32 #(uint32? value))
(def-method (show v show)
`(address32* ,(.common-hex-string value 32))))
(def (maybe-address32* str)
(cond ((maybe-parse-common-hex-string str) => address32)
(else #f)))
(def (address32* str)
(or (maybe-address32* str)
(error "not an address string:" str)))
(TEST
> (address32* "0x480ca000")
#((address32) 1208786944)
> (show #)
(address32* "0x480ca000")
> (address32* "0x00020000")
#((address32) 131072)
> (show #)
(address32* "0x00020000")
> (%try (address32* "480ca000"))
(exception text: "not an address string: \"480ca000\"\n"))