Skip to content

Commit

Permalink
Add minimal encoding support to String#inspect
Browse files Browse the repository at this point in the history
  • Loading branch information
ryoqun committed Oct 8, 2012
1 parent 3fdd8b5 commit 6f04480
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 9 deletions.
4 changes: 0 additions & 4 deletions kernel/common/string.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -446,10 +446,6 @@ def insert(index, other)
ControlCharacters = [10, 9, 7, 11, 12, 13, 27, 8] ControlCharacters = [10, 9, 7, 11, 12, 13, 27, 8]
ControlPrintValue = ["\\n", "\\t", "\\a", "\\v", "\\f", "\\r", "\\e", "\\b"] ControlPrintValue = ["\\n", "\\t", "\\a", "\\v", "\\f", "\\r", "\\e", "\\b"]


def inspect
"\"#{transform(Rubinius::CType::Printed, true)}\""
end

def ljust(width, padstr=" ") def ljust(width, padstr=" ")
justify(width, :left, padstr) justify(width, :left, padstr)
end end
Expand Down
4 changes: 4 additions & 0 deletions kernel/common/string18.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -685,4 +685,8 @@ def []=(index, replacement, three=undefined)
end end
return replacement return replacement
end end

def inspect
"\"#{transform(Rubinius::CType::Printed, true)}\""
end
end end
30 changes: 30 additions & 0 deletions kernel/common/string19.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -810,4 +810,34 @@ def []=(index, replacement, three=undefined)
end end
return replacement return replacement
end end

def inspect
current_encoding = encoding
desired_encoding = Encoding.default_internal || Encoding.default_external

string = generate_inspected_string(current_encoding, desired_encoding)

"\"#{string}\""
end

def generate_inspected_string(current_encoding, desired_encoding)
if current_encoding == desired_encoding and
current_encoding == Encoding::UTF_8
table = Rubinius::CType::UTF8Printed

inspected_string = each_char.collect do |char|
if not char.valid_encoding? or char.ord < 256
table[char.force_encoding(Encoding::BINARY).ord]
else
char
end
end.join
inspected_string.gsub!(/(#[$@{])/, '\\\\\1')

Rubinius::Type.infect(inspected_string, self)
inspected_string
else
transform(Rubinius::CType::Printed, true)
end
end
end end
22 changes: 19 additions & 3 deletions kernel/delta/ctype.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- encoding: us-ascii -*- # -*- encoding: us-ascii -*-


module Rubinius::CType module Rubinius::CType
def self.toprint(num) def self.toprint(num, utf8=false)
# The character literals (?x) are Fixnums in 1.8 and Strings in 1.9 # The character literals (?x) are Fixnums in 1.8 and Strings in 1.9
# so we use literal values instead so this is 1.8/1.9 compatible. # so we use literal values instead so this is 1.8/1.9 compatible.
case num case num
Expand All @@ -14,11 +14,20 @@ def self.toprint(num)
when 13; '\r' when 13; '\r'
when 27; '\e' when 27; '\e'
when 34; '\"' when 34; '\"'
when 35; Rubinius::Tuple['#$', '\#$', '#@', '\#@', '#{', '\#{', '#', '#'] when 35;
unless utf8
Rubinius::Tuple['#$', '\#$', '#@', '\#@', '#{', '\#{', '#', '#']
else
'#' # TODO: '#' escaping is handled by String#generate_inspected_string
end
when 92; '\\\\' when 92; '\\\\'
else else
if num < 32 || num > 126 if num < 32 || num > 126
unprintable_chr(num) unless utf8
unprintable_chr(num)
else
unprintable_utf8_chr(num)
end
else else
num.chr num.chr
end end
Expand All @@ -32,6 +41,13 @@ def self.toprint(num)
i += 1 i += 1
end end


UTF8Printed = Rubinius::Tuple.new 256
i = 0
while i < 256
UTF8Printed[i] = toprint(i, true)
i += 1
end

def toprint def toprint
Printed[self] Printed[self]
end end
Expand Down
6 changes: 5 additions & 1 deletion kernel/delta/ctype18.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ def self.unprintable_chr(num)
c = num.to_s 8 c = num.to_s 8
str.copy_from c, 0, c.size, 4-c.size str.copy_from c, 0, c.size, 4-c.size
end end
end
def self.unprintable_utf8_chr(num)
unprintable_chr(num)
end
end
14 changes: 13 additions & 1 deletion kernel/delta/ctype19.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,4 +6,16 @@ def self.unprintable_chr(num)
c = num.to_s(16).upcase c = num.to_s(16).upcase
str.copy_from c, 0, c.size, 4-c.size str.copy_from c, 0, c.size, 4-c.size
end end
end
def self.unprintable_utf8_chr(num)
if num <= 0x7f
str = "\\u0000"
str.modify!

c = num.to_s(16).upcase
str.copy_from c, 0, c.size, 6-c.size
else
unprintable_chr(num)
end
end
end

0 comments on commit 6f04480

Please sign in to comment.