On this page:
8.1 Contracts and Predicates
text/ c
text?
string-literal?
bytes-literal?
keyword-literal?
8.2 Text Conversions
text->string
text->bytes
text->symbol
text->keyword
text->string-literal
text->bytes-literal
text->identifier
text->keyword-literal
8.3 Text Concatenation
text-append
8.4 Text Comparisons
text=?
text<?
text<=?
text>?
text>=?
Version: 4.1.4.3

8 Text Representations

 (require (planet cce/scheme:4:1/text))

This module provides tools for manipulating and converting textual data.

8.1 Contracts and Predicates

text/c : flat-contract?
(text? v)  boolean?
  v : any/c

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

(string-literal? v)  boolean?
  v : any/c
(bytes-literal? v)  boolean?
  v : any/c
(keyword-literal? v)  boolean?
  v : any/c

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

(text->string text ...)  string?
  text : text/c
(text->bytes text ...)  bytes?
  text : text/c
(text->symbol text ...)  symbol?
  text : text/c
(text->keyword text ...)  keyword?
  text : text/c

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

(text->string-literal text ... [#:stx stx])  string-literal?
  text : text/c
  stx : (or/c syntax? false/c) = #f
(text->bytes-literal text ... [#:stx stx])  bytes-literal?
  text : text/c
  stx : (or/c syntax? false/c) = #f
(text->identifier text ... [#:stx stx])  identifier?
  text : text/c
  stx : (or/c syntax? false/c) = #f
(text->keyword-literal text ... [#:stx stx])  keyword-literal?
  text : text/c
  stx : (or/c syntax? false/c) = #f

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:

  (define (show stx) (values stx (syntax-e stx)))
  > (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

(text-append [#:before before    
  #:between between    
  #:after after]    
  text ...)  text/c
  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)"

  > (text-append #:before "(" #:between ", " #:after ")"
                 "Tom" "Dick" "Harry")

  "(Tom, Dick, Harry)"

8.4 Text Comparisons

(text=? one two)  boolean?
  one : text/c
  two : text/c
(text<? one two)  boolean?
  one : text/c
  two : text/c
(text<=? one two)  boolean?
  one : text/c
  two : text/c
(text>? one two)  boolean?
  one : text/c
  two : text/c
(text>=? one two)  boolean?
  one : text/c
  two : text/c

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