bytes-join
This commit is contained in:
parent
309bb47c7e
commit
2b045b991e
18
collects/racket/bytes.rkt
Normal file
18
collects/racket/bytes.rkt
Normal file
|
@ -0,0 +1,18 @@
|
|||
#lang racket/base
|
||||
|
||||
(provide bytes-append* bytes-join)
|
||||
|
||||
(define bytes-append*
|
||||
(case-lambda [(strs) (apply bytes-append strs)] ; optimize common case
|
||||
[(str . strss) (apply bytes-append (apply list* str strss))]))
|
||||
|
||||
(require (only-in scheme/list add-between))
|
||||
|
||||
(define (bytes-join strs sep)
|
||||
(cond [(not (and (list? strs) (andmap bytes? strs)))
|
||||
(raise-type-error 'bytes-join "list-of-byte-strings" strs)]
|
||||
[(not (bytes? sep))
|
||||
(raise-type-error 'bytes-join "bytes" sep)]
|
||||
[(null? strs) #""]
|
||||
[(null? (cdr strs)) (car strs)]
|
||||
[else (apply bytes-append (add-between strs sep))]))
|
|
@ -16,6 +16,7 @@
|
|||
racket/list
|
||||
racket/vector
|
||||
racket/string
|
||||
racket/bytes
|
||||
racket/function
|
||||
racket/path
|
||||
racket/file
|
||||
|
@ -44,6 +45,7 @@
|
|||
racket/list
|
||||
racket/vector
|
||||
racket/string
|
||||
racket/bytes
|
||||
racket/function
|
||||
racket/path
|
||||
racket/file
|
||||
|
|
|
@ -636,3 +636,34 @@ Returns a string for the current locale's encoding (i.e., the encoding
|
|||
normally identified by @scheme[""]). See also
|
||||
@scheme[system-language+country].}
|
||||
|
||||
@section{Additional Byte String Functions}
|
||||
@note-lib[racket/bytes]
|
||||
@(define string-eval (make-base-eval))
|
||||
@(interaction-eval #:eval string-eval (require racket/bytes racket/list))
|
||||
|
||||
@defproc[(bytes-append* [str bytes?] ... [strs (listof bytes?)]) bytes?]{
|
||||
@; Note: this is exactly the same description as the one for append*
|
||||
|
||||
Like @racket[bytes-append], but the last argument is used as a list
|
||||
of arguments for @scheme[bytes-append], so @scheme[(bytes-append*
|
||||
str ... strs)] is the same as @scheme[(apply bytes-append str
|
||||
... strs)]. In other words, the relationship between
|
||||
@scheme[bytes-append] and @scheme[bytes-append*] is similar to the
|
||||
one between @scheme[list] and @scheme[list*].
|
||||
|
||||
@mz-examples[#:eval string-eval
|
||||
(bytes-append* #"a" #"b" '(#"c" #"d"))
|
||||
(bytes-append* (cdr (append* (map (lambda (x) (list #", " x))
|
||||
'(#"Alpha" #"Beta" #"Gamma")))))
|
||||
]}
|
||||
|
||||
@defproc[(bytes-join [strs (listof bytes?)] [sep bytes?]) bytes?]{
|
||||
|
||||
Appends the byte strings in @scheme[strs], inserting @scheme[sep] between
|
||||
each pair of bytes in @scheme[strs].
|
||||
|
||||
@mz-examples[#:eval string-eval
|
||||
(bytes-join '(#"one" #"two" #"three" #"four") #" potato ")
|
||||
]}
|
||||
|
||||
@close-eval[string-eval]
|
||||
|
|
17
collects/tests/racket/bytes.rktl
Normal file
17
collects/tests/racket/bytes.rktl
Normal file
|
@ -0,0 +1,17 @@
|
|||
|
||||
(load-relative "loadtest.rktl")
|
||||
|
||||
(Section 'bytes)
|
||||
|
||||
(require racket/bytes)
|
||||
|
||||
;; ---------- bytes-join ----------
|
||||
(let ()
|
||||
(test #"" bytes-join '() #" ")
|
||||
(test #"" bytes-join '(#"") #" ")
|
||||
(test #" " bytes-join '(#"" #"") #" ")
|
||||
(test #"x y" bytes-join '(#"x" #"y") #" ")
|
||||
(test #"x" bytes-join '(#"x") #" "))
|
||||
|
||||
|
||||
(report-errs)
|
Loading…
Reference in New Issue
Block a user