Adding a Rex library method for generating an array of mixed case strings.

git-svn-id: file:///home/svn/framework3/trunk@8841 4d416f70-5f16-0410-b530-b9f4589650da
This commit is contained in:
Tod Beardsley 2010-03-17 17:47:08 +00:00
parent 984890a2cd
commit a29ff6f435
1 changed files with 24 additions and 1 deletions

View File

@ -416,7 +416,7 @@ module Text
end end
# #
# Encode a string in a manor useful for HTTP URIs and URI Parameters. # Encode a string in a manner useful for HTTP URIs and URI Parameters.
# #
def self.html_encode(str, mode = 'hex') def self.html_encode(str, mode = 'hex')
case mode case mode
@ -449,6 +449,29 @@ module Text
return buf return buf
end end
#
# Takes a string, and returns an array of all mixed case versions.
#
# Example:
#
# >> Rex::Text.to_mixed_case_array "abc1"
# => ["abc1", "abC1", "aBc1", "aBC1", "Abc1", "AbC1", "ABc1", "ABC1"]
#
def self.to_mixed_case_array(str)
letters = []
str.scan(/./).each { |l| letters << [l.downcase, l.upcase] }
coords = []
(1 << str.size).times { |i| coords << ("%0#{str.size}b" % i) }
mixed = []
coords.each do |coord|
c = coord.scan(/./).map {|x| x.to_i}
this_str = ""
c.each_with_index { |d,i| this_str << letters[i][d] }
mixed << this_str
end
return mixed.uniq
end
# #
# Converts a string a nicely formatted hex dump # Converts a string a nicely formatted hex dump
# #