ASCII art diagram converter: Difference between revisions

Content added Content deleted
m (fix tipo kine ->kind)
(J)
Line 281: Line 281:
| |
| |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+</pre>
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+</pre>

=={{header|J}}==

<lang J>require'strings'

soul=: -. {.
normalize=: [:soul' ',dltb;._2

mask=: 0: _1} '+' = {.
partition=: '|' = mask #"1 soul
labels=: ;@(([: <@}: <@dltb;._1)"1~ '|'&=)@soul
names=: ;:^:(0 = L.)

unpacker=:1 :0
p=. , partition normalize m
p #.;.1 (8#2) ,@:#: ]
)

packer=:1 :0
w=. -#;.1 ,partition normalize m
_8 (#.\ ;) w ({. #:)&.> ]
)

getter=:1 :0
nm=. labels normalize m
(nm i. names@[) { ]
)

setter=:1 :0
q=. ''''
n=. q,~q,;:inv labels normalize m
1 :('(',n,' i.&names m)}')
)

starter=:1 :0
0"0 labels normalize m
)</lang>

Sample definition (note the deliberate introduction of extraneous whitespace in locations the task requires us to ignore it.

<lang j>sample=: 0 :0
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ID |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|QR| Opcode |AA|TC|RD|RA| Z | RCODE |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| QDCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ANCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| NSCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ARCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+

)

unpack=: sample unpacker
pack=: sample unpacker
get=: sample getter
set=: sample setter
start=: sample starter</lang>

Example data for sample definition:

<lang J>
4095 13 5 6144 4096 'ID Opcode RCODE ARCOUNT QDCOUNT' set start
4095 0 13 0 0 0 0 0 5 4096 0 0 6144
pack 4095 13 5 6144 4096 'ID Opcode RCODE ARCOUNT QDCOUNT' set start
15 255 104 5 16 0 0 0 0 0 24 0
unpack 0 10 56 128 0 0 0 0 0 0 0 255
10 0 7 0 0 0 1 0 0 0 0 0 255
'Opcode' get unpack 0 10 56 128 0 0 0 0 0 0 0 255
7</lang>

In other words:

:unpack converts an octet sequence to the corresponding numeric sequence
:pack converts a numeric sequence to the corresponding octet sequence
:get extracts named elements from the numeric sequence
:set updates named elements in the numeric sequence
:start represents the default "all zeros" sequence which may be used to derive other sequences

Note that this implementation assumes that the ascii diagram represents the native word width on a single line, and assumes well formed data.