Skip to content

Commit 36feda5

Browse files
committed
Work around inconsistent encoding result from String#gsub! in #expand_tabs
1 parent 626abbc commit 36feda5

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

History.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* When including a file lossily force-transcode it to the output encoding
55
instead of crashing to preserve as much content as possible. Ruby Bug
66
#4376 by Yui NARUSE.
7-
* Work around inconsistent encoding result from String#sub!. Related to
8-
Ruby Bug #4376.
7+
* Work around inconsistent encoding result from String#sub!, String#gsub!.
8+
Related to Ruby Bug #4376.
99
* Work around inconsistent encoding result from String#[]=. Related to Ruby
1010
Bug #4376.
1111
* When Darkfish fails the file being generated is now reported.

lib/rdoc/text.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,12 @@ def self.encode_fallback character, encoding, fallback
4444
def expand_tabs text
4545
expanded = []
4646

47+
space = ' '
48+
space.force_encoding text.encoding if Object.const_defined? :Encoding
49+
4750
text.each_line do |line|
4851
line.gsub!(/^(.{8}*?)([^\t\r\n]{0,7})\t/) do
49-
"#{$1}#{$2}#{' ' * (8 - $2.size)}"
52+
"#{$1}#{$2}#{space * (8 - $2.size)}"
5053
end until line !~ /\t/
5154

5255
expanded << line

test/test_rdoc_text.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ def test_expand_tabs
5555
expand_tabs(".\t\t."), 'dot tab tab dot')
5656
end
5757

58+
def test_expand_tabs_encoding
59+
skip "Encoding not implemented" unless Object.const_defined? :Encoding
60+
61+
inn = "hello\ns\tdave"
62+
inn.force_encoding Encoding::BINARY
63+
64+
out = expand_tabs inn
65+
66+
assert_equal "hello\ns dave", out
67+
assert_equal Encoding::BINARY, out.encoding
68+
end
69+
5870
def test_flush_left
5971
text = <<-TEXT
6072

0 commit comments

Comments
 (0)