Coverage report: /home/ellis/comp/ext/cl-ppcre/chartest.lisp
Kind | Covered | All | % |
expression | 5 | 87 | 5.7 |
branch | 0 | 10 | 0.0 |
Key
Not instrumented
Conditionalized out
Executed
Not executed
Both branches taken
One branch taken
Neither branch taken
1
;;; $Header: /usr/local/cvsrep/cl-ppcre/chartest.lisp,v 1.5 2009/09/17 19:17:30 edi Exp $
3
;;; Copyright (c) 2008-2009, Dr. Edmund Weitz. All rights reserved.
5
;;; Redistribution and use in source and binary forms, with or without
6
;;; modification, are permitted provided that the following conditions
9
;;; * Redistributions of source code must retain the above copyright
10
;;; notice, this list of conditions and the following disclaimer.
12
;;; * Redistributions in binary form must reproduce the above
13
;;; copyright notice, this list of conditions and the following
14
;;; disclaimer in the documentation and/or other materials
15
;;; provided with the distribution.
17
;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
18
;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21
;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23
;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25
;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
(in-package :cl-ppcre)
31
(defun create-hash-table-from-test-function (test-function start end)
32
"Creates and returns a hash table representing all characters with
33
character codes between START and END which satisfy TEST-FUNCTION."
34
(declare #.*standard-optimize-settings*)
35
(loop with hash-table = (make-hash-table)
36
for code from start below end
37
for char = (code-char code)
38
when (and char (funcall test-function char))
39
do (setf (gethash char hash-table) t)
40
finally (return hash-table)))
42
(defun create-optimized-test-function (test-function &key
44
(end *regex-char-code-limit*)
45
(kind *optimize-char-classes*))
46
"Given a unary test function which is applicable to characters
47
returns a function which yields the same boolean results for all
48
characters with character codes from START to \(excluding) END. If
49
KIND is NIL, TEST-FUNCTION will simply be returned. Otherwise, KIND
52
* :HASH-TABLE - builds a hash table representing all characters which
53
satisfy the test and returns a closure which checks if
54
a character is in that hash table
56
* :CHARSET - instead of a hash table uses a \"charset\" which is a
57
data structure using non-linear hashing and optimized to
58
represent \(sparse) sets of characters in a fast and
59
space-efficient way \(contributed by Nikodemus Siivola)
61
* :CHARMAP - instead of a hash table uses a bit vector to represent
64
You can also use :HASH-TABLE* or :CHARSET* which are like :HASH-TABLE
65
and :CHARSET but use the complement of the set if the set contains
66
more than half of all characters between START and END. This saves
67
space but needs an additional pass across all characters to create the
68
data structure. There is no corresponding :CHARMAP* kind as the bit
69
vectors are already created to cover the smallest possible interval
70
which contains either the set or its complement."
71
(declare #.*standard-optimize-settings*)
75
(let ((charmap (create-charmap-from-test-function test-function start end)))
77
(in-charmap-p char charmap))))
79
(let ((charset (create-charset-from-test-function test-function start end)))
80
(cond ((or (eq kind :charset)
81
(<= (charset-count charset) (ceiling (- end start) 2)))
83
(in-charset-p char charset)))
84
(t (setq charset (create-charset-from-test-function (complement* test-function)
87
(not (in-charset-p char charset)))))))
88
((:hash-table :hash-table*)
89
(let ((hash-table (create-hash-table-from-test-function test-function start end)))
90
(cond ((or (eq kind :hash-table)
91
(<= (hash-table-count hash-table) (ceiling (- end start) 2)))
93
(gethash char hash-table)))
94
(t (setq hash-table (create-hash-table-from-test-function (complement* test-function)
97
(not (gethash char hash-table)))))))))