Strings and Text
In this chapter you will learn:
- The two string literal types (raw and c-strings)
- How to embed expressions in strings using
{...}syntax - How strings with anaphora become functions
- Format specifiers for controlling output
- The string functions available in the
strnamespace
String Literal Types
Section titled “String Literal Types”Eucalypt has two kinds of string literal: raw strings and c-strings.
Raw Strings
Section titled “Raw Strings”A plain double-quoted string is a raw string — backslashes are literal characters with no escape processing. This is convenient for regular expression usage.
greeting: "Hello, World!"path: "C:\Users\alice\docs"regex: "^\d+\.\d+"The r"..." prefix is equivalent and can be used for clarity when
the string contains backslashes:
path: r"C:\Users\alice\docs"regex: r"^\d+\.\d+"Raw strings support interpolation with {...}. Use {{ and }}
for literal braces.
C-Strings (c"...")
Section titled “C-Strings (c"...")”If you require C-style escapes, you can use C-strings:
| Escape | Meaning |
|---|---|
\n |
Newline |
\t |
Tab |
\r |
Carriage return |
\\ |
Literal backslash |
\" |
Literal quote |
\{, \} |
Literal braces |
\xHH |
Hex byte |
\uHHHH |
Unicode code point |
\UHHHHHHHH |
Extended Unicode |
multiline: c"first line\nsecond line"C-strings also support interpolation with {...}.
Basic Interpolation
Section titled “Basic Interpolation”Embed any expression inside a string using curly braces:
name: "World"greeting: "Hello, {name}!"name: Worldgreeting: Hello, World!Interpolation braces accept names and dotted lookups. To use a computed value, bind it to a name first:
x: 3y: 4sum: x + yresult: "{x} + {y} = {sum}"x: 3y: 4sum: 7result: 3 + 4 = 7Nested Lookups in Interpolation
Section titled “Nested Lookups in Interpolation”You can use dotted paths inside interpolation:
data: { foo: { bar: 99 } }label: "{data.foo.bar}"data: foo: bar: 99label: '99'Note: Interpolation braces accept names and dotted lookups, but not arbitrary eucalypt expressions. If you need a computed value, bind it to a name first:
sum: x + yresult: "{sum}"
Escaping Braces
Section titled “Escaping Braces”To include a literal brace in a string, double it:
example: "Use {{braces}} for interpolation"example: Use {braces} for interpolationThe same holds straight after an interpolation: the interpolation’s own
} closes it, and what follows is ordinary text.
name: "x"example: "{{{name}}}"name: xexample: "{x}"This is also needed in regular expressions within interpolated strings:
pattern: "01234" str.match-with("\d{{4}}")Format Specifiers
Section titled “Format Specifiers”Add a format specifier after a colon inside the interpolation braces. These use printf-style format codes:
pi: 3.14159n: 42formatted: "{pi:%.2f}"padded: "{n:%06d}"pi: 3.14159n: 42formatted: '3.14'padded: '000042'Bind the value to a name first, as above — a bare numeral inside the
braces (e.g. "{42:%06d}") is parsed as numbered string anaphora
({0}, {1}, …), not a literal value, and silently produces an
uncallable high-arity function instead of a formatted string.
String Anaphora
Section titled “String Anaphora”When a string contains {} (empty braces) or {0}, {1}, etc.,
the string literal actually defines a function rather than a plain
string value:
eu -e '["a", "b", "c"] map("item: {}")'- 'item: a'- 'item: b'- 'item: c'Numbered anaphora control argument order:
reverse-pair: "{1},{0}"result: reverse-pair(:a, :b)result: b,aYou can mix named references and anaphora:
prefix: "Hello"greet: "{prefix} {}!"result: greet("World")prefix: Helloresult: Hello World!String Functions
Section titled “String Functions”The str namespace contains functions for working with strings.
Conversion
Section titled “Conversion”eu -e '42 str.of''42'Case Conversion
Section titled “Case Conversion”eu -e '"hello" str.to-upper'HELLOeu -e '"GOODBYE" str.to-lower'goodbyeSplitting and Joining
Section titled “Splitting and Joining”Split a string on a pattern:
eu -e '"one-two-three" str.split-on("-")'- one- two- threeJoin a list of strings:
eu -e '["a", "b", "c"] str.join-on(", ")'a, b, cPrefix and Suffix
Section titled “Prefix and Suffix”eu -e '"world" str.prefix("hello ")'hello worldeu -e '"hello" str.suffix("!")'hello!Characters and Letters
Section titled “Characters and Letters”eu -e '"hello" str.letters'- h- e- l- l- oeu -e '"hello" str.letters count'5Regular Expressions
Section titled “Regular Expressions”Testing a Match
Section titled “Testing a Match”eu -e '"hello" str.matches?("^h.*o$")'trueExtracting Matches
Section titled “Extracting Matches”str.match-with returns the full match and capture groups:
eu -e '"192.168.0.1" str.match-with("(\d+)[.](\d+)[.](\d+)[.](\d+)") tail'- '192'- '168'- '0'- '1'str.matches-of returns all occurrences of a pattern:
eu -e '"192.168.0.1" str.matches-of("\d+")'- '192'- '168'- '0'- '1'Base64 and SHA-256
Section titled “Base64 and SHA-256”eu -e '"hello" str.base64-encode'aGVsbG8=eu -e '"hello" str.sha256'2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824Practical Examples
Section titled “Practical Examples”Generating URLs
Section titled “Generating URLs”base: "https://api.example.com"endpoints: ["users", "posts", "comments"] map("{base}/{}")base: https://api.example.comendpoints:- https://api.example.com/users- https://api.example.com/posts- https://api.example.com/commentsFormatting a Table
Section titled “Formatting a Table”rows: [ { name: "Alice" score: 85 }, { name: "Bob" score: 92 }]
` :suppressformat-row(r): "{r.name:%10s} | {r.score:%3d}"
table: rows map(format-row)Key Concepts
Section titled “Key Concepts”- Interpolation uses
{expression}inside double-quoted strings - Empty braces
{}and numbered braces{0},{1}create string functions (anaphora) - Format specifiers follow a colon:
{value:%06d} - Escape literal braces by doubling them:
{{and}}. This works straight after an interpolation too:"{x}}}"is the value ofxfollowed by} - The
strnamespace provides splitting, joining, case conversion, matching, and more