code / tomo-patterns

Lines1.6K C1.0K Markdown343 Tomo178
(175 lines)
1 use ./patterns.tm
3 func main()
4 amelie := "Am\{UE9}lie"
5 amelie2 := "Am\{U65}\{U301}lie"
6 >> $Pat"e".replace_in("Hello", "X")
7 = "HXllo"
9 >> $Pat"l".is_in("Hello")
10 = yes
11 >> $Pat"l{end}".is_in("Hello")
12 = no
13 >> $Pat"{start}l".is_in("Hello")
14 = no
16 >> $Pat"o".is_in("Hello")
17 = yes
18 >> $Pat"o{end}".is_in("Hello")
19 = yes
20 >> $Pat"{start}o".is_in("Hello")
21 = no
23 >> $Pat"H".is_in("Hello")
24 = yes
25 >> $Pat"H{end}".is_in("Hello")
26 = no
27 >> $Pat"{start}H".is_in("Hello")
28 = yes
30 >> $Pat"l".replace_in("Hello", "")
31 = "Heo"
32 >> $Pat"x".replace_in("xxxx", "")
33 = ""
34 >> $Pat"y".replace_in("xxxx", "")
35 = "xxxx"
36 >> $Pat"e ".replace_in("One two three four five six", "")
37 = "Ontwo threfour fivsix"
39 >> $Pat"{start}{space}".replace_in(" one ", "")
40 = "one "
41 >> $Pat"{space}{end}".replace_in(" one ", "")
42 = " one"
44 >> amelie.has_pattern($Pat"$amelie2")
45 = yes
47 >> $Pat"{alpha}".replace_in("one two three", "")
48 = " "
49 >> $Pat"{alpha}".replace_in("one two three", "word")
50 = "word word word"
52 say("Test splitting and joining text:")
54 >> "one two three".split_pattern($Pat" ")
55 = ["one", "two", "three"]
57 >> "one,two,three,".split_pattern($Pat",")
58 = ["one", "two", "three", ""]
60 >> "one two three".split_pattern($Pat"{space}")
61 = ["one", "two", "three"]
63 >> "abc".split_pattern($Pat"")
64 = ["a", "b", "c"]
66 >> ", ".join(["one", "two", "three"])
67 = "one, two, three"
69 >> "".join(["one", "two", "three"])
70 = "onetwothree"
72 >> "+".join(["one"])
73 = "one"
75 >> "+".join([])
76 = ""
78 say("Test text.find_patterns()")
79 >> " #one #two #three ".find_patterns($Pat"#{alpha}")
80 = [PatternMatch(text="#one", index=2, captures=["one"]), PatternMatch(text="#two", index=8, captures=["two"]), PatternMatch(text="#three", index=13, captures=["three"])]
82 >> " #one #two #three ".find_patterns($Pat"#{!space}")
83 = [PatternMatch(text="#one", index=2, captures=["one"]), PatternMatch(text="#two", index=8, captures=["two"]), PatternMatch(text="#three", index=13, captures=["three"])]
85 >> " ".find_patterns($Pat"{alpha}")
86 = []
88 >> " foo(baz(), 1) doop() ".find_patterns($Pat"{id}(?)")
89 = [PatternMatch(text="foo(baz(), 1)", index=2, captures=["foo", "baz(), 1"]), PatternMatch(text="doop()", index=17, captures=["doop", ""])]
91 >> "".find_patterns($Pat'')
92 = []
94 >> "Hello".find_patterns($Pat'')
95 = []
97 say("Test text slicing:")
98 >> "abcdef".slice()
99 = "abcdef"
100 >> "abcdef".slice(from=3)
101 = "cdef"
102 >> "abcdef".slice(to=-2)
103 = "abcde"
104 >> "abcdef".slice(from=2, to=4)
105 = "bcd"
106 >> "abcdef".slice(from=5, to=1)
107 = ""
109 >> house := ""
110 = ""
111 >> house.length
112 = 1
113 >> house.codepoint_names()
114 = ["CJK Unified Ideographs-5BB6"]
115 >> house.utf32_codepoints()
116 = [23478]
118 >> "🐧".codepoint_names()
119 = ["PENGUIN"]
121 >> Text.from_codepoint_names(["not a valid name here buddy"])
122 = none
124 >> "one two; three four".find_patterns($Pat"; {..}")
125 = [PatternMatch(text="; three four", index=8, captures=["three four"])]
127 malicious := "{xxx}"
128 >> $Pat"$malicious"
129 = $Pat"{1{}xxx}"
131 >> $Pat"{lower}".replace_in("Hello", "(@0)")
132 = "H(ello)"
134 >> $Pat"foo(?)".replace_in(" foo(xyz) foo(yyy) foo(z()) ", "baz(@1)")
135 = " baz(xyz) baz(yyy) baz(z()) "
137 >> "<tag>".translate_patterns({$Pat"<"="&lt;", $Pat">"="&gt;"})
138 = "&lt;tag&gt;"
140 >> $Pat"BAD(?)", "good(@1)".replace_in(" BAD(x, fn(y), BAD(z), w) ", recursive=yes)
141 = " good(x, fn(y), good(z), w) "
143 >> $Pat"BAD(?)", "good(@1)".replace_in(" BAD(x, fn(y), BAD(z), w) ", recursive=no)
144 = " good(x, fn(y), BAD(z), w) "
146 >> "Hello".matches_pattern($Pat"{id}")
147 = yes
148 >> "Hello".matches_pattern($Pat"{lower}")
149 = no
150 >> "Hello".matches_pattern($Pat"{upper}")
151 = no
152 >> "Hello...".matches_pattern($Pat"{id}")
153 = no
155 >> "hello world".map_pattern($Pat"world", func(m:PatternMatch) m.text.upper())
156 = "hello WORLD"
158 >> "Abc".repeat(3)
159 = "AbcAbcAbc"
161 >> $Pat"{space}".trim(" abc def ")
162 = "abc def"
163 >> $Pat"{!digit}".trim(" abc123def ")
164 = "123"
165 >> $Pat"{!digit}".trim(" abc123def ", left=no)
166 = " abc123"
167 >> $Pat"{!digit}".trim(" abc123def ", right=no)
168 = "123def "
169 # Only trim single whole matches that bookend the text:
170 >> $Pat"Abc".trim("AbcAbcxxxxxxxxAbcAbc")
171 = "AbcxxxxxxxxAbc"
173 >> $Pat"{..}={..}".replace_in("A=B=C=D", "1:(@1) 2:(@2)")
174 = "1:(A) 2:(B=C=D)"