Initial Revision
[ohcount] / test / src_dir / lua1.lua
1 -- random code from http://lua-users.org/wiki/TextJustification
2 -- easy queue implementation ------------------------------------------
3
4 function q_create()
5     local q = {}
6     q.first = 0
7     q.last = 0
8     return q
9 end
10
11 function q_insert(q, s)
12     q[q.last] = s
13     q.last = q.last + 1
14 end
15
16 function q_empty(q)
17     return q.first >= q.last
18 end
19
20 function q_remove(q)
21     if q_empty(q) then
22         return nil
23     end
24     local s = q[q.first]
25     q[q.first] = nil
26     q.first = q.first+1
27     return s
28 end
29
30 function q_card(q)
31     return q.last - q.first
32 end
33
34 function q_length(q, f)
35     local l, i = 0, q.first
36     while i < q.last do
37         l = l + strlen(q[i])
38         i = i + 1
39     end
40     return l
41 end
42
43 -- line creation routines ------------------------------------------
44
45 -- justifies one line to fit into MAX columns 
46 function justify(q)
47     local blanks = MAX - q_length(q)
48     local skips = q_card(q) - 1
49     local line = q_remove(q)
50     local quotient = floor(blanks/skips)
51     local reminder = blanks/skips - quotient
52     local error = 0
53     while skips > 0 do
54         error = error + reminder
55         if error >= 0.5 then
56             error = error - 1
57             line = line .. strrep(" ", quotient+1)
58         else
59             line = line .. strrep(" ", quotient)
60         end
61         line = line .. q_remove(q)
62         skips = skips - 1
63     end
64     return line or ""
65 end
66
67 -- join all words with one space between them
68 function catenate(q)
69     local line = q_remove(q)
70     while not q_empty(q) do
71         line = line .. " " .. q_remove(q)
72     end
73     return line or ""
74 end
75
76 -- main program -----------------------------------------------------
77 DEFMAX = 72
78 -- tries to get MAX from command-line
79 if not arg or getn(arg) < 1 then
80     MAX = DEFMAX
81 else
82     MAX = tonumber(arg[1])
83     if not MAX or MAX < 0 then
84         MAX = DEFMAX
85     end
86 end
87
88 -- collects all text from stdin
89 text = q_create()
90 line = read()
91 while line do
92     _, n = gsub(line, "(%S+)", function (s) q_insert(%text, s) end)
93     if n == 0 then
94         q_insert(text, "\n")
95     end
96     line = read()
97 end
98
99 -- justify paragraphs
100 line = q_create()
101 word = q_remove(text)
102 size = 0
103 while word do
104     if word == "\n" then
105         if not q_empty(line) then
106             write(catenate(line), "\n\n")
107         else
108             write("\n")
109         end
110         size = 0
111     elseif size + strlen(word) > MAX then
112         write(justify(line), "\n")
113         size = 0
114     end
115     if word ~= "\n" then
116         q_insert(line, word)
117         size = size + strlen(word) + 1
118     end
119     word = q_remove(text)
120 end
121 write(catenate(line), "\n")