on 4fdc19d help sign in
841 lines 13 kB history raw
1+++
2title = "4k: Atom Printing"
3weight = 38
4template = "doc.html"
5+++
7## `++co`
9Literal rendering engine
11`++co` contains three parts:
13- A door that contains arms that operate on the sample coin `lot`.
14- A core of rendering idioms.
15- A core of additional formatting functions used internally.
17#### Accepts
19`lot` is a `coin`.
21#### Source
23The arm begins with the door:
25```hoon
26++ co
27 !:
28 ~% %co ..co ~
29 =< |_ lot=coin
30```
32Then after that the rendering idiom core begins with:
34```hoon
35=| rep=tape
36=< |%
37```
39And finally the third core:
41```hoon
42|%
43```
45---
47### `++rear:co`
49Prepend & render as tape
51Renders a coin `lot` as a tape prepended to the sample tape `rom`.
53#### Accepts
55`lot` is a `coin`, and is the sample of `++co`.
57`rom` is a `tape`.
59#### Produces
61A `tape`.
63#### Source
65```hoon
66++ rear |=(rom=tape rend(rep rom))
67```
69#### Examples
71```
72> (~(rear co %$ %ux 200) "--ha")
73"0xc8--ha"
74```
76---
78### `++rent:co`
80Render as knot
82Renders a coin `lot` as a `knot`.
84#### Accepts
86`lot` is a `coin`, and is the sample of `++co`.
88#### Produces
90A `knot`.
92#### Source
94```hoon
95++ rent ~+ `@ta`(rap 3 rend)
96```
98#### Examples
100```
101> ~(rent co %$ %ux 200)
102~.0xc8
104> `@t`~(rent co %$ %ux 200)
105'0xc8'
106```
108---
110### `++rend:co`
112Render as tape
114Renders a coin `lot` as a `tape`.
116#### Accepts
118`lot` is a `coin`, and is the sample of `++co`.
120#### Produces
122A `tape`.
124#### Source
126```hoon
127++ rend
128 ^- tape
129 ~+
130 ?: ?=(%blob -.lot)
131 ['~' '0' ((v-co 1) (jam p.lot))]
132 ?: ?=(%many -.lot)
133 :- '.'
134 |- ^- tape
135 ?~ p.lot
136 ['_' '_' rep]
137 ['_' (weld (trip (wack rent(lot i.p.lot))) $(p.lot t.p.lot))]
138 =+ [yed=(end 3 p.p.lot) hay=(cut 3 [1 1] p.p.lot)]
139 |- ^- tape
140 ?+ yed (z-co q.p.lot)
141 %c ['~' '-' (weld (rip 3 (wood (tuft q.p.lot))) rep)]
142 %d
143 ?+ hay (z-co q.p.lot)
144 %a
145 =+ yod=(yore q.p.lot)
146 =? rep ?=(^ f.t.yod) ['.' (s-co f.t.yod)]
147 =? rep !&(?=(~ f) =(0 h) =(0 m) =(0 s)):t.yod
148 =. rep ['.' (y-co s.t.yod)]
149 =. rep ['.' (y-co m.t.yod)]
150 ['.' '.' (y-co h.t.yod)]
151 =. rep ['.' (a-co d.t.yod)]
152 =. rep ['.' (a-co m.yod)]
153 =? rep !a.yod ['-' rep]
154 ['~' (a-co y.yod)]
155 ::
156 %r
157 =+ yug=(yell q.p.lot)
158 =? rep ?=(^ f.yug) ['.' (s-co f.yug)]
159 :- '~'
160 ?: &(=(0 d.yug) =(0 m.yug) =(0 h.yug) =(0 s.yug))
161 ['s' '0' rep]
162 =? rep !=(0 s.yug) ['.' 's' (a-co s.yug)]
163 =? rep !=(0 m.yug) ['.' 'm' (a-co m.yug)]
164 =? rep !=(0 h.yug) ['.' 'h' (a-co h.yug)]
165 =? rep !=(0 d.yug) ['.' 'd' (a-co d.yug)]
166 +.rep
167 ==
168 ::
169 %f
170 ?: =(& q.p.lot)
171 ['.' 'y' rep]
172 ?:(=(| q.p.lot) ['.' 'n' rep] (z-co q.p.lot))
173 ::
174 %n ['~' rep]
175 %i
176 ?+ hay (z-co q.p.lot)
177 %f ((ro-co [3 10 4] |=(a=@ ~(d ne a))) q.p.lot)
178 %s ((ro-co [4 16 8] |=(a=@ ~(x ne a))) q.p.lot)
179 ==
180 ::
181 %p
182 =+ sxz=(fein:ob q.p.lot)
183 =+ dyx=(met 3 sxz)
184 :- '~'
185 ?: (lte dyx 1)
186 (weld (trip (tod:po sxz)) rep)
187 =+ dyy=(met 4 sxz)
188 =| imp=@ud
189 |- ^- tape
190 ?: =(imp dyy)
191 rep
192 %= $
193 imp +(imp)
194 rep =/ log (cut 4 [imp 1] sxz)
195 ;: weld
196 (trip (tos:po (rsh 3 log)))
197 (trip (tod:po (end 3 log)))
198 ?:(=((mod imp 4) 0) ?:(=(imp 0) "" "--") "-")
199 rep
200 == ==
201 ::
202 %q
203 :+ '.' '~'
204 =; res=(pair ? tape)
205 (weld q.res rep)
206 %+ roll
207 =* val q.p.lot
208 ?:(=(0 val) ~[0] (rip 3 val))
209 |= [q=@ s=? r=tape]
210 :- !s
211 %+ weld
212 (trip (?:(s tod:po tos:po) q))
213 ?.(&(s !=(r "")) r ['-' r])
214 ::
215 %r
216 ?+ hay (z-co q.p.lot)
217 %d ['.' '~' (r-co (rlyd q.p.lot))]
218 %h ['.' '~' '~' (r-co (rlyh q.p.lot))]
219 %q ['.' '~' '~' '~' (r-co (rlyq q.p.lot))]
220 %s ['.' (r-co (rlys q.p.lot))]
221 ==
222 ::
223 %u
224 ?: ?=(%c hay)
225 %+ welp ['0' 'c' (reap (pad:fa q.p.lot) '1')]
226 (c-co (enc:fa q.p.lot))
227 ::
228 =; gam=(pair tape tape)
229 (weld p.gam ?:(=(0 q.p.lot) `tape`['0' ~] q.gam))
230 ?+ hay [~ ((ox-co [10 3] |=(a=@ ~(d ne a))) q.p.lot)]
231 %b [['0' 'b' ~] ((ox-co [2 4] |=(a=@ ~(d ne a))) q.p.lot)]
232 %i [['0' 'i' ~] ((d-co 1) q.p.lot)]
233 %x [['0' 'x' ~] ((ox-co [16 4] |=(a=@ ~(x ne a))) q.p.lot)]
234 %v [['0' 'v' ~] ((ox-co [32 5] |=(a=@ ~(x ne a))) q.p.lot)]
235 %w [['0' 'w' ~] ((ox-co [64 5] |=(a=@ ~(w ne a))) q.p.lot)]
236 ==
237 ::
238 %s
239 %+ weld
240 ?:((syn:si q.p.lot) "--" "-")
241 $(yed 'u', q.p.lot (abs:si q.p.lot))
242 ::
243 %t
244 ?: =('a' hay)
245 ?: =('s' (cut 3 [2 1] p.p.lot))
246 (weld (rip 3 q.p.lot) rep)
247 ['~' '.' (weld (rip 3 q.p.lot) rep)]
248 ['~' '~' (weld (rip 3 (wood q.p.lot)) rep)]
249 ==
250```
252#### Examples
254```
255> ~(rend co %$ %ux 200)
256"0xc8"
258> ~(rend co %many ~[[%$ ux+200] [%$ p+40]])
259"._0xc8_~~tem__"
261> ~(rend co %$ %p 32.819)
262"~lasmev"
264> ~(rend co %$ %ux 18)
265"0x12"
267> ~(rend co [%$ p=[p=%if q=0x7f00.0001]])
268".127.0.0.1"
270> ~(rend co %many ~[[%$ %ud 20] [%$ %uw 133] [%$ %tas 'sam']])
271"._20_0w25_sam__"
273> ~(rend co %blob [1 1])
274"~0ph"
275```
277---
279### `++a-co:co`
281Render decimal
283Render `dat` as a decimal integer without separators.
285#### Accepts
287`dat` is an atom.
289#### Produces
291A `tape`
293#### Source
295```hoon
296++ a-co |=(dat=@ ((d-co 1) dat))
297```
299#### Examples
301```
302> (a-co:co 123.456.789)
303"123456789"
304```
306---
308### `++c-co:co`
310Render base58check
312Renders the given `atom` as a base58check `tape`.
314#### Accepts
316An `atom`.
318#### Produces
320A `tape`.
322#### Source
324```hoon
325++ c-co (em-co [58 1] |=([? b=@ c=tape] [~(c ne b) c]))
326```
328#### Examples
330```
331> (enc:fa 0xdead.beef)
3320xdead.beef.938b.8b0c
334> (c-co:co 0xdead.beef.938b.8b0c)
335"eFGDJSVvRHd"
336```
338---
340### `++d-co:co`
342Render decimal with min length
344Render `hol` as a decimal integer without separators and with a minimum length
345of `min`. If `hol` has less than `min` digits, leading zeros will be added to
346make up the difference.
348#### Accepts
350`min` is an atom.
352`hol` is an atom.
354#### Produces
356A `tape`.
358#### Source
360```hoon
361++ d-co |=(min=@ (em-co [10 min] |=([? b=@ c=tape] [~(d ne b) c])))
362```
364#### Examples
366```
367> ((d-co:co 1) 123.456)
368"123456"
370> ((d-co:co 9) 123.456)
371"000123456"
372```
374---
376### `++r-co:co`
378Render floating point
380Render decimal float `a` as a `tape`.
382#### Accepts
384`a` is a `++dn`.
386#### Produces
388A `tape`.
390#### Source
392```hoon
393++ r-co
394 |= a=dn
395 ?: ?=([%i *] a) (weld ?:(s.a "inf" "-inf") rep)
396 ?: ?=([%n *] a) (weld "nan" rep)
397 =; rep ?:(s.a rep ['-' rep])
398 =/ f ((d-co 1) a.a)
399 =^ e e.a
400 =/ e=@s (sun:si (lent f))
401 =/ sci :(sum:si e.a e -1)
402 ?: (syn:si (dif:si e.a --3)) [--1 sci] :: 12000 -> 12e3 e>+2
403 ?: !(syn:si (dif:si sci -2)) [--1 sci] :: 0.001 -> 1e-3 e<-2
404 [(sum:si sci --1) --0] :: 1.234e2 -> '.'@3 -> 123 .4
405 =? rep !=(--0 e.a)
406 :(weld ?:((syn:si e.a) "e" "e-") ((d-co 1) (abs:si e.a)))
407 (weld (ed-co e f) rep)
408```
410#### Examples
412```
413> `tape`(r-co:co (rlys .3.14))
414"3.14"
416> `tape`(r-co:co (rlys .1.681557e-39))
417"1.681557e-39"
418```
420---
422### `++s-co:co`
424Render hex list
426Render `esc`, a list of atoms, as hex with a dot before each value. Values less
427than two bytes in length will be padded with zeros.
429#### Accepts
431`esc` is a `(list @)`.
433#### Produces
435A `tape`.
437#### Source
439```hoon
440++ s-co
441 |= esc=(list @) ^- tape
442 ?~ esc rep
443 ['.' =>(.(rep $(esc t.esc)) ((x-co 4) i.esc))]
444```
446#### Examples
448```
449> `tape`(s-co:co ~[0xdead 0xbeef 0xcafe])
450".dead.beef.cafe"
452> `tape`(s-co:co ~[0xa 0xb 0xc])
453".000a.000b.000c"
455> `tape`(s-co:co ~[0xdead.beef])
456".deadbeef"
457```
459---
461### `++v-co:co`
463Render base-32 with minimum length
465Render `hol` as base-32 with a minimum length of `min`. If `hol` is shorter than
466`min` it will be padded with zeros.
468#### Accepts
470`min` is a `@ud`.
472`hol` is an atom.
474#### Produces
476A `tape`.
478#### Source
480```hoon
481++ v-co |=(min=@ (em-co [32 min] |=([? b=@ c=tape] [~(v ne b) c])))
482```
484#### Examples
486```
487> `tape`((v-co:co 1) 0v2l7.eiug3.0mbd9)
488"2l7eiug30mbd9"
490> `tape`((v-co:co 20) 0v2l7.eiug3.0mbd9)
491"00000002l7eiug30mbd9"
492```
494---
496### `++w-co:co`
498Render base-64 with minimum length
500Render `hol` as base-64 with a minimum length of `min`. If `hol` is horter than
501`min` it will be padded with zeros.
503#### Accepts
505`min` is a `@ud`.
507`hol` is an atom.
509#### Produces
511A `tape`.
513#### Source
515```hoon
516++ w-co |=(min=@ (em-co [64 min] |=([? b=@ c=tape] [~(w ne b) c])))
517```
519#### Examples
521```
522> `tape`((w-co:co 1) 0w2.OtBSR.pPVeT)
523"2OtBSRpPVeT"
525> `tape`((w-co:co 20) 0w2.OtBSR.pPVeT)
526"0000000002OtBSRpPVeT"
527```
529---
531### `++x-co:co`
533Render hex with minimum length
535Render `hol` as hex with a minimum length of `min`. If `hol` is horter than
536`min` it will be padded with zeros.
538#### Accepts
540`min` is a `@ud`.
542`hol` is an atom.
544#### Produces
546A `tape`.
548#### Source
550```hoon
551++ x-co |=(min=@ (em-co [16 min] |=([? b=@ c=tape] [~(x ne b) c])))
552```
554#### Examples
556```
557> `tape`((x-co:co 1) 0xdead.beef)
558"deadbeef"
560> `tape`((x-co:co 20) 0xdead.beef)
561"000000000000deadbeef"
562```
564---
566### `++y-co:co`
568Render decimal with at least two digits
570Render `dat` as a decimal with a minimum of two digits. If `dat` is less than
571two digits it will be padded with zeros.
573#### Accepts
575`dat` is an atom.
577#### Produces
579A `tape`.
581#### Source
583```hoon
584++ y-co |=(dat=@ ((d-co 2) dat))
585```
587#### Examples
589```
590> (y-co:co 0)
591"00"
593> (y-co:co 1)
594"01"
596> (y-co:co 123)
597"123"
599> (y-co:co 123.456)
600"123456"
601```
603---
605### `++z-co:co`
607Render '0x'-prefixed hex
609Render `dat` as hex with a `0x` prefix.
611#### Accepts
613`dat` is an atom.
615#### Produces
617A `tape`.
619#### Source
621```hoon
622++ z-co |=(dat=@ `tape`['0' 'x' ((x-co 1) dat)])
623```
625#### Examples
627```
628> (z-co:co 123)
629"0x7b"
631> (z-co:co 0xdead.beef)
632"0xdeadbeef"
634> (z-co:co 0)
635"0x0"
636```
638---
640### `++em-co:co`
642Render in numeric base
644In base `bas`, format `min` digits of `hol` with `par`. This is used internally
645by other rendering functions.
647- `hol` is processed least-significant digit first.
648- All available digits in `hol` will be processed, but
649 `min` digits can exceed the number available in `hol`
650- `par` handles all accumulated output on each call, and can edit it, prepend or
651 append digits, etc.
652- Until `hol` is exhausted, `par`'s sample is `[| digit output]`, subsequently,
653 it's `[& 0 output]`.
655#### Accepts
657`[bas=@ min=@]`, where `bas` is the numeric base and `min` is the minimum
658length.
660`par` is a `gate` of `$-([? @ tape] tape)`.
662`hol` is an atom.
664#### Produces
666A `tape`.
668#### Source
670```hoon
671++ em-co
672 |= [[bas=@ min=@] par=$-([? @ tape] tape)]
673 |= hol=@
674 ^- tape
675 ?: &(=(0 hol) =(0 min))
676 rep
677 =/ [dar=@ rad=@] (dvr hol bas)
678 %= $
679 min ?:(=(0 min) 0 (dec min))
680 hol dar
681 rep (par =(0 dar) rad rep)
682 ==
683```
685#### Examples
687```
688> ((em-co:co [16 10] |=([? b=@ c=tape] [~(x ne b) c])) 0xbeef)
689"000000beef"
690```
692---
694### `++ed-co:co`
696Format with decimal place
698Format `int` by specifying its size with `exp`, which may be negative. This is
699used internally by other rendering functions.
701#### Accepts
703`exp` is a `@s`.
705`int` is a `tape`.
707#### Produces
709A `tape`.
711#### Source
713```hoon
714++ ed-co
715 |= [exp=@s int=tape] ^- tape
716 =/ [pos=? dig=@u] [=(--1 (cmp:si exp --0)) (abs:si exp)]
717 ?. pos
718 (into (weld (reap +(dig) '0') int) 1 '.')
719 =/ len (lent int)
720 ?: (lth dig len) (into int dig '.')
721 (weld int (reap (sub dig len) '0'))
722```
724#### Examples
726```
727> (ed-co:co --3 "100")
728"100"
730> (ed-co:co --5 "100")
731"10000"
733> (ed-co:co -1 "100")
734"0.0100"
736> (ed-co:co -5 "100")
737"0.00000100"
738```
740---
742### `++ox-co:co`
744Format dot-separated digits in numeric base
746In base `bas`, format each digit of `hol` with gate `dug`, with '.' separators
747every `gop` digits. This is used internally by other rendering functions.
749- `hol` is processed least-significant digit first.
750- `dug` handles individual digits, output is prepended.
751- Every segment but the last is zero-padded to `gop`.
753#### Accepts
755`[bas=@ gop=@]` where `bas` is the numeric base and `gop` is dot separator
756frequency.
758`dug` is a `gate` of `$-(@ @)`.
760`hol` is an atom.
762#### Produces
764A `tape`.
766#### Source
768```hoon
769++ ox-co
770 |= [[bas=@ gop=@] dug=$-(@ @)]
771 %+ em-co
772 [(pow bas gop) 0]
773 |= [top=? seg=@ res=tape]
774 %+ weld
775 ?:(top ~ `tape`['.' ~])
776 %. seg
777 %+ em-co(rep res)
778 [bas ?:(top 0 gop)]
779 |=([? b=@ c=tape] [(dug b) c])
780```
782#### Examples
784```
785> ((ox-co:co [2 4] |=(a=@ ~(d ne a))) 0b1011.1101)
786"1011.1101"
788> ((ox-co:co [2 1] |=(a=@ ~(d ne a))) 0b1011.1101)
789"1.0.1.1.1.1.0.1"
791> ((ox-co:co [2 100] |=(a=@ ~(d ne a))) 0b1011.1101)
792"10111101"
793```
795---
797### `++ro-co:co`
799Format dot-prefixed bloqs in numeric base
801In base `bas`, for `buz` bloqs 0 to `dop`, format at least one digit of `hol`,
802prefixed with `.`. This is used internally for `@i` address rendering functions.
804#### Accepts
806`[buz=@ bas=@ dop=@]` where `buz` is the bloq size, `bas` is the numeric base,
807and `dop` is the number of bloqs.
809`dug` is a `gate` of `$-(@ @)`.
811`hol` is an atom.
813#### Produces
815A `tape`.
817#### Source
819```hoon
820++ ro-co
821 |= [[buz=@ bas=@ dop=@] dug=$-(@ @)]
822 |= hol=@
823 ^- tape
824 ?: =(0 dop)
825 rep
826 :- '.'
827 =/ pod (dec dop)
828 %. (cut buz [pod 1] hol)
829 %+ em-co(rep $(dop pod))
830 [bas 1]
831 |=([? b=@ c=tape] [(dug b) c])
832```
834#### Examples
836```
837> ((ro-co:co [3 10 4] |=(a=@ ~(d ne a))) .127.0.0.1)
838".127.0.0.1"
839```
841---