Word wrap: Difference between revisions

Content added Content deleted
m (→‎{{header|Scala}}: Rm Scala imp category)
m ({{out}})
Line 1: Line 1:
{{task}}
{{task}}
[[Category:Text processing]]
[[Category:Text processing]]
Even today, with proportional fonts and complex layouts, there are still [[Template:Lines_too_long|cases]] where you need to wrap text at a specified column. The basic task is to wrap a paragraph of text in a simple way in your language. If there is a way to do this that is built-in, trivial, or provided in a standard library, show that. Otherwise implement the [http://en.wikipedia.org/wiki/Word_wrap#Minimum_length minimum length greedy algorithm from Wikipedia.]
Even today, with proportional fonts and complex layouts, there are still [[Template:Lines_too_long|cases]] where you need to wrap text at a specified column.

The basic task is to wrap a paragraph of text in a simple way in your language.

If there is a way to do this that is built-in, trivial, or provided in a standard library, show that. Otherwise implement the [http://en.wikipedia.org/wiki/Word_wrap#Minimum_length minimum length greedy algorithm from Wikipedia.]


Show your routine working on a sample of text at two different wrap columns.
Show your routine working on a sample of text at two different wrap columns.


'''Extra credit!''' Wrap text using a more sophisticated algorithm such as the Knuth and Plass TeX algorithm. If your language provides this, you get easy extra credit, but you ''must reference documentation'' indicating that the algorithm is something better than a simple minimimum length algorithm.
'''Extra credit!''' Wrap text using a more sophisticated algorithm such as the Knuth and Plass TeX algorithm.
If your language provides this, you get easy extra credit,
but you ''must reference documentation'' indicating that the algorithm
is something better than a simple minimimum length algorithm.


If you have both basic and extra credit solutions, show an example where the two algorithms give different results.
If you have both basic and extra credit solutions, show an example where
the two algorithms give different results.


=={{header|Ada}}==
=={{header|Ada}}==
Line 135: Line 143:
end Wrap;</lang>
end Wrap;</lang>


Output, set to 72 lines (with input picked by cut-and-paste from the task description):
{{out}} set to 72 lines (with input picked by cut-and-paste from the task description):
<pre>Even today, with proportional fonts and complex layouts, there are still
<pre>Even today, with proportional fonts and complex layouts, there are still
cases where you need to wrap text at a specified column. The basic task
cases where you need to wrap text at a specified column. The basic task
Line 170: Line 178:
return, Result
return, Result
}</lang>
}</lang>
{{Output}}
{{Out}}
<pre>72
<pre>72
In olden times when wishing still helped one, there lived a king whose
In olden times when wishing still helped one, there lived a king whose
Line 272: Line 280:
& out$(str$("\n80 columns:\n" wrap$(!Text.80)))
& out$(str$("\n80 columns:\n" wrap$(!Text.80)))
);</lang>
);</lang>
{{out}}
Output:
<pre>72 columns:
<pre>72 columns:
In olden times when wishing still helped one, there lived a king whose
In olden times when wishing still helped one, there lived a king whose
Line 611: Line 619:
}
}
}</lang>
}</lang>
{{out}}
Output:
<pre>------------------------------------------------------------------------
<pre>------------------------------------------------------------------------
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas varius
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas varius
Line 672: Line 680:
(println line))</lang>
(println line))</lang>


{{out}}
Output :
<pre>In olden times when wishing still helped one, there lived a king whose
<pre>In olden times when wishing still helped one, there lived a king whose
daughters were all beautiful, but the youngest was so beautiful that the
daughters were all beautiful, but the youngest was so beautiful that the
Line 867: Line 875:
printfn ""
printfn ""
0</lang>
0</lang>
{{out}}
Output
<pre style="font-size:smaller">------------------------------------------------------------------------
<pre style="font-size:smaller">------------------------------------------------------------------------
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas varius
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas varius
Line 1,018: Line 1,026:
println wordWrap(text)
println wordWrap(text)
println wordWrap(text, 120)</lang>
println wordWrap(text, 120)</lang>
{{out}}
Output:
<pre>In olden times when wishing still helped one, there lived a king whose daughters
<pre>In olden times when wishing still helped one, there lived a king whose daughters
were all beautiful, but the youngest was so beautiful that the sun itself, which
were all beautiful, but the youngest was so beautiful that the sun itself, which
Line 1,212: Line 1,220:
</lang>
</lang>


{{out}}
Output:
<pre>
<pre>
DEFAULT:
DEFAULT:
Line 1,436: Line 1,444:
print(textwrap(example1, 60))</lang>
print(textwrap(example1, 60))</lang>


{{out}}
'''Output:'''

<pre>Even today, with proportional fonts and complex layouts, there are still
<pre>Even today, with proportional fonts and complex layouts, there are still
cases where you need to wrap text at a specified column. The basic task is
cases where you need to wrap text at a specified column. The basic task is
Line 1,483: Line 1,490:
StringJoin@@Riffle[masterString,"\n"]
StringJoin@@Riffle[masterString,"\n"]
];</lang>
];</lang>
Output for width 72 and 80:
{{out}} for width 72 and 80:
<lang>wordWrap[72, 1, string]
<lang>wordWrap[72, 1, string]
wordWrap[80, 1, string]</lang>
wordWrap[80, 1, string]</lang>
{{out}}
Output:
<pre>In olden times when wishing still helped one, there lived a king
<pre>In olden times when wishing still helped one, there lived a king
whose daughters were all beautiful, but the youngest was so beautiful
whose daughters were all beautiful, but the youngest was so beautiful
Line 1,645: Line 1,652:
echo wordWrap(txt, 45)
echo wordWrap(txt, 45)
</lang>
</lang>
{{out}}
Output:
<pre>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus.
<pre>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus.
Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec
Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec
Line 1,733: Line 1,740:
wrap(King, 50)</lang>
wrap(King, 50)</lang>


{{out}}
Output:
<pre>
<pre>
And so let freedom ring from the prodigious hilltops of New Hampshire; let
And so let freedom ring from the prodigious hilltops of New Hampshire; let
Line 1,845: Line 1,852:
Say time('E')
Say time('E')
</pre>
</pre>
{{out}}
Output:
<pre>
<pre>
A nnnnnnnnnnnnnn ooooooooooooooo nnnnnnnnnnnnnn
A nnnnnnnnnnnnnn ooooooooooooooo nnnnnnnnnnnnnn
Line 1,980: Line 1,987:
if $\=='' then say $ /*handle any residual words. */
if $\=='' then say $ /*handle any residual words. */
/*stick a fork in it, we're done.*/</lang>
/*stick a fork in it, we're done.*/</lang>
'''output''' is the same as version 1 using the &nbsp; '''L'''eft &nbsp; option (the default).
{{out}} is the same as version 1 using the &nbsp; '''L'''eft &nbsp; option (the default).


===version 1===
===version 1===
Line 2,144: Line 2,151:
∙ Parameters are variables travelling under an assumed name.
∙ Parameters are variables travelling under an assumed name.
</pre>
</pre>
'''output''' when specifying: &nbsp; <tt> , 155 </tt>
{{out}} when specifying: &nbsp; <tt> , 155 </tt>
<pre style="height:40ex">
<pre style="height:40ex">
────────── Computer programming laws ────────── The Primal Scenario -or- Basic Datum of Experience: ∙ Systems in general work poorly or not at all. ∙
────────── Computer programming laws ────────── The Primal Scenario -or- Basic Datum of Experience: ∙ Systems in general work poorly or not at all. ∙
Line 2,182: Line 2,189:
assumed name.
assumed name.
</pre>
</pre>
Output when specifying: <tt> , 77 </tt>
{{out}} when specifying: <tt> , 77 </tt>
<pre style="height:40ex">
<pre style="height:40ex">
────────── Computer programming laws ────────── The Primal Scenario -or-
────────── Computer programming laws ────────── The Primal Scenario -or-
Line 2,256: Line 2,263:
name.
name.
</pre>
</pre>
Output [justified] when specifying: <tt> , 70 both </tt>
{{out}} [justified] when specifying: <tt> , 70 both </tt>
<pre style="height:40ex">
<pre style="height:40ex">
────────── Computer programming laws ────────── The Primal Scenario -or-
────────── Computer programming laws ────────── The Primal Scenario -or-
Line 2,360: Line 2,367:
Return
Return
o:Return lineout(oid,arg(1))</lang>
o:Return lineout(oid,arg(1))</lang>
Output for widths 72 and 9
{{out}} for widths 72 and 9
<pre>
<pre>
123456789.123456789.123456789.123456789.123456789.123456789.123456789.12
123456789.123456789.123456789.123456789.123456789.123456789.123456789.12
Line 2,713: Line 2,720:
FILE "text" = wrappedtext
FILE "text" = wrappedtext
</lang>
</lang>
{{out}}
Output:
<pre style='height:30ex;overflow:scroll'>
<pre style='height:30ex;overflow:scroll'>
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------