chars 0.2.0 → 0.2.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.
- data/.gemspec +0 -0
- data/.yardopts +1 -1
- data/ChangeLog.md +9 -0
- data/README.md +4 -0
- data/Rakefile +3 -2
- data/chars.gemspec +124 -7
- data/gemspec.yml +5 -3
- data/lib/chars/char_set.rb +157 -38
- data/lib/chars/chars.rb +24 -20
- data/lib/chars/extensions/integer.rb +17 -17
- data/lib/chars/version.rb +1 -1
- data/spec/char_set_spec.rb +63 -89
- data/spec/chars_spec.rb +14 -73
- data/spec/{integer_spec.rb → extensions/integer_spec.rb} +1 -2
- data/spec/{string_spec.rb → extensions/string_spec.rb} +1 -2
- data/spec/spec_helper.rb +1 -0
- metadata +18 -38
data/lib/chars/chars.rb
CHANGED
@@ -2,59 +2,63 @@ require 'chars/char_set'
|
|
2
2
|
|
3
3
|
module Chars
|
4
4
|
# The numeric decimal character set
|
5
|
-
NUMERIC = CharSet
|
5
|
+
NUMERIC = CharSet['0'..'9']
|
6
6
|
|
7
7
|
# The octal character set
|
8
|
-
OCTAL = CharSet
|
8
|
+
OCTAL = CharSet['0'..'7']
|
9
9
|
|
10
10
|
# The upper-case hexadecimal character set
|
11
|
-
UPPERCASE_HEXADECIMAL = NUMERIC
|
11
|
+
UPPERCASE_HEXADECIMAL = NUMERIC | CharSet['A'..'F']
|
12
12
|
|
13
13
|
# The lower-case hexadecimal character set
|
14
|
-
LOWERCASE_HEXADECIMAL = NUMERIC
|
14
|
+
LOWERCASE_HEXADECIMAL = NUMERIC | CharSet['a'..'f']
|
15
15
|
|
16
16
|
# The hexadecimal character set
|
17
|
-
HEXADECIMAL = UPPERCASE_HEXADECIMAL
|
17
|
+
HEXADECIMAL = UPPERCASE_HEXADECIMAL | LOWERCASE_HEXADECIMAL
|
18
18
|
|
19
19
|
# The upper-case alpha character set
|
20
|
-
UPPERCASE_ALPHA = CharSet
|
20
|
+
UPPERCASE_ALPHA = CharSet['A'..'Z']
|
21
21
|
|
22
22
|
# The lower-case alpha character set
|
23
|
-
LOWERCASE_ALPHA = CharSet
|
23
|
+
LOWERCASE_ALPHA = CharSet['a'..'z']
|
24
24
|
|
25
25
|
# The alpha character set
|
26
|
-
ALPHA = UPPERCASE_ALPHA
|
26
|
+
ALPHA = UPPERCASE_ALPHA | LOWERCASE_ALPHA
|
27
27
|
|
28
28
|
# The alpha-numeric character set
|
29
|
-
ALPHA_NUMERIC = ALPHA
|
29
|
+
ALPHA_NUMERIC = ALPHA | NUMERIC
|
30
30
|
|
31
31
|
# The punctuation character set
|
32
|
-
PUNCTUATION = CharSet
|
33
|
-
|
32
|
+
PUNCTUATION = CharSet[' ', '\'', '"', '`', ',', ';', ':', '~', '-',
|
33
|
+
'(', ')', '[', ']', '{', '}', '.', '?', '!']
|
34
34
|
|
35
35
|
# The symbolic character set
|
36
|
-
SYMBOLS = PUNCTUATION
|
37
|
-
'
|
36
|
+
SYMBOLS = PUNCTUATION | CharSet[
|
37
|
+
'@', '#', '$', '%', '^', '&', '*', '_', '+',
|
38
|
+
'=', '|', '\\', '<', '>', '/'
|
39
|
+
]
|
38
40
|
|
39
41
|
# The space character set
|
40
42
|
SPACE = CharSet.new(' ', "\f", "\n", "\r", "\t", "\v")
|
41
43
|
|
42
44
|
# The set of printable characters (not including spaces)
|
43
|
-
VISIBLE = ALPHA_NUMERIC
|
44
|
-
|
45
|
-
|
45
|
+
VISIBLE = ALPHA_NUMERIC | CharSet[
|
46
|
+
'\'', '"', '`', ',', ';', ':', '~', '-',
|
47
|
+
'(', ')', '[', ']', '{', '}', '.', '?', '!', '@', '#', '$',
|
48
|
+
'%', '^', '&', '*', '_', '+', '=', '|', '\\', '<', '>', '/'
|
49
|
+
]
|
46
50
|
|
47
51
|
# The set of printable characters (including spaces)
|
48
|
-
PRINTABLE = ALPHA_NUMERIC
|
52
|
+
PRINTABLE = ALPHA_NUMERIC | PUNCTUATION | SYMBOLS | SPACE
|
49
53
|
|
50
54
|
# The control-char character set
|
51
|
-
CONTROL = CharSet
|
55
|
+
CONTROL = CharSet[0..0x1f, 0x7f]
|
52
56
|
|
53
57
|
# The signed ASCII character set
|
54
|
-
SIGNED_ASCII = CharSet
|
58
|
+
SIGNED_ASCII = CharSet[0..0x7f]
|
55
59
|
|
56
60
|
# The full 8-bit character set
|
57
|
-
ASCII = CharSet
|
61
|
+
ASCII = CharSet[0..0xff]
|
58
62
|
|
59
63
|
#
|
60
64
|
# The decimal-digit character set.
|
@@ -11,7 +11,7 @@ class Integer
|
|
11
11
|
# @see Chars.numeric
|
12
12
|
#
|
13
13
|
def numeric?
|
14
|
-
Chars::NUMERIC.
|
14
|
+
Chars::NUMERIC.include_byte?(self)
|
15
15
|
end
|
16
16
|
|
17
17
|
#
|
@@ -23,7 +23,7 @@ class Integer
|
|
23
23
|
# @see Chars.octal
|
24
24
|
#
|
25
25
|
def octal?
|
26
|
-
Chars::OCTAL.
|
26
|
+
Chars::OCTAL.include_byte?(self)
|
27
27
|
end
|
28
28
|
|
29
29
|
#
|
@@ -37,7 +37,7 @@ class Integer
|
|
37
37
|
# @see Chars.uppercase_hexadecimal
|
38
38
|
#
|
39
39
|
def uppercase_hex?
|
40
|
-
Chars::UPPERCASE_HEXADECIMAL.
|
40
|
+
Chars::UPPERCASE_HEXADECIMAL.include_byte?(self)
|
41
41
|
end
|
42
42
|
|
43
43
|
#
|
@@ -51,7 +51,7 @@ class Integer
|
|
51
51
|
# @see Chars.lowercase_hexadecimal
|
52
52
|
#
|
53
53
|
def lowercase_hex?
|
54
|
-
Chars::LOWERCASE_HEXADECIMAL.
|
54
|
+
Chars::LOWERCASE_HEXADECIMAL.include_byte?(self)
|
55
55
|
end
|
56
56
|
|
57
57
|
#
|
@@ -63,7 +63,7 @@ class Integer
|
|
63
63
|
# @see Chars.hexadecimal
|
64
64
|
#
|
65
65
|
def hex?
|
66
|
-
Chars::HEXADECIMAL.
|
66
|
+
Chars::HEXADECIMAL.include_byte?(self)
|
67
67
|
end
|
68
68
|
|
69
69
|
#
|
@@ -77,7 +77,7 @@ class Integer
|
|
77
77
|
# @see Chars.uppercase_alpha
|
78
78
|
#
|
79
79
|
def uppercase_alpha?
|
80
|
-
Chars::UPPERCASE_ALPHA.
|
80
|
+
Chars::UPPERCASE_ALPHA.include_byte?(self)
|
81
81
|
end
|
82
82
|
|
83
83
|
#
|
@@ -91,7 +91,7 @@ class Integer
|
|
91
91
|
# @see Chars.lowercase_alpha
|
92
92
|
#
|
93
93
|
def lowercase_alpha?
|
94
|
-
Chars::LOWERCASE_ALPHA.
|
94
|
+
Chars::LOWERCASE_ALPHA.include_byte?(self)
|
95
95
|
end
|
96
96
|
|
97
97
|
#
|
@@ -103,7 +103,7 @@ class Integer
|
|
103
103
|
# @see Chars.alpha
|
104
104
|
#
|
105
105
|
def alpha?
|
106
|
-
Chars::ALPHA.
|
106
|
+
Chars::ALPHA.include_byte?(self)
|
107
107
|
end
|
108
108
|
|
109
109
|
#
|
@@ -115,7 +115,7 @@ class Integer
|
|
115
115
|
# @see Chars.alpha_numeric
|
116
116
|
#
|
117
117
|
def alpha_numeric?
|
118
|
-
Chars::ALPHA_NUMERIC.
|
118
|
+
Chars::ALPHA_NUMERIC.include_byte?(self)
|
119
119
|
end
|
120
120
|
|
121
121
|
#
|
@@ -127,7 +127,7 @@ class Integer
|
|
127
127
|
# @see Chars.punctuation
|
128
128
|
#
|
129
129
|
def punctuation?
|
130
|
-
Chars::PUNCTUATION.
|
130
|
+
Chars::PUNCTUATION.include_byte?(self)
|
131
131
|
end
|
132
132
|
|
133
133
|
#
|
@@ -139,7 +139,7 @@ class Integer
|
|
139
139
|
# @see Chars.symbols
|
140
140
|
#
|
141
141
|
def symbolic?
|
142
|
-
Chars::SYMBOLS.
|
142
|
+
Chars::SYMBOLS.include_byte?(self)
|
143
143
|
end
|
144
144
|
|
145
145
|
#
|
@@ -151,7 +151,7 @@ class Integer
|
|
151
151
|
# @see Chars.space
|
152
152
|
#
|
153
153
|
def space?
|
154
|
-
Chars::SPACE.
|
154
|
+
Chars::SPACE.include_byte?(self)
|
155
155
|
end
|
156
156
|
|
157
157
|
#
|
@@ -163,7 +163,7 @@ class Integer
|
|
163
163
|
# @see Chars.visible
|
164
164
|
#
|
165
165
|
def visible?
|
166
|
-
Chars::VISIBLE.
|
166
|
+
Chars::VISIBLE.include_byte?(self)
|
167
167
|
end
|
168
168
|
|
169
169
|
#
|
@@ -175,7 +175,7 @@ class Integer
|
|
175
175
|
# @see Chars.printable
|
176
176
|
#
|
177
177
|
def printable?
|
178
|
-
Chars::PRINTABLE.
|
178
|
+
Chars::PRINTABLE.include_byte?(self)
|
179
179
|
end
|
180
180
|
|
181
181
|
#
|
@@ -188,7 +188,7 @@ class Integer
|
|
188
188
|
# @see Chars.control
|
189
189
|
#
|
190
190
|
def control?
|
191
|
-
Chars::CONTROL.
|
191
|
+
Chars::CONTROL.include_byte?(self)
|
192
192
|
end
|
193
193
|
|
194
194
|
#
|
@@ -200,7 +200,7 @@ class Integer
|
|
200
200
|
# @see Chars.signed_ascii
|
201
201
|
#
|
202
202
|
def signed_ascii?
|
203
|
-
Chars::SIGNED_ASCII.
|
203
|
+
Chars::SIGNED_ASCII.include_byte?(self)
|
204
204
|
end
|
205
205
|
|
206
206
|
#
|
@@ -212,7 +212,7 @@ class Integer
|
|
212
212
|
# @see Chars.ascii
|
213
213
|
#
|
214
214
|
def ascii?
|
215
|
-
Chars::ASCII.
|
215
|
+
Chars::ASCII.include_byte?(self)
|
216
216
|
end
|
217
217
|
|
218
218
|
end
|
data/lib/chars/version.rb
CHANGED
data/spec/char_set_spec.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
|
-
require 'chars/chars'
|
2
|
-
|
3
1
|
require 'spec_helper'
|
2
|
+
require 'chars/chars'
|
4
3
|
|
5
4
|
describe Chars::CharSet do
|
6
5
|
before(:all) do
|
@@ -9,54 +8,44 @@ describe Chars::CharSet do
|
|
9
8
|
@integers = @integer_range.to_a
|
10
9
|
@strings = @string_range.to_a
|
11
10
|
|
12
|
-
@char_set =
|
11
|
+
@char_set = described_class.new(*@strings)
|
13
12
|
end
|
14
13
|
|
15
|
-
|
16
|
-
|
14
|
+
describe "#initialize" do
|
15
|
+
it "may be created with String arguments" do
|
16
|
+
@chars = described_class.new(*@strings)
|
17
17
|
|
18
|
-
|
19
|
-
@chars.include_char?(s).should == true
|
18
|
+
@strings.all? { |s| @chars.include_char?(s) }.should == true
|
20
19
|
end
|
21
|
-
end
|
22
20
|
|
23
|
-
|
24
|
-
|
21
|
+
it "may be created with an Array of Strings" do
|
22
|
+
@chars = described_class.new(@strings)
|
25
23
|
|
26
|
-
|
27
|
-
@chars.include_char?(s).should == true
|
24
|
+
@strings.all? { |s| @chars.include_char?(s) }.should == true
|
28
25
|
end
|
29
|
-
end
|
30
26
|
|
31
|
-
|
32
|
-
|
27
|
+
it "may be created with a Range of Strings" do
|
28
|
+
@chars = described_class.new(@string_range)
|
33
29
|
|
34
|
-
|
35
|
-
@chars.include_char?(s).should == true
|
30
|
+
@strings.all? { |s| @chars.include_char?(s) }.should == true
|
36
31
|
end
|
37
|
-
end
|
38
32
|
|
39
|
-
|
40
|
-
|
33
|
+
it "may be created with Integer arguments" do
|
34
|
+
@chars = described_class.new(*@integers)
|
41
35
|
|
42
|
-
|
43
|
-
@chars.include?(i).should == true
|
36
|
+
@integers.all? { |i| @chars.include?(i) }.should == true
|
44
37
|
end
|
45
|
-
end
|
46
38
|
|
47
|
-
|
48
|
-
|
39
|
+
it "may be created with an Array of Integers" do
|
40
|
+
@chars = described_class.new(@integers)
|
49
41
|
|
50
|
-
|
51
|
-
@chars.include?(i).should == true
|
42
|
+
@integers.all? { |i| @chars.include?(i) }.should == true
|
52
43
|
end
|
53
|
-
end
|
54
44
|
|
55
|
-
|
56
|
-
|
45
|
+
it "may be created with a Range of Integers" do
|
46
|
+
@chars = described_class.new(@integer_range)
|
57
47
|
|
58
|
-
|
59
|
-
@chars.include?(i).should == true
|
48
|
+
@integers.all? { |i| @chars.include?(i) }.should == true
|
60
49
|
end
|
61
50
|
end
|
62
51
|
|
@@ -65,7 +54,7 @@ describe Chars::CharSet do
|
|
65
54
|
end
|
66
55
|
|
67
56
|
it "should include Integers" do
|
68
|
-
@char_set.include
|
57
|
+
@char_set.should include(0x41)
|
69
58
|
end
|
70
59
|
|
71
60
|
it "should be able to select bytes" do
|
@@ -81,7 +70,7 @@ describe Chars::CharSet do
|
|
81
70
|
end
|
82
71
|
|
83
72
|
it "should return a random byte" do
|
84
|
-
@char_set.include
|
73
|
+
@char_set.should include(@char_set.random_byte)
|
85
74
|
end
|
86
75
|
|
87
76
|
it "should return a random char" do
|
@@ -89,117 +78,102 @@ describe Chars::CharSet do
|
|
89
78
|
end
|
90
79
|
|
91
80
|
it "should iterate over n random bytes" do
|
92
|
-
@char_set.each_random_byte(10)
|
93
|
-
@char_set.include?(b)
|
94
|
-
|
81
|
+
@char_set.each_random_byte(10).all? { |b|
|
82
|
+
@char_set.include?(b)
|
83
|
+
}.should == true
|
95
84
|
end
|
96
85
|
|
97
86
|
it "should iterate over n random chars" do
|
98
|
-
@char_set.each_random_char(10)
|
99
|
-
@char_set.include_char?(c)
|
100
|
-
|
87
|
+
@char_set.each_random_char(10).all? { |c|
|
88
|
+
@char_set.include_char?(c)
|
89
|
+
}.should == true
|
101
90
|
end
|
102
91
|
|
103
92
|
it "should return a random Array of bytes" do
|
104
93
|
bytes = @char_set.random_bytes(10)
|
105
94
|
|
106
|
-
bytes.
|
107
|
-
@char_set.include?(b).should == true
|
108
|
-
end
|
95
|
+
bytes.all? { |b| @char_set.include?(b) }.should == true
|
109
96
|
end
|
110
97
|
|
111
98
|
it "should return a random Array of chars" do
|
112
99
|
chars = @char_set.random_chars(10)
|
113
100
|
|
114
|
-
chars.
|
115
|
-
@char_set.include_char?(c).should == true
|
116
|
-
end
|
101
|
+
chars.all? { |c| @char_set.include_char?(c) }.should == true
|
117
102
|
end
|
118
103
|
|
119
104
|
it "should return a random Array of bytes with a varying length" do
|
120
105
|
bytes = @char_set.random_bytes(5..10)
|
121
106
|
|
122
|
-
bytes.length.
|
123
|
-
bytes.
|
124
|
-
@char_set.include?(b).should == true
|
125
|
-
end
|
107
|
+
bytes.length.should be_between(5, 10)
|
108
|
+
bytes.all? { |b| @char_set.include?(b) }.should == true
|
126
109
|
end
|
127
110
|
|
128
111
|
it "should return a random Array of chars with a varying length" do
|
129
112
|
chars = @char_set.random_chars(5..10)
|
130
113
|
|
131
|
-
chars.length.
|
132
|
-
chars.
|
133
|
-
@char_set.include_char?(c).should == true
|
134
|
-
end
|
114
|
+
chars.length.should be_between(5, 10)
|
115
|
+
chars.all? { |c| @char_set.include_char?(c) }.should == true
|
135
116
|
end
|
136
117
|
|
137
118
|
it "should return a random String of chars" do
|
138
|
-
@char_set.random_string(10)
|
139
|
-
|
140
|
-
|
119
|
+
string = @char_set.random_string(10)
|
120
|
+
|
121
|
+
string.chars.all? { |b| @char_set.include_char?(b) }.should == true
|
141
122
|
end
|
142
123
|
|
143
124
|
it "should return a random String of chars with a varying length" do
|
144
125
|
string = @char_set.random_string(5..10)
|
145
126
|
|
146
|
-
string.length.
|
147
|
-
string.
|
148
|
-
@char_set.include?(b).should == true
|
149
|
-
end
|
127
|
+
string.length.should be_between(5, 10)
|
128
|
+
string.chars.all? { |b| @char_set.include_char?(b) }.should == true
|
150
129
|
end
|
151
130
|
|
152
131
|
it "should return a random Array of unique bytes" do
|
153
132
|
bytes = @char_set.random_distinct_bytes(10)
|
154
|
-
|
155
|
-
bytes.
|
156
|
-
|
157
|
-
end
|
133
|
+
|
134
|
+
bytes.uniq.should == bytes
|
135
|
+
bytes.all? { |b| @char_set.include?(b) }.should == true
|
158
136
|
end
|
159
137
|
|
160
138
|
it "should return a random Array of unique chars" do
|
161
139
|
chars = @char_set.random_distinct_chars(10)
|
162
|
-
|
163
|
-
chars.
|
164
|
-
|
165
|
-
end
|
140
|
+
|
141
|
+
chars.uniq.should == chars
|
142
|
+
chars.all? { |c| @char_set.include_char?(c) }.should == true
|
166
143
|
end
|
167
144
|
|
168
145
|
it "should return a random Array of unique bytes with a varying length" do
|
169
146
|
bytes = @char_set.random_distinct_bytes(5..10)
|
170
|
-
|
171
|
-
bytes.
|
172
|
-
bytes.
|
173
|
-
|
174
|
-
end
|
147
|
+
|
148
|
+
bytes.uniq.should == bytes
|
149
|
+
bytes.length.should be_between(5, 10)
|
150
|
+
bytes.all? { |b| @char_set.include?(b) }.should == true
|
175
151
|
end
|
176
152
|
|
177
153
|
it "should return a random Array of unique chars with a varying length" do
|
178
154
|
chars = @char_set.random_distinct_chars(5..10)
|
179
|
-
chars.uniq.length.should == chars.length
|
180
|
-
chars.length.between?(5, 10).should == true
|
181
|
-
chars.each do |c|
|
182
|
-
@char_set.include_char?(c).should == true
|
183
|
-
end
|
184
|
-
end
|
185
155
|
|
156
|
+
chars.uniq.should == chars
|
157
|
+
chars.length.should be_between(5, 10)
|
158
|
+
chars.all? { |c| @char_set.include_char?(c) }.should == true
|
159
|
+
end
|
186
160
|
|
187
161
|
it "should be able to be compared with another set of chars" do
|
188
|
-
|
162
|
+
@char_set.should == described_class['A'..'Z']
|
189
163
|
end
|
190
164
|
|
191
165
|
it "should be able to be unioned with another set of chars" do
|
192
|
-
super_set = (@char_set |
|
166
|
+
super_set = (@char_set | described_class['D'])
|
193
167
|
|
194
|
-
super_set.class.should ==
|
195
|
-
super_set.should ==
|
168
|
+
super_set.class.should == described_class
|
169
|
+
super_set.should == described_class['A'..'Z', 'D']
|
196
170
|
end
|
197
171
|
|
198
172
|
it "should be able to be removed from another set of chars" do
|
199
|
-
sub_set = (@char_set -
|
173
|
+
sub_set = (@char_set - described_class['B'])
|
200
174
|
|
201
|
-
sub_set.class.should ==
|
202
|
-
sub_set.
|
175
|
+
sub_set.class.should == described_class
|
176
|
+
sub_set.should be_subset(@char_set)
|
203
177
|
end
|
204
178
|
|
205
179
|
it "should find one sub-string from a String belonging to the char set" do
|
@@ -214,7 +188,7 @@ describe Chars::CharSet do
|
|
214
188
|
end
|
215
189
|
|
216
190
|
it "should determine if a String is made up of the characters from the char set" do
|
217
|
-
|
218
|
-
|
191
|
+
@char_set.should === "AABCBAA"
|
192
|
+
@char_set.should_not === "AA!!EE"
|
219
193
|
end
|
220
194
|
end
|