Adds xml_char_encode. Like html_encode, but allows xml-safe character through.
git-svn-id: file:///home/svn/framework3/trunk@10214 4d416f70-5f16-0410-b530-b9f4589650daunstable
parent
b65be82387
commit
1d1805306e
|
@ -235,10 +235,10 @@ module Text
|
|||
# Returns the string with nonprintable hex characters sanitized to ascii. Similiar to to_hex,
|
||||
# but regular ASCII is not translated if count is 1.
|
||||
#
|
||||
def self.to_hex_ascii(str, prefix = "\\x", count = 1)
|
||||
def self.to_hex_ascii(str, prefix = "\\x", count = 1, suffix=nil)
|
||||
raise ::RuntimeError, "unable to chunk into #{count} byte chunks" if ((str.length % count) > 0)
|
||||
return str.unpack('H*')[0].gsub(Regexp.new(".{#{count * 2}}", nil, 'n')) { |s|
|
||||
(0x20..0x7e) === s.to_i(16) ? s.to_i(16).chr : prefix + s
|
||||
(0x20..0x7e) === s.to_i(16) ? s.to_i(16).chr : prefix + s + suffix.to_s
|
||||
}
|
||||
end
|
||||
|
||||
|
@ -476,6 +476,13 @@ module Text
|
|||
end
|
||||
end
|
||||
|
||||
#
|
||||
# Encode an ASCII string so it's safe for XML. It's a wrapper for to_hex_ascii.
|
||||
#
|
||||
def self.xml_char_encode(str)
|
||||
self.to_hex_ascii(str, "&#", 1, ";")
|
||||
end
|
||||
|
||||
#
|
||||
# Decode a URI encoded string
|
||||
#
|
||||
|
|
|
@ -24,10 +24,10 @@ class Rex::Text::UnitTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
def test_html_encode
|
||||
assert_equal('A', Rex::Text.html_encode('A'), 'html_encode default')
|
||||
assert_equal('A', Rex::Text.html_encode('A','hex'), 'html_encode hex')
|
||||
assert_equal('A', Rex::Text.html_encode('A','int'), 'html_encode int')
|
||||
assert_equal('A', Rex::Text.html_encode('A','int-wide'), 'html_encode int-wide')
|
||||
assert_equal('A', Rex::Text.html_encode('A'), 'html_encode default')
|
||||
assert_equal('A', Rex::Text.html_encode('A','hex'), 'html_encode hex')
|
||||
assert_equal('A', Rex::Text.html_encode('A','int'), 'html_encode int')
|
||||
assert_equal('A', Rex::Text.html_encode('A','int-wide'), 'html_encode int-wide')
|
||||
|
||||
assert_raises(TypeError) {
|
||||
Rex::Text.html_encode('a', 'umpa lumpa')
|
||||
|
@ -158,6 +158,11 @@ class Rex::Text::UnitTest < Test::Unit::TestCase
|
|||
assert_equal("\\x05Hello\\x06World!\\x03ABC", Rex::Text.to_hex_ascii(str))
|
||||
end
|
||||
|
||||
def test_xml_char_encode
|
||||
str = "\x05hello\x06world"
|
||||
assert_equal("helloworld", Rex::Text.xml_char_encode(str))
|
||||
end
|
||||
|
||||
def test_wordwrap
|
||||
txt = "this is a test of the word wrap features"
|
||||
|
||||
|
|
Loading…
Reference in New Issue