To re or not to re ... ( word wrap function?)
Ignacio Vazquez-Abrams
ignacio at openservices.net
Fri Sep 21 20:03:14 EDT 2001
On Sat, 22 Sep 2001, Boyd Roberts wrote:
> "Ignacio Vazquez-Abrams" <ignacio at openservices.net> a écrit dans le message news:
> mailman.1001111905.15419.python-list at python.org...
> > Yup. The following will do an entire document at once:
> >
> > ---
> > def wordwrap(text, width):
>
> i hope you have small documents or a _large_ amount or ram; your
> solution will not scale.
Well, I think "will" was the wrong word. I should have said "can". It is
entirely possible to feed the function one line or a subset of lines at a time
and it will still perform as expected.
> > while i<len(text):
> > if i+width+1>len(text):
> > i=len(text)
> > else:
> > findnl=string.find(text, '\n', i)
> > findspc=string.rfind(text, ' ', i, i+width+1)
> > if findspc!=-1:
> > if findnl!=-1 and findnl<findspc:
> > i=findnl+1
> > else:
> > text=text[:findspc]+'\n'+text[findspc+1:]
> > i=findspc+1
> > else:
> > findspc=string.find(text, ' ', i)
> > if findspc!=-1:
> > text=text[:findspc]+'\n'+text[findspc+1:]
> > i=findspc+1
> > return text
--
Ignacio Vazquez-Abrams <ignacio at openservices.net>
More information about the Python-list
mailing list