entry #1
written by kimapr
submitted at
0 likes
guesses
- Dolphy (by Indigo)
- Dolphy (by undefined)
- Dolphy (by evie)
- kimapr (by olive)
- undefined (by rbca)
- undefined (by Dolphy)
comments 0
cappuccina.bal ASCII text
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | // Ballerina Cappuccina is here to justify your text!
import ballerina/io; type stringOrInt string|int; any bigword =
`Here_is_an_example_of_a_word_so_long_it_will_have_to_be_BROKEN_by_the_-
word_wrapping_algorithm._It_goes_on_for_150_characters_before_it_finall-
y_ends`;
public function main() { string data = checkpanic
io:fileReadString("/dev/stdin"); stringOrInt[][] lines = []; int maxlen
= 72;
{ int i = 0; stringOrInt[] line = []; int nlength = 0; string[] word =
[]; function() pushw = function() { if word.length() > 0 { string sword
= string:'join("", ...word); int wlen = (line.length() > 0 ? 1 : 0) +
sword.length(); if nlength + wlen > maxlen { lines.push(line); line =
[]; nlength = 0; } if line.length() > 0 { line.push(1); }
line.push(sword); nlength = nlength + wlen; word = []; } }; while true
{ if i >= data.length() { pushw(); break; } int ii = i; string c =
data[i]; i = i + 1; if c == "\u{20}" || c == "\u{9}" || c == "\n" {
pushw(); } else { if word.length() == maxlen { string borrow =
word.pop(); word.push("-"); pushw(); word.push(borrow); } word.push(c);
continue; } if ii+2 < data.length() && c == "\u{20}" && data[ii+1] ==
"\u{20}" && data[ii+2] == "\n" { lines.push(line); line = []; nlength =
0; i = i + 2; continue; } if (ii+1 < data.length() && c == "\n" &&
data[ii+1] == "\n") || (c == "\n" && line.length() == 0) {
lines.push(line); line = []; nlength = 0; continue; } } if
line.length() > 0 { lines.push(line); line = []; nlength = 0; } }
{ int li = 0; int wi = 0; while true { if li+1 >= lines.length() {
break; } int ili = li; var line = lines[li]; li = li + 1; if
line.length() <= 1 || lines[ili+1].length() == 0 { continue; } int
length = 0; foreach string|int o in line { if o is int { length =
length + o; } else { length = length + o.length(); } } while length <
maxlen { wi = (wi + 1) % line.length(); stringOrInt spc = line[wi]; if
spc is int { length = length + 1; line[wi] = spc + 1; } else {
continue; } } } }
foreach any nline in lines { io:println(string:'join("",
...nline.map(function(stringOrInt el) returns string { if el is string
{ return el; } else { string[] spc = []; foreach int _ in int:range(0,
el, 1) { spc.push("\u{20}"); } return string:'join("", ...spc); } })));
} }
|

post a comment