Anonymous user
Word wrap: Difference between revisions
Add source for Rust
(Add source for Rust) |
|||
Line 189:
LA R4,S1
LH R5,LENS1
ICM R5,B'1000',=C' ' padding
MVCL R6,R4 pg=substr(s1,1,lens1)
XPRNT PG,L'PG put skip list(pg)
Line 210:
LA R4,S2
LH R5,LENS2
ICM R5,B'1000',=C' ' padding
MVCL R6,R4 pg=substr(s2,1,lens2)
XPRNT PG,L'PG put skip list(pg)
Line 425:
WrapText(Text, LineLength) {
}</lang>
{{Out}}
Line 657:
/* nonsensical hyphens to make greedy wrapping method look bad */
const char *string = "In olden times when wishing still helped one, there lived a king "
/*
*/
#define PENALTY_LONG
#define PENALTY_SHORT
typedef struct word_t {
} *word;
word make_word_list(const char *s, int *n)
{
}
}
}
int greedy_wrap(word words, int count, int cols, int *breaks)
{
}
}
}
}
}
}
/* tries to make right margin more even; pretty sure there's an off-by-one bug
int balanced_wrap(word words, int count, int cols, int *breaks)
{
else
}
}
}
}
}
}
void show_wrap(word list, int count, int *breaks)
{
}
}
}
int main(void)
{
}</lang>
Line 1,403:
paragraph( String, Max_line_length ) ->
task() ->
lines( [Word | T], Max_line_length ) ->
lines_assemble( Word, {Max, Line_length, Line, Acc} ) when erlang:length(Word) + Line_length > Max -> {Max, erlang:length(Word), Word, [Line | Acc]};
Line 1,603:
Should there be no suitable split in the fragment being appended, then, arbitrarily, if that fragment is short then it is not appended: the line is rolled with trailing spaces. But if it has more than six characters, it will be placed and a crude chop made.
<lang Fortran>
MODULE RIVERRUN
INTEGER BL,BLIMIT,BM
PARAMETER (BLIMIT = 222)
CHARACTER*(BLIMIT) BUMF
INTEGER OUTBUMF
DATA OUTBUMF/0/
PRIVATE BL,BLIMIT,BM
PRIVATE BUMF,OUTBUMF
CONTAINS
INTEGER FUNCTION LSTNB(TEXT) !Sigh. Last Not Blank.
Concocted yet again by R.N.McLean (whom God preserve) December MM.
Code checking reveals that the Compaq compiler generates a copy of the string and then finds the length of that when using the latter-day intrinsic LEN_TRIM. Madness!
Can't DO WHILE (L.GT.0 .AND. TEXT(L:L).LE.' ')
Curse the morons who think it good that the compiler MIGHT evaluate logical expressions fully.
Crude GO TO rather than a DO-loop, because compilers use a loop counter as well as updating the index variable.
Line 1,622:
Checking the indexing of CHARACTER variables for bounds evoked astounding stupidities, such as calculating the length of TEXT(L:L) by subtracting L from L!
Comparison runs of GNASH showed a saving of ~25-30% in its mass data scanning for this, involving all its two-dozen or so single-character comparisons, not just in LSTNB.
CHARACTER*(*),INTENT(IN):: TEXT
INTEGER L
L = LEN(TEXT)
1 IF (L.LE.0) GO TO 2
IF (ICHAR(TEXT(L:L)).GT.ICHAR(" ")) GO TO 2
L = L - 1
GO TO 1
2 LSTNB = L
RETURN
END FUNCTION LSTNB
SUBROUTINE STARTFLOW(OUT,WIDTH)
INTEGER OUT
INTEGER WIDTH
OUTBUMF = OUT
BM = WIDTH
IF (BM.GT.BLIMIT) STOP "Too wide!"
BL = 0
END SUBROUTINE STARTFLOW!Simple enough.
SUBROUTINE FLOW(TEXT)
CHARACTER*(*) TEXT
INTEGER TL
INTEGER T1,T2
INTEGER L
IF (OUTBUMF.LT.0) STOP "Call STARTFLOW first!"
TL = LSTNB(TEXT)
IF (TL.LE.0) THEN
CALL FLUSH
RETURN
END IF
IF (TEXT(1:1).LE." ") CALL FLUSH
Chunks of TEXT are to be appended to BUMF.
T1 = 1
10 IF (BL.GT.0) THEN
BL = BL + 1
BUMF(BL:BL) = " "
END IF
Consider the amount of text to be placed, TEXT(T1:TL)
L = TL - T1 + 1
IF (BM - BL .GE. L) THEN
BUMF(BL + 1:BM + L) = TEXT(T1:TL)
BL = BL + L
IF (BL .GE. BM - 1) CALL FLUSH
RETURN
END IF
Calculate the available space up to the end of a line. BUMF(BL + 1:BM)
L = BM - BL
T2 = T1 + L
IF (TEXT(T2:T2) .LE. " ") GO TO 12
T2 = T2 - 1
11 IF (TEXT(T2:T2) .GT. " ") THEN
T2 = T2 - 1
IF (T2 .GT. T1) GO TO 11
IF (L .LE. 6) THEN
CALL FLUSH
GO TO 10
END IF
T2 = T1 + L - 1
END IF
L = T2 - T1 + 1
12 BUMF(BL + 1:BL + L) = TEXT(T1:T1 + L - 1)
BL = BL + L
CALL FLUSH
Consider what the flushed line didn't take. TEXT(T1 + L:TL)
T1 = T1 + L
13 IF (T1.GT.TL) RETURN
IF (TEXT(T1:T1).LE." ") THEN
T1 = T1 + 1
GO TO 13
END IF
IF (T1.LE.TL) GO TO 10!Does anything remain?
RETURN
CONTAINS
SUBROUTINE FLUSH
IF (BL.GT.0) WRITE (OUTBUMF,"(A)") BUMF(1:BL)
BL = 0
END SUBROUTINE FLUSH
END SUBROUTINE FLOW
END MODULE RIVERRUN
PROGRAM TEST
Line 1,720:
1 READ (IN,2) BUMF
2 FORMAT (A)
IF (BUMF(1:1).NE."C") GO TO 1
CALL STARTFLOW(MSG,66)
3 CALL FLOW(BUMF)
READ (IN,2) BUMF
IF (BUMF(1:1).EQ."C") GO TO 3
CALL FLOW("")
CLOSE (IN)
Line 2,110:
public class WordWrap
{
{
}
{
{
{
}
{
}
}
}
{
}
}
Line 2,610:
=={{header|Lasso}}==
<lang Lasso>define wordwrap(
) => {
}
Line 3,617:
foreach($word in $divide){
}
}
Line 3,699:
foreach ($word in $words)
{
{
else
{
}
Line 3,851:
DataSection
Data.s "In olden times when wishing still helped one, there lived a king "+
EndDataSection
Line 4,627:
whose daughters were all beautiful, but the youngest was so | one, there lived a king whose daughters
beautiful that the sun itself, which has seen so much, was | were all beautiful, but the youngest was
astonished whenever it shone in her face.
</pre>
Without Browser
Line 4,655:
wend
print docOut$</lang>
=={{header|Rust}}==
This is an implementation of the simple greedy algorithm.
<lang Rust>#[derive(Clone, Debug)]
pub struct LineComposer<I> {
words: I,
width: usize,
current: Option<String>,
}
impl<I> LineComposer<I> {
pub(crate) fn new<S>(words: I, width: usize) -> Self
where
I: Iterator<Item = S>,
S: AsRef<str>,
{
LineComposer {
words,
width,
current: None,
}
}
}
impl<I, S> Iterator for LineComposer<I>
where
I: Iterator<Item = S>,
S: AsRef<str>,
{
type Item = String;
fn next(&mut self) -> Option<Self::Item> {
let mut next = match self.words.next() {
None => return self.current.take(),
Some(value) => value,
};
let mut current = self.current.take().unwrap_or_else(String::new);
loop {
let word = next.as_ref();
if self.width <= current.len() + word.len() {
self.current = Some(String::from(word));
// If the first word itself is too long, avoid producing an
// empty line. Continue instead with the next word.
if !current.is_empty() {
return Some(current);
}
}
if !current.is_empty() {
current.push_str(" ")
}
current.push_str(word);
match self.words.next() {
None => return Some(current), // Last line, current remains None
Some(word) => next = word,
}
}
}
}
// This part is just to extend all suitable iterators with LineComposer
pub trait ComposeLines: Iterator {
fn compose_lines(self, width: usize) -> LineComposer<Self>
where
Self: Sized,
Self::Item: AsRef<str>,
{
LineComposer::new(self, width)
}
}
impl<T, S> ComposeLines for T
where
T: Iterator<Item = S>,
S: AsRef<str>,
{
}
fn main() {
let text = r"
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.";
text.split_whitespace()
.compose_lines(80)
.for_each(|line| println!("{}", line));
}
</lang>
{{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|Scala}}==
Line 4,991 ⟶ 5,104:
}
}
var sww = SmartWordWrap();
var words = %w(aaa bb cc ddddd);
var wrapped = sww.wrap(words, 6);
say wrapped;</lang>
{{out}}
Line 5,013 ⟶ 5,126:
set RE "^(.{1,$n})(?:\\s+(.*))?$"
for {set result ""} {[regexp $RE $text -> line text]} {} {
}
return [string trimright $result "\n"]
Line 5,107 ⟶ 5,220:
column = 60
text = "In olden times when wishing still helped one, there lived a king " &_
Call wordwrap(text,column)
Sub wordwrap(s,n)
End Sub
</lang>
Line 5,180 ⟶ 5,293:
sub words(w$, p$(), d$)
end sub</lang>
Line 5,214 ⟶ 5,327:
length=72, calcIndents=True){
sink:=Data();
getIndents:='wrap(w){
reg lines=L(), len=0, prefix="", one=True;
do(2){
}
w.push(lines.xplode()); // put first two lines back to be formated
Line 5,231 ⟶ 5,344:
};
reg len=0, prefix="", w=text.walker(1);
if(calcIndents) len,prefix=getIndents(w);
foreach line in (w){
if(not line.strip()){ // blank line
}else
}
sink
|