Skip to content
Turtle Kitty edited this page May 18, 2017 · 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 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? "x") -> true
(text? "") -> true
(text? 'foo) -> false
(text? 11) -> false

messages

x.n, (x n) -> if n is a number, return the (n-1)th character of the text.

(def xs "foo")
x.0 -> "f"
x.2 -> "o"

x.type -> text

x.clone -> return a fresh copy of the text

x.size -> return the number of runes in the text

x.to-bool -> false if the text is empty (""), true otherwise

x.to-symbol -> interned version: "foo" -> foo

x.to-keyword -> "foo" -> foo:

x.to-number -> returns a number, if possible, or false, if the text is NaN.

(send "5" 'to-number) -> 5
(send "foo" 'to-number) -> false

x.to-stream -> transforms the text to a stream and returns it. Useful for parsing.

x.alphabetic? -> returns true if every character is alphabetic, false otherwise.

x.numeric? -> returns true if every character is numeric, false otherwise.

x.whitespace? -> returns true if every character is whitespace, false otherwise.

x.uc? -> returns true if every character is upper-case, false otherwise.

x.lc? -> returns true if every character is lower-case, false otherwise.

(x.split "regex") -> returns a list of texts split on the given text

(def fbb "foo.bar.baz")
"foo.bar.baz"
(fbb.split "\\.")
   -> ("foo" "bar" "baz")

(x.set! index value) -> mutates the text, sets index to the new value.

(def t "1")
(t.set! 0 "2")
t -> "2"

(x.take n), (x.drop n) -> returns a new string with runes taken or dropped

(def foo "vaquero")
(foo.take 2) -> "va"
(foo.drop 2) -> "quero"

x.trim, x.ltrim, x.rtrim -> Remove spaces from the left, right, or both sides of a text object.

(def fb "  foonballardy!!!  \n")
fb.ltrim -> "foonballardy!!!  \n"
fb.rtrim -> "  foonballardy!!!"
fb.trim -> "foonballardy!!!"

(x.lpad char n), (x.rpad char n) -> Return a text padded to the left or right with until it is of size .

(def foo "NaN")
(foo.lpad " " 7) -> "    NaN"
(foo.rpad "0" 7) -> "NaN0000"

x.chomp -> Return a text with one linefeed removed from the end, if it exists.

(x.index "subtext") -> Return the index where the subtext begins, or false if it does not exist.

(def s "foobarbaz")
(x.index "bar") -> 3

(x.match "regex") -> tests whether the text matchs the regular expression. Sending the "i" flag will make it case-insensitive.

(def s "This is one sexy language.")
(s.match "sexy") -> true
(s.match "SEXY") -> false
(s.match "SEXY" flags: "i") -> true

(x.split "regex") -> splits a text into a list of subtexts based on the given regex.

(def s "foo_2_bar_153_baz")
(s.split (text: _\d+_)) -> ("foo" "bar" "baz")

(x.capture "regex") -> return a nested list captured subtexts.

(def s "foo: 1, bar :99,baz:153")
(s.capture (text: (\w+).*(\d+)))
    -> (("foo" "3"))
(s.capture (text: ((\w+)\s*:\s*(\d+))+))
    -> (("foo: 1" "foo" "1") ("bar :99" "bar" "99") ("baz:153" "baz" "153"))

(x.replace "regex" "replacement") -> Return a new text with replaced. Use the "g" flag to substitute globally.

(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"
Clone this wiki locally