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
- @@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:.
-
#head(c = 5) ⇒ Object
range or int.
-
#hexdump(opt = {}) ⇒ Object
Extends String class to return a hexdump in the style of ‘hexdump -C’.
-
#hexify(opts = {}) ⇒ Object
Convert a string to ASCII hex string supports a few options for format: :delim - delimter between each hex byte :prefix - prefix before each hex byte :suffix - suffix after each hex byte.
-
#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 ⇒ 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(d = /\s*/) ⇒ Object
(also: #dehexify)
Convert ASCII hex string to raw supports only ‘delimiter’ between hex bytes.
-
#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
100 101 102 103 104 105 106 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 100 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”
64 65 66 67 68 69 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 64 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”
77 78 79 80 81 82 83 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 77 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
87 88 89 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 87 def clopa return [self.clip,self.clop] end |
#clopp ⇒ Object
:nodoc:
85 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 85 def clopp; self.clop("?",true); end |
#contains?(rexp) ⇒ Boolean
return true or false for string.match
52 53 54 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 52 def contains?(rexp) return !self.match(rexp).nil? end |
#crc32 ⇒ Object
returns CRC32 checksum for the string object
173 174 175 |
# File 'lib/wwmd/class_extensions/extensions_rbkb.rb', line 173 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
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/wwmd/class_extensions/extensions_rbkb.rb', line 136 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
94 95 96 97 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 94 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
57 58 59 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 57 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
108 109 110 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 108 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:
100 101 102 103 |
# File 'lib/wwmd/urlparse.rb', line 100 def has_ext? #:nodoc: return false if self.basename.split(".",2)[1].empty? return true end |
#head(c = 5) ⇒ Object
range or int
165 166 167 168 169 170 171 172 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 165 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
99 100 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 |
# File 'lib/wwmd/class_extensions/extensions_rbkb.rb', line 99 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(opts = {}) ⇒ Object
Convert a string to ASCII hex string supports a few options for format:
:delim - delimter between each hex byte
:prefix - prefix before each hex byte
:suffix - suffix after each hex byte
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/wwmd/class_extensions/extensions_rbkb.rb', line 45 def hexify(opts={}) s=self delim = opts[:delim] pre = (opts[:prefix] || "") suf = (opts[:suffix] || "") if (rx=opts[:rx]) and not rx.kind_of? Regexp raise "rx must be a regular expression for a character class" end out=Array.new s.each_byte do |c| hc = if (rx and not rx.match c.chr) c.chr else pre + (HEXCHARS[(c >> 4)] + HEXCHARS[(c & 0xf )]) + suf end out << (hc) end out.join(delim) end |
#ip_to_int ⇒ Object
ip address to int
42 43 44 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 42 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
182 183 184 185 186 187 188 189 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 182 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]
47 48 49 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 47 def mac_to_int self.split(':').inject(0) { |a,e| (a << 8) + e.to_i(16) } end |
#md5 ⇒ Object
191 192 193 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 191 def md5 Digest::MD5.digest(self).hexify end |
#mform ⇒ Object
142 143 144 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 142 def mform return self.gsub("\n","").to_form end |
#pbcopy ⇒ Object
207 208 209 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 207 def pbcopy IO.popen('pbcopy', 'r+') { |c| c.print self } end |
#sha1 ⇒ Object
195 196 197 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 195 def sha1 Digest::SHA1.digest(self).hexify end |
#sha256 ⇒ Object
199 200 201 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 199 def sha256 Digest::SHA256.digest(self).hexify end |
#sha512 ⇒ Object
203 204 205 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 203 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
168 169 170 |
# File 'lib/wwmd/class_extensions/extensions_rbkb.rb', line 168 def starts_with?(dat) self[0,dat.size] == dat end |
#strip_html ⇒ Object
strip html tags from string
160 161 162 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 160 def strip_html self.gsub(/<\/?[^>]*>/, "") end |
#strip_up ⇒ Object
37 38 39 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 37 def strip_up self.gsub(/[^\x20-\x7e,\n]/,"").gsub(/^\n/,"") end |
#swap16 ⇒ Object
177 |
# File 'lib/wwmd/class_extensions/extensions_rbkb.rb', line 177 def swap16; unpack("v*").pack("n*"); end |
#to_ascii ⇒ Object
179 |
# File 'lib/wwmd/class_extensions/extensions_rbkb.rb', line 179 def to_ascii; Kconv.kconv(swap16, NKF::ASCII, NKF::UTF16); end |
#to_fn(ext = nil) ⇒ Object
create filename from url changing “/” to “_”
153 154 155 156 157 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 153 def to_fn(ext=nil) ret = self.clip.split("/")[3..-1].join("_") ret += ".#{ext}" if not ext.nil? return ret end |
#to_form ⇒ Object
parse passed GET param string into a form and return the FormArray object
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 121 def to_form 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.extend!(y[0].to_s,y[1].to_s) end return ret end |
#to_form_from_req ⇒ Object Also known as: to_ffr
146 147 148 149 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 146 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
133 134 135 136 137 138 139 140 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 133 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
177 178 179 |
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 177 def to_regexp return Regexp.new(self.gsub(/([\[\]\{\}\(\)\*\$\?])/) { |x| '\\' + x }) end |
#to_utf16 ⇒ Object
178 |
# File 'lib/wwmd/class_extensions/extensions_rbkb.rb', line 178 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(d = /\s*/) ⇒ Object Also known as: dehexify
Convert ASCII hex string to raw supports only ‘delimiter’ between hex bytes
71 72 73 74 75 76 77 78 79 |
# File 'lib/wwmd/class_extensions/extensions_rbkb.rb', line 71 def unhexify(d=/\s*/) s=self.strip out=StringIO.new while m = s.match(/^([A-Fa-f0-9]{1,2})#{d}?/) do out.write m[1].hex.chr s = m.post_match end out.string end |