8 Text Representations
(require (planet cce/scheme:4:0/text)) |
This module provides tools for manipulating and converting textual data.
8.1 Contracts and Predicates
| ||
|
This contract and predicate recognize text values: strings, byte strings, symbols, and keywords, as well as syntax objects containing them.
Examples: |
> (text? "text") |
#t |
> (text? #"text") |
#t |
> (text? 'text) |
#t |
> (text? '#:text) |
#t |
> (text? #'"text") |
#t |
> (text? #'#"text") |
#t |
> (text? #'text) |
#t |
> (text? #'#:text) |
#t |
> (text? '(not text)) |
#f |
| ||
| ||
|
These predicates recognize specific text types stored in syntax objects.
Examples: |
> (string-literal? #'"literal") |
#t |
> (string-literal? "not literal") |
#f |
> (bytes-literal? #'#"literal") |
#t |
> (bytes-literal? #"not literal") |
#f |
> (keyword-literal? #'#:literal) |
#t |
> (keyword-literal? '#:not-literal) |
#f |
8.2 Text Conversions
| ||
| ||
| ||
|
These functions convert text values to specific types, retaining their textual content and concatenating text when necessary.
Examples: |
> (text->string #"concat" #'enate) |
"concatenate" |
> (text->bytes 'concat #'#:enate) |
#"concatenate" |
> (text->symbol '#:concat #'"enate") |
concatenate |
> (text->keyword "concat" #'#"enate") |
#:concatenate |
| |||
| |||
| |||
|
These functions convert text values to specific syntax object types, retaining their textual value, concatenating text when necessary, and deriving syntax object properties from the stx argument.
Examples: | ||
| ||
> (show (text->string-literal #"concat" #'enate)) | ||
#<syntax> | ||
"concatenate" | ||
> (show (text->bytes-literal 'concat #'#:enate)) | ||
#<syntax> | ||
#"concatenate" | ||
> (show (text->identifier '#:concatenate #:stx #'props)) | ||
#<syntax:4:0> | ||
concatenate | ||
> (show (text->keyword-literal "concatenate" #:stx #'props)) | ||
#<syntax:5:0> | ||
#:concatenate |
8.3 Text Concatenation
| ||||||||||||||||||||||||||||
before : string? = "" | ||||||||||||||||||||||||||||
between : string? = "" | ||||||||||||||||||||||||||||
after : string? = "" | ||||||||||||||||||||||||||||
text : text/c |
Produces a text value (of arbitrary type) concatenating each text input. Multiple inputs are joined with between, and given prefix before and suffix after.
Examples: | ||
> (text-append #'one '- #'two) | ||
"one-two" | ||
> (text-append #:before "(" #:between ", " #:after ")") | ||
"()" | ||
> (text-append #:before "(" #:between ", " #:after ")" "Tom") | ||
"(Tom)" | ||
| ||
"(Tom, Dick, Harry)" |
8.4 Text Comparisons
| |||
| |||
| |||
| |||
|
These predicates compare the character content of two text values. They are equivalent to:
(text=? one two) = (string=? (text->string one) (text->string two)) |
(text<? one two) = (string<? (text->string one) (text->string two)) |
(text<=? one two) = (string<=? (text->string one) (text->string two)) |
(text>? one two) = (string>? (text->string one) (text->string two)) |
(text>=? one two) = (string>=? (text->string one) (text->string two)) |
Examples: |
> (text=? #"x" #'y) |
#f |
> (text<? #"x" #'y) |
#t |
> (text<=? #"x" #'y) |
#t |
> (text>? #"x" #'y) |
#f |
> (text>=? #"x" #'y) |
#f |