From b522f815001ed124af53f5f3bbaed78156a2fe4c Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Wed, 17 Nov 2010 14:17:54 -0800 Subject: [PATCH] Fix SystemStackError when trying to format a large block of text in REXML REXML::Formatters::Pretty#wrap used a recursive method call to format text. This switches it to use an iterative approach. --- lib/rexml/formatters/pretty.rb | 13 ++++++++----- test/rexml/test_core.rb | 7 +++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/rexml/formatters/pretty.rb b/lib/rexml/formatters/pretty.rb index 17d217d..07efd8a 100644 --- a/lib/rexml/formatters/pretty.rb +++ b/lib/rexml/formatters/pretty.rb @@ -126,11 +126,14 @@ 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 = [] + last_place = 0 + 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 5fae4f5..5d0ad53 100644 --- a/test/rexml/test_core.rb +++ b/test/rexml/test_core.rb @@ -1151,6 +1151,13 @@ EOL assert_not_equal( c, d ) end + def test_long_text + aaaa = 'aaaa ' * 1_000_000 + a = "#{aaaa}" + f = REXML::Formatters::Pretty.new + xmldoc = REXML::Document.new( a ) + assert_nothing_raised{f.write(xmldoc, b="")} + end def test_ticket_58 doc = REXML::Document.new -- 1.7.3.2