add optional newline argument to `base64-encode'

This commit is contained in:
Matthew Flatt 2012-02-20 08:23:04 -07:00
parent 7c3464f9d7
commit a12f9831a3
3 changed files with 12 additions and 4 deletions

View File

@ -61,7 +61,7 @@
(base64-decode-stream (open-input-bytes src) s)
(get-output-bytes s)))
(define (base64-encode src)
(define (base64-encode src [linesep #"\r\n"])
(let ([s (open-output-bytes)])
(base64-encode-stream (open-input-bytes src) s (bytes 13 10))
(base64-encode-stream (open-input-bytes src) s linesep)
(get-output-bytes s)))

View File

@ -8,11 +8,12 @@ utilities for Base 64 (MIME-standard) encoding and decoding.}
@section[#:tag "base64-procs"]{Functions}
@defproc[(base64-encode [bstr bytes?]) bytes?]{
@defproc[(base64-encode [bstr bytes?] [newline-bstr bytes? #"\r\n"]) bytes?]{
Consumes a byte string and returns its Base 64 encoding as a new byte
string. The returned string is broken into 72-byte lines separated by
CRLF combinations, and always ends with a CRLF combination unless the
@racket[newline-bstr], which defaults to a CRLF combination, and the
result always ends with a @racket[newline-bstr] unless the
input is empty.}

View File

@ -12,4 +12,11 @@
net/qp
mzlib/port)
(test #"" base64-encode #"")
(test #"" base64-encode #"" #"<>")
(test #"V2h5IGRvIGF4ZSBtdXJkZXJlcnMgb25seSBhdHRhY2sKV2hlbiB5b3UncmUgcGFydGlhbGx5\r\nIG51ZGUKT3IgeW91J3JlIHRha2luZyBhIGJhdGg=\r\n"
base64-encode #"Why do axe murderers only attack\nWhen you're partially nude\nOr you're taking a bath")
(test #"V2h5IGRvIGF4ZSBtdXJkZXJlcnMgb25seSBhdHRhY2sKV2hlbiB5b3UncmUgcGFydGlhbGx5<>IG51ZGUKT3IgeW91J3JlIHRha2luZyBhIGJhdGg=<>"
base64-encode #"Why do axe murderers only attack\nWhen you're partially nude\nOr you're taking a bath" #"<>")
(report-errs)