Align columns: Difference between revisions
Content added Content deleted
m (→{{header|D}}: + 1 more) |
|||
Line 555: | Line 555: | ||
justified, right justified, or center justified within its column. |
justified, right justified, or center justified within its column. |
||
</pre> |
</pre> |
||
Another version, using more the Phobos standard library. |
|||
{{works with|D|2.051}} |
|||
<lang d>import std.string, std.stdio, std.algorithm, std.typetuple ; |
|||
void alignText(string txt) { |
|||
string[][] text ; |
|||
uint maxCol = 0 ; |
|||
foreach(l;txt.splitlines) { |
|||
text ~= chomp(l.split("$"), [""]) ; // chomp work for string[] !!! |
|||
maxCol = max(maxCol, text[$-1].length) ; |
|||
} |
|||
auto colMaxWidth = new uint[](maxCol) ; |
|||
foreach(i, ref l;text) { |
|||
if(maxCol > l.length) |
|||
l ~= repeat(" ", maxCol - l.length - 1).split(" ") ; |
|||
foreach(idx,w;l) |
|||
colMaxWidth[idx] = max(colMaxWidth[idx], w.length) ; |
|||
} |
|||
// display justified text |
|||
foreach(justify;TypeTuple!(rjustify, center, ljustify)) { |
|||
foreach(l;text) { |
|||
foreach(idx,word;l) |
|||
writef("%s|",justify(word,colMaxWidth[idx])) ; |
|||
writeln() ; |
|||
} |
|||
writeln(" --- --- ") ; |
|||
} |
|||
}</lang> |
|||
=={{header|E}}== |
=={{header|E}}== |