chars 0.2.1 → 0.3.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.
- checksums.yaml +7 -0
- data/.document +1 -1
- data/.github/workflows/ruby.yml +28 -0
- data/.gitignore +8 -0
- data/.yardopts +1 -1
- data/ChangeLog.md +33 -0
- data/Gemfile +12 -0
- data/LICENSE.txt +1 -1
- data/README.md +130 -50
- data/Rakefile +6 -30
- data/benchmarks/compare.rb +16 -0
- data/benchmarks/substrings.rb +25 -0
- data/chars.gemspec +39 -105
- data/gemspec.yml +9 -5
- data/lib/chars/char_set.rb +371 -162
- data/lib/chars/chars.rb +86 -21
- data/lib/chars/extensions/integer.rb +34 -17
- data/lib/chars/extensions/string.rb +34 -17
- data/lib/chars/string_enumerator.rb +98 -0
- data/lib/chars/version.rb +2 -2
- data/spec/char_set_spec.rb +623 -110
- data/spec/chars_spec.rb +183 -27
- data/spec/extensions/integer_spec.rb +18 -18
- data/spec/extensions/string_spec.rb +20 -18
- data/spec/spec_helper.rb +0 -2
- data/spec/string_enumerator_spec.rb +99 -0
- metadata +57 -77
data/spec/chars_spec.rb
CHANGED
@@ -2,55 +2,211 @@ require 'spec_helper'
|
|
2
2
|
require 'chars/chars'
|
3
3
|
|
4
4
|
describe Chars do
|
5
|
-
|
6
|
-
|
5
|
+
let(:numeric_chars) { '0123456789'.chars }
|
6
|
+
|
7
|
+
describe "NUMERIC" do
|
8
|
+
subject { described_class::NUMERIC }
|
9
|
+
|
10
|
+
it "must contain numeric characters" do
|
11
|
+
expect(subject.chars).to match_array(numeric_chars)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "DIGITS" do
|
16
|
+
subject { described_class::DIGITS }
|
17
|
+
|
18
|
+
it "must equal NUMERIC" do
|
19
|
+
expect(subject).to be(described_class::NUMERIC)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "OCTAL" do
|
24
|
+
subject { described_class::OCTAL }
|
25
|
+
|
26
|
+
it "must contain all octal characters" do
|
27
|
+
expect(subject.chars).to match_array("01234567".chars)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
let(:uppercase_hex_chars) { "0123456789ABCDEF".chars }
|
32
|
+
|
33
|
+
describe "UPPERCASE_HEXADECIMAL" do
|
34
|
+
subject { described_class::UPPERCASE_HEXADECIMAL }
|
35
|
+
|
36
|
+
it "must contain all upper-case hexadecimal characters" do
|
37
|
+
expect(subject.chars).to match_array(uppercase_hex_chars)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
let(:lowercase_hex_chars) { "0123456789abcdef".chars }
|
42
|
+
|
43
|
+
describe "LOWERCASE_HEXADECIMAL" do
|
44
|
+
subject { described_class::LOWERCASE_HEXADECIMAL }
|
45
|
+
|
46
|
+
it "must contain all lower-case hexadecimal characters" do
|
47
|
+
expect(subject.chars).to match_array(lowercase_hex_chars)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "HEXADECIMAL" do
|
52
|
+
subject { described_class::HEXADECIMAL }
|
53
|
+
|
54
|
+
it "must contain both upper-case and lower-case hexadecimal characters" do
|
55
|
+
expect(subject.chars).to match_array(uppercase_hex_chars | lowercase_hex_chars)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
let(:uppercase_alpha_chars) { "ABCDEFGHIJKLMNOPQRSTUVWXYZ".chars }
|
60
|
+
|
61
|
+
describe "UPPERCASE_ALPHA" do
|
62
|
+
subject { described_class::UPPERCASE_ALPHA }
|
63
|
+
|
64
|
+
it "must contain all upper-case alpha characters" do
|
65
|
+
expect(subject.chars).to match_array(uppercase_alpha_chars)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
let(:lowercase_alpha_chars) { "abcdefghijklmnopqrstuvwxyz".chars }
|
70
|
+
|
71
|
+
describe "LOWERCASE_ALPHA" do
|
72
|
+
subject { described_class::LOWERCASE_ALPHA }
|
73
|
+
|
74
|
+
it "must contain all lower-case alpha characters" do
|
75
|
+
expect(subject.chars).to match_array(lowercase_alpha_chars)
|
76
|
+
end
|
7
77
|
end
|
8
78
|
|
9
|
-
|
10
|
-
|
79
|
+
let(:alpha_chars) { uppercase_alpha_chars | lowercase_alpha_chars }
|
80
|
+
|
81
|
+
describe "ALPHA" do
|
82
|
+
subject { described_class::ALPHA }
|
83
|
+
|
84
|
+
it "must contain all alpha characters" do
|
85
|
+
expect(subject.chars).to match_array(alpha_chars)
|
86
|
+
end
|
11
87
|
end
|
12
88
|
|
13
|
-
|
14
|
-
|
89
|
+
let(:alpha_numeric_chars) { alpha_chars | numeric_chars }
|
90
|
+
|
91
|
+
describe "ALPHA_NUMERIC" do
|
92
|
+
subject { described_class::ALPHA_NUMERIC }
|
93
|
+
|
94
|
+
it "must contain all alpha-numeric characters" do
|
95
|
+
expect(subject.chars).to match_array(alpha_numeric_chars)
|
96
|
+
end
|
15
97
|
end
|
16
98
|
|
17
|
-
|
18
|
-
|
99
|
+
let(:punctuation_chars) { " !\"'(),-.:;?[]`{}~".chars }
|
100
|
+
|
101
|
+
describe "PUNCTUATION" do
|
102
|
+
subject { described_class::PUNCTUATION }
|
103
|
+
|
104
|
+
it "must contain all punctuation characters" do
|
105
|
+
expect(subject.chars).to match_array(punctuation_chars)
|
106
|
+
end
|
19
107
|
end
|
20
108
|
|
21
|
-
|
22
|
-
|
109
|
+
let(:symbolic_chars) { " !\"\#$%&'()*+,-./:;<=>?@[\\]^_`{|}~".chars }
|
110
|
+
|
111
|
+
describe "SYMBOLS" do
|
112
|
+
subject { described_class::SYMBOLS }
|
113
|
+
|
114
|
+
it "must contain all symbolic characters" do
|
115
|
+
expect(subject.chars).to match_array(symbolic_chars)
|
116
|
+
end
|
23
117
|
end
|
24
118
|
|
25
|
-
|
26
|
-
|
119
|
+
let(:whitespace_chars) { "\t\n\v\f\r ".chars }
|
120
|
+
|
121
|
+
describe "WHITESPACE" do
|
122
|
+
subject { described_class::WHITESPACE }
|
123
|
+
|
124
|
+
it "must contain all white-space characters" do
|
125
|
+
expect(subject.chars).to match_array(whitespace_chars)
|
126
|
+
end
|
27
127
|
end
|
28
128
|
|
29
|
-
|
30
|
-
|
129
|
+
describe "SPACE" do
|
130
|
+
subject { described_class::SPACE }
|
131
|
+
|
132
|
+
it "must equal WHITESPACE" do
|
133
|
+
expect(subject).to be(described_class::WHITESPACE)
|
134
|
+
end
|
31
135
|
end
|
32
136
|
|
33
|
-
|
34
|
-
|
137
|
+
describe "VISIBLE" do
|
138
|
+
subject { described_class::VISIBLE }
|
139
|
+
|
140
|
+
it "must contain all all alpha-numeric, symbols, and some punctuation" do
|
141
|
+
expect(subject.chars).to match_array(alpha_numeric_chars | "!\"\#$%&'()*+,-./:;<=>?@[\\]^_`{|}~".chars)
|
142
|
+
end
|
35
143
|
end
|
36
144
|
|
37
|
-
|
38
|
-
|
145
|
+
describe "PRINTABLE" do
|
146
|
+
subject { described_class::PRINTABLE }
|
147
|
+
|
148
|
+
it "must contain all alpha-numeric, punctuation, symbols, and whitespace characters" do
|
149
|
+
expect(subject.chars).to match_array(
|
150
|
+
alpha_numeric_chars |
|
151
|
+
punctuation_chars |
|
152
|
+
symbolic_chars |
|
153
|
+
whitespace_chars
|
154
|
+
)
|
155
|
+
end
|
39
156
|
end
|
40
|
-
|
41
|
-
|
42
|
-
|
157
|
+
|
158
|
+
describe "CONTROL" do
|
159
|
+
subject { described_class::CONTROL }
|
160
|
+
|
161
|
+
it "must contain ASCII bytes 0x00 - 0x1f and 0x7f" do
|
162
|
+
expect(subject.bytes).to eq([*0x00..0x1f, 0x7f])
|
163
|
+
end
|
43
164
|
end
|
44
165
|
|
45
|
-
|
46
|
-
|
166
|
+
describe "SIGNED_ASCII" do
|
167
|
+
subject { described_class::SIGNED_ASCII }
|
168
|
+
|
169
|
+
it "must contain ASCII bytes 0x00 - 0x7f" do
|
170
|
+
expect(subject.bytes).to eq([*0x00..0x7f])
|
171
|
+
end
|
47
172
|
end
|
48
173
|
|
49
|
-
|
50
|
-
|
174
|
+
describe "ASCII" do
|
175
|
+
subject { described_class::ASCII }
|
176
|
+
|
177
|
+
it "must contain ASCII bytes 0x00 - 0xff" do
|
178
|
+
expect(subject.bytes).to eq([*0x00..0xff])
|
179
|
+
end
|
51
180
|
end
|
52
181
|
|
53
|
-
|
54
|
-
|
182
|
+
{
|
183
|
+
:numeric => :NUMERIC,
|
184
|
+
:digits => :DIGITS,
|
185
|
+
:octal => :OCTAL,
|
186
|
+
|
187
|
+
:uppercase_hexadecimal => :UPPERCASE_HEXADECIMAL,
|
188
|
+
:lowercase_hexadecimal => :LOWERCASE_HEXADECIMAL,
|
189
|
+
:hexadecimal => :HEXADECIMAL,
|
190
|
+
|
191
|
+
:uppercase_alpha => :UPPERCASE_ALPHA,
|
192
|
+
:lowercase_alpha => :LOWERCASE_ALPHA,
|
193
|
+
:alpha => :ALPHA,
|
194
|
+
|
195
|
+
:punctuation => :PUNCTUATION,
|
196
|
+
:symbols => :SYMBOLS,
|
197
|
+
:whitespace => :WHITESPACE,
|
198
|
+
:space => :SPACE,
|
199
|
+
:visible => :VISIBLE,
|
200
|
+
:printable => :PRINTABLE,
|
201
|
+
:control => :CONTROL,
|
202
|
+
|
203
|
+
:signed_ascii => :SIGNED_ASCII,
|
204
|
+
:ascii => :ASCII
|
205
|
+
}.each do |method,constant|
|
206
|
+
describe ".#{method}" do
|
207
|
+
it "must return #{constant}" do
|
208
|
+
expect(subject.send(method)).to be(described_class.const_get(constant))
|
209
|
+
end
|
210
|
+
end
|
55
211
|
end
|
56
212
|
end
|
@@ -3,71 +3,71 @@ require 'chars/extensions/integer'
|
|
3
3
|
|
4
4
|
describe Integer do
|
5
5
|
it "should recognize numeric bytes" do
|
6
|
-
0x39.
|
6
|
+
expect(0x39).to be_numeric
|
7
7
|
end
|
8
8
|
|
9
9
|
it "should recognize octal bytes" do
|
10
|
-
0x30.
|
10
|
+
expect(0x30).to be_octal
|
11
11
|
end
|
12
12
|
|
13
13
|
it "should recognize upper-case hexadecimal bytes" do
|
14
|
-
0x44.
|
14
|
+
expect(0x44).to be_uppercase_hex
|
15
15
|
end
|
16
16
|
|
17
17
|
it "should recognize lower-case hexadecimal bytes" do
|
18
|
-
0x61.
|
18
|
+
expect(0x61).to be_lowercase_hex
|
19
19
|
end
|
20
20
|
|
21
21
|
it "should recognize hexadecimal bytes" do
|
22
|
-
0x63.
|
22
|
+
expect(0x63).to be_hex
|
23
23
|
end
|
24
24
|
|
25
25
|
it "should recognize upper-case alpha bytes" do
|
26
|
-
0x4c.
|
26
|
+
expect(0x4c).to be_uppercase_alpha
|
27
27
|
end
|
28
28
|
|
29
29
|
it "should recognize lower-case alpha bytes" do
|
30
|
-
0x71.
|
30
|
+
expect(0x71).to be_lowercase_alpha
|
31
31
|
end
|
32
32
|
|
33
33
|
it "should recognize alpha bytes" do
|
34
|
-
0x7a.
|
34
|
+
expect(0x7a).to be_alpha
|
35
35
|
end
|
36
36
|
|
37
37
|
it "should recognize alpha-numeric bytes" do
|
38
|
-
0x69.
|
38
|
+
expect(0x69).to be_alpha_numeric
|
39
39
|
end
|
40
40
|
|
41
41
|
it "should recognize punctuation bytes" do
|
42
|
-
0x60.
|
42
|
+
expect(0x60).to be_punctuation
|
43
43
|
end
|
44
44
|
|
45
45
|
it "should recognize symbolic bytes" do
|
46
|
-
0x26.
|
46
|
+
expect(0x26).to be_symbolic
|
47
47
|
end
|
48
48
|
|
49
49
|
it "should recognize space bytes" do
|
50
|
-
0x20.
|
50
|
+
expect(0x20).to be_space
|
51
51
|
end
|
52
52
|
|
53
53
|
it "should recognize visible bytes" do
|
54
|
-
0x41.
|
55
|
-
0x20.
|
54
|
+
expect(0x41).to be_visible
|
55
|
+
expect(0x20).to_not be_visible
|
56
56
|
end
|
57
57
|
|
58
58
|
it "should recognize printable bytes" do
|
59
|
-
0x3f.
|
59
|
+
expect(0x3f).to be_printable
|
60
60
|
end
|
61
61
|
|
62
62
|
it "should recognize control bytes" do
|
63
|
-
0x1b.
|
63
|
+
expect(0x1b).to be_control
|
64
64
|
end
|
65
65
|
|
66
66
|
it "should recognize signed ASCII bytes" do
|
67
|
-
0x00.
|
67
|
+
expect(0x00).to be_signed_ascii
|
68
68
|
end
|
69
69
|
|
70
70
|
it "should recognize ASCII bytes" do
|
71
|
-
0x80.
|
71
|
+
expect(0x80).to be_ascii
|
72
72
|
end
|
73
73
|
end
|
@@ -1,73 +1,75 @@
|
|
1
|
+
# encoding: US-ASCII
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
require 'chars/extensions/string'
|
3
5
|
|
4
6
|
describe String do
|
5
7
|
it "should recognize numeric strings" do
|
6
|
-
"0987".
|
8
|
+
expect("0987").to be_numeric
|
7
9
|
end
|
8
10
|
|
9
11
|
it "should recognize octal strings" do
|
10
|
-
"012".
|
12
|
+
expect("012").to be_octal
|
11
13
|
end
|
12
14
|
|
13
15
|
it "should recognize upper-case hexadecimal strings" do
|
14
|
-
"2D".
|
16
|
+
expect("2D").to be_uppercase_hex
|
15
17
|
end
|
16
18
|
|
17
19
|
it "should recognize lower-case hexadecimal strings" do
|
18
|
-
"2d".
|
20
|
+
expect("2d").to be_lowercase_hex
|
19
21
|
end
|
20
22
|
|
21
23
|
it "should recognize hexadecimal strings" do
|
22
|
-
"2dE3".
|
24
|
+
expect("2dE3").to be_hex
|
23
25
|
end
|
24
26
|
|
25
27
|
it "should recognize upper-case alpha strings" do
|
26
|
-
"ABC".
|
28
|
+
expect("ABC").to be_uppercase_alpha
|
27
29
|
end
|
28
30
|
|
29
31
|
it "should recognize lower-case alpha strings" do
|
30
|
-
"abc".
|
32
|
+
expect("abc").to be_lowercase_alpha
|
31
33
|
end
|
32
34
|
|
33
35
|
it "should recognize alpha strings" do
|
34
|
-
"abcDEF".
|
36
|
+
expect("abcDEF").to be_alpha
|
35
37
|
end
|
36
38
|
|
37
39
|
it "should recognize alpha-numeric strings" do
|
38
|
-
"abc123".
|
40
|
+
expect("abc123").to be_alpha_numeric
|
39
41
|
end
|
40
42
|
|
41
43
|
it "should recognize punctuation strings" do
|
42
|
-
"[...]".
|
44
|
+
expect("[...]").to be_punctuation
|
43
45
|
end
|
44
46
|
|
45
47
|
it "should recognize symbolic strings" do
|
46
|
-
"++".
|
48
|
+
expect("++").to be_symbolic
|
47
49
|
end
|
48
50
|
|
49
51
|
it "should recognize space strings" do
|
50
|
-
" \t".
|
52
|
+
expect(" \t").to be_space
|
51
53
|
end
|
52
54
|
|
53
55
|
it "should recognize visible strings" do
|
54
|
-
"abc".
|
55
|
-
"ab c".
|
56
|
+
expect("abc").to be_visible
|
57
|
+
expect("ab c").to_not be_visible
|
56
58
|
end
|
57
59
|
|
58
60
|
it "should recognize printable strings" do
|
59
|
-
"abc, [123]\nDEF".
|
61
|
+
expect("abc, [123]\nDEF").to be_printable
|
60
62
|
end
|
61
63
|
|
62
64
|
it "should recognize control strings" do
|
63
|
-
"\b\b\a".
|
65
|
+
expect("\b\b\a").to be_control
|
64
66
|
end
|
65
67
|
|
66
68
|
it "should recognize signed ASCII strings" do
|
67
|
-
"lol\0".
|
69
|
+
expect("lol\0").to be_signed_ascii
|
68
70
|
end
|
69
71
|
|
70
72
|
it "should recognize ASCII strings" do
|
71
|
-
"\xff\xfe".
|
73
|
+
expect("\xff\xfe").to be_ascii
|
72
74
|
end
|
73
75
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'chars/char_set'
|
3
|
+
require 'chars/string_enumerator'
|
4
|
+
|
5
|
+
describe Chars::StringEnumerator do
|
6
|
+
let(:char_set) { Chars::CharSet['a', 'b', 'c'] }
|
7
|
+
let(:length) { 3 }
|
8
|
+
|
9
|
+
subject { described_class.new(char_set,length) }
|
10
|
+
|
11
|
+
describe "#initialize" do
|
12
|
+
it "must set #char_set" do
|
13
|
+
expect(subject.char_set).to eq(char_set)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "must set #length" do
|
17
|
+
expect(subject.length).to eq(length)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "#each" do
|
22
|
+
let(:expected_strings) do
|
23
|
+
%w[
|
24
|
+
aaa
|
25
|
+
aab
|
26
|
+
aac
|
27
|
+
aba
|
28
|
+
abb
|
29
|
+
abc
|
30
|
+
aca
|
31
|
+
acb
|
32
|
+
acc
|
33
|
+
baa
|
34
|
+
bab
|
35
|
+
bac
|
36
|
+
bba
|
37
|
+
bbb
|
38
|
+
bbc
|
39
|
+
bca
|
40
|
+
bcb
|
41
|
+
bcc
|
42
|
+
caa
|
43
|
+
cab
|
44
|
+
cac
|
45
|
+
cba
|
46
|
+
cbb
|
47
|
+
cbc
|
48
|
+
cca
|
49
|
+
ccb
|
50
|
+
ccc
|
51
|
+
]
|
52
|
+
end
|
53
|
+
|
54
|
+
context "when a block is given" do
|
55
|
+
it "must enumerate through each sequential string in the char-set and of the desired length" do
|
56
|
+
expect { |b|
|
57
|
+
subject.each(&b)
|
58
|
+
}.to yield_successive_args(*expected_strings)
|
59
|
+
end
|
60
|
+
|
61
|
+
context "when the length is 1" do
|
62
|
+
let(:length) { 1 }
|
63
|
+
|
64
|
+
it "must enumerate over each individual character" do
|
65
|
+
expect { |b|
|
66
|
+
subject.each(&b)
|
67
|
+
}.to yield_successive_args(*char_set.chars)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
context "when the length is 0" do
|
72
|
+
let(:length) { 0 }
|
73
|
+
|
74
|
+
it "must yield an empty String" do
|
75
|
+
expect { |b|
|
76
|
+
subject.each(&b)
|
77
|
+
}.to yield_successive_args("")
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
context "when #char_set is empty" do
|
82
|
+
let(:char_set) { Chars::CharSet.new }
|
83
|
+
|
84
|
+
it "must yield an empty String" do
|
85
|
+
expect { |b|
|
86
|
+
subject.each(&b)
|
87
|
+
}.to_not yield_control
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
context "when no block is given" do
|
93
|
+
it "must return an Enumerator object" do
|
94
|
+
expect(subject.each).to be_kind_of(Enumerator)
|
95
|
+
expect(subject.each.to_a).to eq(expected_strings)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|