chars 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,23 @@
1
+ === 0.1.0 / 2009-03-16
2
+
3
+ * Initial release.
4
+ * Provides character sets for:
5
+ * Numeric
6
+ * Octal
7
+ * Uppercase Hexadecimal
8
+ * Lowercase Hexadecimal
9
+ * Hexadecimal
10
+ * Uppercase Alpha
11
+ * Lowercase Alpha
12
+ * Alpha
13
+ * Alpha-numeric
14
+ * Punctuation
15
+ * Symbols
16
+ * Space
17
+ * Printable
18
+ * Control
19
+ * ASCII
20
+ * Provides convenience methods for testing wether a String or Integer
21
+ belongs to a certain character set.
22
+ * Supports random text generation using specific character sets.
23
+
@@ -0,0 +1,17 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ lib/chars.rb
6
+ lib/chars/char_set.rb
7
+ lib/chars/chars.rb
8
+ lib/chars/extensions.rb
9
+ lib/chars/extensions/integer.rb
10
+ lib/chars/extensions/string.rb
11
+ lib/chars/version.rb
12
+ tasks/spec.rb
13
+ spec/spec_helper.rb
14
+ spec/char_set_spec.rb
15
+ spec/chars_spec.rb
16
+ spec/integer_spec.rb
17
+ spec/string_spec.rb
@@ -0,0 +1,98 @@
1
+ = Chars
2
+
3
+ * http://chars.rubyforge.org/
4
+ * http://github.com/postmodern/chars
5
+ * Postmodern (postmodern.mod3 at gmail.com)
6
+
7
+ == DESCRIPTION:
8
+
9
+ Chars is a Ruby library for working with various character sets,
10
+ recognizing text and generating random text from specific character sets.
11
+
12
+ == FEATURES:
13
+
14
+ * Provides character sets for:
15
+ * Numeric ('0' - '9')
16
+ * Octal ('0' - '7')
17
+ * Uppercase Hexadecimal ('0' - '9', 'A' - 'F')
18
+ * Lowercase Hexadecimal ('0' - '9', 'a' - 'f')
19
+ * Hexadecimal ('0' - '9', 'a' - 'f', 'A' - 'F')
20
+ * Uppercase Alpha ('A' - 'Z')
21
+ * Lowercase Alpha ('a' - 'z')
22
+ * Alpha ('a' - 'z', 'A' - 'Z')
23
+ * Alpha-numeric ('0' - '9', 'a' - 'z', 'A' - 'Z')
24
+ * Punctuation (' ', '\'', '"', '`', ',', ';', ':', '~', '-', '(', ')',
25
+ '[', ']', '{', '}', '.', '?', '!')
26
+ * Symbols (' ', '\'', '"', '`', ',', ';', ':', '~', '-', '(', ')',
27
+ '[', ']', '{', '}', '.', '?', '!', '@', '#', '$', '%', '^', '&', '*',
28
+ '_', '+', '=', '|', '\\', '<', '>', '/')
29
+ * Space (' ', '\f', '\n', '\r', '\t', '\v')
30
+ * Printable ('0' - '9', 'a' - 'z', 'A' - 'Z', ' ', '\'', '"', '`', ',',
31
+ ';', ':', '~', '-', '(', ')', '[', ']', '{', '}', '.', '?', '!', '@',
32
+ '#', '$', '%', '^', '&', '*', '_', '+', '=', '|', '\\', '<', '>', '/',
33
+ '\f', '\n', '\r', '\t', '\v')
34
+ * Control ('\x00' - '\x1f', '\x7f')
35
+ * ASCII ('\x00' - '\x7f')
36
+ * All ('\x00' - '\xff')
37
+
38
+ == EXAMPLES:
39
+
40
+ * Determine wether a byte belongs to a character set:
41
+
42
+ 0x41.alpha?
43
+ # => true
44
+
45
+ * Determine wether a String belongs to a character set:
46
+
47
+ "22e1c0".hex?
48
+ # => true
49
+
50
+ * Return a random character from the set of all characters:
51
+
52
+ Chars.all.random_char
53
+ # => "\x94"
54
+
55
+ * Return a random Array of characters from the alpha-numeric character set:
56
+
57
+ Chars.alpha_numeric.random_array(10)
58
+ # => ["Q", "N", "S", "4", "x", "z", "3", "M", "F", "F"]
59
+
60
+ * Return a random String from the set of all characters:
61
+
62
+ Chars.all.random_string(10)
63
+ # => "\xc2h\xad\xccm7\x1e6J\x13"
64
+
65
+ * Return a random String with a random length between 5 and 10, from the
66
+ set of space characters:
67
+
68
+ Chars.space.random_string(5..10)
69
+ # => "\r\v\n\t\n\f"
70
+
71
+ == INSTALL:
72
+
73
+ $ sudo gem install chars
74
+
75
+ == LICENSE:
76
+
77
+ The MIT License
78
+
79
+ Copyright (c) 2009 Hal Brodigan
80
+
81
+ Permission is hereby granted, free of charge, to any person obtaining
82
+ a copy of this software and associated documentation files (the
83
+ 'Software'), to deal in the Software without restriction, including
84
+ without limitation the rights to use, copy, modify, merge, publish,
85
+ distribute, sublicense, and/or sell copies of the Software, and to
86
+ permit persons to whom the Software is furnished to do so, subject to
87
+ the following conditions:
88
+
89
+ The above copyright notice and this permission notice shall be
90
+ included in all copies or substantial portions of the Software.
91
+
92
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
93
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
94
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
95
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
96
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
97
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
98
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,14 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require './tasks/spec.rb'
6
+ require './lib/chars/version.rb'
7
+
8
+ Hoe.new('chars', Chars::VERSION) do |p|
9
+ p.rubyforge_name = 'chars'
10
+ p.developer('Postmodern','postmodern.mod3@gmail.com')
11
+ p.remote_rdoc_dir = '/'
12
+ end
13
+
14
+ # vim: syntax=Ruby
@@ -0,0 +1,4 @@
1
+ require 'chars/char_set'
2
+ require 'chars/chars'
3
+ require 'chars/extensions'
4
+ require 'chars/version'
@@ -0,0 +1,228 @@
1
+ require 'set'
2
+
3
+ module Chars
4
+ class CharSet < SortedSet
5
+
6
+ #
7
+ # Creates a new CharSet object with the given _chars_.
8
+ #
9
+ def initialize(*chars)
10
+ super()
11
+
12
+ merge_chars = lambda { |element|
13
+ if element.kind_of?(String)
14
+ element.each_byte(&merge_chars)
15
+ elsif element.kind_of?(Integer)
16
+ self << element
17
+ elsif element.kind_of?(Enumerable)
18
+ element.each(&merge_chars)
19
+ end
20
+ }
21
+
22
+ merge_chars.call(chars)
23
+ end
24
+
25
+ alias include_byte? include?
26
+ alias bytes to_a
27
+ alias each_byte each
28
+ alias select_bytes select
29
+ alias map_bytes map
30
+
31
+ #
32
+ # Returns +true+ if the character set includes the specified _char_,
33
+ # returns +false+ otherwise.
34
+ #
35
+ def include_char?(char)
36
+ return false unless char.respond_to?(:each_byte)
37
+
38
+ char.each_byte do |b|
39
+ return include?(b)
40
+ end
41
+ end
42
+
43
+ #
44
+ # Returns all the characters within the character set as Strings.
45
+ #
46
+ def chars
47
+ map { |b| b.chr }
48
+ end
49
+
50
+ #
51
+ # Iterates over every character within the character set, passing
52
+ # each to the given _block_.
53
+ #
54
+ def each_char(&block)
55
+ each { |b| block.call(b.chr) } if block
56
+ end
57
+
58
+ #
59
+ # Selects an Array of characters from the character set that match
60
+ # the given _block_.
61
+ #
62
+ def select_chars(&block)
63
+ chars.select(&block)
64
+ end
65
+
66
+ #
67
+ # Maps the characters of the character set using the given _block_.
68
+ #
69
+ def map_chars(&block)
70
+ chars.map(&block)
71
+ end
72
+
73
+ #
74
+ # Returns a random byte from the character set.
75
+ #
76
+ def random_byte
77
+ self.entries[rand(self.length)]
78
+ end
79
+
80
+ #
81
+ # Returns a random char from the character set.
82
+ #
83
+ def random_char
84
+ random_byte.chr
85
+ end
86
+
87
+ #
88
+ # Pass a random byte to the specified _block_, _n_ times.
89
+ #
90
+ def each_random_byte(n,&block)
91
+ n.times { block.call(random_byte) }
92
+ end
93
+
94
+ #
95
+ # Pass a random character to the specified _block_, _n_ times.
96
+ #
97
+ def each_random_char(n,&block)
98
+ each_random_byte(n) { |b| block.call(b.chr) }
99
+ end
100
+
101
+ #
102
+ # Returns an Array of the specified _length_ containing
103
+ # random bytes from the character set. The specified _length_ may
104
+ # be an Integer, Array or a Range of lengths.
105
+ #
106
+ def random_bytes(length)
107
+ if (length.kind_of?(Array) || length.kind_of?(Range))
108
+ return Array.new(length.sort_by { rand }.first) { random_byte }
109
+ else
110
+ return Array.new(length) { random_byte }
111
+ end
112
+ end
113
+
114
+ #
115
+ # Returns an Array of the specified _length_ containing
116
+ # random characters from the character set. The specified _length_
117
+ # may be an Integer, Array or a Range of lengths.
118
+ #
119
+ def random_chars(length)
120
+ random_bytes(length).map { |b| b.chr }
121
+ end
122
+
123
+ #
124
+ # Returns a String of the specified _length_ containing
125
+ # random characters from the character set.
126
+ #
127
+ def random_string(length)
128
+ random_chars(length).join
129
+ end
130
+
131
+ #
132
+ # Finds sub-strings within the specified _data_ that are part of the
133
+ # character set, using the given _options_.
134
+ #
135
+ # _options_ may contain the following keys:
136
+ # <tt>:length</tt>:: The minimum length of matching sub-strings
137
+ # within the _data_. Defaults to 4, if not
138
+ # specified.
139
+ # <tt>:offsets</tt>:: Specifies wether to return a Hash of the
140
+ # offsets within the _data_ and the matched
141
+ # sub-strings. If not specified a simple
142
+ # Array will be returned of the matched
143
+ # sub-strings.
144
+ #
145
+ def strings_in(data,options={})
146
+ min_length = (options[:length] || 4)
147
+
148
+ if options[:offsets]
149
+ found = {}
150
+ found_substring = lambda { |offset,substring|
151
+ found[offset] = substring
152
+ }
153
+ else
154
+ found = []
155
+ found_substring = lambda { |offset,substring|
156
+ found << substring
157
+ }
158
+ end
159
+
160
+ return found if data.length < min_length
161
+
162
+ index = 0
163
+
164
+ while index <= (data.length - min_length)
165
+ if self =~ data[index...(index + min_length)]
166
+ sub_index = (index + min_length)
167
+
168
+ while self.include_char?(data[sub_index..sub_index])
169
+ sub_index += 1
170
+ end
171
+
172
+ found_substring.call(index,data[index...sub_index])
173
+ index = sub_index
174
+ else
175
+ index += 1
176
+ end
177
+ end
178
+
179
+ return found
180
+ end
181
+
182
+ #
183
+ # Creates a new CharSet object containing the both the characters
184
+ # of the character set and the specified _other_set_.
185
+ #
186
+ def |(other_set)
187
+ super(CharSet.new(other_set))
188
+ end
189
+
190
+ alias + |
191
+
192
+ #
193
+ # Returns +true+ if all of the bytes within the specified _string_
194
+ # are included in the character set, returns +false+ otherwise.
195
+ #
196
+ # Chars.alpha =~ "hello"
197
+ # # => true
198
+ #
199
+ def =~(string)
200
+ return false unless string.respond_to?(:each_byte)
201
+
202
+ string.each_byte do |b|
203
+ return false unless include?(b)
204
+ end
205
+
206
+ return true
207
+ end
208
+
209
+ #
210
+ # Inspects the character set.
211
+ #
212
+ def inspect
213
+ "#<#{self.class.name}: {" + map { |b|
214
+ case b
215
+ when (0x07..0x0d), (0x20..0x7e)
216
+ b.chr.dump
217
+ when 0x00
218
+ # sly hack to make char-sets more friendly
219
+ # to us C programmers
220
+ '"\0"'
221
+ else
222
+ "0x%02x" % b
223
+ end
224
+ }.join(', ') + "}>"
225
+ end
226
+
227
+ end
228
+ end
@@ -0,0 +1,165 @@
1
+ require 'chars/char_set'
2
+
3
+ module Chars
4
+ # The numeric decimal character set
5
+ NUMERIC = CharSet.new('0'..'9')
6
+
7
+ # The octal character set
8
+ OCTAL = CharSet.new('0'..'7')
9
+
10
+ # The upper-case hexadecimal character set
11
+ UPPERCASE_HEXADECIMAL = NUMERIC + ('A'..'F')
12
+
13
+ # The lower-case hexadecimal character set
14
+ LOWERCASE_HEXADECIMAL = NUMERIC + ('a'..'f')
15
+
16
+ # The hexadecimal character set
17
+ HEXADECIMAL = UPPERCASE_HEXADECIMAL + LOWERCASE_HEXADECIMAL
18
+
19
+ # The upper-case alpha character set
20
+ UPPERCASE_ALPHA = CharSet.new('A'..'Z')
21
+
22
+ # The lower-case alpha character set
23
+ LOWERCASE_ALPHA = CharSet.new('a'..'z')
24
+
25
+ # The alpha character set
26
+ ALPHA = UPPERCASE_ALPHA + LOWERCASE_ALPHA
27
+
28
+ # The alpha-numeric character set
29
+ ALPHA_NUMERIC = ALPHA + NUMERIC
30
+
31
+ # The punctuation character set
32
+ PUNCTUATION = CharSet.new(' ', '\'', '"', '`', ',', ';', ':', '~', '-',
33
+ '(', ')', '[', ']', '{', '}', '.', '?', '!')
34
+
35
+ # The symbolic character set
36
+ SYMBOLS = PUNCTUATION + ['@', '#', '$', '%', '^', '&', '*', '_', '+',
37
+ '=', '|', '\\', '<', '>', '/']
38
+
39
+ # The space character set
40
+ SPACE = CharSet.new(' ', "\f", "\n", "\r", "\t", "\v")
41
+
42
+ # The set of printable characters (including spaces)
43
+ PRINTABLE = ALPHA_NUMERIC + PUNCTUATION + SYMBOLS + SPACE
44
+
45
+ # The control-char character set
46
+ CONTROL = CharSet.new(0..0x1f, 0x7f)
47
+
48
+ # The ASCII character set
49
+ ASCII = CharSet.new(0..0x7f)
50
+
51
+ # The full 8-bit character set
52
+ ALL = CharSet.new(0..0xff)
53
+
54
+ #
55
+ # The numeric decimal character set.
56
+ #
57
+ def Chars.numeric
58
+ NUMERIC
59
+ end
60
+
61
+ #
62
+ # The octal character set.
63
+ #
64
+ def Chars.octal
65
+ OCTAL
66
+ end
67
+
68
+ #
69
+ # The upper-case hexadecimal character set.
70
+ #
71
+ def Chars.uppercase_hexadecimal
72
+ UPPERCASE_HEXADECIMAL
73
+ end
74
+
75
+ #
76
+ # The lower-case hexadecimal character set.
77
+ #
78
+ def Chars.lowercase_hexadecimal
79
+ LOWERCASE_HEXADECIMAL
80
+ end
81
+
82
+ #
83
+ # The hexadecimal character set.
84
+ #
85
+ def Chars.hexadecimal
86
+ HEXADECIMAL
87
+ end
88
+
89
+ #
90
+ # The upper-case alpha character set.
91
+ #
92
+ def Chars.uppercase_alpha
93
+ UPPERCASE_ALPHA
94
+ end
95
+
96
+ #
97
+ # The lower-case alpha character set.
98
+ #
99
+ def Chars.lowercase_alpha
100
+ LOWERCASE_ALPHA
101
+ end
102
+
103
+ #
104
+ # The alpha character set.
105
+ #
106
+ def Chars.alpha
107
+ ALPHA
108
+ end
109
+
110
+ #
111
+ # The alpha-numeric character set.
112
+ #
113
+ def Chars.alpha_numeric
114
+ ALPHA_NUMERIC
115
+ end
116
+
117
+ #
118
+ # The punctuation character set.
119
+ #
120
+ def Chars.punctuation
121
+ PUNCTUATION
122
+ end
123
+
124
+ #
125
+ # The symbolic character set.
126
+ #
127
+ def Chars.symbols
128
+ SYMBOLS
129
+ end
130
+
131
+ #
132
+ # The space character set.
133
+ #
134
+ def Chars.space
135
+ SPACE
136
+ end
137
+
138
+ #
139
+ # The set of printable characters, including spaces.
140
+ #
141
+ def Chars.printable
142
+ PRINTABLE
143
+ end
144
+
145
+ #
146
+ # The control-char character set.
147
+ #
148
+ def Chars.control
149
+ CONTROL
150
+ end
151
+
152
+ #
153
+ # The ASCII character set.
154
+ #
155
+ def Chars.ascii
156
+ ASCII
157
+ end
158
+
159
+ #
160
+ # The full 8-bit character set.
161
+ #
162
+ def Chars.all
163
+ ALL
164
+ end
165
+ end
@@ -0,0 +1,2 @@
1
+ require 'chars/extensions/integer'
2
+ require 'chars/extensions/string'
@@ -0,0 +1,65 @@
1
+ require 'chars/chars'
2
+
3
+ class Integer
4
+
5
+ def numeric?
6
+ Chars::NUMERIC.include?(self)
7
+ end
8
+
9
+ def octal?
10
+ Chars::OCTAL.include?(self)
11
+ end
12
+
13
+ def uppercase_hex?
14
+ Chars::UPPERCASE_HEXADECIMAL.include?(self)
15
+ end
16
+
17
+ def lowercase_hex?
18
+ Chars::LOWERCASE_HEXADECIMAL.include?(self)
19
+ end
20
+
21
+ def hex?
22
+ Chars::HEXADECIMAL.include?(self)
23
+ end
24
+
25
+ def uppercase_alpha?
26
+ Chars::UPPERCASE_ALPHA.include?(self)
27
+ end
28
+
29
+ def lowercase_alpha?
30
+ Chars::LOWERCASE_ALPHA.include?(self)
31
+ end
32
+
33
+ def alpha?
34
+ Chars::ALPHA.include?(self)
35
+ end
36
+
37
+ def alpha_numeric?
38
+ Chars::ALPHA_NUMERIC.include?(self)
39
+ end
40
+
41
+ def punctuation?
42
+ Chars::PUNCTUATION.include?(self)
43
+ end
44
+
45
+ def symbolic?
46
+ Chars::SYMBOLS.include?(self)
47
+ end
48
+
49
+ def space?
50
+ Chars::SPACE.include?(self)
51
+ end
52
+
53
+ def printable?
54
+ Chars::PRINTABLE.include?(self)
55
+ end
56
+
57
+ def control?
58
+ Chars::CONTROL.include?(self)
59
+ end
60
+
61
+ def ascii?
62
+ Chars::ASCII.include?(self)
63
+ end
64
+
65
+ end
@@ -0,0 +1,65 @@
1
+ require 'chars/chars'
2
+
3
+ class String
4
+
5
+ def numeric?
6
+ Chars::NUMERIC =~ self
7
+ end
8
+
9
+ def octal?
10
+ Chars::OCTAL =~ self
11
+ end
12
+
13
+ def uppercase_hex?
14
+ Chars::UPPERCASE_HEXADECIMAL =~ self
15
+ end
16
+
17
+ def lowercase_hex?
18
+ Chars::LOWERCASE_HEXADECIMAL =~ self
19
+ end
20
+
21
+ def hex?
22
+ Chars::HEXADECIMAL =~ self
23
+ end
24
+
25
+ def uppercase_alpha?
26
+ Chars::UPPERCASE_ALPHA =~ self
27
+ end
28
+
29
+ def lowercase_alpha?
30
+ Chars::LOWERCASE_ALPHA =~ self
31
+ end
32
+
33
+ def alpha?
34
+ Chars::ALPHA =~ self
35
+ end
36
+
37
+ def alpha_numeric?
38
+ Chars::ALPHA_NUMERIC =~ self
39
+ end
40
+
41
+ def punctuation?
42
+ Chars::PUNCTUATION =~ self
43
+ end
44
+
45
+ def symbolic?
46
+ Chars::SYMBOLS =~ self
47
+ end
48
+
49
+ def space?
50
+ Chars::SPACE =~ self
51
+ end
52
+
53
+ def printable?
54
+ Chars::PRINTABLE =~ self
55
+ end
56
+
57
+ def control?
58
+ Chars::CONTROL =~ self
59
+ end
60
+
61
+ def ascii?
62
+ Chars::ASCII =~ self
63
+ end
64
+
65
+ end
@@ -0,0 +1,4 @@
1
+ module Chars
2
+ # Chars version
3
+ VERSION = '0.1.0'
4
+ end
@@ -0,0 +1,186 @@
1
+ require 'chars/chars'
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Chars::CharSet do
6
+ before(:all) do
7
+ @integer_range = (0x41..0x43)
8
+ @string_range = ('A'..'C')
9
+ @integers = @integer_range.to_a
10
+ @strings = @string_range.to_a
11
+
12
+ @char_set = Chars::CharSet.new(*@strings)
13
+ end
14
+
15
+ it "may be created with String arguments" do
16
+ @chars = Chars::CharSet.new(*@strings)
17
+
18
+ @strings.each do |s|
19
+ @chars.include_char?(s).should == true
20
+ end
21
+ end
22
+
23
+ it "may be created with an Array of Strings" do
24
+ @chars = Chars::CharSet.new(@strings)
25
+
26
+ @strings.each do |s|
27
+ @chars.include_char?(s).should == true
28
+ end
29
+ end
30
+
31
+ it "may be created with a Range of Strings" do
32
+ @chars = Chars::CharSet.new(@string_range)
33
+
34
+ @strings.each do |s|
35
+ @chars.include_char?(s).should == true
36
+ end
37
+ end
38
+
39
+ it "may be created with Integer arguments" do
40
+ @chars = Chars::CharSet.new(*@integers)
41
+
42
+ @integers.each do |i|
43
+ @chars.include?(i).should == true
44
+ end
45
+ end
46
+
47
+ it "may be created with an Array of Integers" do
48
+ @chars = Chars::CharSet.new(@integers)
49
+
50
+ @integers.each do |i|
51
+ @chars.include?(i).should == true
52
+ end
53
+ end
54
+
55
+ it "may be created with a Range of Integers" do
56
+ @chars = Chars::CharSet.new(@integer_range)
57
+
58
+ @integers.each do |i|
59
+ @chars.include?(i).should == true
60
+ end
61
+ end
62
+
63
+ it "should include Strings" do
64
+ @char_set.include_char?('A').should == true
65
+ end
66
+
67
+ it "should include Integers" do
68
+ @char_set.include?(0x41).should == true
69
+ end
70
+
71
+ it "should be able to select bytes" do
72
+ @sub_chars = @char_set.select_bytes { |c| c <= 0x42 }
73
+
74
+ @sub_chars.should == [0x41, 0x42]
75
+ end
76
+
77
+ it "should be able to select chars" do
78
+ @sub_chars = @char_set.select_chars { |c| c <= 'B' }
79
+
80
+ @sub_chars.should == ['A', 'B']
81
+ end
82
+
83
+ it "should return a random byte" do
84
+ @char_set.include?(@char_set.random_byte).should == true
85
+ end
86
+
87
+ it "should return a random char" do
88
+ @char_set.include_char?(@char_set.random_char).should == true
89
+ end
90
+
91
+ it "should iterate over n random bytes" do
92
+ @char_set.each_random_byte(10) do |b|
93
+ @char_set.include?(b).should == true
94
+ end
95
+ end
96
+
97
+ it "should iterate over n random chars" do
98
+ @char_set.each_random_char(10) do |c|
99
+ @char_set.include_char?(c).should == true
100
+ end
101
+ end
102
+
103
+ it "should return a random Array of bytes" do
104
+ bytes = @char_set.random_bytes(10)
105
+
106
+ bytes.each do |b|
107
+ @char_set.include?(b).should == true
108
+ end
109
+ end
110
+
111
+ it "should return a random Array of chars" do
112
+ chars = @char_set.random_chars(10)
113
+
114
+ chars.each do |c|
115
+ @char_set.include_char?(c).should == true
116
+ end
117
+ end
118
+
119
+ it "should return a random Array of bytes with a varying length" do
120
+ bytes = @char_set.random_bytes(5..10)
121
+
122
+ bytes.length.between?(5, 10).should == true
123
+ bytes.each do |b|
124
+ @char_set.include?(b).should == true
125
+ end
126
+ end
127
+
128
+ it "should return a random Array of chars with a varying length" do
129
+ chars = @char_set.random_chars(5..10)
130
+
131
+ chars.length.between?(5, 10).should == true
132
+ chars.each do |c|
133
+ @char_set.include_char?(c).should == true
134
+ end
135
+ end
136
+
137
+ it "should return a random String of chars" do
138
+ @char_set.random_string(10).each_byte do |b|
139
+ @char_set.include?(b).should == true
140
+ end
141
+ end
142
+
143
+ it "should return a random String of chars with a varying length" do
144
+ string = @char_set.random_string(5..10)
145
+
146
+ string.length.between?(5, 10)
147
+ string.each_byte do |b|
148
+ @char_set.include?(b).should == true
149
+ end
150
+ end
151
+
152
+ it "should be able to be compared with another set of chars" do
153
+ (@char_set == Chars::CharSet['A', 'B', 'C']).should == true
154
+ (@char_set == Chars::CharSet['A', 'C', 'B']).should == true
155
+ end
156
+
157
+ it "should be able to be unioned with another set of chars" do
158
+ super_set = (@char_set | Chars::CharSet['D'])
159
+
160
+ super_set.class.should == Chars::CharSet
161
+ super_set.should == Chars::CharSet['A', 'B', 'C', 'D']
162
+ end
163
+
164
+ it "should be able to be removed from another set of chars" do
165
+ sub_set = (@char_set - Chars::CharSet['B'])
166
+
167
+ sub_set.class.should == Chars::CharSet
168
+ sub_set.subset?(@char_set).should == true
169
+ end
170
+
171
+ it "should find one sub-string from a String belonging to the char set" do
172
+ @char_set.strings_in("AAAA").should == ["AAAA"]
173
+ end
174
+
175
+ it "should find sub-strings from a String belonging to the char set" do
176
+ @char_set.strings_in("AAAAXBXCCCCCC").should == [
177
+ "AAAA",
178
+ "CCCCCC"
179
+ ]
180
+ end
181
+
182
+ it "should determine if a String is made up of the characters from the char set" do
183
+ (@char_set =~ "AABCBAA").should == true
184
+ (@char_set =~ "AADDEE").should_not == true
185
+ end
186
+ end
@@ -0,0 +1,107 @@
1
+ require 'chars/chars'
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Chars do
6
+ before(:all) do
7
+ @numeric_string = Chars.numeric.random_string(10)
8
+ @octal_string = Chars.octal.random_string(10)
9
+ @uppercase_hex_string = Chars.uppercase_hexadecimal.random_string(10)
10
+ @lowercase_hex_string = Chars.lowercase_hexadecimal.random_string(10)
11
+ @hex_string = Chars.hexadecimal.random_string(10)
12
+ @uppercase_alpha_string = Chars.uppercase_alpha.random_string(10)
13
+ @lowercase_alpha_string = Chars.lowercase_alpha.random_string(10)
14
+ @alpha_string = Chars.alpha.random_string(10)
15
+ @alpha_numeric_string = Chars.alpha_numeric.random_string(10)
16
+ @space_string = Chars.space.random_string(10)
17
+ @punctuation_string = Chars.punctuation.random_string(10)
18
+ @symbols_string = Chars.symbols.random_string(10)
19
+ @control_string = Chars.control.random_string(10)
20
+ @ascii_string = Chars.ascii.random_string(10)
21
+ @all_string = Chars.all.random_string(10)
22
+ end
23
+
24
+ it "should provide a numeric CharSet" do
25
+ @numeric_string.length.should == 10
26
+ @numeric_string.each_byte do |b|
27
+ Chars::NUMERIC.include?(b).should == true
28
+ end
29
+ end
30
+
31
+ it "should provide an octal CharSet" do
32
+ @octal_string.length.should == 10
33
+ @octal_string.each_byte do |b|
34
+ Chars::OCTAL.include?(b).should == true
35
+ end
36
+ end
37
+
38
+ it "should provide an upper-case hexadecimal CharSet" do
39
+ @uppercase_hex_string.length.should == 10
40
+ @uppercase_hex_string.each_byte do |b|
41
+ Chars::UPPERCASE_HEXADECIMAL.include?(b).should == true
42
+ end
43
+ end
44
+
45
+ it "should provide a lower-case hexadecimal CharSet" do
46
+ @lowercase_hex_string.length.should == 10
47
+ @lowercase_hex_string.each_byte do |b|
48
+ Chars::LOWERCASE_HEXADECIMAL.include?(b).should == true
49
+ end
50
+ end
51
+
52
+ it "should provide a hexadecimal CharSet" do
53
+ @hex_string.length.should == 10
54
+ @hex_string.each_byte do |b|
55
+ Chars::HEXADECIMAL.include?(b).should == true
56
+ end
57
+ end
58
+
59
+ it "should provide an upper-case alpha CharSet" do
60
+ @uppercase_alpha_string.length.should == 10
61
+ @uppercase_alpha_string.each_byte do |b|
62
+ Chars::UPPERCASE_ALPHA.include?(b).should == true
63
+ end
64
+ end
65
+
66
+ it "should provide a lower-case alpha CharSet" do
67
+ @lowercase_alpha_string.length.should == 10
68
+ @lowercase_alpha_string.each_byte do |b|
69
+ Chars::LOWERCASE_ALPHA.include?(b).should == true
70
+ end
71
+ end
72
+
73
+ it "should provide an alpha CharSet" do
74
+ @alpha_string.length.should == 10
75
+ @alpha_string.each_byte do |b|
76
+ Chars::ALPHA.include?(b).should == true
77
+ end
78
+ end
79
+
80
+ it "should provide an alpha-numeric CharSet" do
81
+ @alpha_numeric_string.length.should == 10
82
+ @alpha_numeric_string.each_byte do |b|
83
+ Chars::ALPHA_NUMERIC.include?(b).should == true
84
+ end
85
+ end
86
+
87
+ it "should provide a space CharSet" do
88
+ @space_string.length.should == 10
89
+ @space_string.each_byte do |b|
90
+ Chars::SPACE.include?(b).should == true
91
+ end
92
+ end
93
+
94
+ it "should provide a punctuation CharSet" do
95
+ @punctuation_string.length.should == 10
96
+ @punctuation_string.each_byte do |b|
97
+ Chars::PUNCTUATION.include?(b).should == true
98
+ end
99
+ end
100
+
101
+ it "should provide a symbols CharSet" do
102
+ @symbols_string.length.should == 10
103
+ @symbols_string.each_byte do |b|
104
+ Chars::SYMBOLS.include?(b).should == true
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,65 @@
1
+ require 'chars/extensions/integer'
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Integer do
6
+ it "should recognize numeric bytes" do
7
+ 0x39.should be_numeric
8
+ end
9
+
10
+ it "should recognize octal bytes" do
11
+ 0x30.should be_octal
12
+ end
13
+
14
+ it "should recognize upper-case hexadecimal bytes" do
15
+ 0x44.should be_uppercase_hex
16
+ end
17
+
18
+ it "should recognize lower-case hexadecimal bytes" do
19
+ 0x61.should be_lowercase_hex
20
+ end
21
+
22
+ it "should recognize hexadecimal bytes" do
23
+ 0x63.should be_hex
24
+ end
25
+
26
+ it "should recognize upper-case alpha bytes" do
27
+ 0x4c.should be_uppercase_alpha
28
+ end
29
+
30
+ it "should recognize lower-case alpha bytes" do
31
+ 0x71.should be_lowercase_alpha
32
+ end
33
+
34
+ it "should recognize alpha bytes" do
35
+ 0x7a.should be_alpha
36
+ end
37
+
38
+ it "should recognize alpha-numeric bytes" do
39
+ 0x69.should be_alpha_numeric
40
+ end
41
+
42
+ it "should recognize punctuation bytes" do
43
+ 0x60.should be_punctuation
44
+ end
45
+
46
+ it "should recognize symbolic bytes" do
47
+ 0x26.should be_symbolic
48
+ end
49
+
50
+ it "should recognize space bytes" do
51
+ 0x20.should be_space
52
+ end
53
+
54
+ it "should recognize printable bytes" do
55
+ 0x3f.should be_printable
56
+ end
57
+
58
+ it "should recognize control bytes" do
59
+ 0x1b.should be_control
60
+ end
61
+
62
+ it "should recognize ASCII bytes" do
63
+ 0x00.should be_ascii
64
+ end
65
+ end
@@ -0,0 +1,5 @@
1
+ require 'rubygems'
2
+ gem 'rspec', '>=1.1.3'
3
+ require 'spec'
4
+
5
+ require 'chars/version'
@@ -0,0 +1,65 @@
1
+ require 'chars/extensions/string'
2
+
3
+ require 'spec_helper'
4
+
5
+ describe String do
6
+ it "should recognize numeric strings" do
7
+ "0987".should be_numeric
8
+ end
9
+
10
+ it "should recognize octal strings" do
11
+ "012".should be_octal
12
+ end
13
+
14
+ it "should recognize upper-case hexadecimal strings" do
15
+ "2D".should be_uppercase_hex
16
+ end
17
+
18
+ it "should recognize lower-case hexadecimal strings" do
19
+ "2d".should be_lowercase_hex
20
+ end
21
+
22
+ it "should recognize hexadecimal strings" do
23
+ "2dE3".should be_hex
24
+ end
25
+
26
+ it "should recognize upper-case alpha strings" do
27
+ "ABC".should be_uppercase_alpha
28
+ end
29
+
30
+ it "should recognize lower-case alpha strings" do
31
+ "abc".should be_lowercase_alpha
32
+ end
33
+
34
+ it "should recognize alpha strings" do
35
+ "abcDEF".should be_alpha
36
+ end
37
+
38
+ it "should recognize alpha-numeric strings" do
39
+ "abc123".should be_alpha_numeric
40
+ end
41
+
42
+ it "should recognize punctuation strings" do
43
+ "[...]".should be_punctuation
44
+ end
45
+
46
+ it "should recognize symbolic strings" do
47
+ "++".should be_symbolic
48
+ end
49
+
50
+ it "should recognize space strings" do
51
+ " \t".should be_space
52
+ end
53
+
54
+ it "should recognize printable strings" do
55
+ "abc, [123]\nDEF".should be_printable
56
+ end
57
+
58
+ it "should recognize control strings" do
59
+ "\b\b\a".should be_control
60
+ end
61
+
62
+ it "should recognize ASCII strings" do
63
+ "lol\0".should be_ascii
64
+ end
65
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec/rake/spectask'
2
+
3
+ desc "Run all specifications"
4
+ Spec::Rake::SpecTask.new(:spec) do |t|
5
+ t.libs += ['lib', 'spec']
6
+ t.spec_opts = ['--colour', '--format', 'specdoc']
7
+ end
8
+
9
+ task :default => :spec
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chars
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Postmodern
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-16 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.10.0
24
+ version:
25
+ description: Chars is a Ruby library for working with various character sets, recognizing text and generating random text from specific character sets.
26
+ email:
27
+ - postmodern.mod3@gmail.com
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - History.txt
34
+ - Manifest.txt
35
+ - README.txt
36
+ files:
37
+ - History.txt
38
+ - Manifest.txt
39
+ - README.txt
40
+ - Rakefile
41
+ - lib/chars.rb
42
+ - lib/chars/char_set.rb
43
+ - lib/chars/chars.rb
44
+ - lib/chars/extensions.rb
45
+ - lib/chars/extensions/integer.rb
46
+ - lib/chars/extensions/string.rb
47
+ - lib/chars/version.rb
48
+ - tasks/spec.rb
49
+ - spec/spec_helper.rb
50
+ - spec/char_set_spec.rb
51
+ - spec/chars_spec.rb
52
+ - spec/integer_spec.rb
53
+ - spec/string_spec.rb
54
+ has_rdoc: true
55
+ homepage: http://chars.rubyforge.org/
56
+ post_install_message:
57
+ rdoc_options:
58
+ - --main
59
+ - README.txt
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ version:
74
+ requirements: []
75
+
76
+ rubyforge_project: chars
77
+ rubygems_version: 1.3.1
78
+ signing_key:
79
+ specification_version: 2
80
+ summary: Chars is a Ruby library for working with various character sets, recognizing text and generating random text from specific character sets.
81
+ test_files: []
82
+