chars 0.2.4 → 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
@@ -6,6 +6,11 @@ module Chars
6
6
  # The numeric decimal character set
7
7
  NUMERIC = CharSet['0'..'9']
8
8
 
9
+ # @see NUMERIC
10
+ #
11
+ # @since 0.3.0
12
+ DIGITS = NUMERIC
13
+
9
14
  # The octal character set
10
15
  OCTAL = CharSet['0'..'7']
11
16
 
@@ -41,7 +46,11 @@ module Chars
41
46
  ]
42
47
 
43
48
  # The space character set
44
- SPACE = CharSet[' ', "\f", "\n", "\r", "\t", "\v"]
49
+ #
50
+ # @since 0.3.0
51
+ WHITESPACE = CharSet[' ', "\f", "\n", "\r", "\t", "\v"]
52
+
53
+ SPACE = WHITESPACE
45
54
 
46
55
  # The set of printable characters (not including spaces)
47
56
  VISIBLE = ALPHA_NUMERIC | CharSet[
@@ -74,6 +83,15 @@ module Chars
74
83
  NUMERIC
75
84
  end
76
85
 
86
+ #
87
+ # @see numeric
88
+ #
89
+ # @since 0.3.0
90
+ #
91
+ def self.digits
92
+ numeric
93
+ end
94
+
77
95
  #
78
96
  # The octal-digit character set.
79
97
  #
@@ -195,15 +213,26 @@ module Chars
195
213
  end
196
214
 
197
215
  #
198
- # The white-space character set.
216
+ # The whitespace character set.
199
217
  #
200
218
  # @return [CharSet]
201
- # The white-space character set.
219
+ # The whitespace character set.
202
220
  #
203
221
  # @see SPACE
204
222
  #
223
+ # @since 0.3.0
224
+ #
225
+ def self.whitespace
226
+ WHITESPACE
227
+ end
228
+
229
+ #
230
+ # The whitespace character set.
231
+ #
232
+ # @see #whitespace
233
+ #
205
234
  def self.space
206
- SPACE
235
+ whitespace
207
236
  end
208
237
 
209
238
  #
@@ -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
2
  # chars version
3
- VERSION = '0.2.4'
3
+ VERSION = '0.3.0'
4
4
  end