chars 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/chars/chars.rb CHANGED
@@ -1,9 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'chars/char_set'
2
4
 
3
5
  module Chars
4
6
  # The numeric decimal character set
5
7
  NUMERIC = CharSet['0'..'9']
6
8
 
9
+ # @see NUMERIC
10
+ #
11
+ # @since 0.3.0
12
+ DIGITS = NUMERIC
13
+
7
14
  # The octal character set
8
15
  OCTAL = CharSet['0'..'7']
9
16
 
@@ -39,7 +46,11 @@ module Chars
39
46
  ]
40
47
 
41
48
  # The space character set
42
- SPACE = CharSet.new(' ', "\f", "\n", "\r", "\t", "\v")
49
+ #
50
+ # @since 0.3.0
51
+ WHITESPACE = CharSet[' ', "\f", "\n", "\r", "\t", "\v"]
52
+
53
+ SPACE = WHITESPACE
43
54
 
44
55
  # The set of printable characters (not including spaces)
45
56
  VISIBLE = ALPHA_NUMERIC | CharSet[
@@ -66,17 +77,30 @@ module Chars
66
77
  # @return [CharSet]
67
78
  # The decimal-digit character set.
68
79
  #
69
- def Chars.numeric
80
+ # @see NUMERIC
81
+ #
82
+ def self.numeric
70
83
  NUMERIC
71
84
  end
72
85
 
86
+ #
87
+ # @see numeric
88
+ #
89
+ # @since 0.3.0
90
+ #
91
+ def self.digits
92
+ numeric
93
+ end
94
+
73
95
  #
74
96
  # The octal-digit character set.
75
97
  #
76
98
  # @return [CharSet]
77
99
  # The octal-digit character set.
78
100
  #
79
- def Chars.octal
101
+ # @see OCTAL
102
+ #
103
+ def self.octal
80
104
  OCTAL
81
105
  end
82
106
 
@@ -86,7 +110,9 @@ module Chars
86
110
  # @return [CharSet]
87
111
  # The upper-case hexadecimal character set.
88
112
  #
89
- def Chars.uppercase_hexadecimal
113
+ # @see UPPERCASE_HEXADECIMAL
114
+ #
115
+ def self.uppercase_hexadecimal
90
116
  UPPERCASE_HEXADECIMAL
91
117
  end
92
118
 
@@ -96,7 +122,9 @@ module Chars
96
122
  # @return [CharSet]
97
123
  # The lower-case hexadecimal character set.
98
124
  #
99
- def Chars.lowercase_hexadecimal
125
+ # @see LOWERCASE_HEXADECIMAL
126
+ #
127
+ def self.lowercase_hexadecimal
100
128
  LOWERCASE_HEXADECIMAL
101
129
  end
102
130
 
@@ -106,7 +134,9 @@ module Chars
106
134
  # @return [CharSet]
107
135
  # The hexadecimal character set.
108
136
  #
109
- def Chars.hexadecimal
137
+ # @see HEXADECIMAL
138
+ #
139
+ def self.hexadecimal
110
140
  HEXADECIMAL
111
141
  end
112
142
 
@@ -116,7 +146,9 @@ module Chars
116
146
  # @return [CharSet]
117
147
  # The upper-case alphabetic character set.
118
148
  #
119
- def Chars.uppercase_alpha
149
+ # @see UPPERCASE_ALPHA
150
+ #
151
+ def self.uppercase_alpha
120
152
  UPPERCASE_ALPHA
121
153
  end
122
154
 
@@ -126,7 +158,9 @@ module Chars
126
158
  # @return [CharSet]
127
159
  # The lower-case alphabetic character set.
128
160
  #
129
- def Chars.lowercase_alpha
161
+ # @see LOWERCASE_ALPHA
162
+ #
163
+ def self.lowercase_alpha
130
164
  LOWERCASE_ALPHA
131
165
  end
132
166
 
@@ -136,7 +170,9 @@ module Chars
136
170
  # @return [CharSet]
137
171
  # The alphabetic character set.
138
172
  #
139
- def Chars.alpha
173
+ # @see ALPHA
174
+ #
175
+ def self.alpha
140
176
  ALPHA
141
177
  end
142
178
 
@@ -146,7 +182,9 @@ module Chars
146
182
  # @return [CharSet]
147
183
  # The alpha-numeric character set.
148
184
  #
149
- def Chars.alpha_numeric
185
+ # @see ALPHA_NUMERIC
186
+ #
187
+ def self.alpha_numeric
150
188
  ALPHA_NUMERIC
151
189
  end
152
190
 
@@ -156,7 +194,9 @@ module Chars
156
194
  # @return [CharSet]
157
195
  # The punctuation character set.
158
196
  #
159
- def Chars.punctuation
197
+ # @see PUNCTUATION
198
+ #
199
+ def self.punctuation
160
200
  PUNCTUATION
161
201
  end
162
202
 
@@ -166,18 +206,33 @@ module Chars
166
206
  # @return [CharSet]
167
207
  # The symbolic character set.
168
208
  #
169
- def Chars.symbols
209
+ # @see SYMBOLS
210
+ #
211
+ def self.symbols
170
212
  SYMBOLS
171
213
  end
172
214
 
173
215
  #
174
- # The white-space character set.
216
+ # The whitespace character set.
175
217
  #
176
218
  # @return [CharSet]
177
- # The white-space character set.
219
+ # The whitespace character set.
220
+ #
221
+ # @see SPACE
222
+ #
223
+ # @since 0.3.0
224
+ #
225
+ def self.whitespace
226
+ WHITESPACE
227
+ end
228
+
229
+ #
230
+ # The whitespace character set.
178
231
  #
179
- def Chars.space
180
- SPACE
232
+ # @see #whitespace
233
+ #
234
+ def self.space
235
+ whitespace
181
236
  end
182
237
 
183
238
  #
@@ -186,7 +241,9 @@ module Chars
186
241
  # @return [CharSet]
187
242
  # The visible character set.
188
243
  #
189
- def Chars.visible
244
+ # @see VISIBLE
245
+ #
246
+ def self.visible
190
247
  VISIBLE
191
248
  end
192
249
 
@@ -196,7 +253,9 @@ module Chars
196
253
  # @return [CharSet]
197
254
  # The printable character set.
198
255
  #
199
- def Chars.printable
256
+ # @see PRINTABLE
257
+ #
258
+ def self.printable
200
259
  PRINTABLE
201
260
  end
202
261
 
@@ -206,7 +265,9 @@ module Chars
206
265
  # @return [CharSet]
207
266
  # The control-character character set.
208
267
  #
209
- def Chars.control
268
+ # @see CONTROL
269
+ #
270
+ def self.control
210
271
  CONTROL
211
272
  end
212
273
 
@@ -216,7 +277,9 @@ module Chars
216
277
  # @return [CharSet]
217
278
  # The signed ASCII character set.
218
279
  #
219
- def Chars.signed_ascii
280
+ # @see SIGNED_ASCII
281
+ #
282
+ def self.signed_ascii
220
283
  SIGNED_ASCII
221
284
  end
222
285
 
@@ -226,7 +289,9 @@ module Chars
226
289
  # @return [CharSet]
227
290
  # The ASCII character set.
228
291
  #
229
- def Chars.ascii
292
+ # @see ASCII
293
+ #
294
+ def self.ascii
230
295
  ASCII
231
296
  end
232
297
  end
@@ -8,7 +8,8 @@ class Integer
8
8
  # @return [Boolean]
9
9
  # Specifies whether the byte belongs to the decimal-digit character set.
10
10
  #
11
- # @see Chars.numeric
11
+ # @see Chars::NUMERIC
12
+ # @see Chars::CharSet#include_byte?
12
13
  #
13
14
  def numeric?
14
15
  Chars::NUMERIC.include_byte?(self)
@@ -20,7 +21,8 @@ class Integer
20
21
  # @return [Boolean]
21
22
  # Specifies whether the byte belongs to the octal-digit character set.
22
23
  #
23
- # @see Chars.octal
24
+ # @see Chars::OCTAL
25
+ # @see Chars::CharSet#include_byte?
24
26
  #
25
27
  def octal?
26
28
  Chars::OCTAL.include_byte?(self)
@@ -34,7 +36,8 @@ class Integer
34
36
  # Specifies whether the byte belongs to the upper-case hexadecimal
35
37
  # character set.
36
38
  #
37
- # @see Chars.uppercase_hexadecimal
39
+ # @see Chars::UPPERCASE_HEXADECIMAL
40
+ # @see Chars::CharSet#include_byte?
38
41
  #
39
42
  def uppercase_hex?
40
43
  Chars::UPPERCASE_HEXADECIMAL.include_byte?(self)
@@ -48,7 +51,8 @@ class Integer
48
51
  # Specifies whether the byte belongs to the lower-case hexadecimal
49
52
  # character set.
50
53
  #
51
- # @see Chars.lowercase_hexadecimal
54
+ # @see Chars::LOWERCASE_HEXADECIMAL
55
+ # @see Chars::CharSet#include_byte?
52
56
  #
53
57
  def lowercase_hex?
54
58
  Chars::LOWERCASE_HEXADECIMAL.include_byte?(self)
@@ -60,7 +64,8 @@ class Integer
60
64
  # @return [Boolean]
61
65
  # Specifies whether the byte belongs to the hexadecimal character set.
62
66
  #
63
- # @see Chars.hexadecimal
67
+ # @see Chars::HEXADECIMAL
68
+ # @see Chars::CharSet#include_byte?
64
69
  #
65
70
  def hex?
66
71
  Chars::HEXADECIMAL.include_byte?(self)
@@ -74,7 +79,8 @@ class Integer
74
79
  # Specifies whether the byte belongs to the upper-case alphabetic
75
80
  # character set.
76
81
  #
77
- # @see Chars.uppercase_alpha
82
+ # @see Chars::UPPERCASE_ALPHA
83
+ # @see Chars::CharSet#include_byte?
78
84
  #
79
85
  def uppercase_alpha?
80
86
  Chars::UPPERCASE_ALPHA.include_byte?(self)
@@ -88,7 +94,8 @@ class Integer
88
94
  # Specifies whether the byte belongs to the lower-case alphabetic
89
95
  # character set.
90
96
  #
91
- # @see Chars.lowercase_alpha
97
+ # @see Chars::LOWERCASE_ALPHA
98
+ # @see Chars::CharSet#include_byte?
92
99
  #
93
100
  def lowercase_alpha?
94
101
  Chars::LOWERCASE_ALPHA.include_byte?(self)
@@ -100,7 +107,8 @@ class Integer
100
107
  # @return [Boolean]
101
108
  # Specifies whether the byte belongs to the alphabetic character set.
102
109
  #
103
- # @see Chars.alpha
110
+ # @see Chars::ALPHA
111
+ # @see Chars::CharSet#include_byte?
104
112
  #
105
113
  def alpha?
106
114
  Chars::ALPHA.include_byte?(self)
@@ -112,7 +120,8 @@ class Integer
112
120
  # @return [Boolean]
113
121
  # Specifies whether the byte belongs to the alpha-numeric character set.
114
122
  #
115
- # @see Chars.alpha_numeric
123
+ # @see Chars::ALPHA_NUMERIC
124
+ # @see Chars::CharSet#include_byte?
116
125
  #
117
126
  def alpha_numeric?
118
127
  Chars::ALPHA_NUMERIC.include_byte?(self)
@@ -124,7 +133,8 @@ class Integer
124
133
  # @return [Boolean]
125
134
  # Specifies whether the byte belongs to the punctuation character set.
126
135
  #
127
- # @see Chars.punctuation
136
+ # @see Chars::PUNCTUATION
137
+ # @see Chars::CharSet#include_byte?
128
138
  #
129
139
  def punctuation?
130
140
  Chars::PUNCTUATION.include_byte?(self)
@@ -136,7 +146,8 @@ class Integer
136
146
  # @return [Boolean]
137
147
  # Specifies whether the byte belongs to the symbolic character set.
138
148
  #
139
- # @see Chars.symbols
149
+ # @see Chars::SYMBOLS
150
+ # @see Chars::CharSet#include_byte?
140
151
  #
141
152
  def symbolic?
142
153
  Chars::SYMBOLS.include_byte?(self)
@@ -148,7 +159,8 @@ class Integer
148
159
  # @return [Boolean]
149
160
  # Specifies whether the byte belongs to the white-space character set.
150
161
  #
151
- # @see Chars.space
162
+ # @see Chars::SPACE
163
+ # @see Chars::CharSet#include_byte?
152
164
  #
153
165
  def space?
154
166
  Chars::SPACE.include_byte?(self)
@@ -160,7 +172,8 @@ class Integer
160
172
  # @return [Boolean]
161
173
  # Specifies whether the byte belongs to the visible character set.
162
174
  #
163
- # @see Chars.visible
175
+ # @see Chars::VISIBLE
176
+ # @see Chars::CharSet#include_byte?
164
177
  #
165
178
  def visible?
166
179
  Chars::VISIBLE.include_byte?(self)
@@ -172,7 +185,8 @@ class Integer
172
185
  # @return [Boolean]
173
186
  # Specifies whether the byte belongs to the printable character set.
174
187
  #
175
- # @see Chars.printable
188
+ # @see Chars::PRINTABLE
189
+ # @see Chars::CharSet#include_byte?
176
190
  #
177
191
  def printable?
178
192
  Chars::PRINTABLE.include_byte?(self)
@@ -185,7 +199,8 @@ class Integer
185
199
  # Specifies whether the byte belongs to the control-character character
186
200
  # set.
187
201
  #
188
- # @see Chars.control
202
+ # @see Chars::CONTROL
203
+ # @see Chars::CharSet#include_byte?
189
204
  #
190
205
  def control?
191
206
  Chars::CONTROL.include_byte?(self)
@@ -197,7 +212,8 @@ class Integer
197
212
  # @return [Boolean]
198
213
  # Specifies whether the byte belongs to the signed-ASCII character set.
199
214
  #
200
- # @see Chars.signed_ascii
215
+ # @see Chars::SIGNED_ASCII
216
+ # @see Chars::CharSet#include_byte?
201
217
  #
202
218
  def signed_ascii?
203
219
  Chars::SIGNED_ASCII.include_byte?(self)
@@ -209,7 +225,8 @@ class Integer
209
225
  # @return [Boolean]
210
226
  # Specifies whether the byte belongs to the ASCII character set.
211
227
  #
212
- # @see Chars.ascii
228
+ # @see Chars::ASCII
229
+ # @see Chars::CharSet#include_byte?
213
230
  #
214
231
  def ascii?
215
232
  Chars::ASCII.include_byte?(self)
@@ -9,7 +9,8 @@ class String
9
9
  # Specifies whether the String belongs to the decimal-digit character
10
10
  # set.
11
11
  #
12
- # @see Chars.numeric
12
+ # @see Chars::NUMERIC
13
+ # @see Chars::CharSet#===
13
14
  #
14
15
  def numeric?
15
16
  Chars::NUMERIC === self
@@ -22,7 +23,8 @@ class String
22
23
  # Specifies whether the String belongs to the octal-digit character
23
24
  # set.
24
25
  #
25
- # @see Chars.octal
26
+ # @see Chars::OCTAL
27
+ # @see Chars::CharSet#===
26
28
  #
27
29
  def octal?
28
30
  Chars::OCTAL === self
@@ -36,7 +38,8 @@ class String
36
38
  # Specifies whether the String belongs to the upper-case hexadecimal
37
39
  # character set.
38
40
  #
39
- # @see Chars.uppercase_hexadecimal
41
+ # @see Chars::UPPERCASE_HEXADECIMAL
42
+ # @see Chars::CharSet#===
40
43
  #
41
44
  def uppercase_hex?
42
45
  Chars::UPPERCASE_HEXADECIMAL === self
@@ -50,7 +53,8 @@ class String
50
53
  # Specifies whether the String belongs to the lower-case hexadecimal
51
54
  # character set.
52
55
  #
53
- # @see Chars.lowercase_hexadecimal
56
+ # @see Chars::LOWERCASE_HEXADECIMAL
57
+ # @see Chars::CharSet#===
54
58
  #
55
59
  def lowercase_hex?
56
60
  Chars::LOWERCASE_HEXADECIMAL === self
@@ -62,7 +66,8 @@ class String
62
66
  # @return [Boolean]
63
67
  # Specifies whether the String belongs to the hexadecimal character set.
64
68
  #
65
- # @see Chars.hexadecimal
69
+ # @see Chars::HEXADECIMAL
70
+ # @see Chars::CharSet#===
66
71
  #
67
72
  def hex?
68
73
  Chars::HEXADECIMAL === self
@@ -76,7 +81,8 @@ class String
76
81
  # Specifies whether the String belongs to the upper-case alphabetic
77
82
  # character set.
78
83
  #
79
- # @see Chars.uppercase_alpha
84
+ # @see Chars::UPPERCASE_ALPHA
85
+ # @see Chars::CharSet#===
80
86
  #
81
87
  def uppercase_alpha?
82
88
  Chars::UPPERCASE_ALPHA === self
@@ -90,7 +96,8 @@ class String
90
96
  # Specifies whether the String belongs to the lower-case alphabetic
91
97
  # character set.
92
98
  #
93
- # @see Chars.lowercase_alpha
99
+ # @see Chars::LOWERCASE_ALPHA
100
+ # @see Chars::CharSet#===
94
101
  #
95
102
  def lowercase_alpha?
96
103
  Chars::LOWERCASE_ALPHA === self
@@ -102,7 +109,8 @@ class String
102
109
  # @return [Boolean]
103
110
  # Specifies whether the String belongs to the alphabetic character set.
104
111
  #
105
- # @see Chars.alpha
112
+ # @see Chars::ALPHA
113
+ # @see Chars::CharSet#===
106
114
  #
107
115
  def alpha?
108
116
  Chars::ALPHA === self
@@ -116,7 +124,8 @@ class String
116
124
  # Specifies whether the String belongs to the alpha-numeric character
117
125
  # set.
118
126
  #
119
- # @see Chars.alpha_numeric
127
+ # @see Chars::ALPHA_NUMERIC
128
+ # @see Chars::CharSet#===
120
129
  #
121
130
  def alpha_numeric?
122
131
  Chars::ALPHA_NUMERIC === self
@@ -128,7 +137,8 @@ class String
128
137
  # @return [Boolean]
129
138
  # Specifies whether the String belongs to the punctuation character set.
130
139
  #
131
- # @see Chars.punctuation
140
+ # @see Chars::PUNCTUATION
141
+ # @see Chars::CharSet#===
132
142
  #
133
143
  def punctuation?
134
144
  Chars::PUNCTUATION === self
@@ -140,7 +150,8 @@ class String
140
150
  # @return [Boolean]
141
151
  # Specifies whether the String belongs to the symbolic character set.
142
152
  #
143
- # @see Chars.symbols
153
+ # @see Chars::SYMBOLS
154
+ # @see Chars::CharSet#===
144
155
  #
145
156
  def symbolic?
146
157
  Chars::SYMBOLS === self
@@ -152,7 +163,8 @@ class String
152
163
  # @return [Boolean]
153
164
  # Specifies whether the String belongs to the white-space character set.
154
165
  #
155
- # @see Chars.space
166
+ # @see Chars::SPACE
167
+ # @see Chars::CharSet#===
156
168
  #
157
169
  def space?
158
170
  Chars::SPACE === self
@@ -164,7 +176,8 @@ class String
164
176
  # @return [Boolean]
165
177
  # Specifies whether the String belongs to the visible character set.
166
178
  #
167
- # @see Chars.visible
179
+ # @see Chars::VISIBLE
180
+ # @see Chars::CharSet#===
168
181
  #
169
182
  def visible?
170
183
  Chars::VISIBLE === self
@@ -176,7 +189,8 @@ class String
176
189
  # @return [Boolean]
177
190
  # Specifies whether the String belongs to the printable character set.
178
191
  #
179
- # @see Chars.printable
192
+ # @see Chars::PRINTABLE
193
+ # @see Chars::CharSet#===
180
194
  #
181
195
  def printable?
182
196
  Chars::PRINTABLE === self
@@ -190,7 +204,8 @@ class String
190
204
  # Specifies whether the String belongs to the control-character
191
205
  # character set.
192
206
  #
193
- # @see Chars.control
207
+ # @see Chars::CONTROL
208
+ # @see Chars::CharSet#===
194
209
  #
195
210
  def control?
196
211
  Chars::CONTROL === self
@@ -203,7 +218,8 @@ class String
203
218
  # Specifies whether the String belongs to the signed-ASCII character
204
219
  # set.
205
220
  #
206
- # @see Chars.signed_ascii
221
+ # @see Chars::SIGNED_ASCII
222
+ # @see Chars::CharSet#===
207
223
  #
208
224
  def signed_ascii?
209
225
  Chars::SIGNED_ASCII === self
@@ -215,7 +231,8 @@ class String
215
231
  # @return [Boolean]
216
232
  # Specifies whether the String belongs to the ASCII character set.
217
233
  #
218
- # @see Chars.ascii
234
+ # @see Chars::ASCII
235
+ # @see Chars::CharSet#===
219
236
  #
220
237
  def ascii?
221
238
  Chars::ASCII === self
@@ -0,0 +1,98 @@
1
+ module Chars
2
+ #
3
+ # Enumerates through every possible string belonging to a character set and
4
+ # of a given length.
5
+ #
6
+ # @api private
7
+ #
8
+ # @since 0.3.0
9
+ #
10
+ class StringEnumerator
11
+
12
+ include Enumerable
13
+
14
+ # The character set to generate the strings from.
15
+ #
16
+ # @return [CharSet]
17
+ attr_reader :char_set
18
+
19
+ # The desired length of each string.
20
+ #
21
+ # @return [Integer]
22
+ attr_reader :length
23
+
24
+ #
25
+ # Initializes the string enumerator.
26
+ #
27
+ # @param [Chars::CharSet] char_set
28
+ # The character set to generate the strings from.
29
+ #
30
+ # @param [Integer] length
31
+ # The desired length of each string.
32
+ #
33
+ def initialize(char_set,length)
34
+ @char_set = char_set
35
+ @length = length
36
+ end
37
+
38
+ #
39
+ # Enumerates through every possible string belonging to {#char_set} and
40
+ # {#length} long.
41
+ #
42
+ # @yield [string]
43
+ # The given block will be passed each sequential string.
44
+ #
45
+ # @yieldparam [String] string
46
+ # A string belonging to {#char_set} and {#length} long.
47
+ #
48
+ # @return [Enumerator]
49
+ # If no block is given, an Enumerator will be returned.
50
+ #
51
+ def each
52
+ return enum_for(__method__) unless block_given?
53
+
54
+ if @char_set.empty?
55
+ return
56
+ elsif @length == 0
57
+ yield ""
58
+ return
59
+ end
60
+
61
+ chars = char_set.chars
62
+ first_char = chars.first
63
+ last_char = chars.last
64
+
65
+ next_char = {}
66
+
67
+ chars.each_cons(2) do |c1,c2|
68
+ next_char[c1] = c2
69
+ end
70
+
71
+ string = String.new(first_char * @length)
72
+
73
+ last_index = @length - 1
74
+
75
+ loop do
76
+ chars.each do |c|
77
+ string[last_index] = c
78
+
79
+ yield string.dup
80
+ end
81
+
82
+ last_index.downto(0) do |i|
83
+ if string[i] == last_char
84
+ string[i] = first_char
85
+
86
+ if i == 0
87
+ return
88
+ end
89
+ else
90
+ string[i] = next_char[string[i]]
91
+ break
92
+ end
93
+ end
94
+ end
95
+ end
96
+
97
+ end
98
+ end
data/lib/chars/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  module Chars
2
- # Chars version
3
- VERSION = '0.2.1'
2
+ # chars version
3
+ VERSION = '0.3.0'
4
4
  end