1 | ;;; mismatch.lisp |
---|
2 | ;;; |
---|
3 | ;;; Copyright (C) 2003 Peter Graves |
---|
4 | ;;; $Id: mismatch.lisp 12516 2010-03-03 21:05:41Z astalla $ |
---|
5 | ;;; |
---|
6 | ;;; This program is free software; you can redistribute it and/or |
---|
7 | ;;; modify it under the terms of the GNU General Public License |
---|
8 | ;;; as published by the Free Software Foundation; either version 2 |
---|
9 | ;;; of the License, or (at your option) any later version. |
---|
10 | ;;; |
---|
11 | ;;; This program is distributed in the hope that it will be useful, |
---|
12 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | ;;; GNU General Public License for more details. |
---|
15 | ;;; |
---|
16 | ;;; You should have received a copy of the GNU General Public License |
---|
17 | ;;; along with this program; if not, write to the Free Software |
---|
18 | ;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
19 | ;;; |
---|
20 | ;;; As a special exception, the copyright holders of this library give you |
---|
21 | ;;; permission to link this library with independent modules to produce an |
---|
22 | ;;; executable, regardless of the license terms of these independent |
---|
23 | ;;; modules, and to copy and distribute the resulting executable under |
---|
24 | ;;; terms of your choice, provided that you also meet, for each linked |
---|
25 | ;;; independent module, the terms and conditions of the license of that |
---|
26 | ;;; module. An independent module is a module which is not derived from |
---|
27 | ;;; or based on this library. If you modify this library, you may extend |
---|
28 | ;;; this exception to your version of the library, but you are not |
---|
29 | ;;; obligated to do so. If you do not wish to do so, delete this |
---|
30 | ;;; exception statement from your version. |
---|
31 | ;;; MISMATCH (from ECL) |
---|
32 | |
---|
33 | (in-package "COMMON-LISP") |
---|
34 | |
---|
35 | (require "EXTENSIBLE-SEQUENCES-BASE") |
---|
36 | |
---|
37 | (export 'mismatch) |
---|
38 | |
---|
39 | ;;; From ECL. |
---|
40 | |
---|
41 | (defun bad-seq-limit (x &optional y) |
---|
42 | (error "bad sequence limit ~a" (if y (list x y) x))) |
---|
43 | |
---|
44 | (defun the-end (x y) |
---|
45 | (cond ((sys::fixnump x) |
---|
46 | (unless (<= x (length y)) |
---|
47 | (bad-seq-limit x)) |
---|
48 | x) |
---|
49 | ((null x) |
---|
50 | (length y)) |
---|
51 | (t (bad-seq-limit x)))) |
---|
52 | |
---|
53 | (defun the-start (x) |
---|
54 | (cond ((sys::fixnump x) |
---|
55 | (unless (>= x 0) |
---|
56 | (bad-seq-limit x)) |
---|
57 | x) |
---|
58 | ((null x) 0) |
---|
59 | (t (bad-seq-limit x)))) |
---|
60 | |
---|
61 | (defmacro with-start-end (start end seq &body body) |
---|
62 | `(let* ((,start (if ,start (the-start ,start) 0)) |
---|
63 | (,end (the-end ,end ,seq))) |
---|
64 | (unless (<= ,start ,end) (bad-seq-limit ,start ,end)) |
---|
65 | ,@ body)) |
---|
66 | |
---|
67 | (defun call-test (test test-not item keyx) |
---|
68 | (cond (test (funcall test item keyx)) |
---|
69 | (test-not (not (funcall test-not item keyx))) |
---|
70 | (t (eql item keyx)))) |
---|
71 | |
---|
72 | (defun test-error() |
---|
73 | (error "both test and test are supplied")) |
---|
74 | |
---|
75 | (defun mismatch (sequence1 sequence2 &rest args &key from-end test test-not |
---|
76 | (key #'identity) start1 start2 end1 end2) |
---|
77 | (and test test-not (test-error)) |
---|
78 | (if (and (or (listp sequence1) (arrayp sequence1)) |
---|
79 | (or (listp sequence2) (arrayp sequence2))) |
---|
80 | (with-start-end start1 end1 sequence1 |
---|
81 | (with-start-end start2 end2 sequence2 |
---|
82 | (if (not from-end) |
---|
83 | (do ((i1 start1 (1+ i1)) |
---|
84 | (i2 start2 (1+ i2))) |
---|
85 | ((or (>= i1 end1) (>= i2 end2)) |
---|
86 | (if (and (>= i1 end1) (>= i2 end2)) nil i1)) |
---|
87 | (unless (call-test test test-not |
---|
88 | (funcall key (elt sequence1 i1)) |
---|
89 | (funcall key (elt sequence2 i2))) |
---|
90 | (return i1))) |
---|
91 | (do ((i1 (1- end1) (1- i1)) |
---|
92 | (i2 (1- end2) (1- i2))) |
---|
93 | ((or (< i1 start1) (< i2 start2)) |
---|
94 | (if (and (< i1 start1) (< i2 start2)) nil (1+ i1))) |
---|
95 | (unless (call-test test test-not |
---|
96 | (funcall key (elt sequence1 i1)) |
---|
97 | (funcall key (elt sequence2 i2))) |
---|
98 | (return (1+ i1))))))) |
---|
99 | (apply #'sequence:mismatch sequence1 sequence2 args))) |
---|