How to resolve the algorithm ASCII art diagram converter step by step in the Lua programming language
How to resolve the algorithm ASCII art diagram converter step by step in the Lua programming language
Table of Contents
Problem Statement
Given the RFC 1035 message diagram from Section 4.1.1 (Header section format) as a string: http://www.ietf.org/rfc/rfc1035.txt Where (every column of the table is 1 bit): Write a function, member function, class or template that accepts a similar multi-line string as input to define a data structure or something else able to decode or store a header with that specified bit structure. If your language has macros, introspection, code generation, or powerful enough templates, then accept such string at compile-time to define the header data structure statically. Such "Header" function or template should accept a table with 8, 16, 32 or 64 columns, and any number of rows. For simplicity the only allowed symbols to define the table are + - | (plus, minus, pipe), and whitespace. Lines of the input string composed just of whitespace should be ignored. Leading and trailing whitespace in the input string should be ignored, as well as before and after each table row. The box for each bit of the diagram takes four chars "+--+". The code should perform a little of validation of the input string, but for brevity a full validation is not required. Bonus: perform a thoroughly validation of the input string.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm ASCII art diagram converter step by step in the Lua programming language
Source code in the lua programming language
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
You may also check:How to resolve the algorithm Terminal control/Ringing the terminal bell step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Stack traces step by step in the Perl programming language
You may also check:How to resolve the algorithm Averages/Root mean square step by step in the Euphoria programming language
You may also check:How to resolve the algorithm Infinity step by step in the Racket programming language
You may also check:How to resolve the algorithm Loops/Continue step by step in the JavaScript programming language