Class: String
- Inherits:
-
Object
- Object
- String
- Includes:
- WWMD::Encoding
- Defined in:
- lib/wwmd/urlparse.rb,
lib/wwmd/viewstate/viewstate_yaml.rb,
lib/wwmd/class_extensions/extensions_base.rb,
lib/wwmd/class_extensions/extensions_rbkb.rb,
lib/wwmd/class_extensions/extensions_encoding.rb
Constant Summary collapse
- HANDLERS =
[:https,:http,:ftp,:file]
- @@he =
HTMLEntities.new
Instance Method Summary collapse
-
#b64d ⇒ Object
base 64 decode.
-
#b64e ⇒ Object
base 64 encode.
-
#basename(ext = nil) ⇒ Object
File.basename.
-
#clip(pref = "?") ⇒ Object
return everything in the string (url) before the first get param “foo.bar.com/page.asp?somearg=foo&otherarg=bar”.clip => “foo.bar.com/page.asp”.
-
#clop(pref = "?", preftoo = false) ⇒ Object
return everything in the string (url) after the first get parameter without the leading ‘?’.
- #clopa ⇒ Object (also: #clipa)
-
#clopp ⇒ Object
:nodoc:.
-
#contains?(rexp) ⇒ Boolean
return true or false for
string.match
. -
#crc32 ⇒ Object
returns CRC32 checksum for the string object.
-
#dehexdump(opt = {}) ⇒ Object
(also: #dedump)
converts a hexdump back to binary - takes the same options as hexdump() fairly flexible.
-
#dirname ⇒ Object
File.dirname with a trailing slash.
-
#edecode ⇒ Object
decode html entities in string.
-
#eencode(sym = nil) ⇒ Object
html entity encode string sym = :basic :named :decimal :hexadecimal.
-
#empty? ⇒ Boolean
strip the string and return true if empty.
-
#escape(reg = nil, unicodify = false) ⇒ Object
URI.escape using defaults or passed regexp.
-
#escape_all ⇒ Object
URI.escape all characters in string.
- #escape_default(reg = ) ⇒ Object
-
#escape_url(reg = ) ⇒ Object
URI.escape.
-
#escape_xss(reg = ) ⇒ Object
:nodoc:.
- #extname ⇒ Object
- #from_qp ⇒ Object
-
#has_ext? ⇒ Boolean
:nodoc:.
- #has_proto? ⇒ Boolean
- #hdp ⇒ Object
-
#head(c = 5) ⇒ Object
range or int.
-
#hexdump(opt = {}) ⇒ Object
Extends String class to return a hexdump in the style of ‘hexdump -C’.
- #hexify ⇒ Object
-
#ip_to_int ⇒ Object
ip address to int.
-
#is_guid? ⇒ Boolean
check if this string is a guid.
-
#ishex? ⇒ Boolean
shortcut for hex sanity with regex.
-
#mac_to_int ⇒ Object
mac address to int [uses ‘:’ as delimiter].
- #md5 ⇒ Object
- #mform ⇒ Object
- #pbcopy ⇒ Object
- #sha1 ⇒ Object
- #sha256 ⇒ Object
- #sha512 ⇒ Object
-
#starts_with?(dat) ⇒ Boolean
Does string “start with” dat? no clue whether/when this is faster than a regex, but it is easier than escaping regex characters.
-
#strip_html ⇒ Object
strip html tags from string.
- #strip_up ⇒ Object
- #swap16 ⇒ Object
- #to_ascii ⇒ Object
-
#to_fn(ext = nil) ⇒ Object
create filename from url changing “/” to “_”.
-
#to_form(action = nil) ⇒ Object
parse passed GET param string into a form and return the FormArray object.
- #to_form_from_req ⇒ Object (also: #to_ffr)
- #to_form_from_show ⇒ Object
-
#to_qp ⇒ Object
quoted printable.
-
#to_regexp ⇒ Object
return a literal regexp object for this string.
- #to_utf16 ⇒ Object
-
#to_yaml(opts = {}) ⇒ Object
patch not for general use do not try this at home.
-
#unescape ⇒ Object
URI.unescape.
- #unhexify ⇒ Object (also: #dehexify)
-
#write(fname = nil) ⇒ Object
write string to passed filename if filename is nil? will raise an error.
Methods included from WWMD::Encoding
Instance Method Details
#b64d ⇒ Object
base 64 decode
8 9 10 |
# File 'lib/wwmd/class_extensions/extensions_encoding.rb', line 8 def b64d self.unpack("m").first end |
#b64e ⇒ Object
base 64 encode
13 14 15 |
# File 'lib/wwmd/class_extensions/extensions_encoding.rb', line 13 def b64e [self].pack("m").gsub("\n","") end |
#basename(ext = nil) ⇒ Object
File.basename
109 110 111 112 113 114 115 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 109 def basename(ext=nil) if ext File.basename(self,ext) else File.basename(self) end end |
#clip(pref = "?") ⇒ Object
return everything in the string (url) before the first get param “foo.bar.com/page.asp?somearg=foo&otherarg=bar”.clip
> “foo.bar.com/page.asp”
73 74 75 76 77 78 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 73 def clip(pref="?") if (v = self.index(pref)) return self[0..(v-1)] end return self end |
#clop(pref = "?", preftoo = false) ⇒ Object
return everything in the string (url) after the first get parameter without the leading ‘?’
pass true as the second param to also get back the ? “foo.bar.com/page.asp?somearg=foo&otherarg=bar”.clop
> “somearg=foo&otherarg=bar”
86 87 88 89 90 91 92 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 86 def clop(pref="?",preftoo=false) (preftoo == false) ? add = "" : add = pref if (v = self.index(pref)) return add + self[(v+1)..-1] end return nil end |
#clopa ⇒ Object Also known as: clipa
96 97 98 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 96 def clopa return [self.clip,self.clop] end |
#clopp ⇒ Object
:nodoc:
94 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 94 def clopp; self.clop("?",true); end |
#contains?(rexp) ⇒ Boolean
return true or false for string.match
61 62 63 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 61 def contains?(rexp) return !self.match(rexp).nil? end |
#crc32 ⇒ Object
returns CRC32 checksum for the string object
178 179 180 |
# File 'lib/wwmd/class_extensions/extensions_rbkb.rb', line 178 def crc32 Zlib.crc32 self end |
#dehexdump(opt = {}) ⇒ Object Also known as: dedump
converts a hexdump back to binary - takes the same options as hexdump() fairly flexible. should work both with ‘xxd’ and ‘hexdump -C’ style dumps
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/wwmd/class_extensions/extensions_rbkb.rb', line 141 def dehexdump(opt={}) s=self out = opt[:out] || StringIO.new len = (opt[:len] and opt[:len] > 0)? opt[:len] : 16 hcrx = /[A-Fa-f0-9]/ dumprx = /^(#{hcrx}+):?\s*((?:#{hcrx}{2}\s*){0,#{len}})/ off = opt[:start_addr] || 0 i=1 # iterate each line of hexdump s.split(/\r?\n/).each do |hl| # match and check offset if m = dumprx.match(hl) and $1.hex == off i+=1 # take the data chunk and unhexify it raw = $2.unhexify off += out.write(raw) else raise "Hexdump parse error on line #{i} #{s}" end end if out.class == StringIO out.string end end |
#dirname ⇒ Object
File.dirname with a trailing slash
103 104 105 106 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 103 def dirname return self if self.match(/\/$/) File.dirname(self) + "/" end |
#edecode ⇒ Object
decode html entities in string
66 67 68 |
# File 'lib/wwmd/class_extensions/extensions_encoding.rb', line 66 def edecode return @@he.decode(self) end |
#eencode(sym = nil) ⇒ Object
html entity encode string
sym = :basic :named :decimal :hexadecimal
60 61 62 63 |
# File 'lib/wwmd/class_extensions/extensions_encoding.rb', line 60 def eencode(sym=nil) sym = :named if sym.nil? @@he.encode(self,sym) end |
#empty? ⇒ Boolean
strip the string and return true if empty
66 67 68 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 66 def empty? return self.strip == '' end |
#escape(reg = nil, unicodify = false) ⇒ Object
URI.escape using defaults or passed regexp
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/wwmd/class_extensions/extensions_encoding.rb', line 18 def escape(reg=nil,unicodify=false) if reg.nil? ret = URI.escape(self) elsif reg.kind_of?(Symbol) case reg when :none; return self when :default; ret = URI.escape(self) else; ret = URI.escape(self,WWMD::ESCAPE[reg]) end else ret = URI.escape(self,reg) end if unicodify ret.gsub!(/%/,"%u00") end return ret end |
#escape_all ⇒ Object
URI.escape all characters in string
49 50 51 |
# File 'lib/wwmd/class_extensions/extensions_encoding.rb', line 49 def escape_all#:nodoc: self.escape(/.*/) end |
#escape_default(reg = ) ⇒ Object
45 46 47 |
# File 'lib/wwmd/class_extensions/extensions_encoding.rb', line 45 def escape_default(reg=WWMD::ESCAPE[:default]) self.escape(reg) end |
#escape_url(reg = ) ⇒ Object
URI.escape
37 38 39 |
# File 'lib/wwmd/class_extensions/extensions_encoding.rb', line 37 def escape_url(reg=WWMD::ESCAPE[:url])#:nodoc: self.escape(reg) end |
#escape_xss(reg = ) ⇒ Object
:nodoc:
41 42 43 |
# File 'lib/wwmd/class_extensions/extensions_encoding.rb', line 41 def escape_xss(reg=WWMD::ESCAPE[:xss])#:nodoc: self.escape(reg) end |
#extname ⇒ Object
117 118 119 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 117 def extname self.split('.').last end |
#from_qp ⇒ Object
75 76 77 |
# File 'lib/wwmd/class_extensions/extensions_encoding.rb', line 75 def from_qp self.unpack("M").first end |
#has_ext? ⇒ Boolean
:nodoc:
121 122 123 124 |
# File 'lib/wwmd/urlparse.rb', line 121 def has_ext? #:nodoc: return false if self.basename.split(".",2)[1].empty? return true end |
#has_proto? ⇒ Boolean
113 114 115 116 117 118 119 |
# File 'lib/wwmd/urlparse.rb', line 113 def has_proto? begin return true if HANDLERS.include?(self.split(":").first.downcase.to_sym) rescue return false end end |
#hdp ⇒ Object
134 135 136 |
# File 'lib/wwmd/class_extensions/extensions_rbkb.rb', line 134 def hdp puts hexdump end |
#head(c = 5) ⇒ Object
range or int
176 177 178 179 180 181 182 183 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 176 def head(c=5) if c.kind_of?(Range) then range = c else range = (0..(c - 1)) end self.split("\n")[range].join("\n") end |
#hexdump(opt = {}) ⇒ Object
Extends String class to return a hexdump in the style of ‘hexdump -C’
:len => optionally specify a length other than 16 for a wider or thinner dump. If length is an odd number, it will be rounded up.
:out => optionally specify an alternate IO object for output. By default, hexdump will output to STDOUT. Pass a StringIO object and it will return it as a string.
Example: xxd = dat.hexdump(:len => 16, :out => StringIO.new) xxd => a hexdump
xxd = dat.hexdump(:len => 16, :out => STDERR) xxd => nil
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/wwmd/class_extensions/extensions_rbkb.rb', line 101 def hexdump(opt={}) s=self out = opt[:out] || StringIO.new len = (opt[:len] and opt[:len] > 0)? opt[:len] + (opt[:len] % 2) : 16 off = opt[:start_addr] || 0 offlen = opt[:start_len] || 8 hlen=len/2 s.scan(/(?:.|\n){1,#{len}}/) do |m| out.write(off.to_s(16).rjust(offlen, "0") + ' ') i=0 m.each_byte do |c| out.write c.to_s(16).rjust(2,"0") + " " out.write(' ') if (i+=1) == hlen end out.write(" " * (len-i) ) # pad out.write(" ") if i < hlen out.write(" |" + m.tr("\0-\37\177-\377", '.') + "|\n") off += m.length end out.write(off.to_s(16).rjust(offlen,'0') + "\n") if out.class == StringIO out.string end end |
#hexify ⇒ Object
37 38 39 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 37 def hexify self.unpack("H*").first.upcase end |
#ip_to_int ⇒ Object
ip address to int
51 52 53 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 51 def ip_to_int self.split('.').inject(0) { |a,e| (a << 8) + e.to_i } end |
#is_guid? ⇒ Boolean
check if this string is a guid
193 194 195 196 197 198 199 200 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 193 def is_guid? begin Guid.from_s(self) rescue => e return false end return true end |
#ishex? ⇒ Boolean
shortcut for hex sanity with regex
37 |
# File 'lib/wwmd/class_extensions/extensions_rbkb.rb', line 37 def ishex? ; (self =~ /^[a-f0-9]+$/i)? true : false ; end |
#mac_to_int ⇒ Object
mac address to int [uses ‘:’ as delimiter]
56 57 58 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 56 def mac_to_int self.split(':').inject(0) { |a,e| (a << 8) + e.to_i(16) } end |
#md5 ⇒ Object
202 203 204 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 202 def md5 Digest::MD5.digest(self).hexify end |
#mform ⇒ Object
153 154 155 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 153 def mform return self.gsub("\n","").to_form end |
#pbcopy ⇒ Object
218 219 220 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 218 def pbcopy IO.popen('pbcopy', 'r+') { |c| c.print self } end |
#sha1 ⇒ Object
206 207 208 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 206 def sha1 Digest::SHA1.digest(self).hexify end |
#sha256 ⇒ Object
210 211 212 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 210 def sha256 Digest::SHA256.digest(self).hexify end |
#sha512 ⇒ Object
214 215 216 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 214 def sha512 Digest::SHA512.digest(self).hexify end |
#starts_with?(dat) ⇒ Boolean
Does string “start with” dat? no clue whether/when this is faster than a regex, but it is easier than escaping regex characters
173 174 175 |
# File 'lib/wwmd/class_extensions/extensions_rbkb.rb', line 173 def starts_with?(dat) self[0,dat.size] == dat end |
#strip_html ⇒ Object
strip html tags from string
171 172 173 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 171 def strip_html self.gsub(/<\/?[^>]*>/, "") end |
#strip_up ⇒ Object
46 47 48 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 46 def strip_up self.gsub(/[^\x20-\x7e,\n]/,"").gsub(/^\n/,"") end |
#swap16 ⇒ Object
182 |
# File 'lib/wwmd/class_extensions/extensions_rbkb.rb', line 182 def swap16; unpack("v*").pack("n*"); end |
#to_ascii ⇒ Object
184 |
# File 'lib/wwmd/class_extensions/extensions_rbkb.rb', line 184 def to_ascii; Kconv.kconv(swap16, NKF::ASCII, NKF::UTF16); end |
#to_fn(ext = nil) ⇒ Object
create filename from url changing “/” to “_”
164 165 166 167 168 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 164 def to_fn(ext=nil) ret = self.clip.split("/")[3..-1].join("_") ret += ".#{ext}" if not ext.nil? return ret end |
#to_form(action = nil) ⇒ Object
parse passed GET param string into a form and return the FormArray object
130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 130 def to_form(action=nil) if self.split("\n").size > 1 return self.to_form_from_show end ret = FormArray.new self.split("&").each do |x| y = x.split("=",2) ret[y[0].to_s] = y[1].to_s # ret.extend!(y[0].to_s,y[1].to_s) end ret.action = action if action return ret end |
#to_form_from_req ⇒ Object Also known as: to_ffr
157 158 159 160 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 157 def to_form_from_req # self.split("\x0d\x0a\x0d\x0a")[1].to_form self.split("\n\n")[1].to_form end |
#to_form_from_show ⇒ Object
144 145 146 147 148 149 150 151 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 144 def to_form_from_show self.split("\n").map { |a| key,val = a.split("=",2) key = key.split(" ")[-1] val = val.strip if val ["#{key}=#{val}"] }.join("&").to_form.squeeze_keys! end |
#to_qp ⇒ Object
quoted printable
71 72 73 |
# File 'lib/wwmd/class_extensions/extensions_encoding.rb', line 71 def to_qp [self].pack("M") end |
#to_regexp ⇒ Object
return a literal regexp object for this string
escape regexp operators
188 189 190 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 188 def to_regexp return Regexp.new(self.gsub(/([\[\]\{\}\(\)\*\$\?])/) { |x| '\\' + x }) end |
#to_utf16 ⇒ Object
183 |
# File 'lib/wwmd/class_extensions/extensions_rbkb.rb', line 183 def to_utf16; Kconv.kconv(self, NKF::UTF16, NKF::ASCII).swap16; end |
#to_yaml(opts = {}) ⇒ Object
patch not for general use do not try this at home.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/wwmd/viewstate/viewstate_yaml.rb', line 6 def to_yaml( opts = {} ) YAML::quick_emit( is_complex_yaml? ? object_id : nil, opts ) do |out| if is_binary_data? out.scalar( "tag:yaml.org,2002:binary", [self].pack("m"), :literal ) elsif ( self =~ /\r\n/ ) # out.scalar( "tag:yaml.org,2002:binary", [self].pack("m"), :literal ) out.scalar( taguri, self.gsub(/\r\n/,"\r\r\n"), :quote2 ) elsif to_yaml_properties.empty? out.scalar( taguri, self, self =~ /^:/ ? :quote2 : to_yaml_style ) else out.map( taguri, to_yaml_style ) do |map| map.add( 'str', "#{self}" ) to_yaml_properties.each do |m| map.add( m, instance_variable_get( m ) ) end end end end end |
#unescape ⇒ Object
URI.unescape
54 55 56 |
# File 'lib/wwmd/class_extensions/extensions_encoding.rb', line 54 def unescape URI.unescape(self) end |
#unhexify ⇒ Object Also known as: dehexify
41 42 43 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 41 def unhexify [self].pack("H*") end |