~ chicken-core (chicken-5) 8fad80cf0fc99b2718575b3b4445119c253c2324
commit 8fad80cf0fc99b2718575b3b4445119c253c2324 Author: felix <felix@call-with-current-continuation.org> AuthorDate: Mon Feb 15 17:18:35 2010 +0100 Commit: felix <felix@call-with-current-continuation.org> CommitDate: Mon Feb 15 17:18:35 2010 +0100 deprecated glob\?, added scan-lines diff --git a/manual/Unit regex b/manual/Unit regex index 5872fe49..e68cfc33 100644 --- a/manual/Unit regex +++ b/manual/Unit regex @@ -57,16 +57,6 @@ Converts the file-pattern {{PATTERN}} into a regular expression. ? -=== glob? - -<procedure>(glob? STRING)</procedure> - -Does the {{STRING}} have any "glob" wildcards? - -A string without any "glob" wildcards does not meet the criteria, -even though it technically is a valid "glob" file-pattern. - - === regexp <procedure>(regexp STRING [IGNORECASE [IGNORESPACE [UTF8]]])</procedure> diff --git a/manual/Unit utils b/manual/Unit utils index 20d581bb..e2b693b5 100644 --- a/manual/Unit utils +++ b/manual/Unit utils @@ -81,6 +81,7 @@ A parameter that holds a list of default options that should be given to {{csc}} after invocation of the {{compile-file}} procedure. The initial default options are {{-scrutinize -O2 -d2}}. + === Shell argument quoting ==== qs @@ -94,6 +95,18 @@ is simply enclosed in double-quote ({{"}}) characters, on UNIXish systems, characters that would have a special meaning to the shell are escaped using backslash ({{\}}). + +=== Scanning through an input port + +==== scan-lines + +<procedure>(scan-lines REGEXP [PORT])</procedure> + +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. + + Previous: [[Unit posix]] Next: [[Unit tcp]] diff --git a/regex.scm b/regex.scm index 95151252..88f703f9 100644 --- a/regex.scm +++ b/regex.scm @@ -245,8 +245,7 @@ ;;; Glob support: -;FIXME is it worthwhile making this accurate? -(define (glob? str) +(define (glob? str) ; DEPRECATED (##sys#check-string str 'glob?) (let loop ([idx (fx- (string-length str) 1)]) (and (fx<= 0 idx) @@ -295,17 +294,19 @@ ;;; Grep-like function on list: (define grep - (let ((string-search string-search)) + (let ((string-search string-search) + (regexp regexp)) (lambda (rx lst #!optional (acc (lambda (x) x))) (##sys#check-list lst 'grep) - (let loop ((lst lst)) - (if (null? lst) - '() - (let ((x (##sys#slot lst 0)) - (r (##sys#slot lst 1)) ) - (if (string-search rx (acc x)) - (cons x (loop r)) - (loop r) ) ) ) ) ) ) ) + (let ((rx (regexp rx))) + (let loop ((lst lst)) + (if (null? lst) + '() + (let ((x (##sys#slot lst 0)) + (r (##sys#slot lst 1)) ) + (if (string-search rx (acc x)) + (cons x (loop r)) + (loop r) ) ) ) ) ) ) ) ) ;;; Escape regular expression (suggested by Peter Bex): diff --git a/types.db b/types.db index 7974c6bf..5c05723f 100644 --- a/types.db +++ b/types.db @@ -890,7 +890,7 @@ ;; regex (glob->regexp (procedure glob->regexp (string) string)) -(glob? (procedure glob? (string) boolean)) +(glob? deprecated) (grep (procedure grep (* list #!optional (procedure (*) *)) list)) (regexp (procedure regexp (* #!optional * * *) (struct regexp))) (regexp-escape (procedure regexp-escape (string) string)) @@ -1391,5 +1391,6 @@ (qs (procedure qs (string) string)) (compile-file (procedure compile-file (string #!rest) string)) (compile-file-options (procedure compile-file-options (#!optional list) list)) +(scan-lines (procedure scan-lines (* #!optional port) *)) ;; missing: setup-api, setup-download diff --git a/utils.scm b/utils.scm index 3b299f70..dd13914a 100644 --- a/utils.scm +++ b/utils.scm @@ -27,7 +27,7 @@ (declare (unit utils) - (uses extras srfi-13 posix files) + (uses extras srfi-13 posix files regex) (usual-integrations) (fixnum) (hide chop-pds) @@ -125,3 +125,18 @@ (abort ex)) (load-file f) f))))))) + + +;;; Scan lines until regex matches + +(define scan-lines + (let ((regexp regexp) + (read-line read-line) + (string-search string-search)) + (lambda (rx #!optional (port ##sys#standard-input)) + (let ((rx (regexp rx))) + (let loop () + (let ((ln (read-line port))) + (and (not (eof-object? ln)) + (let ((m (string-search rx ln))) + (or m (loop))))))))))Trap