on c19bc7a help sign in
1003 lines 15 kB history raw
1:- :~ navhome/'/docs/'
2 sort/'29'
3 title/'4b: Text Processing'
4 ==
6;>
8# 4b: text processing
10### `++at`
12```
13 ++ at
14 |_ a=@
15```
17XX document
21### `++r`
23```
24 ++ r
25 ?: ?& (gte (met 3 a) 2)
26 |-
27 ?: =(0 a)
28 &
29 =+ vis=(end 3 1 a)
30 ?& ?|(=('-' vis) ?&((gte vis 'a') (lte vis 'z')))
31 $(a (rsh 3 1 a))
32 ==
33 ==
34 rtam
35 ?: (lte (met 3 a) 2)
36 rud
37 rux
38```
41XX document
43### `++rf`
45```
46 ++ rf `tape`[?-(a $& '&', $| '|', * !!) ~]
47```
50XX document
52### `++rn`
54```
55 ++ rn `tape`[?>(=(0 a) '~') ~]
56```
58XX document
60### `++rt`
62```
63 ++ rt `tape`['\'' (weld (mesc (trip a)) `tape`['\'' ~])]
64```
66XX document
68### `++rta`
70```
71 ++ rta rt
72```
74XX document
76### `++rtam`
78```
79 ++ rtam `tape`['%' (trip a)]
80```
82XX document
84### `++rub`
86```
87 ++ rub `tape`['0' 'b' (rum 2 ~ |=(b/@ (add '0' b)))]
88```
90XX document
92### `++rud`
94```
95 ++ rud (rum 10 ~ |=(b/@ (add '0' b)))
96```
98XX document
100### `++rum`
102```
103 ++ rum
104 |= {b/@ c/tape d/$-(@ @)}
105 ^- tape
106 ?: =(0 a)
107 [(d 0) c]
108 =+ e=0
109 |- ^- tape
110 ?: =(0 a)
111 c
112 =+ f=&(!=(0 e) =(0 (mod e ?:(=(10 b) 3 4))))
113 %= $
114 a (div a b)
115 c [(d (mod a b)) ?:(f [?:(=(10 b) ',' '-') c] c)]
116 e +(e)
117 ==
118 ::
119```
121XX document
123### `++rup`
125```
126 ++ rup
127 =+ b=(met 3 a)
128 ^- tape
129 :- '-'
130 |- ^- tape
131 ?: (gth (met 5 a) 1)
132 %+ weld
133 $(a (rsh 5 1 a), b (sub b 4))
134 `tape`['-' '-' $(a (end 5 1 a), b 4)]
135 ?: =(0 b)
136 ['~' ~]
137 ?: (lte b 1)
138 (trip (tos:po a))
139 |- ^- tape
140 ?: =(2 b)
141 =+ c=(rsh 3 1 a)
142 =+ d=(end 3 1 a)
143 (weld (trip (tod:po c)) (trip (tos:po (mix c d))))
144 =+ c=(rsh 3 2 a)
145 =+ d=(end 3 2 a)
146 (weld ^$(a c, b (met 3 c)) `tape`['-' $(a (mix c d), b 2)])
147 ::
148```
150XX document
152### `++ruv`
154```
155 ++ ruv
156 ^- tape
157 :+ '0'
158 'v'
159 %^ rum
160 64
161 ~
162 |= b/@
163 ?: =(63 b)
164 '+'
165 ?: =(62 b)
166 '-'
167 ?:((lth b 26) (add 65 b) ?:((lth b 52) (add 71 b) (sub b 4)))
168 ::
169```
171XX document
173### `++rux`
175```
176 ++ rux `tape`['0' 'x' (rum 16 ~ |=(b/@ (add b ?:((lth b 10) 48 87))))]
177 --
178 :::::::::::::::::::::::::::::::::::::::::::::::::::::: ::
179```
181XX document
183---
186---
188### `++cass`
190To lowercase
192Produce the case insensitive (all lowercase) `++cord` of a `++tape`.
194#### Accepts
196`vib` is a `++tape`.
198#### Produces
200A `++cord`.
202#### Source
204```
205 ++ cass :: lowercase
206 |= vib/tape
207 %+ rap 3
208 (turn vib |=(a/@ ?.(&((gte a 'A') (lte a 'Z')) a (add 32 a))))
209```
211#### Examples
213```
214 > (cass "john doe")
215 7.309.170.810.699.673.450
217 > `cord`(cass "john doe")
218 'john doe'
220 > (cass "abc, 123, !@#")
221 2.792.832.775.110.938.439.066.079.945.313
223 > `cord`(cass "abc, 123, !@#")
224 'abc, 123, !@#'
225```
227---
228### `++crip`
230Tape to cord
232Produce a `++cord` from a `++tape`.
234#### Accepts
236`a` is a `++tape`.
238#### Produces
240A `++cord`.
242#### Source
244```
245 ++ crip |=(a=tape `@t`(rap 3 a)) :: tape to cord
246```
248#### Examples
250```
251 > (crip "john doe")
252 'john doe'
254 > (crip "abc 123 !@#")
255 'abc 123 !@#'
257 > `@ud`(crip "abc")
258 6.513.249
259```
261---
262### `++cuss`
264To uppercase
266Turn all occurances of lowercase letters in any `++tape` into uppercase
267letters, as a `++cord`.
269#### Accepts
271`vib` is a `++tape`.
273#### Produces
275A `++cord`.
277#### Source
279```
280 ++ cuss :: uppercase
281 |= vib=tape
282 ^- @t
283 %+ rap 3
284 (turn vib |=(a=@ ?.(&((gte a 'a') (lte a 'z')) a (sub a 32))))
285```
287#### Examples
289```
290 > (cuss "john doe")
291 'JOHN DOE'
293 > (cuss "abc ABC 123 !@#")
294 'ABC ABC 123 !@#'
296 > `@ud`(cuss "abc")
297 4.407.873
299 > (cuss "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsQqRrVvWwXxYyZz")
300 'AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPPQQRRSSQQRRVVWWXXYYZZ'
301```
303---
304### `++mesc`
306Escape special chars
308Escape special characters, used in `++show`.
310#### Accepts
312`vib` is a `++tape`.
314#### Produces
316A `++tape`.
318#### Source
320```
321 ++ mesc :: ctrl code escape
322 |= vib/tape
323 ^- tape
324 ?~ vib
325 ~
326 ?: =('\\' i.vib)
327 ['\\' '\\' $(vib t.vib)]
328 ?: ?|((gth i.vib 126) (lth i.vib 32) =(`@`39 i.vib))
329 ['\\' (welp ~(rux at i.vib) '/' $(vib t.vib))]
330 [i.vib $(vib t.vib)]
331```
333#### Examples
335```
336 > (mesc "ham lus")
337 "ham lus"
338 > (mesc "bas\\hur")
339 "bas\\\\hur"
340 > (mesc "as'saß")
341 "as\0x27/sa\0xc3/\0x9f/"
342```
345---
346### `++runt`
348Prepend `n` times
350Add `a` repetitions of character `b` to the head of `++tape` `c`.
352#### Accepts
354`a` and `b` are atoms.
356`c` is a `++tape`.
358#### Produces
360A `++tape`.
362#### Source
364```
365 ++ runt :: prepend repeatedly
366 |= [[a/@ b/@] c/tape]
367 ^- tape
368 ?: =(0 a)
369 c
370 [b $(a (dec a))]
371 ::
372```
374#### Examples
376```
377 > (runt [2 '/'] "ham")
378 "//ham"
379 > (runt [10 'a'] "")
380 "aaaaaaaaaa"
381```
384---
385### `++sand`
387Soft-cast by odor
389Soft-cast validity by odor.
391#### Accepts
393`a` is a `++span` (`@ta`).
395`b` is an atom.
397#### Produces
399A `(unit @)`.
401#### Source
403```
404 ++ sand :: atom sanity
405 |= a/@ta
406 |= b/@ ^- (unit @)
407 ?.(((sane a) b) ~ [~ b])
408 ::
409```
411#### Examples
413```
414 > `(unit @ta)`((sand %ta) 'sym-som')
415 [~ ~.sym-som]
416 > `(unit @ta)`((sand %ta) 'err!')
417 ~
418```
421---
422### `++sane`
424Check odor validity
426Check validity by odor. Produces a gate.
428#### Accepts
430`a` is a `++span` (`@ta`).
432`b` is an atom.
434#### Produces
436A boolean.
438#### Source
440```
441 ++ sane :: atom sanity
442 |= a/@ta
443 |= b/@ ^- ?
444 ?. =(%t (end 3 1 a))
445 ~|(%sane-stub !!)
446 =+ [inx=0 len=(met 3 b)]
447 ?: =(%tas a)
448 |- ^- ?
449 ?: =(inx len) &
450 =+ cur=(cut 3 [inx 1] b)
451 ?& ?| &((gte cur 'a') (lte cur 'z'))
452 &(=('-' cur) !=(0 inx) !=(len inx))
453 &(&((gte cur '0') (lte cur '9')) !=(0 inx))
454 ==
455 $(inx +(inx))
456 ==
457 ?: =(%ta a)
458 |- ^- ?
459 ?: =(inx len) &
460 =+ cur=(cut 3 [inx 1] b)
461 ?& ?| &((gte cur 'a') (lte cur 'z'))
462 &((gte cur '0') (lte cur '9'))
463 |(=('-' cur) =('~' cur) =('_' cur) =('.' cur))
464 ==
465 $(inx +(inx))
466 ==
467 |- ^- ?
468 ?: =(0 b) &
469 =+ cur=(end 3 1 b)
470 ?: &((lth cur 32) !=(10 cur)) |
471 =+ len=(teff cur)
472 ?& |(=(1 len) =+(i=1 |-(|(=(i len) &((gte (cut 3 [i 1] b) 128) $(i +(i)))))))
473 $(b (rsh 3 len b))
474 ==
475 ::
476```
478#### Examples
480```
481 > ((sane %tas) %mol)
482 %.y
483 > ((sane %tas) 'lam')
484 %.y
485 > ((sane %tas) 'more ace')
486 %.n
487```
490---
491### `++teff`
493UTF8 Length
495Produces the number of utf8 bytes.
497#### Accepts
499`a` is a `@t`.
501#### Produces
503An atom.
505#### Source
507```
508 |= a/@t ^- @
509 =+ b=(end 3 1 a)
510 ~| %bad-utf8
511 ?: =(0 b)
512 ?>(=(`@`0 a) 0)
513 ?> |((gte b 32) =(10 b))
514 ?:((lte b 127) 1 ?:((lte b 223) 2 ?:((lte b 239) 3 4)))
515```
517#### Examples
519```
520 > (teff 'a')
521 1
522 > (teff 'ß')
523 2
524```
527---
528### `++trim`
530Tape split
532Split first `a` characters off `++tape` `b`.
534#### Accepts
536`a` is an atom.
538`b` is a `++tape`.
540#### Produces
542A cell of `++tape`s, `p` and `q`.
544#### Source
546```
547 ++ trim :: 31-bit nonzero
548 |= key/@
549 =+ syd=0xcafe.babe
550 |- ^- @
551 =+ haz=(spec syd key)
552 =+ ham=(mix (rsh 0 31 haz) (end 0 31 haz))
553 ?.(=(0 ham) ham $(syd +(syd)))
554 --
555 ::
556```
558#### Examples
560```
561 > (trim 5 "lasok termun")
562 [p="lasok" q=" termun"]
563 > (trim 5 "zam")
564 [p="zam" q=""]
565```
568---
569### `++trip`
571Cord to tape
573Produce a `++tape` from `++cord`.
575#### Accepts
577`a` is an atom.
579#### Produces
581A `++tape`.
583#### Source
585```
586 ++ trip :: cord to tape
587 ~/ %trip
588 |= a/@ ^- tape
589 ?: =(0 (met 3 a))
590 ~
591 [^-(@ta (end 3 1 a)) $(a (rsh 3 1 a))]
592 ::
593```
595#### Examples
597```
598 > (trip 'john doe')
599 "john doe"
600 > (trip 'abc 123 !@#')
601 "abc 123 !@#"
602 > (trip 'abc')
603 "abc"
604```
607---
608### `++tuba`
610UTF8 to UTF32 tape
612Convert `++tape` to a `++list` of codepoints (`@c`).
614#### Accepts
616`a` is a `++tape`.
618#### Produces
620A `++list` of codepoints `@c`.
622#### Source
624```
625 ++ tuba :: utf8 to utf32 tape
626 |= a/tape
627 ^- (list @c)
628 (rip 5 (turf (rap 3 a))) :: XX horrible
629 ::
630```
632#### Examples
634```
635 > (tuba "я тут")
636 ~[~-~44f. ~-. ~-~442. ~-~443. ~-~442.]
637 > (tuba "chars")
638 ~[~-c ~-h ~-a ~-r ~-s]
639```
642---
643### `++tufa`
645UTF32 to UTF8 tape
647Wrap a `++list` of utf32 codepoints into a utf8 `++tape`.
649#### Accepts
651`a` is a `++list` of `@c`.
653#### Produces
655A `++tape`.
657#### Source
659```
660 ++ tufa :: utf32 to utf8 tape
661 |= a=(list @c)
662 ^- tape
663 ?~ a ""
664 (weld (rip 3 (tuft i.a)) $(a t.a))
665 ::
666```
668#### Examples
670```
671 > (tufa ~[~-~44f. ~-. ~-~442. ~-~443. ~-~442.])
672 "я тут"
673 > (tufa ((list @c) ~[%a %b 0xb1 %c]))
674 "ab±c"
675```
678---
679### `++tuft`
681UTF32 to UTF8 text
683Convert utf32 glyph to
684LSB utf8 `++cord`.
686#### Accepts
688`a` is a codepoint (`@c`).
690#### Produces
692A `++cord`.
694#### Source
696```
697 ++ tuft :: utf32 to utf8 text
698 |= a/@c
699 ^- @t
700 %+ rap 3
701 |- ^- (list @)
702 ?: =(`@`0 a)
703 ~
704 =+ b=(end 5 1 a)
705 =+ c=$(a (rsh 5 1 a))
706 ?: (lte b 0x7f)
707 [b c]
708 ?: (lte b 0x7ff)
709 :* (mix 0b1100.0000 (cut 0 [6 5] b))
710 (mix 0b1000.0000 (end 0 6 b))
711 c
712 ==
713 ?: (lte b 0xffff)
714 :* (mix 0b1110.0000 (cut 0 [12 4] b))
715 (mix 0b1000.0000 (cut 0 [6 6] b))
716 (mix 0b1000.0000 (end 0 6 b))
717 c
718 ==
719 :* (mix 0b1111.0000 (cut 0 [18 3] b))
720 (mix 0b1000.0000 (cut 0 [12 6] b))
721 (mix 0b1000.0000 (cut 0 [6 6] b))
722 (mix 0b1000.0000 (end 0 6 b))
723 c
724 ==
725```
727#### Examples
729```
730 > (tuft `@c`%a)
731 'a'
732 > (tuft `@c`0xb6)
733 '¶'
734```
737---
738### `++turf`
740UTF8 to UTF32 cord
742Convert utf8 (`++cord`) to utf32 codepoints.
744#### Accepts
746`a` is a `@t`.
748#### Produces
750A `@c`, UTF-32 codepoint.
752#### Source
754```
755 ++ turf :: utf8 to utf32
756 |= a/@t
757 ^- @c
758 %+ rap 5
759 |- ^- (list @c)
760 =+ b=(teff a)
761 ?: =(0 b) ~
762 =+ ^= c
763 %+ can 0
764 %+ turn
765 ^- (list {p/@ q/@})
766 ?+ b !!
767 $1 [[0 7] ~]
768 $2 [[8 6] [0 5] ~]
769 $3 [[16 6] [8 6] [0 4] ~]
770 $4 [[24 6] [16 6] [8 6] [0 3] ~]
771 ==
772 |=({p/@ q/@} [q (cut 0 [p q] a)])
773 ?. =((tuft c) (end 3 b a)) ~|(%bad-utf8 !!)
774 [c $(a (rsh 3 b a))]
775 ::
776```
778#### Examples
780```
781 > (turf 'my ßam')
782 ~-my.~df.am
783 > 'я тут'
784 'я тут'
785 > (turf 'я тут')
786 ~-~44f..~442.~443.~442.
787 > `@ux`'я тут'
788 0x82.d183.d182.d120.8fd1
789 > `@ux`(turf 'я тут')
790 0x442.0000.0443.0000.0442.0000.0020.0000.044f
791```
794---
795### `++wack`
797Coin format encode
799Escape `++span` `~` as `~~` and `_` as `~-`. Used for printing.
801#### Accepts
803`a` is a `++span` (`@ta`).
805#### Produces
807A `++span` (`@ta`).
809#### Source
811```
812 ++ wack :: coin format
813 |= a/@ta
814 ^- @ta
815 =+ b=(rip 3 a)
816 %+ rap 3
817 |- ^- tape
818 ?~ b
819 ~
820 ?: =('~' i.b) ['~' '~' $(b t.b)]
821 ?: =('_' i.b) ['~' '-' $(b t.b)]
822 [i.b $(b t.b)]
823 ::
824```
826#### Examples
828```
829 > (wack '~20_sam~')
830 ~.~~20~-sam~~
832 > `@t`(wack '~20_sam~')
833 '~~20~-sam~~'
835 > ~(rend co %many ~[`ud/5 `ta/'~20_sam'])
836 "._5_~~.~~20~-sam__"
838 > ._5_~~.~~20~-sam__
839 [5 ~.~20_sam]
840```
842---
843### `++wick`
845Coin format decode
847Unescape `++span` `~~` as `~` and `~-` as `_`.
849#### Accepts
851`a` is a an atom.
853#### Produces
855A `++span` `@ta`.
857#### Source
859```
860 ++ wick :: knot format
861 |= a/@
862 ^- (unit @ta)
863 =+ b=(rip 3 a)
864 =- ?^(b ~ (some (rap 3 (flop c))))
865 =| c/tape
866 |- ^- {b/tape c/tape}
867 ?~ b [~ c]
868 ?. =('~' i.b)
869 $(b t.b, c [i.b c])
870 ?~ t.b [b ~]
871 ?- i.t.b
872 $'~' $(b t.t.b, c ['~' c])
873 $'-' $(b t.t.b, c ['_' c])
874 @ [b ~]
875 ==
876 ::
877```
879#### Examples
881```
882 > `@t`(wick '~-ams~~lop')
883 '_ams~lop'
885 > `@t`(wick (wack '~20_sam~'))
886 '~20_sam~'
887```
890---
891### `++woad`
893Unescape cord
895Unescape `++cord` codepoints.
897#### Accepts
899`a` is a `@ta`.
901#### Produces
903A `++cord`.
905#### Source
907```
908 ++ woad :: cord format
909 |= a/@ta
910 ^- @t
911 %+ rap 3
912 |- ^- (list @)
913 ?: =(`@`0 a)
914 ~
915 =+ b=(end 3 1 a)
916 =+ c=(rsh 3 1 a)
917 ?: =('.' b)
918 [' ' $(a c)]
919 ?. =('~' b)
920 [b $(a c)]
921 => .(b (end 3 1 c), c (rsh 3 1 c))
922 ?+ b =- (weld (rip 3 (tuft p.d)) $(a q.d))
923 ^= d
924 =+ d=0
925 |- ^- {p/@ q/@}
926 ?: =('.' b)
927 [d c]
928 ?< =(0 c)
929 %= $
930 b (end 3 1 c)
931 c (rsh 3 1 c)
932 d %+ add (mul 16 d)
933 %+ sub b
934 ?: &((gte b '0') (lte b '9')) 48
935 ?>(&((gte b 'a') (lte b 'z')) 87)
936 ==
937 $'.' ['.' $(a c)]
938 $'~' ['~' $(a c)]
939 ==
940 ::
941```
943#### Examples
945```
946 > (woad ~.~b6.20.as)
947 '¶20 as'
948```
951---
952### `++wood`
954Escape cord
956Escape `++cord` codepoints.
958#### Accepts
960`a` is a `++span` (`@ta`).
962#### Produces
964A `++span` (`@ta`).
966#### Source
968```
969 ++ wood :: cord format
970 |= a/@t
971 ^- @ta
972 %+ rap 3
973 |- ^- (list @)
974 ?: =(`@`0 a)
975 ~
976 =+ b=(teff a)
977 =+ c=(turf (end 3 b a))
978 =+ d=$(a (rsh 3 b a))
979 ?: ?| &((gte c 'a') (lte c 'z'))
980 &((gte c '0') (lte c '9'))
981 =(`@`'-' c)
982 ==
983 [c d]
984 ?+ c
985 :- '~'
986 =+ e=(met 2 c)
987 |- ^- tape
988 ?: =(0 e)
989 ['.' d]
990 =. e (dec e)
991 =+ f=(rsh 2 e c)
992 [(add ?:((lte f 9) 48 87) f) $(c (end 2 e c))]
993 ::
994```
996#### Examples
998```
999 > (wood 'my ßam')
1000 ~.my.~df.am
1001```
1003---