mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* 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
This commit is contained in:
parent
c6e4767068
commit
6fcd0b37b3
3 changed files with 33 additions and 5 deletions
10
ChangeLog
10
ChangeLog
|
@ -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.
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue