string_extensions_refi 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/string_extensions_refi.rb +84 -0
  2. metadata +67 -0
@@ -0,0 +1,84 @@
1
+ # encoding: UTF-8
2
+
3
+ class String
4
+
5
+ # creates an aconym from a string
6
+ def acronymize
7
+ return self.gsub(/(([a-zA-Z0-9])([a-zA-Z0-9])*)./,"\\2")
8
+ end
9
+
10
+ # this method is very likely removed soon
11
+ # blob_to_hash allows this:
12
+ #tmp = <<eof
13
+ #metadata:
14
+ # create table metadata(
15
+ # nam string,
16
+ #
17
+ # primary key(nam)
18
+ # );
19
+ #sheet:
20
+ # create table sheet(
21
+ # id integer,
22
+ # nam string,
23
+ # );
24
+ #some_name:
25
+ #\tblah
26
+ #\t+blup
27
+ #\n
28
+ #\ttub
29
+ #other_name:
30
+ #\tfoo
31
+ #eof
32
+ #a_hash = tmp.blob_to_hash
33
+ #puts a_hash[metadata]
34
+ def blob_to_hash
35
+ tmp = {}
36
+ scan(/^(.+): *\n(((\t.*\n)|(\n))*)/){|m|
37
+ name = m[0]
38
+ value = m[1].strip_a_lot
39
+ tmp[name] = value
40
+ }
41
+ tmp
42
+ end
43
+
44
+ # substitutes all tabs, newlines and multiple spaces
45
+ # with a single space
46
+ # (and removes leading and trailing spaces after that)
47
+ def strip_a_lot
48
+ #return self.gsub(/\t|\n/," ").gsub(/ +/," ").strip
49
+ return self.gsub(/[\t\n ]+/," ").strip
50
+ end
51
+
52
+
53
+ def unescape_c_string #(s)
54
+ s = self
55
+ state = 0
56
+
57
+ # this speeds things up a lot if no backslashes are used at all
58
+ unless s.index("\\")
59
+ return s
60
+ else
61
+ # TODO the index could be used to speed things up...
62
+ end
63
+ res = ''
64
+ s.each_char { |c|
65
+ case state
66
+ when 0
67
+ case c
68
+ when "\\" then state = 1
69
+ else res << c
70
+ end
71
+ when 1
72
+ case c
73
+ when "\\" then res << "\\" ; state = 0
74
+ when '"' then res << "\"" ; state = 0
75
+ when 'n' then res << "\n" ; state = 0
76
+ when 't' then res << "\t" ; state = 0
77
+ else res << "\\" ; res << c ; state = 0
78
+ end
79
+ end
80
+ }
81
+ return res
82
+ end
83
+ end
84
+
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: string_extensions_refi
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Robert Fey
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-07-21 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: |-
22
+ unescaping c strings,
23
+ create acronyms from strings,
24
+ collapse tab newline and space characters to a single space with strip_a_lot
25
+ email: feyrob@gmail.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files: []
31
+
32
+ files:
33
+ - lib/string_extensions_refi.rb
34
+ has_rdoc: true
35
+ homepage: http://rubygems.org/gems/string_extensions_refi
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ segments:
49
+ - 0
50
+ version: "0"
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ requirements: []
60
+
61
+ rubyforge_project: nowarning
62
+ rubygems_version: 1.3.7
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: some extensions for the standard string class
66
+ test_files: []
67
+