▼ |  | Unstable

1 | Guidelines for developing unstable libraries
2 | Bytes
3 | Classes and Objects
4 | Contracts
5 | Definitions
6 | Dictionaries
7 | Directories
8 | Exceptions
9 | Filesystem
10 | Functions
11 | Lists
12 | Net
13 | Path
14 | PLane T Packages
15 | Ports
16 | Pretty-Printing
17 | Regular Expressions
18 | Requiring Modules
19 | Sandboxed Evaluation
20 | Scribble Documentation
21 | Sets
22 | S-Expression Diff
23 | Source Locations
24 | Strings
25 | Structs
26 | Syntax
27 | Planet Package Macros
28 | Text Representations
29 | Multiple Values
30 | XML and CSS
31 | Polymorphic Contracts
32 | Finding Mutated Variables
33 | Find
34 | Interface-Oriented Programming for Classes
35 | Sequences
36 | Hash Tables
37 | Match
38 | Generics
39 | Mark Parameters
40 | Debugging
41 | Contracts for macro subexpressions
42 | Contracts for struct type properties
43 | GUI libraries

► | 28 | Text Representations

28.1 | Contracts and Predicates
28.2 | Text Conversions and Concatenation
28.3 | Text Comparisons

On this page:

28.1`` Contracts and Predicates
text/ c
text?
string-literal?
bytes-literal?
keyword-literal?
28.2`` Text Conversions and Concatenation
text->string
text->bytes
text->symbol
text->keyword
text->string-literal
text->bytes-literal
text->identifier
text->keyword-literal
28.3`` Text Comparisons
text=?
text<?
text<=?
text>?
text>=?

Version: 5.0.2
top ← prev up next →

### 28`` Text Representations

Carl Eastlund <cce@racket-lang.org>

( require unstable/text )

This library is unstable; compatibility will not be maintained. See Unstable for more information.

This module provides tools for manipulating and converting textual data.

#### 28.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

#### 28.2`` Text Conversions and Concatenation

( text->string | [ | #:before before |  |  |  |
|  | #:between between |  |  |  |
|  | #:after after] |  |  |  |
|  | text ...) |  | → |  | string?

before : text/c = ""
between : text/c = ""
after : text/c = ""
text : text/c

( text->bytes | [ | #:before before |  |  |  |
|  | #:between between |  |  |  |
|  | #:after after] |  |  |  |
|  | text ...) |  | → |  | bytes?

before : text/c = ""
between : text/c = ""
after : text/c = ""
text : text/c

( text->symbol | [ | #:before before |  |  |  |
|  | #:between between |  |  |  |
|  | #:after after] |  |  |  |
|  | text ...) |  | → |  | symbol?

before : text/c = ""
between : text/c = ""
after : text/c = ""
text : text/c

( text->keyword | [ | #:before before |  |  |  |
|  | #:between between |  |  |  |
|  | #:after after] |  |  |  |
|  | text ...) |  | → |  | keyword?

before : text/c = ""
between : text/c = ""
after : text/c = ""
text : text/c

These functions convert text values to specific types. They concatenate each text argument, adding before and after to the front and back of the result and between between each argument.

Examples:
> ( text->string #"concat" #'enate)
"concatenate"
> ( text->bytes #:between "-" 'concat #'#:enate)
#"concat-enate"
> ( text->symbol #:before "(" #:after ")" '#:concat #'"enate")
'|(concatenate)|
> ( text->keyword #:before #' < #:between #' - #:after #' > "concat" #'#"enate")
'#:<concat-enate>

( text->string-literal | [ | #:before before |  |  |  |
|  | #:between between |  |  |  |
|  | #:after after |  |  |  |
|  | #:stx stx] |  |  |  |
|  | text ...) |  | → |  | string-literal?

before : text/c = ""
between : text/c = ""
after : text/c = ""
stx : ( or/c syntax? false/c ) = #f
text : text/c

( text->bytes-literal | [ | #:before before |  |  |  |
|  | #:between between |  |  |  |
|  | #:after after |  |  |  |
|  | #:stx stx] |  |  |  |
|  | text ...) |  | → |  | bytes-literal?

before : text/c = ""
between : text/c = ""
after : text/c = ""
stx : ( or/c syntax? false/c ) = #f
text : text/c

( text->identifier | [ | #:before before |  |  |  |
|  | #:between between |  |  |  |
|  | #:after after |  |  |  |
|  | #:stx stx] |  |  |  |
|  | text ...) |  | → |  | identifier?

before : text/c = ""
between : text/c = ""
after : text/c = ""
stx : ( or/c syntax? false/c ) = #f
text : text/c

( text->keyword-literal | [ | #:before before |  |  |  |
|  | #:between between |  |  |  |
|  | #:after after |  |  |  |
|  | #:stx stx] |  |  |  |
|  | text ...) |  | → |  | keyword-literal?

before : text/c = ""
between : text/c = ""
after : text/c = ""
stx : ( or/c syntax? false/c ) = #f
text : text/c

These functions convert text values to specific syntax object types, deriving syntax object properties from the stx argument. They concatenate each text argument, adding before and after to the front and back of the result and between between each argument.

Examples:
> ( text->string-literal #"concat" #'enate)
#<syntax "concatenate">
> ( text->bytes-literal #:between "-" 'concat #'#:enate)
#<syntax #"concat-enate">

> ( text->identifier #:before "(" #:after ")"
#:stx #'props
'#:concat #'"enate")

#<syntax:4:0 |(concatenate)|>

> ( text->keyword-literal #:before #' < #:between #' - #:after #' >
#:stx #'props
"concat" #'#"enate")

#<syntax:5:0 #:<concat-enate>>

#### 28.3`` 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

top ← prev up next →