Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/string_extensions_refi.rb
Instance Method Summary collapse
-
#acronymize ⇒ Object
creates an aconym from a string.
-
#blob_to_hash ⇒ Object
this method is very likely removed soon blob_to_hash allows this: tmp = <<eof metadata: create table metadata( nam string, .
-
#strip_a_lot ⇒ Object
substitutes all tabs, newlines and multiple spaces with a single space (and removes leading and trailing spaces after that).
-
#unescape_c_string ⇒ Object
(s).
Instance Method Details
#acronymize ⇒ Object
creates an aconym from a string
6 7 8 |
# File 'lib/string_extensions_refi.rb', line 6 def acronymize return self.gsub(/(([a-zA-Z0-9])([a-zA-Z0-9])*)./,"\\2") end |
#blob_to_hash ⇒ Object
this method is very likely removed soon blob_to_hash allows this: tmp = <<eof metadata: create table metadata( nam string,
primary key(nam) ); sheet: create table sheet( id integer, nam string, ); some_name: tblah t+blup n ttub other_name: tfoo eof a_hash = tmp.blob_to_hash puts a_hash
34 35 36 37 38 39 40 41 42 |
# File 'lib/string_extensions_refi.rb', line 34 def blob_to_hash tmp = {} scan(/^(.+): *\n(((\t.*\n)|(\n))*)/){|m| name = m[0] value = m[1].strip_a_lot tmp[name] = value } tmp end |
#strip_a_lot ⇒ Object
substitutes all tabs, newlines and multiple spaces with a single space (and removes leading and trailing spaces after that)
47 48 49 50 |
# File 'lib/string_extensions_refi.rb', line 47 def strip_a_lot #return self.gsub(/\t|\n/," ").gsub(/ +/," ").strip return self.gsub(/[\t\n ]+/," ").strip end |
#unescape_c_string ⇒ Object
(s)
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/string_extensions_refi.rb', line 53 def unescape_c_string #(s) s = self state = 0 # this speeds things up a lot if no backslashes are used at all unless s.index("\\") return s else # TODO the index could be used to speed things up... end res = '' s.each_char { |c| case state when 0 case c when "\\" then state = 1 else res << c end when 1 case c when "\\" then res << "\\" ; state = 0 when '"' then res << "\"" ; state = 0 when 'n' then res << "\n" ; state = 0 when 't' then res << "\t" ; state = 0 else res << "\\" ; res << c ; state = 0 end end } return res end |