<br><div class="gmail_quote">On Tue, Dec 7, 2010 at 12:25 AM, Eli Barzilay <span dir="ltr"><<a href="mailto:eli@barzilay.org">eli@barzilay.org</a>></span> wrote:<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Actually, there's a problem -- there is already an optional argument<br>
for the prefix, and if an output port is added, it should be added<br>
before that for consistency but that will be incompatible.</blockquote><div><br></div><div>That's unfortunate. Looking at v4.2.5 I did not see the input-prefix param, so that's probably added in v5.0. I guess it's either be a new name (something like regexp-replace*/out and regexp-replace/out) or added after the prefix. </div>
<div><br></div><div>I did a quick proof of concept against 4.2.5 (which doesn't have the inconsistency of input-prefix), and it works AFAICT. Of course it's better if this is implemented in the same layer as other regexp functions. </div>
<div><br></div></div><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><div class="gmail_quote"><div><div><font class="Apple-style-span" face="'courier new', monospace">(define (insert-spec->callback spec)</font></div>
</div></div><div class="gmail_quote"><div><div><font class="Apple-style-span" face="'courier new', monospace"> (define (make-substitution matched)</font></div></div></div><div class="gmail_quote"><div><div><font class="Apple-style-span" face="'courier new', monospace"> (lambda (m . rest)</font></div>
</div></div><div class="gmail_quote"><div><div><font class="Apple-style-span" face="'courier new', monospace"> (cond ((bytes=? m #"\\&") #"&")</font></div></div></div><div class="gmail_quote">
<div><div><font class="Apple-style-span" face="'courier new', monospace"> ((bytes=? m #"\\\\") #"\\")</font></div></div></div><div class="gmail_quote"><div><div><font class="Apple-style-span" face="'courier new', monospace"> ((bytes=? m #"&") (car matched))</font></div>
</div></div><div class="gmail_quote"><div><div><font class="Apple-style-span" face="'courier new', monospace"> ((= 2 (length rest))</font></div></div></div><div class="gmail_quote"><div><div><font class="Apple-style-span" face="'courier new', monospace"> (list-ref matched (string->number (bytes->string/utf-8 (cadr rest)))))</font></div>
</div></div><div class="gmail_quote"><div><div><font class="Apple-style-span" face="'courier new', monospace"> (else #""))))</font></div></div></div><div class="gmail_quote"><div><div><font class="Apple-style-span" face="'courier new', monospace"> (lambda (matched . parts)</font></div>
</div></div><div class="gmail_quote"><div><div><font class="Apple-style-span" face="'courier new', monospace"> (regexp-replace* (byte-pregexp #"(\\&|\\\\([0-9]+))") </font></div></div></div><div class="gmail_quote">
<div><div><font class="Apple-style-span" face="'courier new', monospace"> spec</font></div></div></div><div class="gmail_quote"><div><div><font class="Apple-style-span" face="'courier new', monospace"> (make-substitution (cons matched parts)))))</font></div>
</div></div><div class="gmail_quote"><div><div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div></div></div><div class="gmail_quote"><div><div><font class="Apple-style-span" face="'courier new', monospace"><div>
(define (regexp-replace*/out pat in insert (out #f)) ;; what does #f mean?</div><div> (define (helper insert) </div><div> (let loop ((matched (regexp-match pat in 0 #f out)))</div><div> (unless (not matched) </div>
<div> (display (apply insert matched) out)</div><div> (loop (regexp-match pat in 0 #f out)))))</div><div> (if (input-port? in)</div><div> (if (output-port? out)</div><div> (helper (if (procedure? insert)</div>
<div> insert</div><div> (insert-spec->callback insert)))</div><div> (regexp-replace* pat (port->bytes in) insert))</div><div> (regexp-replace* pat in insert)))</div>
<div><br></div><div>(define (regexp-replace/out pat in insert (out #f))</div><div> (define (helper insert)</div><div> (let ((matched (regexp-match pat in 0 #f out)))</div><div> (unless (not matched)</div><div> (display (apply insert matched) out)</div>
<div> (copy-port in out))))</div><div> (if (input-port? in)</div><div> (if (output-port? out)</div><div> (helper (if (procedure? insert)</div><div> insert</div><div> (insert-spec->callback insert)))</div>
<div> (regexp-replace pat (port->bytes in) insert))</div><div> (regexp-replace pat in insert)))</div><div><br></div></font></div></div></div><div class="gmail_quote"><div><font class="Apple-style-span" face="'courier new', monospace">> (regexp-replace*/out #px"[0-9]+" (open-input-string "1929,1939,1949,1959") "+\\0" (current-output-port))</font></div>
</div><div class="gmail_quote"><div><font class="Apple-style-span" face="'courier new', monospace">+1929,+1939,+1949,+1959</font></div></div></blockquote><div class="gmail_quote"><div><br></div><div>Cheers,</div><div>
yc</div><div><br></div><div><br></div><div> </div></div>