Coverage report: /home/ellis/comp/ext/cl-ppcre/chartest.lisp

KindCoveredAll%
expression587 5.7
branch010 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 $
2
 
3
 ;;; Copyright (c) 2008-2009, Dr. Edmund Weitz. All rights reserved.
4
 
5
 ;;; Redistribution and use in source and binary forms, with or without
6
 ;;; modification, are permitted provided that the following conditions
7
 ;;; are met:
8
 
9
 ;;;   * Redistributions of source code must retain the above copyright
10
 ;;;     notice, this list of conditions and the following disclaimer.
11
 
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.
16
 
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.
28
 
29
 (in-package :cl-ppcre)
30
 
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)))
41
 
42
 (defun create-optimized-test-function (test-function &key
43
                                                      (start 0)
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
50
 should be one of:
51
 
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
55
 
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)
60
 
61
 * :CHARMAP - instead of a hash table uses a bit vector to represent
62
              the set of characters
63
 
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*)
72
   (ecase kind
73
     ((nil) test-function)
74
     (:charmap
75
      (let ((charmap (create-charmap-from-test-function test-function start end)))
76
        (lambda (char)
77
          (in-charmap-p char charmap))))
78
     ((:charset :charset*)
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)))
82
               (lambda (char)
83
                 (in-charset-p char charset)))
84
              (t (setq charset (create-charset-from-test-function (complement* test-function)
85
                                                                  start end))
86
                 (lambda (char)
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)))
92
               (lambda (char)
93
                 (gethash char hash-table)))
94
              (t (setq hash-table (create-hash-table-from-test-function (complement* test-function)
95
                                                                        start end))
96
                 (lambda (char)
97
                   (not (gethash char hash-table)))))))))