Skip to content
TurtleKitty edited this page May 10, 2019 · 3 revisions

text

A text object is a string of human-readable characters encoded in UTF-8.

Text literals can be created with quotes. Standard C-style escapes apply:

"\t" ; <tab>
"\n" ; <linefeed>
"\r" ; <return>
"\\" ; \

"\\Hello,\tworld!\r\n" creates a text with one backslash, one tab, one carriage return, and one newline.

Texts can also be created with the #(text ...) reader.
This can be particularly useful for creating things with lots of \, like regular expressions. Parentheses are allowed as long as they are properly nested.

#(text ((\w+)\s*:\s*((\w+)&(\d+)))) ; "((\\w+)\\s*:\\s*((\\w+)&(\\d+)))"

A third way to construct texts is with the #(template ...) reader. This allows parentheses as well, and also allows the insertion of arbitrary Vaquero expressions using Mustache-like syntax.

(let (x 2 y 3)
    #(template x: {{ x }} y: {{ y }} (z: {{ (+ x y) }}))) ; "x: 2 y: 3 (z: 5)"

predicate

(text? x)

(text? "foo")  ; true
(text? 'foo)   ; false
(text? "x")    ; true
(text? "")     ; true
(text? "11")   ; true
(text? 11)     ; false

messages

(def empty "")
(def txt "foo")

txt.0    ; "f"
txt.1    ; "o"
txt.2    ; "o"

txt.type    ; (text)
txt.clone   ; "foo"
txt.size    ; 3
empty.size  ; 0

txt.to-bool    ; true
empty.to-bool  ; false

txt.to-symbol  ; foo
txt.to-keyword ; foo:

(def número "2357")

número.to-number  ; 2357
txt.to-number     ; false

txt.to-stream     ; returns a source stream based on the text

; predicates

(def abc "abcdef")
(def ABC "ABCDEF")
(def leet "A b C d E f")
(def space "   \t\n")

abc.alphabetic?      ; true
abc.numeric?         ; false
abc.whitespace?      ; false

número.alphabetic?   ; false
número.numeric?      ; true
número.whitespace?   ; false

leet.alphabetic?     ; false
leet.numeric?        ; false
leet.whitespace?     ; false

space.alphabetic?    ; false
space.numeric?       ; false
space.whitespace?    ; true

abc.lc?  ; true
abc.uc?  ; false
ABC.lc?  ; false
ABC.uc?  ; true
leet.lc? ; false
leet.uc? ; false

; mutation

(def t "bar")

(t.set! 0 "c") ; "car"
(t.set! 2 "t") ; "cat"
t              ; cat

; conversion

(def V "vaquero")

(V.take 2)  ; "va"
(V.drop 2)  ; "quero"

leet.uc     ; "A B C D E F"
leet.lc     ; "a b c d e f"

; trim or add whitespace et al

(def fb "  foonballardy!!!  \n")

fb.ltrim    ; "foonballardy!!!  \n"
fb.rtrim    ; "  foonballardy!!!"
fb.trim     ; "foonballardy!!!"

(def foo "NaN")

(foo.lpad " " 7)  ; "    NaN"  ; leftpad is built-in!
(foo.rpad "0" 7)  ; "NaN0000"

; chomp returns a text with one linefeed removed from the end, if it exists.

(send "Yay!\n" 'chomp)  ; "Yay!"

(def s "subtext")

(s.index "sub")   ; 0
(s.index "tex")   ; 3

; regexen

(def s "This is one sexy language.")

(s.match "sexy")              ; true
(s.match "SEXY")              ; false
(s.match "SEXY" flags: "i")   ; true - the "i" flag make the match case-insensitive

(def fbb "foo.bar.baz")

(fbb.split "\\.") ; ("foo" "bar" "baz")

(def _num_ "foo_2_bar_153_baz")

(_num_.split #(text _\d+_))   ; ("foo" "bar" "baz")

(def rec "foo: 1, bar :99,baz:153")

(rec.capture #(text (\w+).*(\d+)))
   ; (("foo" "3"))

(rec.capture #(text ((\w+)\s*:\s*(\d+))+))
   ; (("foo: 1" "foo" "1") ("bar :99" "bar" "99") ("baz:153" "baz" "153"))

(def s "foo 1 foo 2 foo 3")

(s.replace "foo" "bar")             ; "bar 1 foo 2 foo 3"
(s.replace "foo" "bar" flags: "g")  ; "bar 1 bar 2 bar 3" ; the "g" flag substitutes globally
Clone this wiki locally