ASCII art diagram converter: Difference between revisions

m (Cleanup)
Line 1,018:
ARCOUNT 16 0110100110100100
</pre>
 
=={{header|Lua}}==
Provided mainly to illustrate the string-parsing aspect, not necessarily the bit-structure aspect...
<lang lua>local function validate(diagram)
local lines = {}
for s in diagram:gmatch("[^\r\n]+") do
s = s:match("^%s*(.-)%s*$")
if s~="" then lines[#lines+1]=s end
end
-- "a little of validation".."for brevity"
assert(#lines>0, "FAIL: no non-empty lines")
assert(#lines%2==1, "FAIL: even number of lines")
return lines
end
 
local function parse(lines)
local schema, offset = {}, 0
for i = 2,#lines,2 do
for part in lines[i]:gmatch("\|([^\|]+)") do
schema[#schema+1] = { name=part:match("^%s*(.-)%s*$"), numbits=(#part+1)/3, offset=offset }
offset = offset + (#part+1)/3
end
end
return schema
end
 
local diagram = [[
 
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ID |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|QR| Opcode |AA|TC|RD|RA| Z | RCODE |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| QDCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
 
| ANCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| NSCOUNT |
 
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ARCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
 
]] -- extra whitespace added for testing
 
local schema = parse(validate(diagram))
print("NAME NUMBITS OFFSET")
print("-------- -------- --------")
for i = 1,#schema do
local item = schema[i]
print(string.format("%-8s %8d %8d", item.name, item.numbits, item.offset))
end</lang>
{{out}}
<pre>NAME NUMBITS OFFSET
-------- -------- --------
ID 16 0
QR 1 16
Opcode 4 17
AA 1 21
TC 1 22
RD 1 23
RA 1 24
Z 3 25
RCODE 4 28
QDCOUNT 16 32
ANCOUNT 16 48
NSCOUNT 16 64
ARCOUNT 16 80</pre>
 
=={{header|Nim}}==
Anonymous user