20 Jun, 2010, Runter wrote in the 21st comment:
Votes: 0
More Rubyesque.


# extend String class to define format method.
class String
def format(count=80, preserve=false)
# split the string, then collect the raw_format as an array, and join the entire array into one string.
split("\n",-1).collect! { |each_paragraph| if preserve then "" else " " end + each_paragraph.raw_format!(count, preserve)}.join("\n")

# format a string raw. sub out any line feeds.
def raw_format!(count=80, preserve=false)
lstrip! unless preserve # strip leading spaces.
return self if length < count

# Look for a space, break it off there, then recursively call the function again.
(count-1).downto(0) do |backwards_offset|
if self[backwards_offset].chr == ' ' # Split here if it's a space.
part = (self.slice!(0…backwards_offset) || "") + "\n"
return part + self.raw_format!(count)
return self # if we get this far then there was no space.

Output with test strings:
Preservation mode:
855 He seyde, "Syn I shal bigynne the game,
What, welcome be the cut, a Goddes name!
Now lat us ryde, and herkneth what I seye."
And with that word we ryden forth oure weye,
And he bigan with right a myrie cheere
860 His tale anon, and seyde as ye may heere.

No preservation:
Suspendisse semper urna. Aenean ullamcorper pede nunc eu lectus eget nisl.
Donec sollicitudin vitae, Curabitur ultrices urna, id erat sit amet, est. Duis
quam tristique senectus et interdum eu, luctus et enim. Mauris convallis
varius. Morbi placerat quis,

felis. augue. Maecenas interdum adipiscing elit. Ut id diam mauris, adipiscing
elit. Nam nec felis tincidunt est sem, accumsan eu, ullamcorper lorem. Donec at
laoreet risus. Phasellus dignissim. Nunc vulputate. Morbi accumsan

consectetuer ut, tempus ultrices, gravida sit amet, consectetuer vulputate
vitae, mauris. Pellentesque molestie sed, viverra auctor, ante imperdiet orci
id ipsum. Proin cursus aliquam.
21 Jun, 2010, David Haley wrote in the 22nd comment:
Votes: 0
I like how preservation mode has the side effect of translating Latin to Old English and introducing line numbers. :smile:
21 Jun, 2010, Noplex wrote in the 23rd comment:
Votes: 0
I clicked on this thread because whenever I see .f I immediately wonder who in their right mind is writing Fortran nowadays. It seems I'm the only fool stuck maintaining it :-).
16 Aug, 2010, Oliver wrote in the 24th comment:
Votes: 0
As a heads up, I uploaded the format_string() function I found. It preserves intentional newlines when formatting. It seems like there've been more people than just me who had problems.

Here it is: http://www.mudbytes.net/file-2768