~ chicken-core (chicken-5) e78ff953fa241b3d31deda11f1c99a872fdc25c4
commit e78ff953fa241b3d31deda11f1c99a872fdc25c4 Author: felix <felix@call-with-current-continuation.org> AuthorDate: Fri Feb 19 08:15:22 2010 +0100 Commit: felix <felix@call-with-current-continuation.org> CommitDate: Fri Feb 19 08:15:22 2010 +0100 scan-input-lines also accepts predicate as first argument diff --git a/manual/Unit utils b/manual/Unit utils index a537abda..8154fb5d 100644 --- a/manual/Unit utils +++ b/manual/Unit utils @@ -106,6 +106,9 @@ Reads lines from {{PORT}} (defaults to the result of {{(current-input-port)}}) using {{read-line}} and returns the result of {{(string-search REGEXP LINE)}}, if the match succeeds. If no match could be found, {{#f}} is returned. +{{REGEXP}} may also be a procedure of one argument which is called for each +input line and should return a non-false value on success. + Previous: [[Unit posix]] diff --git a/utils.scm b/utils.scm index 3a81fbeb..0d60bd72 100644 --- a/utils.scm +++ b/utils.scm @@ -127,16 +127,18 @@ f))))))) -;;; Scan lines until regex matches +;;; Scan lines until regex or predicate matches (define scan-input-lines (let ((regexp regexp) (read-line read-line) (string-search string-search)) (lambda (rx #!optional (port ##sys#standard-input)) - (let ((rx (regexp rx))) + (let ((rx (if (procedure? rx) + rx + (cut string-search (regexp rx) <>)))) (let loop () (let ((ln (read-line port))) (and (not (eof-object? ln)) - (let ((m (string-search rx ln))) - (or m (loop)))))))))) + (or (rx ln) + (loop)))))))))Trap