diff options
-rw-r--r-- | ChangeLog | 10 | ||||
-rw-r--r-- | lib/rexml/formatters/pretty.rb | 12 | ||||
-rw-r--r-- | test/rexml/test_core.rb | 16 |
3 files changed, 33 insertions, 5 deletions
@@ -1,3 +1,13 @@ +Thu Nov 18 23:21:23 2010 Kouhei Sutou <kou@cozmixng.org> + + * 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. + Thu Nov 18 22:58:43 2010 Nobuyoshi Nakada <nobu@ruby-lang.org> * include/ruby/io.h (rb_io_buffer_t): extract from rb_io_t. 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 diff --git a/test/rexml/test_core.rb b/test/rexml/test_core.rb index 5fae4f54e3..d9280f6aed 100644 --- a/test/rexml/test_core.rb +++ b/test/rexml/test_core.rb @@ -1151,6 +1151,22 @@ EOL assert_not_equal( c, d ) end + def test_pretty_format_long_text_finite + n = 1_000_000 + long_text = 'aaaa ' * n + xml = "<doc>#{long_text}</doc>" + formatter = REXML::Formatters::Pretty.new + document = REXML::Document.new(xml) + output = "" + assert_nothing_raised do + formatter.write(document, output) + end + assert_equal("<doc>\n" + + ((" " + (" aaaa" * 15) + "\n") * (n / 15)) + + " " + ("aaaa " * (n % 15)) + "\n" + + "</doc>", + output) + end def test_ticket_58 doc = REXML::Document.new |