[racket] rexp-match and optional output port

From: Steve Byan (stevebyan at verizon.net)
Date: Thu Apr 11 10:26:25 EDT 2013

Section 3.7.4 "Regexp Matching" of the "Racket Reference" says in part:

"If the optional output-port is provided as an output port, the part of input from its beginning (not start-pos) that precedes the match is written to the port. All of input up to end-pos is written to the port if no match is found. This functionality is most useful when input is an input port."

Given this input file:

multivac:~ smb$ cat /Users/smb/Library/Mail/V2/Mailboxes/personal.mbox/PMRC.mbox/Info.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">

I expect the following racket function to copy the entire input port to the output port:

#lang racket

(define (plist-file-match-sorted-descending plist-path)
 (let ([input-port (open-input-file plist-path)]
       [output-port (open-output-file (path-replace-suffix plist-path ".plist~") #:exists 'replace)])
   (if (regexp-match "<key>SortedDescending</key>\n\t<string>YES</string>" input-port 0 #f output-port)
       (displayln "sorted descending")
       (displayln "sorted ascending"))))

(define test-path (string->path "/Users/smb/Library/Mail/V2/Mailboxes/personal.mbox/PMRC.mbox/Info.plist"))

I apply the function to the file path:

Welcome to DrRacket, version 5.3.3 [3m].
Language: racket; memory limit: 128 MB.
> (plist-file-match-sorted-descending test-path)
sorted ascending

But despite my expectations the resulting Info.plist~ file is empty: 

multivac:~ smb$ ls -l /Users/smb/Library/Mail/V2/Mailboxes/personal.mbox/PMRC.mbox/Info.plist~
-rw-r--r--  1 smb  staff  0 Apr 10 17:09 /Users/smb/Library/Mail/V2/Mailboxes/personal.mbox/PMRC.mbox/Info.plist~

What am I missing?

Best regards,

Steve Byan <stevebyan at me.com>
Littleton, MA 01460

Posted on the users mailing list.