September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,223 @@
* Word wrap 29/01/2017
WORDWRAP CSECT
USING WORDWRAP,R13
B 72(R15) skip savearea
DC 17F'0' savearea
STM R14,R12,12(R13) prolog
ST R13,4(R15) " <-
ST R15,8(R13) " ->
LR R13,R15 " addressability
MVC S2,=CL96' ' s2=''
SR R0,R0
STH R0,LENS2 lens2=0
LA R8,1 i=1
LOOPI CH R8,=AL2(NTS) do i=1 to hbound(ts)
BH ELOOPI --
LH R4,LENS2
LTR R4,R4 if lens2=0
BNZ IFLENS2 then
LR R1,R8 i
MH R1,=H'48'
LA R14,TS-48(R1)
MVC S(48),0(R14) s=ts(i)
MVC S+48(48),=CL48' '
LA R12,L'TS jmax=length(ts)
B EIFLENS2 else
IFLENS2 MVC S,=CL96' ' s=''
LA R6,S @s
LH R7,LENS2
LA R4,S2 @s2
LH R5,LENS2
MVCL R6,R4 substr(s,1,lens2)=substr(s2,1,lens2)
LH R2,LENS2
LA R2,1(R2) lens2+1
LR R1,R8 i
MH R1,=H'48'
LA R14,TS-48(R1) @ts(i)
LA R15,S-1
AR R15,R2
MVC 0(48,R15),0(R14) substr(s,lens2+1,48)=ts(i)
LA R12,L'S jmax=length(s)
EIFLENS2 MVI OKS2,X'01' oks2=true
WHILEOK CLI OKS2,X'01' do while(oks2)
BNE EWHILEOK --
LR R9,R12 j=jmax /*loop1*/
LOOPJ1 CH R9,=H'1' do j=jmax to 1 by -1
BL ELOOPJ1 --
LA R14,S-1 @s-1
AR R14,R9 j
MVC CJ(1),0(R14) cj=substr(s,j,1)
CLI CJ,C' ' if cj^=' '
BNE ELOOPJ1 then leave j
BCTR R9,0 j=j-1
B LOOPJ1 end do j
ELOOPJ1 STH R9,LENS lens=j {length of s}
MVI OKJ,X'00' okj=false /*loop2*/
LH R11,W js=w
LH R4,W
CH R4,LENS if w>lens
BNH IFWLENS
LH R11,LENS js=lens
IFWLENS LR R9,R11 j=js
LOOPJ2 CH R9,=H'1' do j=js to 1 by -1
BL ELOOPJ2 --
LA R14,S-1 @s-1
AR R14,R9 +j
MVC CJ(1),0(R14) cj=substr(s,j,1)
CLI CJ,C' ' if cj=' '
BNE ITERJ2 then
MVI OKJ,X'01' okj=true
B ELOOPJ2 leave j
ITERJ2 BCTR R9,0 j=j-1
B LOOPJ2 end do j
ELOOPJ2 CLI OKJ,X'00' if ^okj
BNE ELOOPK
MVI OKK,X'00' okk=false /*loop3*/
LH R10,W k=w
LOOPK CH R10,LENS do k=w to lens
BH ELOOPK --
LA R14,S-1 @s-1
AR R14,R10 +k
MVC CK(1),0(R14) ck=substr(s,k,1)
CLI CK,C' ' if ck=' '
BNE ITERK then
MVI OKK,X'01' okk=true
B ELOOPK leave k
ITERK LA R10,1(R10) k=k+1
B LOOPK end do k
ELOOPK MVC S2,=CL96' ' s2=' '
SR R0,R0
STH R0,LENS2 lens2=0
MVI CAS,X'01' cas=true
LH R1,LENS
CH R1,W lens<w
BL IFLENSLW
MVI CAS,X'00' cas=false
IFLENSLW CLI CAS,X'00' if ^cas
BNE IFNOTCAS then
CLI OKJ,X'01' if okj
BNE NOKJ then
STH R9,LENS1 lens1=j
LH R2,LENS
SR R2,R9 -j
LA R2,1(R2)
STH R2,LENS2 lens2=lens-j+1
LA R6,S1
LR R7,R9 j
LA R4,S
LR R5,R7
MVCL R6,R4 s1=substr(s,1,j)
LH R4,LENS2
LTR R4,R4 if lens2>0
BNP ELJLENS2 then
LA R6,S2
LH R7,LENS2
LA R4,S(R9) @s(j+1)
LR R5,R7
MVCL R6,R4 s2=substr(s,j+1,lens2)
B EFJLENS2
ELJLENS2 SR R0,R0 else
STH R0,LENS2 lens2=0
EFJLENS2 B IFNOTCAS
NOKJ CLI OKK,X'01' else if okk
BNE NOTOKK
STH R10,LENS1 lens1=k
LH R2,LENS
SR R2,R10 -k
LA R2,1(R2)
STH R2,LENS2 lens2=lens-k+1
LA R6,S1
LR R7,R10 k
LA R4,S
LR R5,R7
MVCL R6,R4 s1=substr(s,1,k)
LH R4,LENS2
LTR R4,R4 if lens2>0
BNP ELKLENS2 then
LA R6,S2
LH R7,LENS2
LA R4,S(R10) @s(k+1)
LR R5,R7
MVCL R6,R4 s2=substr(s,k+1,lens2)
B EFKLENS2 else
ELKLENS2 SR R0,R0
STH R0,LENS2 lens2=0
EFKLENS2 B IFNOTCAS else
NOTOKK LH R0,LENS
STH R0,LENS1 lens1=lens
MVC S1,S s1=s
IFNOTCAS CLI CAS,X'01' if cas
BNE ELCAS then
LH R7,LENS
LA R7,1(R7)
LA R6,S2
LA R4,S
LR R5,R7
MVCL R6,R4 s2=substr(s,1,lens+1)
LH R2,LENS
LA R2,1(R2)
STH R2,LENS2 lens2=lens+1
B EFCAS else
ELCAS LA R6,PG
LA R7,L'PG
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)
EFCAS MVI OKS2,X'00' oks2=false
LH R4,LENS2
CH R4,W if lens2>w
BNH EFWLENS2 then
MVI OKS2,X'01' oks2=true
LH R0,LENS2
STH R0,LENS lens=lens2
MVC S,S2 s=s2
EFWLENS2 B WHILEOK end while
EWHILEOK LA R8,1(R8) i=i+1
B LOOPI end do i
ELOOPI LH R4,LENS2
LTR R4,R4 if lens2^=0
BZ EFLENS2N then
LA R6,PG
LA R7,L'PG
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)
EFLENS2N L R13,4(0,R13) epilog
LM R14,R12,12(R13) " restore
XR R15,R15 " rc=0
BR R14 exit
TS DC CL48'In olden times when wishing still helped one,'
DC CL48'there lived a king whose daughters were all,'
DC CL48'beautiful, but the youngest was so beautiful'
DC CL48'that the sun itself, which has seen so much,'
DC CL48'was astonished whenever it shone in her face.'
DC CL48'Close by the king''s castle lay a great dark'
DC CL48'forest, and under an old lime tree in the'
DC CL48'forest was a well, and when the day was very'
DC CL48'warm, the king''s child went out into the forest'
DC CL48'and sat down by the side of the cool fountain,'
DC CL48'and when she was bored she took a golden ball,'
DC CL48'and threw it up on high and caught it, and this'
DC CL48'ball was her favorite plaything.'
TSE DC 0C
NTS EQU (TSE-TS)/L'TS
W DC H'36' <-- input width 12<=w<=80
LENS DS H
S DS CL96
LENS1 DS H
S1 DS CL96
LENS2 DS H
S2 DS CL96
OKJ DS X
OKK DS X
OKS2 DS X
CAS DS X
CJ DS CL1
CK DS CL1
PG DS CL80
YREGS
END WORDWRAP

View file

@ -0,0 +1,13 @@
paragraph$ = "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."
PRINT ALIGN$(paragraph$, 72, 0)
PRINT ALIGN$(paragraph$, 90, 0)

View file

@ -0,0 +1,40 @@
@echo off
set "input=Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur!"
rem call the function (the second parameter is the line width)
call :wrap "%input%" 40
echo(
call :wrap "%input%" 70
pause>nul
exit /b 0
:: The procedure
:wrap
set "line="
set "tmp_str=%~1"
set /a "width=%2", "width-=1"
:proc_loop
rem check if we are done already
if "%tmp_str%"=="" (
setlocal enabledelayedexpansion
if defined line echo(!line!
endlocal & goto :EOF
)
rem not yet done, so take a word and process it
for /f "tokens=1,* delims= " %%A in ("%tmp_str%") do (
set "word=%%A"
set "tmp_str=%%B"
setlocal enabledelayedexpansion
if "!line!"=="" (set "testline=!word!") else (set "testline=!line! !word!")
if "!testline:~%width%,1!" == "" (
set "line=!testline!"
) else (
echo(!line!
set "line=!word!"
)
)
endlocal & set "line=%line%"
goto proc_loop

View file

@ -1,4 +1,4 @@
;; Wrap line base on regular expression
(defn wrap-line [size text]
(re-seq (re-pattern (str ".{0," size "}\\s"))
(re-seq (re-pattern (str ".{1," size "}\\s|.{1," size "}"))
(clojure.string/replace text #"\n" " ")))

View file

@ -1,12 +1,3 @@
(def 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.")
(doseq [line (wrap-line 72 text)]
(println line))
;; cl-format based version
(defn wrap-line [size text]
(clojure.pprint/cl-format nil (str "~{~<~%~1," size ":;~A~> ~}") (clojure.string/split text #" ")))

View file

@ -0,0 +1,12 @@
(def 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.")
(doseq [line (wrap-line 72 text)]
(println line))

View file

@ -0,0 +1,50 @@
import extensions.
import system'routines.
import extensions.
import extensions'text.
literal 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.".
extension wrapOp
{
wrap(IntNumber lineWidth)
[
int currentWidth := 0.
^ TokenEnumerator
new(self);
selectBy(:word)
[
currentWidth += word length.
if (currentWidth > lineWidth)
[
currentWidth := word length + 1.
^ 'newLine + word + " "
];
[
currentWidth += 1.
^ word + " "
]
];
summarize(String new).
]
}
program =
[
console printLine(String new; write:"-" length:72).
console printLine(text wrap(72)).
console printLine(String new; write:"-" length:80).
console printLine(text wrap(80)).
].

View file

@ -0,0 +1,6 @@
String wordWrap(str, width=80) {
str.tokenize(' ').inject([[]]) { rows, word ->
if (rows.last().join(' ').length() + word.length() <= width) rows.last() << word else rows << [word]
rows
}.collect { it.join(' ') }.join('\n')
}

View file

@ -0,0 +1,8 @@
import groovy.transform.TailRecursive
import static java.lang.Math.min
@TailRecursive
String wordWrap(str, w, i=w, b=''<<'', len=str.length()-1, x=0) {
b.setCharAt(x = (b << str[b.length()..i]).lastIndexOf(' '), '\n' as char)
b.length()+w >= len ? b << str[i..-1] : wordWrap(str, w, min(x+w+1, len), b, len, 0)
}

View file

@ -0,0 +1,2 @@
def a = new StringBuilder()
def a = '' << ''

View file

@ -1,23 +1,25 @@
ss = "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."
ss =
concat
[ "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."
]
wordwrap maxlen = (wrap_ 0) . words where
wrap_ _ [] = "\n"
wrap_ pos (w:ws)
-- at line start: put down the word no matter what
| pos == 0 = w ++ wrap_ (pos + lw) ws
| pos + lw + 1 > maxlen = '\n':wrap_ 0 (w:ws)
| otherwise = " " ++ w ++ wrap_ (pos + lw + 1) ws
where lw = length w
wordwrap maxlen = wrap_ 0 . words
where
wrap_ _ [] = "\n"
wrap_ pos (w:ws)
-- at line start: put down the word no matter what
| pos == 0 = w ++ wrap_ (pos + lw) ws
| pos + lw + 1 > maxlen = '\n' : wrap_ 0 (w : ws)
| otherwise = ' ' : w ++ wrap_ (pos + lw + 1) ws
where
lw = length w
main = do
putStr $ wordwrap 72 ss
putStr "\n"
putStr $ wordwrap 32 ss
main = mapM_ putStr [wordwrap 72 ss, "\n", wordwrap 32 ss]

View file

@ -1,21 +1,24 @@
import Data.List
import Data.List (inits, tails, tail)
teststring = "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."
testString =
concat
[ "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."
]
wwrap'' _ [] = []
wwrap'' i ss = (\(a,b) -> a : wwrap'' i b)
$ last . filter ((<=i) . length . unwords . fst)
$ zip (inits ss) (tails ss)
wWrap'' _ [] = []
wWrap'' i ss =
(\(a, b) -> a : wWrap'' i b) $
last . filter ((<= i) . length . unwords . fst) $ zip (inits ss) (tails ss)
wwrap :: Int -> String -> String
wwrap i = unlines . map unwords . wwrap'' i . words . concat . lines
wWrap :: Int -> String -> String
wWrap i = unlines . map unwords . wWrap'' i . words . concat . lines
main = putStrLn $ wwrap 80 teststring
main = putStrLn $ wWrap 80 testString

View file

@ -0,0 +1,37 @@
// version 1.1.3
val 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."
fun greedyWordwrap(text: String, lineWidth: Int): String {
val words = text.split(' ')
val sb = StringBuilder(words[0])
var spaceLeft = lineWidth - words[0].length
for (word in words.drop(1)) {
val len = word.length
if (len + 1 > spaceLeft) {
sb.append("\n").append(word)
spaceLeft = lineWidth - len
}
else {
sb.append(" ").append(word)
spaceLeft -= (len + 1)
}
}
return sb.toString()
}
fun main(args: Array<String>) {
println("Greedy algorithm - wrapped at 72:")
println(greedyWordwrap(text, 72))
println("\nGreedy algorithm - wrapped at 80:")
println(greedyWordwrap(text, 80))
}

View file

@ -0,0 +1,35 @@
(define (get-one-word start)
(let loop ((chars #null) (end start))
(let ((char (car end)))
(if (has? (list #\space #\newline) char)
(values (reverse chars) (force (cdr end)))
(loop (cons char chars) (force (cdr end)))))))
(define (get-all-words string)
(let loop ((words #null) (start (str-iter string)))
(let* ((word next (get-one-word start)))
(if (null? next)
(reverse words)
(loop (cons (runes->string word) words) next)))))
(define (get-one-line words n)
(let loop ((line #null) (words words) (i 0))
(let*((word (car words))
(len (string-length word)))
(if (null? (cdr words))
(values (reverse (cons word line)) #null)
(if (> (+ i len) n 1)
(values (reverse line) words)
(loop (cons word line) (cdr words) (+ i len 1)))))))
(define (get-all-lines words n)
(let loop ((lines #null) (words words))
(let* ((line words (get-one-line words n)))
(if (null? words)
(reverse (cons line lines))
(loop (cons line lines) words)))))
(define (hyphenation width string)
(let*((words (get-all-words string))
(lines (get-all-lines words width)))
lines))

View file

@ -0,0 +1,25 @@
string s = substitute("""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.""","\n"," ")
procedure word_wrap(string s, integer maxwidth)
sequence words = split(s)
string line = words[1]
for i=2 to length(words) do
string word = words[i]
if length(line)+length(word)+1>maxwidth then
puts(1,line&"\n")
line = word
else
line &= " "&word
end if
end for
puts(1,line&"\n")
end procedure
word_wrap(s,72)
word_wrap(s,80)

View file

@ -0,0 +1,40 @@
>>> import textwrap
>>> help(textwrap.fill)
Help on function fill in module textwrap:
fill(text, width=70, **kwargs)
Fill a single paragraph of text, returning a new string.
Reformat the single paragraph in 'text' to fit in lines of no more
than 'width' columns, and return a new string containing the entire
wrapped paragraph. As with wrap(), tabs are expanded and other
whitespace characters converted to space. See TextWrapper class for
available keyword args to customize wrapping behaviour.
>>> txt = '''\
Reformat the single paragraph in 'text' to fit in lines of no more
than 'width' columns, and return a new string containing the entire
wrapped paragraph. As with wrap(), tabs are expanded and other
whitespace characters converted to space. See TextWrapper class for
available keyword args to customize wrapping behaviour.'''
>>> print(textwrap.fill(txt, width=75))
Reformat the single paragraph in 'text' to fit in lines of no more than
'width' columns, and return a new string containing the entire wrapped
paragraph. As with wrap(), tabs are expanded and other whitespace
characters converted to space. See TextWrapper class for available keyword
args to customize wrapping behaviour.
>>> print(textwrap.fill(txt, width=45))
Reformat the single paragraph in 'text' to
fit in lines of no more than 'width' columns,
and return a new string containing the entire
wrapped paragraph. As with wrap(), tabs are
expanded and other whitespace characters
converted to space. See TextWrapper class
for available keyword args to customize
wrapping behaviour.
>>> print(textwrap.fill(txt, width=85))
Reformat the single paragraph in 'text' to fit in lines of no more than 'width'
columns, and return a new string containing the entire wrapped paragraph. As with
wrap(), tabs are expanded and other whitespace characters converted to space. See
TextWrapper class for available keyword args to customize wrapping behaviour.
>>>

View file

@ -0,0 +1,39 @@
(import (scheme base)
(scheme write)
(only (srfi 13) string-join string-tokenize))
;; word wrap, using greedy algorithm with minimum lines
(define (simple-word-wrap str width)
(let loop ((words (string-tokenize str))
(line-length 0)
(line '())
(lines '()))
(cond ((null? words)
(reverse (cons (reverse line) lines)))
((> (+ line-length (string-length (car words)))
width)
(if (null? line)
(loop (cdr words) ; case where word exceeds line length
0
'()
(cons (list (car words)) lines))
(loop words ; word must go to next line, so finish current line
0
'()
(cons (reverse line) lines))))
(else
(loop (cdr words) ; else, add word to current line
(+ 1 line-length (string-length (car words)))
(cons (car words) line)
lines)))))
;; run examples - text from RnRS report
(define *text* "Programming languages should be designed not by piling feature on top of feature, but by removing the weaknesses and restrictions that make additional features appear necessary. Scheme demonstrates that a very small number of rules for forming expressions, with no restrictions on how they are composed, suffice to form a practical and efficient programming language that is flexible enough to support most of the major programming paradigms in use today.")
(define (show-para algorithm width)
(display (make-string width #\-)) (newline)
(for-each (lambda (line) (display (string-join line " ")) (newline))
(algorithm *text* width)))
(show-para simple-word-wrap 50)
(show-para simple-word-wrap 60)

View file

@ -0,0 +1,37 @@
fcn formatText(text, // text can be String,Data,File, -->Data
length=72, calcIndents=True){
sink:=Data();
getIndents:='wrap(w){ // look at first two lines to indent paragraph
reg lines=L(), len=0, prefix="", one=True;
do(2){
if(w._next()){
lines.append(line:=w.value);
word:=line.split(Void,1)[0,1]; // get first word, if line !blank
if(word){
p:=line[0,line.find(word[0]]);
if(one){ sink.write(p); len=p.len(); one=False; }
else prefix=p;
}
}
}
w.push(lines.xplode()); // put first two lines back to be formated
return(len,prefix);
};
reg len=0, prefix="", w=text.walker(1); // lines
if(calcIndents) len,prefix=getIndents(w);
foreach line in (w){
if(not line.strip()){ // blank line
sink.write("\n",line); // blank line redux
if(calcIndents) len,prefix=getIndents(w);
else len=0; // restart formating
}else
len=line.split().reduce('wrap(len,word){
n:=word.len();
if(len==0) { sink.write(word); return(n); }
nn:=n+1+len; if(nn<=length) { sink.write(" ",word); return(nn); }
sink.write("\n",prefix,word); return(prefix.len()+word.len());
},len);
}
sink
}

View file

@ -0,0 +1 @@
formatText(File("frog.txt")).text.println();

View file

@ -0,0 +1,2 @@
[1..].zipWith("%2d: %s".fmt,formatText(File("frog.txt")).walker(1))
.pump(String).println();

View file

@ -0,0 +1 @@
formatText("this\n is a test foo bar\n\ngreen eggs and spam",10).text.println();