Word wrap: Difference between revisions
Content added Content deleted
No edit summary |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 829: | Line 829: | ||
return 0; |
return 0; |
||
}</lang> |
}</lang> |
||
=={{header|C++}}== |
|||
Basic task. |
|||
{{trans|Go}} |
|||
<lang cpp>#include <iostream> |
|||
#include <sstream> |
|||
#include <string> |
|||
const char *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." |
|||
}; |
|||
std::string wrap(const char *text, size_t line_length = 72) |
|||
{ |
|||
std::istringstream words(text); |
|||
std::ostringstream wrapped; |
|||
std::string word; |
|||
if (words >> word) { |
|||
wrapped << word; |
|||
size_t space_left = line_length - word.length(); |
|||
while (words >> word) { |
|||
if (space_left < word.length() + 1) { |
|||
wrapped << '\n' << word; |
|||
space_left = line_length - word.length(); |
|||
} else { |
|||
wrapped << ' ' << word; |
|||
space_left -= word.length() + 1; |
|||
} |
|||
} |
|||
} |
|||
return wrapped.str(); |
|||
} |
|||
int main() |
|||
{ |
|||
std::cout << "Wrapped at 72:\n" << wrap(text) << "\n\n"; |
|||
std::cout << "Wrapped at 80:\n" << wrap(text, 80) << "\n"; |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
Wrapped at 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. |
|||
Wrapped at 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.</pre> |
|||
=={{header|C sharp}}== |
=={{header|C sharp}}== |
||
Line 992: | Line 922: | ||
venenatis feugiat, augue orci pellentesque risus, nec pretium lacus enim eu |
venenatis feugiat, augue orci pellentesque risus, nec pretium lacus enim eu |
||
nibh.</pre> |
nibh.</pre> |
||
=={{header|C++}}== |
|||
Basic task. |
|||
{{trans|Go}} |
|||
<lang cpp>#include <iostream> |
|||
#include <sstream> |
|||
#include <string> |
|||
const char *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." |
|||
}; |
|||
std::string wrap(const char *text, size_t line_length = 72) |
|||
{ |
|||
std::istringstream words(text); |
|||
std::ostringstream wrapped; |
|||
std::string word; |
|||
if (words >> word) { |
|||
wrapped << word; |
|||
size_t space_left = line_length - word.length(); |
|||
while (words >> word) { |
|||
if (space_left < word.length() + 1) { |
|||
wrapped << '\n' << word; |
|||
space_left = line_length - word.length(); |
|||
} else { |
|||
wrapped << ' ' << word; |
|||
space_left -= word.length() + 1; |
|||
} |
|||
} |
|||
} |
|||
return wrapped.str(); |
|||
} |
|||
int main() |
|||
{ |
|||
std::cout << "Wrapped at 72:\n" << wrap(text) << "\n\n"; |
|||
std::cout << "Wrapped at 80:\n" << wrap(text, 80) << "\n"; |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
Wrapped at 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. |
|||
Wrapped at 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.</pre> |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
Line 2,065: | Line 2,065: | ||
</lang> |
</lang> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
Line 2,622: | Line 2,621: | ||
Wikipedia. |
Wikipedia. |
||
</pre> |
</pre> |
||
=={{header|M2000 Interpreter}}== |
=={{header|M2000 Interpreter}}== |
||
M2000 Environment (where actual a M2000 script executed) provide the means to print proportional text, in console, in layer in console form, in user windows layers, and in printer paper. To render proportional text we can use: |
M2000 Environment (where actual a M2000 script executed) provide the means to print proportional text, in console, in layer in console form, in user windows layers, and in printer paper. To render proportional text we can use: |
||
Line 2,803: | Line 2,803: | ||
eleven! |
eleven! |
||
</pre> |
</pre> |
||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
Line 3,130: | Line 3,129: | ||
$_ = $s; |
$_ = $s; |
||
s/\s*(.{1,25})\s/$1\n/g, print;</lang> |
s/\s*(.{1,25})\s/$1\n/g, print;</lang> |
||
=={{header|Perl 6}}== |
|||
<lang perl6>my $s = "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."; |
|||
$s ~~ s:g/»\s+/ /; |
|||
$s ~~ s/\s*$/\n\n/; |
|||
say $s.subst(/ \s* (. ** 1..66) \s /, -> $/ { "$0\n" }, :g); |
|||
say $s.subst(/ \s* (. ** 1..25) \s /, -> $/ { "$0\n" }, :g);</lang> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 3,721: | Line 3,704: | ||
(wrap (string-split text) 70) |
(wrap (string-split text) 70) |
||
</lang> |
</lang> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
<lang perl6>my $s = "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."; |
|||
$s ~~ s:g/»\s+/ /; |
|||
$s ~~ s/\s*$/\n\n/; |
|||
say $s.subst(/ \s* (. ** 1..66) \s /, -> $/ { "$0\n" }, :g); |
|||
say $s.subst(/ \s* (. ** 1..25) \s /, -> $/ { "$0\n" }, :g);</lang> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 4,696: | Line 4,697: | ||
threw it up on high and caught it, and this ball was her favorite |
threw it up on high and caught it, and this ball was her favorite |
||
plaything.</pre> |
plaything.</pre> |
||
=={{header|TPP}}== |
=={{header|TPP}}== |
||
The text presentation program automatically provides word wrap: |
The text presentation program automatically provides word wrap: |