1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
|
Text.as_c_string:
description: >
Converts a `Text` value to a C-style string.
return:
type: 'CString'
description: >
A C-style string (`CString`) representing the text.
args:
text:
type: 'Text'
description: >
The text to be converted to a C-style string.
example: |
>> "Hello".as_c_string()
= CString("Hello")
Text.at:
description: >
Get the graphical cluster at a given index. This is similar to `str[i]` with
ASCII text, but has more correct behavior for unicode text.
return:
type: 'Text'
description: >
A `Text` with the single graphical cluster at the given index.
note: >
Negative indices are counted from the back of the text, so `-1` means the
last cluster, `-2` means the second-to-last, and so on.
args:
text:
type: 'Text'
description: >
The text from which to get a cluster.
index:
type: 'Int'
description: >
The index of the graphical cluster (1-indexed).
example: |
>> "Amélie".at(3)
= "é"
Text.by_line:
description: >
Returns an iterator function that can be used to iterate over the lines in a
text.
return:
type: 'func(->Text?)'
description: >
An iterator function that returns one line at a time, until it runs out and
returns `none`.
note: >
This function ignores a trailing newline if there is one. If you don't want
this behavior, use `text.by_split($/{1 nl}/)` instead.
args:
text:
type: 'Text'
description: >
The text to be iterated over, line by line.
example: |
text := "
line one
line two
"
for line in text.by_line()
# Prints: "line one" then "line two":
say(line)
Text.by_split:
description: >
Returns an iterator function that can be used to iterate over text separated by
a delimiter.
return:
type: 'func(->Text?)'
description: >
An iterator function that returns one chunk of text at a time, separated by the
given delimiter, until it runs out and returns `none`.
args:
text:
type: 'Text'
description: >
The text to be iterated over in delimited chunks.
delimiter:
type: 'Text'
default: '""'
description: >
An exact delimiter to use for splitting the text.
note: >
To split based on a set of delimiters, use Text.by_split_any().
If an empty text is given as the delimiter, then each split will be the
graphical clusters of the text.
example: |
text := "one,two,three"
for chunk in text.by_split(",")
# Prints: "one" then "two" then "three":
say(chunk)
Text.by_split_any:
description: >
Returns an iterator function that can be used to iterate over text separated by
one or more characters (grapheme clusters) from a given text of delimiters.
return:
type: 'func(->Text?)'
description: >
An iterator function that returns one chunk of text at a time, separated by the
given delimiter characters, until it runs out and returns `none`.
args:
text:
type: 'Text'
description: >
The text to be iterated over in delimited chunks.
delimiters:
type: 'Text'
default: '" $\t\r\n"'
description: >
Grapheme clusters to use for splitting the text.
note: >
Splitting will occur on every place where one or more of the grapheme
clusters in `delimiters` occurs.
To split based on an exact delimiter, use Text.by_split().
example: |
text := "one,two,;,three"
for chunk in text.by_split_any(",;")
# Prints: "one" then "two" then "three":
say(chunk)
Text.bytes:
description: >
Converts a `Text` value to a list of bytes representing a UTF8 encoding of
the text.
return:
type: '[Byte]'
description: >
A list of bytes (`[Byte]`) representing the text in UTF8 encoding.
args:
text:
type: 'Text'
description: >
The text to be converted to UTF8 bytes.
example: |
>> "Amélie".bytes()
= [65, 109, 195, 169, 108, 105, 101]
Text.caseless_equals:
description: >
Checks whether two texts are equal, ignoring the casing of the letters (i.e.
case-insensitive comparison).
return:
type: 'Bool'
description: >
`yes` if `a` and `b` are equal to each other, ignoring casing, otherwise `no`.
args:
a:
type: 'Text'
description: >
The first text to compare case-insensitively.
b:
type: 'Text'
description: >
The second text to compare case-insensitively.
language:
type: 'Text'
default: '"C"'
description: >
The ISO 639 language code for which casing rules to use.
example: |
>> "A".caseless_equals("a")
= yes
# Turkish lowercase "I" is "ı" (dotless I), not "i"
>> "I".caseless_equals("i", language="tr_TR")
= no
Text.codepoint_names:
description: >
Returns a list of the names of each codepoint in the text.
return:
type: '[Text]'
description: >
A list of codepoint names (`[Text]`).
args:
text:
type: 'Text'
description: >
The text from which to extract codepoint names.
example: |
>> "Amélie".codepoint_names()
= ["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"]
Text.ends_with:
description: >
Checks if the `Text` ends with a literal suffix text.
return:
type: 'Bool'
description: >
`yes` if the text has the target, `no` otherwise.
args:
text:
type: 'Text'
description: >
The text to be searched.
suffix:
type: 'Text'
description: >
The literal suffix text to check for.
example: |
>> "hello world".ends_with("world")
= yes
Text.from:
description: >
Get a slice of the text, starting at the given position.
return:
type: 'Text'
description: >
The text from the given grapheme cluster to the end of the text.
note: >
A negative index counts backwards from the end of the text, so `-1` refers
to the last cluster, `-2` the second-to-last, etc. Slice ranges will be
truncated to the length of the text.
args:
text:
type: 'Text'
description: >
The text to be sliced.
first:
type: 'Int'
description: >
The index to begin the slice.
example: |
>> "hello".from(2)
= "ello"
>> "hello".from(-2)
= "lo"
Text.from_bytes:
description: >
Returns text that has been constructed from the given UTF8 bytes.
note: >
The text will be normalized, so the resulting text's UTF8 bytes may not
exactly match the input.
return:
type: '[Text]'
description: >
A new text based on the input UTF8 bytes after normalization has been applied.
args:
bytes:
type: '[Byte]'
description: >
The UTF-8 bytes of the desired text.
example: |
>> Text.from_bytes([195, 133, 107, 101])
= "Åke"
Text.from_c_string:
description: >
Converts a C-style string to a `Text` value.
return:
type: 'Text'
description: >
A `Text` value representing the C-style string.
args:
str:
type: 'CString'
description: >
The C-style string to be converted.
example: |
>> Text.from_c_string(CString("Hello"))
= "Hello"
Text.from_codepoint_names:
description: >
Returns text that has the given codepoint names (according to the Unicode
specification) as its codepoints.
note: >
The text will be normalized, so the resulting text's codepoints may not
exactly match the input codepoints.
return:
type: '[Text]'
description: >
A new text with the specified codepoints after normalization has been applied.
Any invalid names are ignored.
args:
codepoint_names:
type: '[Text]'
description: >
The names of each codepoint in the desired text (case-insentive).
example: |
>> Text.from_codepoint_names([
"LATIN CAPITAL LETTER A WITH RING ABOVE",
"LATIN SMALL LETTER K",
"LATIN SMALL LETTER E",
]
= "Åke"
Text.from_codepoints:
description: >
Returns text that has been constructed from the given UTF32 codepoints.
note: >
The text will be normalized, so the resulting text's codepoints may not
exactly match the input codepoints.
return:
type: '[Text]'
description: >
A new text with the specified codepoints after normalization has been applied.
args:
codepoints:
type: '[Int32]'
description: >
The UTF32 codepoints in the desired text.
example: |
>> Text.from_codepoints([197, 107, 101])
= "Åke"
Text.has:
description: >
Checks if the `Text` contains some target text.
return:
type: 'Bool'
description: >
`yes` if the target text is found, `no` otherwise.
args:
text:
type: 'Text'
description: >
The text to be searched.
target:
type: 'Text'
description: >
The text to search for.
example: |
>> "hello world".has("wo")
= yes
>> "hello world".has("xxx")
= no
Text.join:
description: >
Joins a list of text pieces with a specified glue.
return:
type: 'Text'
description: >
A single `Text` value with the pieces joined by the glue.
args:
glue:
type: 'Text'
description: >
The text used to join the pieces.
pieces:
type: '[Text]'
description: >
The list of text pieces to be joined.
example: |
>> ", ".join(["one", "two", "three"])
= "one, two, three"
Text.middle_pad:
description: >
Pad some text on the left and right side so it reaches a target width.
return:
type: 'Text'
description: >
Text with length at least `width`, with extra padding on the left and right as
needed. If `pad` has length greater than 1, it may be partially repeated to
reach the exact desired length.
args:
text:
type: 'Text'
description: >
The text to pad.
width:
type: 'Int'
description: >
The target width.
pad:
type: 'Text'
default: '" "'
description: >
The padding text.
language:
type: 'Text'
default: '"C"'
description: >
The ISO 639 language code for which character width to use.
example: |
>> "x".middle_pad(6)
= " x "
>> "x".middle_pad(10, "ABC")
= "ABCAxABCAB"
Text.left_pad:
description: >
Pad some text on the left side so it reaches a target width.
return:
type: 'Text'
description: >
Text with length at least `width`, with extra padding on the left as needed. If
`pad` has length greater than 1, it may be partially repeated to reach the
exact desired length.
args:
text:
type: 'Text'
description: >
The text to pad.
width:
type: 'Int'
description: >
The target width.
pad:
type: 'Text'
default: '" "'
description: >
The padding text.
language:
type: 'Text'
default: '"C"'
description: >
The ISO 639 language code for which character width to use.
example: |
>> "x".left_pad(5)
= " x"
>> "x".left_pad(5, "ABC")
= "ABCAx"
Text.lines:
description: >
Splits the text into a list of lines of text, preserving blank lines,
ignoring trailing newlines, and handling `\r\n` the same as `\n`.
return:
type: '[Text]'
description: >
A list of substrings resulting from the split.
args:
text:
type: 'Text'
description: >
The text to be split into lines.
example: |
>> "one\ntwo\nthree".lines()
= ["one", "two", "three"]
>> "one\ntwo\nthree\n".lines()
= ["one", "two", "three"]
>> "one\ntwo\nthree\n\n".lines()
= ["one", "two", "three", ""]
>> "one\r\ntwo\r\nthree\r\n".lines()
= ["one", "two", "three"]
>> "".lines()
= []
Text.lower:
description: >
Converts all characters in the text to lowercase.
return:
type: 'Text'
description: >
The lowercase version of the text.
args:
text:
type: 'Text'
description: >
The text to be converted to lowercase.
language:
type: 'Text'
default: '"C"'
description: >
The ISO 639 language code for which casing rules to use.
example: |
>> "AMÉLIE".lower()
= "amélie"
>> "I".lower(language="tr_TR")
>> "ı"
Text.quoted:
description: >
Formats the text with quotation marks and escapes.
return:
type: 'Text'
description: >
The text formatted as a quoted text.
args:
text:
type: 'Text'
description: >
The text to be quoted.
color:
type: 'Bool'
default: 'no'
description: >
Whether to add color formatting.
quotation_mark:
type: 'Text'
default: '`"`'
description: >
The quotation mark to use.
example: |
>> "one\ntwo".quoted()
= "\"one\\ntwo\""
Text.repeat:
description: >
Repeat some text multiple times.
return:
type: 'Text'
description: >
The text repeated the given number of times.
args:
text:
type: 'Text'
description: >
The text to repeat.
count:
type: 'Int'
description: >
The number of times to repeat it. (Negative numbers are equivalent to zero).
example: |
>> "Abc".repeat(3)
= "AbcAbcAbc"
Text.replace:
description: >
Replaces occurrences of a target text with a replacement text.
return:
type: 'Text'
description: >
The text with occurrences of the target replaced.
args:
text:
type: 'Text'
description: >
The text in which to perform replacements.
target:
type: 'Text'
description: >
The target text to be replaced.
replacement:
type: 'Text'
description: >
The text to replace the target with.
example: |
>> "Hello world".replace("world", "there")
= "Hello there"
Text.reversed:
description: >
Return a text that has the grapheme clusters in reverse order.
return:
type: 'Text'
description: >
A reversed version of the text.
args:
text:
type: 'Text'
description: >
The text to reverse.
example: |
>> "Abc".reversed()
= "cbA"
Text.right_pad:
description: >
Pad some text on the right side so it reaches a target width.
return:
type: 'Text'
description: >
Text with length at least `width`, with extra padding on the right as needed. If
`pad` has length greater than 1, it may be partially repeated to reach the
exact desired length.
args:
text:
type: 'Text'
description: >
The text to pad.
width:
type: 'Int'
description: >
The target width.
pad:
type: 'Text'
default: '" "'
description: >
The padding text.
language:
type: 'Text'
default: '"C"'
description: >
The ISO 639 language code for which character width to use.
example: |
>> "x".right_pad(5)
= "x "
>> "x".right_pad(5, "ABC")
= "xABCA"
Text.slice:
description: >
Get a slice of the text.
return:
type: 'Text'
description: >
The text that spans the given grapheme cluster indices.
note: >
A negative index counts backwards from the end of the text, so `-1` refers
to the last cluster, `-2` the second-to-last, etc. Slice ranges will be
truncated to the length of the text.
args:
text:
type: 'Text'
description: >
The text to be sliced.
from:
type: 'Int'
default: '1'
description: >
The index of the first grapheme cluster to include (1-indexed).
to:
type: 'Int'
default: '-1'
description: >
The index of the last grapheme cluster to include (1-indexed).
example: |
>> "hello".slice(2, 3)
= "el"
>> "hello".slice(to=-2)
= "hell"
>> "hello".slice(from=2)
= "ello"
Text.split:
description: >
Splits the text into a list of substrings based on exact matches of a delimiter.
return:
type: '[Text]'
description: >
A list of subtexts resulting from the split.
args:
text:
type: 'Text'
description: >
The text to be split.
delimiter:
type: 'Text'
default: '""'
description: >
The delimiter used to split the text.
note: >
To split based on a set of delimiters, use Text.split_any().
If an empty text is given as the delimiter, then each split will be the
graphical clusters of the text.
example: |
>> "one,two,,three".split(",")
= ["one", "two", "", "three"]
>> "abc".split()
= ["a", "b", "c"]
Text.split_any:
description: >
Splits the text into a list of substrings at one or more occurrences of a set
of delimiter characters (grapheme clusters).
return:
type: '[Text]'
description: >
A list of subtexts resulting from the split.
args:
text:
type: 'Text'
description: >
The text to be split.
delimiters:
type: 'Text'
default: '" $\t\r\n"'
description: >
A text containing delimiters to use for splitting the text.
note: >
Splitting will occur on every place where one or more of the grapheme
clusters in `delimiters` occurs.
To split based on an exact delimiter, use Text.split().
example: |
>> "one, two,,three".split_any(", ")
= ["one", "two", "three"]
Text.starts_with:
description: >
Checks if the `Text` starts with a literal prefix text.
return:
type: 'Bool'
description: >
`yes` if the text has the given prefix, `no` otherwise.
args:
text:
type: 'Text'
description: >
The text to be searched.
prefix:
type: 'Text'
description: >
The literal prefix text to check for.
example: |
>> "hello world".starts_with("hello")
= yes
Text.title:
description: >
Converts the text to title case (capitalizing the first letter of each word).
return:
type: 'Text'
description: >
The text in title case.
args:
text:
type: 'Text'
description: >
The text to be converted to title case.
language:
type: 'Text'
default: '"C"'
description: >
The ISO 639 language code for which casing rules to use.
example: |
>> "amélie".title()
= "Amélie"
# In Turkish, uppercase "i" is "İ"
>> "i".title(language="tr_TR")
= "İ"
Text.to:
description: >
Get a slice of the text, ending at the given position.
return:
type: 'Text'
description: >
The text up to and including the given grapheme cluster.
note: >
A negative index counts backwards from the end of the text, so `-1` refers
to the last cluster, `-2` the second-to-last, etc. Slice ranges will be
truncated to the length of the text.
args:
text:
type: 'Text'
description: >
The text to be sliced.
last:
type: 'Int'
description: >
The index of the last grapheme cluster to include (1-indexed).
example: |
>> "goodbye".to(3)
= "goo"
>> "goodbye".to(-2)
= "goodby"
Text.translate:
description: >
Takes a table mapping target texts to their replacements and performs all the
replacements in the table on the whole text. At each position, the first
matching replacement is applied and the matching moves on to *after* the
replacement text, so replacement text is not recursively modified. See
Text.replace() for more information about replacement behavior.
return:
type: 'Text'
description: >
The text with all occurrences of the targets replaced with their corresponding
replacement text.
args:
translations:
type: '{Text=Text}'
description: >
A table mapping from target text to its replacement.
example: |
>> "A <tag> & an amperand".translate({
"&" = "&",
"<" = "<",
">" = ">",
'"" = """,
"'" = "'",
}
= "A <tag> & an ampersand"
Text.trim:
description: >
Trims the given characters (grapheme clusters) from the left and/or right side of the text.
return:
type: 'Text'
description: >
The text without the trim characters at either end.
args:
text:
type: 'Text'
description: >
The text to be trimmed.
to_trim:
type: 'Text'
default: '" $\t\r\n"'
description: >
The characters to remove from the left/right of the text.
left:
type: 'Bool'
default: 'yes'
description: >
Whether or not to trim from the front of the text.
right:
type: 'Bool'
default: 'yes'
description: >
Whether or not to trim from the back of the text.
example: |
>> " x y z \n".trim()
= "x y z"
>> "one,".trim(",")
= "one"
>> " xyz ".trim(right=no)
= "xyz "
Text.upper:
description: >
Converts all characters in the text to uppercase.
return:
type: 'Text'
description: >
The uppercase version of the text.
args:
text:
type: 'Text'
description: >
The text to be converted to uppercase.
language:
type: 'Text'
default: '"C"'
description: >
The ISO 639 language code for which casing rules to use.
example: |
>> "amélie".upper()
= "AMÉLIE"
# In Turkish, uppercase "i" is "İ"
>> "i".upper(language="tr_TR")
= "İ"
Text.utf32_codepoints:
description: >
Returns a list of Unicode code points for UTF32 encoding of the text.
return:
type: '[Int32]'
description: >
A list of 32-bit integer Unicode code points (`[Int32]`).
args:
text:
type: 'Text'
description: >
The text from which to extract Unicode code points.
example: |
>> "Amélie".utf32_codepoints()
= [65, 109, 233, 108, 105, 101]
Text.width:
description: >
Returns the display width of the text as seen in a terminal with appropriate
font rendering. This is usually the same as the text's `.length`, but there are
some characters like emojis that render wider than 1 cell.
note: >
This will not always be exactly accurate when your terminal's font
rendering can't handle some unicode displaying correctly.
return:
type: 'Int'
description: >
An integer representing the display width of the text.
args:
text:
type: 'Text'
description: >
The text whose length you want.
example: |
>> "Amélie".width()
= 6
>> "🤠".width()
= 2
Text.without_prefix:
description: >
Returns the text with a given prefix removed (if present).
return:
type: 'Text'
description: >
A text without the given prefix (if present) or the unmodified text if the
prefix is not present.
args:
text:
type: 'Text'
description: >
The text to remove the prefix from.
prefix:
type: 'Text'
description: >
The prefix to remove.
example: |
>> "foo:baz".without_prefix("foo:")
= "baz"
>> "qux".without_prefix("foo:")
= "qux"
Text.without_suffix:
description: >
Returns the text with a given suffix removed (if present).
return:
type: 'Text'
description: >
A text without the given suffix (if present) or the unmodified text if the
suffix is not present.
args:
text:
type: 'Text'
description: >
The text to remove the suffix from.
suffix:
type: 'Text'
description: >
The suffix to remove.
example: |
>> "baz.foo".without_suffix(".foo")
= "baz"
>> "qux".without_suffix(".foo")
= "qux"
|