pax_global_header00006660000000000000000000000064133547710730014524gustar00rootroot0000000000000052 comment=fcaf1b05d50b3e299dfdf127b9379bf2dbb5733a split-sequence-1.5.0/000077500000000000000000000000001335477107300144705ustar00rootroot00000000000000split-sequence-1.5.0/LICENSE000066400000000000000000000020601335477107300154730ustar00rootroot00000000000000Copyright (C) 2001-2018, Arthur Lemmens et al. 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. split-sequence-1.5.0/README.md000066400000000000000000000072471335477107300157610ustar00rootroot00000000000000SPLIT-SEQUENCE ============== [SPLIT-SEQUENCE](http://cliki.net/split-sequence) is a member of the [Common Lisp Utilities](http://cliki.net/Common%20Lisp%20Utilities) family of programs, designed by community consensus. _Function_ __SPLIT-SEQUENCE, SPLIT-SEQUENCE-IF, SPLIT-SEQUENCE-IF-NOT__ __Syntax:__ __split-sequence__ _delimiter sequence `&key` count remove-empty-subseqs from-end start end test test-not key ⇒ list, index_ __split-sequence-if__ _predicate sequence `&key` count remove-empty-subseqs from-end start end key ⇒ list, index_ __split-sequence-if-not__ _predicate sequence `&key` count remove-empty-subseqs from-end start end key ⇒ list, index_ __Arguments and Values:__ _delimiter_—an _object_. _predicate_—a designator for a _function_ of one _argument_ that returns a _generalized boolean_. _sequence_—a _proper sequence_. _count_—an _integer_ or __nil__. The default is __nil__. _remove-empty-subseqs_—a _generalized boolean_. The default is _false_. _from-end_—a _generalized boolean_. The default is _false_. _start, end_—_bounding index designators_ of _sequence_. The defaults for _start_ and _end_ are __0__ and __nil__, respectively. _test_—a _designator_ for a _function_ of two _arguments_ that returns a _generalized boolean_. _test-not_—a _designator_ for a _function_ of two _arguments_ that returns a _generalized boolean_. _key_—a _designator_ for a _function_ of one _argument_, or __nil__. _list_—a _proper sequence_. _index_—an _integer_ greater than or equal to zero, and less than or equal to the _length_ of the _sequence_. __Description:__ Splits _sequence_ into a list of subsequences delimited by objects _satisfying the test_. _List_ is a list of sequences of the same kind as _sequence_ that has elements consisting of subsequences of _sequence_ that were delimited in the argument by elements _satisfying the test_. Index is an index into _sequence_ indicating the unprocessed region, suitable as an argument to [subseq](http://www.lispworks.com/documentation/HyperSpec/Body/f_subseq.htm) to continue processing in the same manner if desired. The _count_ argument, if supplied, limits the number of subsequences in the first return value; if more than _count_ delimited subsequences exist in _sequence_, the _count_ leftmost delimited subsequences will be in order in the first return value, and the second return value will be the index into _sequence_ at which processing stopped. If _from-end_ is non-null, _sequence_ is conceptually processed from right to left, accumulating the subsequences in reverse order; _from-end_ only makes a difference in the case of a non-null _count_ argument. In the presence of _from-end_, the _count_ rightmost delimited subsequences will be in the order that they are in _sequence_ in the first return value, and the second is the index indicating the end of the unprocessed region. The _start_ and _end_ keyword arguments permit a certain subsequence of the _sequence_ to be processed without the need for a copying stage; their use is conceptually equivalent to partitioning the subsequence delimited by _start_ and _end_, only without the need for copying. If _remove-empty-subseqs_ is null (the default), then empty subsequences will be included in the result. In all cases, the subsequences in the first return value will be in the order that they appeared in _sequence_. __Examples:__
SPLIT-SEQUENCE> (split-sequence #\Space "A stitch in time saves nine.")
⇒ ("A" "stitch" "in" "time" "saves" "nine.")
⇒ 28

SPLIT-SEQUENCE> (split-sequence #\, "foo,bar ,baz, foobar , barbaz,")
⇒ ("foo" "bar " "baz" " foobar " " barbaz" "")
⇒ 30
split-sequence-1.5.0/split-sequence.asd000066400000000000000000000015111335477107300201200ustar00rootroot00000000000000;;; -*- Lisp -*- (defsystem :split-sequence :author "Arthur Lemmens " :maintainer "Sharp Lispers " :description "Splits a sequence into a list of subsequences delimited by objects satisfying a test." :license "MIT" :version (:read-file-form "version.sexp") :components ((:static-file "version.sexp") (:file "split-sequence"))) (defsystem :split-sequence/tests :author "Arthur Lemmens " :maintainer "Sharp Lispers " :description "Split-Sequence test suite" :license "MIT" :depends-on (:split-sequence :fiveam) :components ((:file "tests"))) (defmethod perform ((o test-op) (c (eql (find-system :split-sequence)))) (load-system :split-sequence/tests) (symbol-call :5am :run! :split-sequence)) split-sequence-1.5.0/split-sequence.lisp000066400000000000000000000177441335477107300203370ustar00rootroot00000000000000;;;; -*- Mode: Lisp; indent-tabs-mode: nil -*- ;;; ;;; SPLIT-SEQUENCE ;;; ;;; This code was based on Arthur Lemmens' in ;;; ; ;;; ;;; changes include: ;;; ;;; * altering the behaviour of the :from-end keyword argument to ;;; return the subsequences in original order, for consistency with ;;; CL:REMOVE, CL:SUBSTITUTE et al. (:from-end being non-NIL only ;;; affects the answer if :count is less than the number of ;;; subsequences, by analogy with the above-referenced functions). ;;; ;;; * changing the :maximum keyword argument to :count, by analogy ;;; with CL:REMOVE, CL:SUBSTITUTE, and so on. ;;; ;;; * naming the function SPLIT-SEQUENCE rather than PARTITION rather ;;; than SPLIT. ;;; ;;; * adding SPLIT-SEQUENCE-IF and SPLIT-SEQUENCE-IF-NOT. ;;; ;;; * The second return value is now an index rather than a copy of a ;;; portion of the sequence; this index is the `right' one to feed to ;;; CL:SUBSEQ for continued processing. ;;; There's a certain amount of code duplication here, which is kept ;;; to illustrate the relationship between the SPLIT-SEQUENCE ;;; functions and the CL:POSITION functions. (defpackage :split-sequence (:use :common-lisp) (:export #:split-sequence #:split-sequence-if #:split-sequence-if-not)) (in-package :split-sequence) (deftype array-index (&optional (length array-dimension-limit)) `(integer 0 (,length))) (declaim (ftype (function (&rest t) (values list integer)) split-sequence split-sequence-if split-sequence-if-not)) (declaim (ftype (function (function sequence array-index (or null array-index) (or null array-index) boolean) (values list integer)) split-from-start split-from-end)) (macrolet ((check-bounds (sequence start end) (let ((length (gensym (string '#:length)))) `(let ((,length (length ,sequence))) (check-type ,start unsigned-byte "a non-negative integer") (when ,end (check-type ,end unsigned-byte "a non-negative integer or NIL")) (unless ,end (setf ,end ,length)) (unless (<= ,start ,end ,length) (error "Wrong sequence bounds. start: ~S end: ~S" ,start ,end)))))) (defun split-sequence (delimiter sequence &key (start 0) (end nil) (from-end nil) (count nil) (remove-empty-subseqs nil) (test #'eql) (test-not nil) (key #'identity)) "Return a list of subsequences in seq delimited by delimiter. If :remove-empty-subseqs is NIL, empty subsequences will be included in the result; otherwise they will be discarded. All other keywords work analogously to those for CL:SUBSTITUTE. In particular, the behaviour of :from-end is possibly different from other versions of this function; :from-end values of NIL and T are equivalent unless :count is supplied. The second return value is an index suitable as an argument to CL:SUBSEQ into the sequence indicating where processing stopped." (check-bounds sequence start end) (cond ((and (not from-end) (null test-not)) (split-from-start (lambda (sequence start) (position delimiter sequence :start start :key key :test test)) sequence start end count remove-empty-subseqs)) ((and (not from-end) test-not) (split-from-start (lambda (sequence start) (position delimiter sequence :start start :key key :test-not test-not)) sequence start end count remove-empty-subseqs)) ((and from-end (null test-not)) (split-from-end (lambda (sequence end) (position delimiter sequence :end end :from-end t :key key :test test)) sequence start end count remove-empty-subseqs)) (t (split-from-end (lambda (sequence end) (position delimiter sequence :end end :from-end t :key key :test-not test-not)) sequence start end count remove-empty-subseqs)))) (defun split-sequence-if (predicate sequence &key (start 0) (end nil) (from-end nil) (count nil) (remove-empty-subseqs nil) (key #'identity)) "Return a list of subsequences in seq delimited by items satisfying predicate. If :remove-empty-subseqs is NIL, empty subsequences will be included in the result; otherwise they will be discarded. All other keywords work analogously to those for CL:SUBSTITUTE-IF. In particular, the behaviour of :from-end is possibly different from other versions of this function; :from-end values of NIL and T are equivalent unless :count is supplied. The second return value is an index suitable as an argument to CL:SUBSEQ into the sequence indicating where processing stopped." (check-bounds sequence start end) (if from-end (split-from-end (lambda (sequence end) (position-if predicate sequence :end end :from-end t :key key)) sequence start end count remove-empty-subseqs) (split-from-start (lambda (sequence start) (position-if predicate sequence :start start :key key)) sequence start end count remove-empty-subseqs))) (defun split-sequence-if-not (predicate sequence &key (count nil) (remove-empty-subseqs nil) (from-end nil) (start 0) (end nil) (key #'identity)) "Return a list of subsequences in seq delimited by items satisfying \(CL:COMPLEMENT predicate). If :remove-empty-subseqs is NIL, empty subsequences will be included in the result; otherwise they will be discarded. All other keywords work analogously to those for CL:SUBSTITUTE-IF-NOT. In particular, the behaviour of :from-end is possibly different from other versions of this function; :from-end values of NIL and T are equivalent unless :count is supplied. The second return value is an index suitable as an argument to CL:SUBSEQ into the sequence indicating where processing stopped." (check-bounds sequence start end) (if from-end (split-from-end (lambda (sequence end) (position-if-not predicate sequence :end end :from-end t :key key)) sequence start end count remove-empty-subseqs) (split-from-start (lambda (sequence start) (position-if-not predicate sequence :start start :key key)) sequence start end count remove-empty-subseqs)))) (defun split-from-end (position-fn sequence start end count remove-empty-subseqs) (declare (optimize (speed 3) (debug 0))) (loop :for right := end :then left :for left := (max (or (funcall position-fn sequence right) -1) (1- start)) :unless (and (= right (1+ left)) remove-empty-subseqs) ; empty subseq we don't want :if (and count (>= nr-elts count)) ;; We can't take any more. Return now. :return (values (nreverse subseqs) right) :else :collect (subseq sequence (1+ left) right) into subseqs :and :sum 1 :into nr-elts :until (< left start) :finally (return (values (nreverse subseqs) (1+ left))))) (defun split-from-start (position-fn sequence start end count remove-empty-subseqs) (declare (optimize (speed 3) (debug 0))) (let ((length (length sequence))) (loop :for left := start :then (+ right 1) :for right := (min (or (funcall position-fn sequence left) length) end) :unless (and (= right left) remove-empty-subseqs) ; empty subseq we don't want :if (and count (>= nr-elts count)) ;; We can't take any more. Return now. :return (values subseqs left) :else :collect (subseq sequence left right) :into subseqs :and :sum 1 :into nr-elts :until (>= right end) :finally (return (values subseqs right))))) (pushnew :split-sequence *features*) split-sequence-1.5.0/tests.lisp000066400000000000000000000026001335477107300165210ustar00rootroot00000000000000;;;; -*- Mode: Lisp; indent-tabs-mode: nil -*- (defpackage :split-sequence/tests (:use :common-lisp :split-sequence :fiveam)) (in-package :split-sequence/tests) (in-suite* :split-sequence) ;;;; SPLIT-SEQUENCE (test (split-sequence.1 :compile-at :definition-time) (is (equalp (split-sequence #\; "a;;b;c") (values '("a" "" "b" "c") 6)))) (test (split-sequence.2 :compile-at :definition-time) (is (equalp (split-sequence #\; "a;;b;c" :from-end t) (values '("a" "" "b" "c") 0)))) (test (split-sequence.3 :compile-at :definition-time) (is (equalp (split-sequence #\; "a;;b;c" :from-end t :count 1) (values '("c") 4)))) (test (split-sequence.4 :compile-at :definition-time) (is (equalp (split-sequence #\; "a;;b;c" :remove-empty-subseqs t) (values '("a" "b" "c") 6)))) (test (split-sequence.5 :compile-at :definition-time) (is (equalp (split-sequence #\; ";oo;bar;ba;" :start 1 :end 9) (values '("oo" "bar" "b") 9)))) (test (split-sequence-if.1 :compile-at :definition-time) (is (equalp (split-sequence-if (lambda (x) (member x '(#\a #\b))) "abracadabra") (values '("" "" "r" "c" "d" "" "r" "") 11)))) (test (split-sequence-if-not.1 :compile-at :definition-time) (is (equalp (split-sequence-if-not (lambda (x) (member x '(#\a #\b))) "abracadabra") (values '("ab" "a" "a" "ab" "a") 11)))) split-sequence-1.5.0/version.sexp000066400000000000000000000000301335477107300170470ustar00rootroot00000000000000;; -*- lisp -*- "1.5.0"