From 6fcd0b37b3cf9b37f3bd2833cef614ae22f3f7ca Mon Sep 17 00:00:00 2001 From: kou Date: Thu, 18 Nov 2010 14:25:03 +0000 Subject: * lib/rexml/formatters/pretty.rb (REXML::Formatters::Pretty#wrap): REXML::Formatters::Pretty#wrap used a recursive method call to format text. This switches it to use an iterative approach. [ruby-core:33245] Patch by Jeremy Evans. Thanks!!! * test/rexml/test_core.rb: add a test for it. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29827 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/rexml/formatters/pretty.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'lib/rexml') diff --git a/lib/rexml/formatters/pretty.rb b/lib/rexml/formatters/pretty.rb index 17d217d1dc..1747397700 100644 --- a/lib/rexml/formatters/pretty.rb +++ b/lib/rexml/formatters/pretty.rb @@ -126,11 +126,13 @@ module REXML end def wrap(string, width) - # Recursively wrap string at width. - return string if string.length <= width - place = string.rindex(' ', width) # Position in string with last ' ' before cutoff - return string if place.nil? - return string[0,place] + "\n" + wrap(string[place+1..-1], width) + parts = [] + while string.length > width and place = string.rindex(' ', width) + parts << string[0...place] + string = string[place+1..-1] + end + parts << string + parts.join("\n") end end -- cgit v1.2.3