Word wrap: Difference between revisions

Add ed example
(→‎{{header|C}}: Add a simple in-place method)
(Add ed example)
 
(6 intermediate revisions by 5 users not shown)
Line 1,952:
 
</syntaxhighlight>
 
=={{header|ed}}==
 
Regex-matching the (greedy) 0-79 chunks of text and putting newlines after them. A 72, 60, or any other wrap column can be achieved by tweaking the <tt>79</tt> constant in the code.
 
<syntaxhighlight lang="sed">
H
g/.*/s/(.{0,79})([ ]|$)/\1\
/g
,p
Q
</syntaxhighlight>
 
{{out}}
 
<pre>$ cat word-wrap-80.ed | ed -lEGs word-wrap.input
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 has seen so much, was astonished whenever it shone in her face.
Close by the king's castle lay a great dark forest, and under an old lime-tree
in the forest was a well, and when the day was very warm, the king's child went
out into the forest and sat down by the side of the cool fountain, and when she
was bored she took a golden ball, and threw it up on high and caught it, and
this ball was her favorite plaything.</pre>
 
=={{header|Elena}}==
ELENA 46.x :
<syntaxhighlight lang="elena">import extensions;
import system'routines;
Line 1,978 ⟶ 2,002:
^ TokenEnumerator
.new(self)
.selectBy::(word)
{
currentWidth += word.Length;
Line 1,985 ⟶ 2,009:
currentWidth := word.Length + 1;
^ newLinenewLineConstant + word + " "
}
else
Line 4,124 ⟶ 4,148:
Mississippi. From every mountainside, let freedom
ring.</pre>
 
=={{header|PascalABC.NET}}==
<syntaxhighlight lang="delphi">
function Wrap(words: sequence of string; lineWidth: integer): sequence of string;
begin
var currentWidth := 0;
foreach var word in words do
begin
if currentWidth <> 0 then
if currentWidth + word.Length < lineWidth then
begin
currentWidth += 1;
yield ' ';
end
else
begin
currentWidth := 0;
yield NewLine;
end;
currentWidth += word.Length;
yield word;
end;
end;
 
function Wrap(text: string; lineWidth: integer): string
:= Wrap(text.ToWords(' '#13#10),lineWidth).JoinToString('');
 
begin
var text := '''
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas varius sapien
vel purus hendrerit vehicula. Integer hendrerit viverra turpis, ac sagittis arcu
pharetra id. Sed dapibus enim non dui posuere sit amet rhoncus tellus
consectetur. Proin blandit lacus vitae nibh tincidunt cursus. Cum sociis natoque
penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nam tincidunt
purus at tortor tincidunt et aliquam dui gravida. Nulla consectetur sem vel
felis vulputate et imperdiet orci pharetra. Nam vel tortor nisi. Sed eget porta
tortor. Aliquam suscipit lacus vel odio faucibus tempor. Sed ipsum est,
condimentum eget eleifend ac, ultricies non dui. Integer tempus, nunc sed
venenatis feugiat, augue orci pellentesque risus, nec pretium lacus enim eu
nibh.
''';
Wrap(text,50).Println;
end.
</syntaxhighlight>
{{out}}
<pre>
Lorem ipsum dolor sit amet, consectetur adipiscing
elit. Maecenas varius sapien vel purus hendrerit
vehicula. Integer hendrerit viverra turpis, ac
sagittis arcu pharetra id. Sed dapibus enim non
dui posuere sit amet rhoncus tellus consectetur.
Proin blandit lacus vitae nibh tincidunt cursus.
Cum sociis natoque penatibus et magnis dis
parturient montes, nascetur ridiculus mus. Nam
tincidunt purus at tortor tincidunt et aliquam dui
gravida. Nulla consectetur sem vel felis vulputate
et imperdiet orci pharetra. Nam vel tortor nisi.
Sed eget porta tortor. Aliquam suscipit lacus vel
odio faucibus tempor. Sed ipsum est, condimentum
eget eleifend ac, ultricies non dui. Integer
tempus, nunc sed venenatis feugiat, augue orci
pellentesque risus, nec pretium lacus enim eu
nibh.
</pre>
 
 
=={{header|Perl}}==
Line 5,883 ⟶ 5,972:
<syntaxhighlight lang="ruby">class String {
method wrap(width) {
var txt = self.gsub(/\s+/, " ");
var len = txt.len;
var para = [];
var i = 0;
while (i < len) {
var j = (i + width);
while ((j < len) && (txt.char_at(j)  != ' ')) { --j };
para.append(txt.substr(i, j-i));
i = j+1;
};
return para.join("\n");
}
}
 
var text = 'aaa bb cc ddddd';
say text.wrap(6);</syntaxhighlight>
 
{{out}}
Line 5,934 ⟶ 6,023:
root << [
array.first(i+1).join(' '),
self.prepare_words(array.ftslice(i+1), depth+1, callback)
]
 
Line 5,977 ⟶ 6,066:
self.combine([], path, { |combination|
var score = 0
combination.ftfirst(0, -21).each { |line|
score += (width - line.len -> sqr)
}
Line 5,991 ⟶ 6,080:
}
}
 
var sww = SmartWordWrap();
 
var words = %w(aaa bb cc ddddd);
var wrapped = sww.wrap(words, 6);
 
say wrapped;</syntaxhighlight>
{{out}}
<pre>
Line 6,034 ⟶ 6,123:
occaecat cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.</pre>
 
=={{header|Tailspin}}==
A simple greedy algorithm that will always put one word on a line even if the word is longer than the desired width.
<syntaxhighlight lang="tailspin">
templates break&{width:}
composer words
<word>* (<WS>*)
rule word: (<WS>*) [<'\S'>+]
end words
def chars: [$ -> words];
@: $chars(first);
[$chars(first~..last)... -> #] -> '$... -> '$;$#10;';$@...;' !
 
when <[](..~($width-$@::length))> do ..|@: ' '; $... -> ..|@: $;
otherwise '$@...;' ! @: $;
end break
 
'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 has seen so much, was astonished whenever it shone in
her face. Close by the king''s castle lay a great dark forest, and under
an old lime-tree in the forest was a well, and when the day was very
warm, the king''s child went out into the forest and sat down by the side
of the cool fountain, and when she was bored she took a golden ball, and
threw it up on high and caught it, and this ball was her favorite
plaything.' -> break&{width: 80} -> !OUT::write</syntaxhighlight>
{{out}}
<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
has seen so much, was astonished whenever it shone in her face. Close by the
king's castle lay a great dark forest, and under an old lime-tree in the forest
was a well, and when the day was very warm, the king's child went out into the
forest and sat down by the side of the cool fountain, and when she was bored she
took a golden ball, and threw it up on high and caught it, and this ball was her
favorite plaything.
</pre>
 
=={{header|Tcl}}==
Line 6,249 ⟶ 6,375:
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="ecmascriptwren">var greedyWordWrap = Fn.new { |text, lineWidth|
var words = text.split(" ")
var sb = words[0]
104

edits