Sort an outline at every level: Difference between revisions

Line 921:
Trying to parse another bad outline:
Error: bad indent UInt8[0x20, 0x20, 0x20], expected UInt8[0x20, 0x20, 0x20, 0x20]
</pre>
 
=={{header|Phix}}==
<lang Phix>procedure print_children(sequence lines, children, string indent, bool bRev)
sequence tags = custom_sort(lines,children)
if bRev then tags = reverse(tags) end if
for i=1 to length(tags) do
integer ti = tags[i]
printf(1,"%s%s\n",{indent,lines[ti][1]})
children = lines[ti][$]
if children!={} then
print_children(lines,children,lines[ti][2],bRev)
end if
end for
end procedure
 
constant spaced = """
zeta
beta
gamma
lambda
kappa
mu
delta
alpha
theta
iota
epsilon
""",
tabbed = substitute(spaced," ","\t"),
confused = substitute_all(spaced,{" gamma"," kappa"},{"gamma","\t kappa"}),
ragged = substitute_all(spaced,{" gamma","kappa"},{"gamma"," kappa"}),
tests = {spaced,tabbed,confused,ragged},
names = "spaced,tabbed,confused,ragged"
 
procedure test(sequence lines)
sequence pi = {-1}, -- indents (to locate parents)
pdx = {0}, -- indexes for ""
children = {},
roots = {}
for i=1 to length(lines) do
string line = trim_tail(lines[i]),
text = trim_head(line)
integer indent = length(line)-length(text)
-- remove any completed parents
while length(pi) and indent<=pi[$] do
pi = pi[1..$-1]
pdx = pdx[1..$-1]
end while
integer parent = 0
if length(pi) then
parent = pdx[$]
if parent=0 then
if indent!=0 then ?9/0 end if
roots &= i
else
if lines[parent][$]={} then
lines[parent][2] = line[1..indent]
elsif lines[parent][2]!=line[1..indent] then
printf(1,"**inconsistent indent** (%s, line %d)\n\n",{text,i})
return
end if
lines[parent][$] &= i -- (update children)
end if
end if
pi &= indent
pdx &= i
lines[i] = {text,"",children}
end for
printf(1,"ascending:\n")
print_children(lines,roots,"",false)
printf(1,"\ndescending:\n")
print_children(lines,roots,"",true)
printf(1,"\n")
end procedure
 
for t=1 to length(tests) do
string name = split(names,",")[t]
-- printf(1,"Test %d (%s):\n%s\n",{t,name,tests[t]})
printf(1,"Test %d (%s):\n",{t,name})
sequence lines = split(tests[t],"\n",no_empty:=true)
test(lines)
end for</lang>
{{out}}
<pre>
Test 1 (spaced):
ascending:
alpha
epsilon
iota
theta
zeta
beta
delta
gamma
kappa
lambda
mu
 
descending:
zeta
gamma
mu
lambda
kappa
delta
beta
alpha
theta
iota
epsilon
 
Test 2 (tabbed):
ascending:
alpha
epsilon
iota
theta
zeta
beta
delta
gamma
kappa
lambda
mu
 
descending:
zeta
gamma
mu
lambda
kappa
delta
beta
alpha
theta
iota
epsilon
 
Test 3 (confused):
**inconsistent indent** (gamma, line 3)
 
Test 4 (ragged):
**inconsistent indent** (gamma, line 3)
</pre>
 
7,794

edits