Line wrapping

This should do the job:

def wrap(s)
return s.gsub(/[1].{0,74}(?:\s|\Z)/){($& + 5.chr).
gsub(/\n\005/,“\n”).gsub(/\005/,“\n”)}
end

···

On 2003-08-23 03:18:26 +0900, Andreas Schwarz wrote:

def wrap(s)
return s.gsub(/.{1,74}(?:\s|\Z)/){($& + 5.chr).
gsub(/\n\005/,“\n”).gsub(/\005/,“\n”)}
end

Any hints how I can change the function so that it doesn’t touch lines
with quoting characters (“>”, “|”) in the beginning?


I have long suspected a cozy little link between virus writers and
antivirus software makers. The latter certainly needs the former, both to
keep viruses in the news and to provide a steady revenue stream from updates.
– Bruce Schneier, CRYPTO-GRAM, July 15, 2002


  1. ^>| ↩︎

Florian Frank wrote:

···

On 2003-08-23 03:18:26 +0900, Andreas Schwarz wrote:

def wrap(s)
return s.gsub(/.{1,74}(?:\s|\Z)/){($& + 5.chr).
gsub(/\n\005/,“\n”).gsub(/\005/,“\n”)}
end

Any hints how I can change the function so that it doesn’t touch lines
with quoting characters (“>”, “|”) in the beginning?

This should do the job:

def wrap(s)
return s.gsub(/[1].{0,74}(?:\s|\Z)/){($& + 5.chr).
gsub(/\n\005/,“\n”).gsub(/\005/,“\n”)}
end

No, it doesn’t, but I’ve found this solution:

def wrap(s)
s = s.gsub(/.{0,74}(?:\s|\Z)/){($& + 5.chr).gsub(/\n\005/,“\n”)}
return s.gsub(/((\n|^)[>|\s][>|].?)\005/, “\1”).gsub(/\005/,“\n”)
end

Andreas


AVR-Tutorial, über 350 Links
Forum für AVRGCC und MSPGCC
http://www.mikrocontroller.net


  1. ^>| ↩︎

Andreas Schwarz wrote:

Florian Frank wrote:

def wrap(s)
return s.gsub(/.{1,74}(?:\s|\Z)/){($& + 5.chr).
gsub(/\n\005/,“\n”).gsub(/\005/,“\n”)}
end

Any hints how I can change the function so that it doesn’t touch lines
with quoting characters (“>”, “|”) in the beginning?

This should do the job:

def wrap(s)
return s.gsub(/[1].{0,74}(?:\s|\Z)/){($& + 5.chr).
gsub(/\n\005/,“\n”).gsub(/\005/,“\n”)}
end

No, it doesn’t, but I’ve found this solution:

It doesn’t work correctly, I have to think about it again…

···

On 2003-08-23 03:18:26 +0900, Andreas Schwarz wrote:


AVR-Tutorial, über 350 Links
Forum für AVRGCC und MSPGCC
http://www.mikrocontroller.net


  1. ^>| ↩︎

Andreas Schwarz wrote:

It doesn’t work correctly, I have to think about it again…

OK, now this finally seems to work:

def wrap(a, max_len)
s = a.gsub(/.{0,#{max_len}}(?:\s|\Z)/){($& + 5.chr).gsub(/\n\005/,“\n”)}
while(s.sub!(/((\n|^)[>|\s][>|].?)\005/, “\1”)) do end
return s.gsub(/\005/,“\n”)
end

I don’t think it is a very good solution, so if someone comes up
with a better one I would really like to see it.

Andreas

···


AVR-Tutorial, über 350 Links
Forum für AVRGCC und MSPGCC
http://www.mikrocontroller.net

Hi –

Andreas Schwarz wrote:

It doesn’t work correctly, I have to think about it again…

See if this comes close…

def wrap(s,len)
s.gsub(/[1].*?(?=[2]|\Z)/m) do |x|
x.squeeze(" “).split(/\s+/).join(” ").
gsub(/(\S.{1,#{len}}( |\Z))/) { |s| “#{s}\n” }
end
end

David

···

On Sat, 23 Aug 2003, Andreas Schwarz wrote:


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav


  1. ^>| ↩︎

  2. >| ↩︎

Here’s some C code to do line wrapping (written by Joseph K. Myers)
quickly, efficiently and simply. The code is presented verbatim:

=begin
/* An explanation is more troublesome than the problem. */

int main(int argc, char **argv) {
char b[4096]; int l=0, m=-1, m1=-1, s1=0, c, i, s, w;
if (argc < 2 || sscanf(argv[1], “%3d”, &w) < 1)
w = 72;
while ((s=read(0, b, m == -1 ? 4096 : m)) > 0) {
for (i=0; i<s; i++) {
c = b[i];
if (c == ‘\n’)
l=0, m=-1;
else {
if (c == ’ ’ || c == ‘\t’ || c == ‘\r’)
m = i;
if (++l > w && m != -1) {
b[m] = ‘\n’;
l = i - (i < m ? m-s1 : m);
m = -1;
}
}
}
if (m1 != -1)
write(1, &b[m1], s1-m1);
write(1, b, m == -1 ? s : m);
m1=m, s1=s;
}
if (m != -1)
write(1, &b[m], s1-m);
return s;
}
=end

I was planning on wrapping Ruby around this after I learned enough C,
Swig and Ruby, but perhaps someone else would like to do it.

It can also be called by system:

wrap < infile > outfile

or, if synchronization is needed:

require 'shell’
shell = Shell.new
shell.transact do
wrap < infile > outfile
end

Regards,

Mark