Word wrap: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(6 intermediate revisions by 5 users not shown)
Line 973:
 
=={{header|C}}==
===Smart wrapping===
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
Line 1,152 ⟶ 1,153:
return 0;
}</syntaxhighlight>
 
===In-place greedy===
Long words exceeding the line length are not wrapped.
<syntaxhighlight lang="c">
#include <stdio.h>
#include <string.h>
 
void wrap_text(char *line_start, int width) {
char *last_space = 0;
char *p;
 
for (p = line_start; *p; p++) {
if (*p == '\n') {
line_start = p + 1;
}
 
if (*p == ' ') {
last_space = p;
}
 
if (p - line_start > width && last_space) {
*last_space = '\n';
line_start = last_space + 1;
last_space = 0;
}
}
}
 
char const text[] =
"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.";
 
int main(void) {
char buf[sizeof(text)];
 
puts("--- 80 ---");
memcpy(buf, text, sizeof(text));
wrap_text(buf, 80);
puts(buf);
 
puts("\n--- 72 ---");
memcpy(buf, text, sizeof(text));
wrap_text(buf, 72);
puts(buf);
}
</syntaxhighlight>
{{out}}
<pre>
--- 80 ---
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.
 
--- 72 ---
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|C sharp}}==
Line 1,835 ⟶ 1,912:
venenatis feugiat, augue orci pellentesque risus, nec pretium lacus enim eu
nibh.</pre>
 
=={{header|EasyLang}}==
 
<syntaxhighlight lang="easylang">
linew = 40
#
ind = 1
repeat
if ind > len words$[]
inp$ = input
words$[] = strsplit inp$ " "
ind = 1
.
until inp$ = ""
w$ = words$[ind]
ind += 1
if len out$ + len w$ + 1 <= linew
if out$ <> ""
out$ &= " "
.
out$ &= w$
else
print out$
out$ = w$
.
.
print out$
#
input_data
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.’
 
</syntaxhighlight>
 
=={{header|Elena}}==
ELENA 46.x :
<syntaxhighlight lang="elena">import extensions;
import system'routines;
Line 1,861 ⟶ 1,978:
^ TokenEnumerator
.new(self)
.selectBy::(word)
{
currentWidth += word.Length;
Line 1,868 ⟶ 1,985:
currentWidth := word.Length + 1;
^ newLinenewLineConstant + word + " "
}
else
Line 5,766 ⟶ 5,883:
<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,817 ⟶ 5,934:
root << [
array.first(i+1).join(' '),
self.prepare_words(array.ftslice(i+1), depth+1, callback)
]
 
Line 5,860 ⟶ 5,977:
self.combine([], path, { |combination|
var score = 0
combination.ftfirst(0, -21).each { |line|
score += (width - line.len -> sqr)
}
Line 5,874 ⟶ 5,991:
}
}
 
var sww = SmartWordWrap();
 
var words = %w(aaa bb cc ddddd);
var wrapped = sww.wrap(words, 6);
 
say wrapped;</syntaxhighlight>
{{out}}
<pre>
Line 5,917 ⟶ 6,034:
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,132 ⟶ 6,286:
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="ecmascriptwren">var greedyWordWrap = Fn.new { |text, lineWidth|
var words = text.split(" ")
var sb = words[0]
9,482

edits