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