sed: Support single pattern addresses.

This commit is contained in:
Timothy Sample 2018-12-09 14:03:27 -05:00
parent 194c098ab7
commit 1302c8bf28
4 changed files with 23 additions and 0 deletions

View File

@ -144,6 +144,7 @@ tests='
100-sed-undo
100-sed-file
100-sed-fooRbar
100-sed-pattern-address
100-sed-autoconf-dirname
100-tar

View File

@ -97,6 +97,14 @@
((and m+ (_ _ ...)) (proc str m+))
(_ str))))
(define (address->pred address)
(if (string? address)
(let* ((flags `(,(if (extended?) regexp/extended regexp/basic)))
(pattern (replace-escapes address))
(regexp (apply make-regexp pattern flags)))
(cut regexp-exec regexp <>))
(error "SED: unsupported address type" address)))
(define (execute-function function str)
(match function
(('s pattern replacement flags)
@ -108,6 +116,12 @@
(() str)
((('always function) . rest)
(execute-commands rest (execute-function function str)))
((('at address function) . rest)
;; XXX: This should be "compiled" ahead of time so that it only
;; runs once intead of once per line.
(if ((address->pred address) str)
(execute-commands rest (execute-function function str))
(execute-commands rest str)))
((cmd . rest) (error "SED: could not process command" cmd))))
(define* (edit-stream commands #:optional

View File

@ -0,0 +1,5 @@
input='bar
baz
bam'
echo "$input" | \sed '/baz/ s/a/i/'

View File

@ -0,0 +1,3 @@
bar
biz
bam