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
|
; ACL2 String Library
; Copyright (C) 2009-2013 Centaur Technology
;
; Contact:
; Centaur Technology Formal Verification Group
; 7600-C N. Capital of Texas Highway, Suite 300, Austin, TX 78731, USA.
; http://www.centtech.com/
;
; License: (An MIT/X11-style 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 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.
;
; Original author: Jared Davis <jared@centtech.com>
(in-package "STR")
(include-book "coerce")
(include-book "std/util/bstar" :dir :system)
(local (include-book "explode-atom"))
(local (include-book "arithmetic"))
(local (in-theory (disable explode-atom)))
(defsection insert-underscores
:parents (hexify)
(defund insert-underscores (x)
(declare (xargs :guard t))
(cond ((atom x)
x)
((equal (mod (len x) 4) 0)
(list* #\_ (car x) (insert-underscores (cdr x))))
(t
(list* (car x) (insert-underscores (cdr x))))))
(local (in-theory (enable insert-underscores)))
(defthm character-listp-of-insert-underscores
(implies (force (character-listp x))
(character-listp (insert-underscores x)))))
(defsection hexify
:parents (numbers)
:short "Convert numbers into readable hex strings."
:long "<p>@(call hexify) is a dumb but useful printing utility for displaying
numbers in hex. It is typically used in @(see cw) statements, e.g.,:</p>
@({
ACL2 !> (cw \"foo is ~s0~%\" (str::hexify (1- (expt 2 32))))
foo is #uxFFFF_FFFF
NIL
ACL2 !>
})
<p>The @('#ux') is for compatibility with the @(see acl2::sharp-u-reader).</p>
<p>See also @(see natstr) which converts numbers into decimal strings (without
underscores) and @(see binify) which is like @('hexify') but for binary instead
of hex.</p>
<h3>Usage</h3>
<p>@(call hexify) always returns a @(see stringp).</p>
<p>@('x') must be an integer, string, or symbol; otherwise a @(see hard-error)
will be caused.</p>
<p>Normally @('x') is an integer. In this case, we convert it into an
msb-first hex string with an underscore inserted every four characters. This
makes it easier to read long values.</p>
<p>When @('x') is a string we just return it unchanged.</p>
<p>When @('x') is a symbol we just return its name.</p>"
(defun hexify (x)
(declare (xargs :guard t))
(cond ((integerp x)
(b* ((xsign (< x 0))
(xabs (abs x))
(chars (explode-atom xabs 16)) ;; looks like BEEF...
(nice-chars (list* #\# #\u #\x
(append (and xsign '(#\-))
(cons (first chars)
(insert-underscores (nthcdr 1 chars)))))))
(implode nice-chars)))
((symbolp x)
(symbol-name x))
((stringp x)
x)
(t
(prog2$ (er hard? 'hexify "Unexpected argument ~x0.~%" x)
"")))))
(defsection binify
:parents (numbers)
:short "Convert numbers into readable binary strings."
:long "<p>@(call binify) is identical to @(see str::hexify) except that it
produces binary output instead of hex output.</p>"
(defun binify (x)
(declare (xargs :guard t))
(cond ((integerp x)
(b* ((xsign (< x 0))
(xabs (abs x))
(chars (explode-atom xabs 2))
(nice-chars (list* #\# #\u #\b
(append (and xsign '(#\-))
(cons (first chars)
(insert-underscores (nthcdr 1 chars)))))))
(implode nice-chars)))
((symbolp x)
(symbol-name x))
((stringp x)
x)
(t
(prog2$ (er hard? 'binify "Unexpected argument ~x0.~%" x)
"")))))
|