rdoba 0.0.3 → 0.1

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.
@@ -0,0 +1,93 @@
1
+ #!/usr/bin/ruby -KU
2
+ #<Encoding:UTF-8>
3
+
4
+ require 'rdoba/common'
5
+ require 'rdoba/strings'
6
+
7
+ class String
8
+ alias :_rdoba_to_i :to_i
9
+ def to_i(base = 10, *opts)
10
+ v = parse_opts(opts)
11
+ if v[:be]
12
+ (str, sign, num) = (self.match /\s*(-?)([0-9a-fx]+)/u).to_a
13
+ if str
14
+ n = num.reverse._rdoba_to_i(base)
15
+ sign.empty? && n || -n
16
+ else
17
+ 0
18
+ end
19
+ else
20
+ _rdoba_to_i(base)
21
+ end
22
+ end
23
+ end
24
+
25
+ class Fixnum
26
+ alias :_rdoba_to_s :to_s
27
+ def to_s(base = 10, *opts)
28
+ v = parse_opts(opts)
29
+
30
+ return _rdoba_to_s(base) unless v[:padding] or v[:style_formatting]
31
+
32
+ raise "Base of number can't be equal or less then zero" if base <= 0
33
+ raise "Padding count numberr can't be equal or less then zero" if v[:padding] <= 0
34
+ value = self
35
+ minus = if value < 0
36
+ value = -value
37
+ true
38
+ end
39
+ res = ''
40
+ while value != 0
41
+ value, rem = value.divmod(base)
42
+ rem += 0x40 - 0x39 if rem >= 10
43
+ res += (0x30 + rem).chr
44
+ end
45
+ res += "0" * (v[:padding].to_i - res.size) if res.size < v[:padding].to_i
46
+ res += 'x0' if v[:style_formatting] and base == 16
47
+ res += '-' if minus
48
+ res.reverse
49
+ end
50
+ end
51
+
52
+ class Numeric
53
+ def to_p(*opts)
54
+ v = parse_opts(opts)
55
+
56
+ value = self
57
+ minus = if value < 0
58
+ value = -value
59
+ true
60
+ end
61
+ res = ''
62
+ while value != 0
63
+ value, rem = value.divmod(256)
64
+ res += rem.chr
65
+ end
66
+
67
+ pad_char = if minus
68
+ negres += ''
69
+ over = 1
70
+ res.each_byte do |byte|
71
+ negbyte = 255 - byte + over
72
+ negres += if negbyte > 255
73
+ over = 1
74
+ 0
75
+ else
76
+ over = 0
77
+ negbyte
78
+ end
79
+ end
80
+ res = negres
81
+ "\xFF"
82
+ else
83
+ "\0"
84
+ end
85
+
86
+ res += pad_char * (v[:padding].to_i - res.size) if res.size < v[:padding].to_i
87
+
88
+ plain = (v[:be] ? res.reverse(String::ByteByByte) : res).to_p
89
+ plain
90
+ end
91
+ end
92
+
93
+
data/lib/rdoba/re.rb ADDED
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/ruby -KU
2
+ #<Encoding:UTF-8>
3
+
4
+ class String
5
+ def to_res
6
+ ostr = self.dup
7
+ res = ''
8
+ while true
9
+ m = ostr.match(/(?:([+\[\]\\().*?{}^$\/|])|«([^«]*)»)/u)
10
+ break unless m
11
+ res += m.pre_match + (m[2] || m[1] && ('\\' + m[1]))
12
+ ostr = m.post_match
13
+ end
14
+
15
+ res + ostr
16
+ end
17
+
18
+ def to_re
19
+ /#{to_res}/ui
20
+ end
21
+ end
22
+
@@ -0,0 +1,62 @@
1
+ #!/usr/bin/ruby -KU
2
+ #<Encoding:UTF-8>
3
+
4
+ require 'rdoba/common'
5
+ require 'rdoba/debug'
6
+
7
+ module Kernel
8
+ private
9
+ def require_dir(dir, name)
10
+ dbp11 "[require_dir] <<< dir = #{dir}, name = #{name}"
11
+ begin
12
+ rdir = File.join(dir, name)
13
+ return false unless File.directory?(rdir)
14
+ rdir = File.join(dir, name)
15
+ $: << rdir unless $:.include?(rdir)
16
+ dbp14 "[require_dir]> Found dir #{rdir}"
17
+ Dir.foreach(rdir) do |file|
18
+ next unless file =~ /(.*)\.(rb|so)$/
19
+ dbp14 "[require_dir]> Loading ... #{$1}"
20
+ require $1
21
+ end
22
+ true
23
+ rescue
24
+ false
25
+ end
26
+ end
27
+
28
+ def sub_require(name)
29
+ dbp11 "[sub_require] <<< name = #{name} "
30
+ $:.each do |dir|
31
+ begin
32
+ Dir.foreach(dir) do |file|
33
+ next unless file =~ /^#{name}\.(rb|so)$/
34
+ dbp14 "[sub_require]> Require Dir #{dir}/#{name} for #{file}"
35
+ r1 = require_dir(dir, name + '.modules')
36
+ r2 = require_dir(dir, name)
37
+ dbp14 "[sub_require]> Require Dir #{(r1 || r2) && 'passed' || 'failed'} ... #{name}"
38
+ end
39
+ rescue
40
+ end
41
+ end unless $".include?(name)
42
+ true
43
+ end
44
+
45
+ public
46
+
47
+ alias :__require__ :require
48
+ def require(name, *opts)
49
+ v = parse_opts(opts)
50
+ dbp11 "[require] <<< name = #{name}"
51
+ begin
52
+ res = __require__ name
53
+ rescue => bang
54
+ puts "Lib internal error: #{$!.class} -> #{$!}\n\t#{$@.join("\n\t")}"
55
+ exit
56
+ end
57
+ dbp14 "[require]> Loaded? #{name}... #{res}"
58
+ res = sub_require(name) if res and v[:recursive]
59
+ res
60
+ end
61
+ end
62
+
@@ -0,0 +1,45 @@
1
+ #!/usr/bin/ruby -KU
2
+
3
+ class Numeric
4
+ Roman = { 1 => 'I', 4 => 'IV', 5 => 'V', 9 => 'IX', 10 => 'X', 40 => 'XL', 50 => 'L',
5
+ 90 => 'XC', 100 => 'C', 400 => 'CD', 500 => 'D', 900 => 'CM', 1000 => 'M' }
6
+ Romani = Roman.keys.sort
7
+
8
+ def to_rom
9
+ res = ''
10
+ num = self
11
+ i = Romani.size - 1
12
+
13
+ while num > 0
14
+ if num >= Romani[i]
15
+ res << Roman[Romani[i]]
16
+ num -= Romani[i]
17
+ else
18
+ i -= 1
19
+ end
20
+ end
21
+ res
22
+ end
23
+
24
+ end
25
+
26
+ class String
27
+ def rom
28
+ h = Numeric::Roman.reverse
29
+ keys = h.keys.sort do |x,y| x.size < y.size ? 1 : x.size > y.size ? -1 : x <=> y end
30
+ str = self.upcase
31
+ res = 0
32
+ while str and not str.empty?
33
+ raise "Invalid roman number" if (keys.each do |key|
34
+ if str =~ /^#{key}(.*)/
35
+ str = $1
36
+ res += h[key]
37
+ break nil
38
+ end
39
+ end)
40
+ end
41
+ res
42
+ end
43
+ end
44
+
45
+
@@ -0,0 +1,149 @@
1
+ #!/usr/bin/ruby -KU
2
+ #<Encoding:UTF-8>
3
+
4
+ class String
5
+
6
+ def self.upcase(char)
7
+ chr = char.class == String ? char.ord : char.to_i
8
+ if chr >= 0x430 and chr < 0x450
9
+ chr -= 0x20
10
+ elsif chr >= 0x410 and chr < 0x430
11
+ elsif chr >= 0x400 and chr < 0x410 or
12
+ chr >= 0x450 and chr < 0x482 or
13
+ chr >= 0x48A and chr < 0x524 or
14
+ chr >= 0xA642 and chr < 0xA668 or
15
+ chr >= 0xA680 and chr < 0xA698
16
+ chr -= 1 if (chr % 1) == 1
17
+ else
18
+ return chr.chr.__upcase__
19
+ end
20
+ chr.chr
21
+ end
22
+
23
+ def self.downcase(char)
24
+ chr = (char.class == String) ? char.ord : char.to_i
25
+ if chr >= 0x410 and chr < 0x430
26
+ chr += 0x20
27
+ elsif chr >= 0x430 and chr < 0x450
28
+ elsif chr >= 0x400 and chr < 0x410 or
29
+ chr >= 0x450 and chr < 0x482 or
30
+ chr >= 0x48A and chr < 0x524 or
31
+ chr >= 0xA642 and chr < 0xA668 or
32
+ chr >= 0xA680 and chr < 0xA698
33
+ chr += 1 if (chr % 1) == 0
34
+ else
35
+ return chr.chr.__downcase__
36
+ end
37
+ chr.chr
38
+ end
39
+
40
+ if RUBY_VERSION < '1.9'
41
+
42
+ alias :setbyte :[]=
43
+
44
+ def encoding
45
+ 'UTF-8'
46
+ end
47
+
48
+ def force_encoding(*args)
49
+ self
50
+ end
51
+
52
+ def ord
53
+ a = nil
54
+ self.each_byte do |b|
55
+ c = b & 0xC0
56
+ case c
57
+ when 0xc0
58
+ a = (b & 0x3F)
59
+ when 0x80
60
+ return (a << 6) + (b & 0x3F)
61
+ else
62
+ return b
63
+ end
64
+ end
65
+ end
66
+
67
+ end
68
+
69
+ FirstChar = 0
70
+ alias :__upcase__ :upcase
71
+ def upcase(option = nil)
72
+ if option == FirstChar
73
+ r = self.dup
74
+ r[0] = String.upcase(self.ord)
75
+ r
76
+ elsif self.match(/[Ѐ-ҁҊ-ԣꙀ-ꙧꚀꚗ]/u)
77
+ self.unpack('U*').map do |chr| String.upcase(chr) end.join
78
+ else; __upcase__ end
79
+ end
80
+
81
+ alias :__downcase__ :downcase
82
+ def downcase(option = nil)
83
+ if option == FirstChar
84
+ r = self.dup
85
+ r[0] = String.downcase(self.ord)
86
+ r
87
+ elsif self.match(/[Ѐ-ҁҊ-ԣꙀ-ꙧꚀꚗ]/u)
88
+ self.unpack('U*').map do |chr| String.downcase(chr) end.join
89
+ else; __downcase__ end
90
+ end
91
+
92
+ alias :to_p :to_s
93
+
94
+ ByteByByte = 0
95
+ alias :__reverse__ :reverse
96
+ def reverse(step = 1)
97
+ case step
98
+ when ByteByByte
99
+ arr = []
100
+ self.each_byte do |byte| arr << byte.chr end
101
+ arr.reverse.join
102
+ when 1
103
+ __reverse__
104
+ else
105
+ res = ''
106
+ offset = (self.size + 1) / step * step - step
107
+ (0..offset).step(step) do |shift|
108
+ res += self[offset - shift..offset - shift + 1]
109
+ end
110
+ res
111
+ end
112
+ end
113
+
114
+ def compare_to(value, opts = {} )
115
+ if opts == :ignore_diacritics or
116
+ (opts.class == Hash and opts.key? :ignore_diacritics)
117
+ # TODO verify composite range
118
+ def crop_diacritics(x)
119
+ (x < 0x300 or
120
+ x > 0x36f and x < 0x483 or
121
+ x > 0x487 and x < 0xa67c or
122
+ x > 0xa67d) && x || nil
123
+ end
124
+
125
+ (self.unpack('U*').map do |x| crop_diacritics(x)
126
+ end.compact) <=> (value.unpack('U*').map do |x| crop_diacritics(x)
127
+ end.compact)
128
+ else
129
+ self <=> value
130
+ end
131
+ end
132
+ end
133
+
134
+ class Fixnum
135
+ alias :__chr__ :chr
136
+ def chr
137
+ if self >= 256
138
+ num = self; s = "\0"; byte = 0x80; a = []
139
+ while num >= 0x40
140
+ s.setbyte(0, byte + (num & 0x3F))
141
+ a << s.dup; num >>= 6; byte = 0x40
142
+ end
143
+ s.setbyte(0, 0xC0 + (num & 0x3F))
144
+ a << s
145
+ a.reverse.join.force_encoding('UTF-8')
146
+ else; __chr__ end
147
+ end
148
+ end
149
+
@@ -0,0 +1,3 @@
1
+ module Rdoba
2
+ VERSION = "0.1"
3
+ end
data/lib/rdoba/yaml.rb ADDED
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require 'rdoba/common'
4
+
5
+ class Object
6
+ def to_yml( o = {} )
7
+ level = o[:level] || 0
8
+ res = ''
9
+ res += '---' if level == 0
10
+ res += case self.class.to_sym
11
+ when :Hash
12
+ rs = ''
13
+ self.keys.sort do |x,y|
14
+ (ix, iy) = o[:order] ? [ o[:order].index(x), o[:order].index(y) ] : [ nil, nil ]
15
+ (ix and iy) ? ix <=> iy : (ix ? -1 : (iy ? 1 : x <=> y))
16
+ end.each do |key|
17
+ value = self[key]
18
+ rs += "\n" + ' ' * level * 2 + key.to_yml( { :level => level + 1, :order => o[:order] } )
19
+ rs += ': ' + value.to_yml( { :level => level + 1, :order => o[:order] } )
20
+ end
21
+ rs.empty? and "{}" or rs
22
+ when :Array
23
+ rs = ''
24
+ self.each do |value|
25
+ rs += "\n" + ' ' * level * 2 + '- ' + value.to_yml( { :level => level + 1, :order => o[:order] } )
26
+ end
27
+ rs.empty? and "[]" or rs
28
+ when :Fixnum
29
+ self.to_s
30
+ when :String
31
+ if self =~ /^["'\-:!#={}\[\]~]/ or self =~ /\s+$/ or self =~ /:\s/
32
+ if self.count("'") < self.count('"')
33
+ "'#{self.gsub("'","\\'")}'"
34
+ else
35
+ "\"#{self.gsub('"','\"')}\""
36
+ end
37
+ else
38
+ self
39
+ end
40
+ when :NilClass
41
+ ''
42
+ else
43
+ $stderr.puts "Unsupported class #{self.class} to export to yml"; ''
44
+ end
45
+ res
46
+ end
47
+ end
48
+