2024-04-28 11:58:55 -07:00
func main():
2025-03-10 21:09:17 -07:00
str := "Hello Amélie!"
2024-09-03 10:19:41 -07:00
!! Testing strings like $str
2024-07-04 13:23:05 -07:00
2024-04-12 10:09:31 -07:00
>> str:upper()
= "HELLO AMÉLIE!"
>> str:lower()
= "hello amélie!"
>> str:lower():title()
= "Hello Amélie!"
2024-12-26 14:31:33 -08:00
>> str[1]
= "H"
2024-03-03 14:49:40 -08:00
2025-03-07 13:19:12 -08:00
>> "I":lower()
= "i"
>> "I":lower(language="tr_TR")
= "ı "
>> "i":upper()
= "I"
>> "i":upper(language="tr_TR")
= "İ"
>> "ian":title()
= "Ian"
>> "ian":title(language="tr_TR")
= "İan"
>> "I":caseless_equals("ı ")
= no
>> "I":caseless_equals("ı ", language="tr_TR")
= yes
2024-12-26 14:31:33 -08:00
>> str[9]
= "é"
2024-07-04 13:23:05 -07:00
2024-04-12 10:09:31 -07:00
>> \UE9
= "é"
2024-03-03 16:12:53 -08:00
2024-04-12 10:09:31 -07:00
>> \U65\U301
= "é"
2024-03-03 16:12:53 -08:00
2024-09-11 22:43:00 -07:00
>> \{Penguin}:codepoint_names()
2024-09-06 22:43:36 -07:00
= ["PENGUIN"]
2024-09-11 22:43:00 -07:00
>> \[31;1]
2024-09-15 11:03:16 -07:00
= "$\e[31;1m"
2024-09-11 22:43:00 -07:00
2024-04-12 10:09:31 -07:00
>> \UE9 == \U65\U301
= yes
2024-03-03 16:40:01 -08:00
2025-03-10 21:09:17 -07:00
amelie := "Am$(\UE9)lie"
2024-09-02 20:07:08 -07:00
>> amelie:split()
2024-04-12 10:09:31 -07:00
= ["A", "m", "é", "l", "i", "e"] : [Text]
2024-09-02 17:22:13 -07:00
>> amelie:utf32_codepoints()
2024-11-19 10:41:52 -08:00
= [65, 109, 233, 108, 105, 101]
2024-11-19 10:30:45 -08:00
>> amelie:bytes()
2024-11-19 10:41:52 -08:00
= [0x41, 0x6D, 0xC3, 0xA9, 0x6C, 0x69, 0x65]
2024-11-05 12:18:32 -08:00
>> Text.from_bytes([:Byte 0x41, 0x6D, 0xC3, 0xA9, 0x6C, 0x69, 0x65])!
2024-09-15 12:33:47 -07:00
= "Amélie"
2024-11-05 12:18:32 -08:00
>> Text.from_bytes([Byte(0xFF)])
2024-12-07 13:04:25 -08:00
= none : Text?
2024-03-03 22:34:12 -08:00
2025-03-10 21:09:17 -07:00
amelie2 := "Am$(\U65\U301)lie"
2024-09-02 20:07:08 -07:00
>> amelie2:split()
2024-04-12 10:09:31 -07:00
= ["A", "m", "é", "l", "i", "e"] : [Text]
2024-09-02 17:22:13 -07:00
>> amelie2:utf32_codepoints()
2024-11-19 10:41:52 -08:00
= [65, 109, 233, 108, 105, 101]
2024-11-19 10:30:45 -08:00
>> amelie2:bytes()
2024-11-19 10:41:52 -08:00
= [0x41, 0x6D, 0xC3, 0xA9, 0x6C, 0x69, 0x65]
2024-09-02 17:22:13 -07:00
>> amelie:codepoint_names()
2024-09-02 18:18:15 -07:00
= ["LATIN CAPITAL LETTER A", "LATIN SMALL LETTER M", "LATIN SMALL LETTER E WITH ACUTE", "LATIN SMALL LETTER L", "LATIN SMALL LETTER I", "LATIN SMALL LETTER E"]
2024-09-02 17:22:13 -07:00
>> amelie2:codepoint_names()
2024-09-02 18:18:15 -07:00
= ["LATIN CAPITAL LETTER A", "LATIN SMALL LETTER M", "LATIN SMALL LETTER E WITH ACUTE", "LATIN SMALL LETTER L", "LATIN SMALL LETTER I", "LATIN SMALL LETTER E"]
2024-03-09 15:22:12 -08:00
2024-09-03 10:19:41 -07:00
>> "Hello":replace($/e/, "X")
2024-04-12 10:09:31 -07:00
= "HXllo"
2024-05-20 12:19:31 -07:00
2024-09-03 10:19:41 -07:00
>> "Hello":has($/l/)
2024-05-20 12:19:31 -07:00
= yes
2024-09-03 11:27:09 -07:00
>> "Hello":has($/l{end}/)
2024-05-20 12:19:31 -07:00
= no
2024-09-03 11:27:09 -07:00
>> "Hello":has($/{start}l/)
2024-05-20 12:19:31 -07:00
= no
2024-09-03 10:19:41 -07:00
>> "Hello":has($/o/)
2024-05-20 12:19:31 -07:00
= yes
2024-09-03 11:27:09 -07:00
>> "Hello":has($/o{end}/)
2024-05-20 12:19:31 -07:00
= yes
2024-09-03 11:27:09 -07:00
>> "Hello":has($/{start}o/)
2024-05-20 12:19:31 -07:00
= no
2024-09-03 10:19:41 -07:00
>> "Hello":has($/H/)
2024-05-20 12:19:31 -07:00
= yes
2024-09-03 11:27:09 -07:00
>> "Hello":has($/H{end}/)
2024-05-20 12:19:31 -07:00
= no
2024-09-03 11:27:09 -07:00
>> "Hello":has($/{start}H/)
2024-05-20 12:19:31 -07:00
= yes
2024-09-03 10:19:41 -07:00
>> "Hello":replace($/l/, "")
2024-05-20 12:19:31 -07:00
= "Heo"
2024-09-03 10:19:41 -07:00
>> "xxxx":replace($/x/, "")
2024-05-20 12:19:31 -07:00
= ""
2024-09-03 10:19:41 -07:00
>> "xxxx":replace($/y/, "")
2024-05-20 12:19:31 -07:00
= "xxxx"
2024-09-03 10:19:41 -07:00
>> "One two three four five six":replace($/e /, "")
2024-05-20 12:19:31 -07:00
= "Ontwo threfour fivsix"
2024-09-03 11:27:09 -07:00
>> " one ":replace($/{start}{space}/, "")
2024-09-02 17:22:13 -07:00
= "one "
2024-09-03 11:27:09 -07:00
>> " one ":replace($/{space}{end}/, "")
2024-09-02 17:22:13 -07:00
= " one"
2024-05-20 12:19:31 -07:00
2024-09-03 10:19:41 -07:00
>> amelie:has($/$amelie2/)
2025-03-10 21:09:17 -07:00
= yes
2024-07-01 10:09:26 -07:00
>> multiline := "
line one
line two
"
2024-09-15 11:03:16 -07:00
= "line one$\nline two"
2024-07-01 10:09:26 -07:00
2024-09-03 10:19:41 -07:00
!! Interpolation tests:
2024-08-18 11:44:15 -07:00
>> "A $(1+2)"
= "A 3"
>> 'A $(1+2)'
2024-09-15 11:03:16 -07:00
= 'A $(1+2)'
2024-08-18 11:44:15 -07:00
>> `A $(1+2)`
= "A 3"
>> $"A $(1+2)"
= "A 3"
>> $$"A $(1+2)"
2024-09-15 11:03:16 -07:00
= 'A $(1+2)'
2024-08-18 11:44:15 -07:00
>> $="A =(1+2)"
= "A 3"
2024-09-06 09:17:43 -07:00
>> ${one {nested} two $(1+2)}
= "one {nested} two 3"
2024-09-02 19:30:19 -07:00
2024-09-03 11:27:09 -07:00
>> "one two three":replace($/{alpha}/, "")
2024-09-02 19:42:02 -07:00
= " "
2024-09-03 11:27:09 -07:00
>> "one two three":replace($/{alpha}/, "word")
2024-09-02 19:42:02 -07:00
= "word word word"
2024-09-02 19:30:19 -07:00
2025-03-10 21:09:17 -07:00
c := "É̩"
2024-09-02 19:30:19 -07:00
>> c:codepoint_names()
= ["LATIN CAPITAL LETTER E WITH ACUTE", "COMBINING VERTICAL LINE BELOW"]
2024-11-03 22:17:47 -08:00
>> c == Text.from_codepoint_names(c:codepoint_names())!
2024-09-02 19:30:19 -07:00
= yes
>> c == Text.from_codepoints(c:utf32_codepoints())
= yes
2024-11-19 10:30:45 -08:00
>> c == Text.from_bytes(c:bytes())!
2024-09-02 19:30:19 -07:00
= yes
2024-09-02 19:57:49 -07:00
>> "one$(\n)two$(\n)three":lines()
= ["one", "two", "three"]
>> "one$(\n)two$(\n)three$(\n)":lines()
= ["one", "two", "three"]
>> "one$(\n)two$(\n)three$(\n\n)":lines()
= ["one", "two", "three", ""]
>> "one$(\r\n)two$(\r\n)three$(\r\n)":lines()
= ["one", "two", "three"]
2024-09-02 21:54:48 -07:00
>> "":lines()
= []
2024-09-02 20:07:08 -07:00
2024-09-03 10:19:41 -07:00
!! Test splitting and joining text:
>> "one two three":split($/ /)
2024-09-02 20:07:08 -07:00
= ["one", "two", "three"]
2024-09-03 10:19:41 -07:00
>> "one,two,three,":split($/,/)
2024-09-02 20:07:08 -07:00
= ["one", "two", "three", ""]
2024-09-03 11:27:09 -07:00
>> "one two three":split($/{space}/)
2024-09-02 20:07:08 -07:00
= ["one", "two", "three"]
2024-09-03 10:19:41 -07:00
>> "abc":split($//)
2024-09-02 20:07:08 -07:00
= ["a", "b", "c"]
2024-09-02 20:13:02 -07:00
>> ", ":join(["one", "two", "three"])
= "one, two, three"
>> "":join(["one", "two", "three"])
= "onetwothree"
>> "+":join(["one"])
= "one"
>> "+":join([:Text])
= ""
2024-09-02 20:24:16 -07:00
>> "":split()
= []
2024-09-02 20:26:55 -07:00
2024-09-03 10:19:41 -07:00
!! Test text:find_all()
2024-11-09 13:27:54 -08:00
>> " #one #two #three ":find_all($/#{alpha}/)
= [Match(text="#one", index=2, captures=["one"]), Match(text="#two", index=8, captures=["two"]), Match(text="#three", index=13, captures=["three"])]
2024-09-02 20:26:55 -07:00
2024-11-09 13:27:54 -08:00
>> " #one #two #three ":find_all($/#{!space}/)
= [Match(text="#one", index=2, captures=["one"]), Match(text="#two", index=8, captures=["two"]), Match(text="#three", index=13, captures=["three"])]
2024-09-02 20:26:55 -07:00
2024-09-03 11:27:09 -07:00
>> " ":find_all($/{alpha}/)
2024-09-02 20:26:55 -07:00
= []
2024-09-03 11:27:09 -07:00
>> " foo(baz(), 1) doop() ":find_all($/{id}(?)/)
2024-11-09 13:27:54 -08:00
= [Match(text="foo(baz(), 1)", index=2, captures=["foo", "baz(), 1"]), Match(text="doop()", index=17, captures=["doop", ""])]
2024-09-02 20:26:55 -07:00
2024-09-03 10:19:41 -07:00
>> "":find_all($Pattern'')
2024-09-02 20:26:55 -07:00
= []
2024-09-03 10:19:41 -07:00
>> "Hello":find_all($Pattern'')
2024-09-02 20:26:55 -07:00
= []
2024-09-02 20:47:16 -07:00
2024-09-03 10:19:41 -07:00
!! Test text:find()
2024-09-03 11:27:09 -07:00
>> " one two three ":find($/{id}/, start=-999)
2024-12-07 13:04:25 -08:00
= none : Match?
2024-09-03 11:27:09 -07:00
>> " one two three ":find($/{id}/, start=999)
2024-12-07 13:04:25 -08:00
= none : Match?
2024-09-03 11:27:09 -07:00
>> " one two three ":find($/{id}/)
2024-11-21 10:00:53 -08:00
= Match(text="one", index=2, captures=["one"]) : Match?
2024-09-03 11:27:09 -07:00
>> " one two three ":find($/{id}/, start=5)
2024-11-21 10:00:53 -08:00
= Match(text="two", index=8, captures=["two"]) : Match?
2024-09-02 20:47:16 -07:00
2024-09-03 10:19:41 -07:00
!! Test text slicing:
2024-09-02 20:56:08 -07:00
>> "abcdef":slice()
= "abcdef"
>> "abcdef":slice(from=3)
= "cdef"
>> "abcdef":slice(to=-2)
= "abcde"
>> "abcdef":slice(from=2, to=4)
= "bcd"
>> "abcdef":slice(from=5, to=1)
= ""
2024-09-02 22:20:36 -07:00
>> house := "家"
= "家"
>> house.length
= 1
>> house:codepoint_names()
2024-09-02 22:30:07 -07:00
= ["CJK Unified Ideographs-5BB6"]
2024-09-02 22:20:36 -07:00
>> house:utf32_codepoints()
2024-11-19 10:41:52 -08:00
= [23478]
2024-09-02 22:30:07 -07:00
>> "🐧":codepoint_names()
= ["PENGUIN"]
2024-09-02 22:32:00 -07:00
>> Text.from_codepoint_names(["not a valid name here buddy"])
2024-12-07 13:04:25 -08:00
= none : Text?
2024-09-02 22:32:00 -07:00
2024-09-03 11:27:09 -07:00
>> "one two; three four":find_all($/; {..}/)
2024-11-09 13:27:54 -08:00
= [Match(text="; three four", index=8, captures=["three four"])]
2024-09-03 11:27:09 -07:00
2025-03-10 21:09:17 -07:00
malicious := "{xxx}"
2024-09-03 10:19:41 -07:00
>> $/$malicious/
2024-09-03 11:27:09 -07:00
= $/{1{}xxx}/
2024-09-03 10:19:41 -07:00
2024-09-03 19:56:53 -07:00
>> "Hello":replace($/{lower}/, "(\0)")
2024-09-03 17:48:11 -07:00
= "H(ello)"
2024-09-03 19:56:53 -07:00
>> " foo(xyz) foo(yyy) foo(z()) ":replace($/foo(?)/, "baz(\1)")
= " baz(xyz) baz(yyy) baz(z()) "
2024-09-03 17:48:11 -07:00
2025-01-12 13:49:58 -08:00
>> "<tag>":replace_all({$/</="<", $/>/=">"})
2024-09-03 17:48:11 -07:00
= "<tag>"
2024-09-03 10:19:41 -07:00
2024-09-03 20:16:45 -07:00
>> " BAD(x, fn(y), BAD(z), w) ":replace($/BAD(?)/, "good(\1)", recursive=yes)
= " good(x, fn(y), good(z), w) "
>> " BAD(x, fn(y), BAD(z), w) ":replace($/BAD(?)/, "good(\1)", recursive=no)
= " good(x, fn(y), BAD(z), w) "
2024-09-03 21:34:27 -07:00
>> "Hello":matches($/{id}/)
2024-11-21 10:00:53 -08:00
= ["Hello"] : [Text]?
2024-09-03 21:34:27 -07:00
>> "Hello":matches($/{lower}/)
2024-12-07 13:04:25 -08:00
= none : [Text]?
2024-09-03 21:34:27 -07:00
>> "Hello":matches($/{upper}/)
2024-12-07 13:04:25 -08:00
= none : [Text]?
2024-09-03 21:34:27 -07:00
>> "Hello...":matches($/{id}/)
2024-12-07 13:04:25 -08:00
= none : [Text]?
2024-09-16 12:12:54 -07:00
if matches := "hello world":matches($/{id} {id}/):
>> matches
= ["hello", "world"]
else:
fail("Failed to match")
2024-09-04 18:02:37 -07:00
2024-11-09 13:27:54 -08:00
>> "hello world":map($/world/, func(m:Match): m.text:upper())
2024-09-04 18:02:37 -07:00
= "hello WORLD"
2024-09-04 18:22:03 -07:00
>> "Abc":repeat(3)
= "AbcAbcAbc"
2024-09-05 00:47:15 -07:00
2024-09-05 21:03:28 -07:00
>> " abc def ":trim()
= "abc def"
>> " abc123def ":trim($/{!digit}/)
= "123"
>> " abc123def ":trim($/{!digit}/, trim_left=no)
= " abc123"
>> " abc123def ":trim($/{!digit}/, trim_right=no)
= "123def "
# Only trim single whole matches that bookend the text:
>> "AbcAbcxxxxxxxxAbcAbc":trim($/Abc/)
= "AbcxxxxxxxxAbc"
2024-09-06 00:29:07 -07:00
>> "A=B=C=D":replace($/{..}={..}/, "1:(\1) 2:(\2)")
= "1:(A) 2:(B=C=D)"
2024-09-09 02:31:25 -07:00
>> "abcde":starts_with("ab")
= yes
>> "abcde":starts_with("bc")
= no
>> "abcde":ends_with("de")
= yes
>> "abcde":starts_with("cd")
= no
2024-12-19 12:32:19 -08:00
>> ("hello" ++ " " ++ "Amélie"):reversed()
= "eilémA olleh" : Text
2024-09-05 00:47:15 -07:00
do:
!! Testing concatenation-stability:
2025-03-10 21:09:17 -07:00
ab := Text.from_codepoint_names(["LATIN SMALL LETTER E", "COMBINING VERTICAL LINE BELOW"])!
2024-09-05 00:47:15 -07:00
>> ab:codepoint_names()
= ["LATIN SMALL LETTER E", "COMBINING VERTICAL LINE BELOW"]
>> ab.length
= 1
2025-03-10 21:09:17 -07:00
a := Text.from_codepoint_names(["LATIN SMALL LETTER E"])!
b := Text.from_codepoint_names(["COMBINING VERTICAL LINE BELOW"])!
2024-09-05 00:47:15 -07:00
>> (a++b):codepoint_names()
= ["LATIN SMALL LETTER E", "COMBINING VERTICAL LINE BELOW"]
>> (a++b) == ab
= yes
>> (a++b).length
= 1
2025-02-07 17:06:00 -08:00
do:
2025-03-10 21:09:17 -07:00
concat := "e" ++ Text.from_codepoints([Int32(0x300)])
2025-02-07 17:06:00 -08:00
>> concat.length
= 1
2025-03-10 21:09:17 -07:00
concat2 := concat ++ Text.from_codepoints([Int32(0x302)])
2025-02-07 17:06:00 -08:00
>> concat2.length
= 1
2025-03-10 21:09:17 -07:00
concat3 := concat2 ++ Text.from_codepoints([Int32(0x303)])
2025-02-07 17:06:00 -08:00
>> concat3.length
= 1
2025-03-10 21:09:17 -07:00
final := Text.from_codepoints([Int32(0x65), Int32(0x300), Int32(0x302), Int32(0x303)])
2025-02-07 17:06:00 -08:00
>> final.length
= 1
>> concat3 == final
= yes
2025-03-10 21:09:17 -07:00
concat4 := Text.from_codepoints([Int32(0x65), Int32(0x300)]) ++ Text.from_codepoints([Int32(0x302), Int32(0x303)])
2025-02-07 17:06:00 -08:00
>> concat4.length
= 1
>> concat4 == final
= yes
2025-03-07 13:56:23 -08:00
>> "x":left_pad(5)
= " x"
>> "x":right_pad(5)
= "x "
>> "x":middle_pad(5)
= " x "
>> "1234":left_pad(8, "XYZ")
= "XYZX1234" : Text
>> "1234":right_pad(8, "XYZ")
= "1234XYZX" : Text
>> "1234":middle_pad(9, "XYZ")
= "XY1234XYZ" : Text