/usr/share/acl2-4.3/books/str/html-encode.lisp is in acl2-books-source 4.3-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | ; ACL2 String Library
; Copyright (C) 2009-2010 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/
;
; This program is free software; you can redistribute it and/or modify it under
; the terms of the GNU General Public License as published by the Free Software
; Foundation; either version 2 of the License, or (at your option) any later
; version. This program is distributed in the hope that it will be useful but
; WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
; more details. You should have received a copy of the GNU General Public
; License along with this program; if not, write to the Free Software
; Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
;
; Original author: Jared Davis <jared@centtech.com>
(in-package "STR")
(include-book "tools/bstar" :dir :system)
(local (include-book "make-event/assert" :dir :system))
(local (include-book "arithmetic"))
; HTML Encoding
;
; We now implement some routines to encode HTML entities such as < and & into
; the <, &, etc.
;
; In principle, this conversion may not be entirely legitimate in the sense of
; any particular HTML specification, since X might contain non-printable
; characters or other similarly unlikely garbage. But it seems like what we
; implement is pretty reasonable.
;
; We convert #\Newline characters into the sequence <br/>#\Newline. This may
; not be the desired behavior in certain applications, but is very convenient
; for what we are trying to accomplish in VL.
(defconst *html-space* (coerce " " 'list))
(defconst *html-newline* (append (coerce "<br/>" 'list) (list #\Newline)))
(defconst *html-less* (coerce "<" 'list))
(defconst *html-greater* (coerce ">" 'list))
(defconst *html-amp* (coerce "&" 'list))
(defconst *html-quote* (coerce """ 'list))
(defund repeated-revappend (n x y)
(declare (xargs :guard (and (natp n)
(true-listp x))))
(if (zp n)
y
(repeated-revappend (- n 1) x (revappend x y))))
(defthm character-listp-of-repeated-revappend
(implies (and (character-listp x)
(character-listp y))
(character-listp (repeated-revappend n x y)))
:hints(("Goal" :in-theory (enable repeated-revappend))))
(encapsulate
()
(local (include-book "arithmetic-3/floor-mod/floor-mod" :dir :system))
(defund distance-to-tab (col tabsize)
(declare (xargs :guard (and (natp col)
(posp tabsize))))
(mbe :logic
(nfix (- tabsize (rem col tabsize)))
:exec
(- tabsize (rem col tabsize)))))
(defund html-encode-chars-aux (x col tabsize acc)
(declare (xargs :guard (and (character-listp x)
(natp col)
(posp tabsize)
(character-listp acc))))
; Inputs:
;
; - X, a list of characters which we are currently transforming into HTML.
; - Col, our current column
; - Acc, an ordinary character list, onto which we accumulate the encoded
; HTML, in reverse order.
;
; We return (mv col' acc'), where col' is the new column number and acc is
; an updated accumulator that includes the HTML encoding of X in reverse.
(if (atom x)
(mv (mbe :logic (nfix col) :exec col)
acc)
(let ((char1 (car x)))
(html-encode-chars-aux
(cdr x)
(cond ((eql char1 #\Newline)
0)
((eql char1 #\Tab)
(+ col (distance-to-tab col tabsize)))
(t
(+ 1 col)))
tabsize
(case char1
;; Cosmetic: avoid inserting unless the last char is a
;; space or newline. This makes the HTML a little easier to
;; read.
(#\Space (if (or (atom acc)
(eql (car acc) #\Space)
(eql (car acc) #\Newline))
(revappend *html-space* acc)
(cons #\Space acc)))
(#\Newline (revappend *html-newline* acc))
(#\< (revappend *html-less* acc))
(#\> (revappend *html-greater* acc))
(#\& (revappend *html-amp* acc))
(#\" (revappend *html-quote* acc))
(#\Tab (repeated-revappend (distance-to-tab col tabsize) *html-space* acc))
(otherwise (cons char1 acc)))))))
(defund html-encode-string-aux (x n xl col tabsize acc)
(declare (xargs :guard (and (stringp x)
(natp n)
(natp xl)
(natp col)
(posp tabsize)
(character-listp acc)
(<= n xl)
(= xl (length x)))
:measure (nfix (- (nfix xl) (nfix n))))
(type string x)
(type integer n xl col tabsize))
(if (mbe :logic (zp (- (nfix xl) (nfix n)))
:exec (= n xl))
(mv (mbe :logic (nfix col) :exec col)
acc)
(let ((char1 (char x n)))
(html-encode-string-aux
x
(mbe :logic (+ 1 (nfix n)) :exec (+ 1 n))
xl
(cond ((eql char1 #\Newline)
0)
((eql char1 #\Tab)
(+ col (distance-to-tab col tabsize)))
(t
(+ 1 col)))
tabsize
(case char1
;; Cosmetic: avoid inserting unless the last char is a
;; space or newline. This makes the HTML a little easier to
;; read.
(#\Space (if (or (atom acc)
(eql (car acc) #\Space)
(eql (car acc) #\Newline))
(revappend *html-space* acc)
(cons #\Space acc)))
(#\Newline (revappend *html-newline* acc))
(#\< (revappend *html-less* acc))
(#\> (revappend *html-greater* acc))
(#\& (revappend *html-amp* acc))
(#\" (revappend *html-quote* acc))
(#\Tab (repeated-revappend (distance-to-tab col tabsize) *html-space* acc))
(otherwise (cons char1 acc)))
))))
;; Bleh. Should probably prove they are equal, but whatever.
(local (ACL2::assert! (b* ((x "blah
tab: <boo> & \"foo\" blah blah")
((mv str-col str-ans)
(html-encode-string-aux x 0 (length x) 0 8 nil))
((mv char-col char-ans)
(html-encode-chars-aux (coerce x 'list) 0 8 nil))
(- (cw "~s0~%" (coerce (reverse str-ans) 'string))))
(and (equal str-col char-col)
(equal str-ans char-ans)))))
(defthm natp-of-html-encode-chars-aux
(natp (mv-nth 0 (html-encode-chars-aux x col tabsize acc)))
:rule-classes :type-prescription
:hints(("Goal" :in-theory (enable html-encode-chars-aux))))
(defthm natp-of-html-encode-string-aux
(natp (mv-nth 0 (html-encode-string-aux x n xl col tabsize acc)))
:rule-classes :type-prescription
:hints(("Goal" :in-theory (enable html-encode-string-aux))))
(defthm character-listp-of-html-encode-chars-aux
(implies (and (character-listp x)
(natp col)
(character-listp acc))
(character-listp (mv-nth 1 (html-encode-chars-aux x col tabsize acc))))
:hints(("Goal" :in-theory (enable html-encode-chars-aux))))
(defthm character-listp-of-html-encode-string-aux
(implies (and (stringp x)
(natp n)
(natp xl)
(natp col)
(character-listp acc)
(<= n xl)
(= xl (length x)))
(character-listp (mv-nth 1 (html-encode-string-aux x n xl col tabsize acc))))
:hints(("Goal" :in-theory (enable html-encode-string-aux))))
(defund html-encode-string (x tabsize)
(declare (xargs :guard (and (stringp x)
(posp tabsize)))
(type string x)
(type integer tabsize))
(mv-let (col acc)
(html-encode-string-aux x 0 (length x) 0 tabsize nil)
(declare (ignore col))
(reverse (coerce acc 'string))))
(defthm stringp-of-html-encode-string
(stringp (html-encode-string x tabsize))
:rule-classes :type-prescription
:hints(("Goal" :in-theory (enable html-encode-string))))
|