wildmat 0.9.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.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :rubygems
2
+
3
+ gemspec
4
+ gem 'rspec'
@@ -0,0 +1,31 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ wildmat (0.9.0)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.1.3)
10
+ rake (0.9.2.2)
11
+ rspec (2.8.0)
12
+ rspec-core (~> 2.8.0)
13
+ rspec-expectations (~> 2.8.0)
14
+ rspec-mocks (~> 2.8.0)
15
+ rspec-core (2.8.0)
16
+ rspec-expectations (2.8.0)
17
+ diff-lcs (~> 1.1.2)
18
+ rspec-mocks (2.8.0)
19
+ spec (0.0.0)
20
+ yard (0.7.5)
21
+
22
+ PLATFORMS
23
+ java
24
+ ruby
25
+
26
+ DEPENDENCIES
27
+ rake
28
+ rspec
29
+ spec
30
+ wildmat!
31
+ yard
@@ -0,0 +1,67 @@
1
+ # wildmat
2
+
3
+ * https://github.com/genehsu/wildmat
4
+
5
+ ## DESCRIPTION
6
+
7
+ This is a gem to create regexp objects from wildmat patterns
8
+
9
+ All wildmat patterns match an entire string. The pattern matching
10
+ operations are as follows: (from wikipedia http://en.wikipedia.org/wiki/Wildmat)
11
+
12
+ * Asterisk (\*) to match any sequence of zero or more characters.
13
+ * Question mark (?) to match any single character.
14
+ * Set of specified characters. It is specified as a list of characters,
15
+ or as a range of characters where the beginning and end of the range
16
+ are separated by a minus (or dash) character, or as any combination
17
+ of lists and ranges. The dash can also be included in the set as a
18
+ character if it is the beginning or end of the set. This set is
19
+ enclosed in square brackets. The close square bracket (]) may be
20
+ used in a set if it is the first character in the set.
21
+ * Negation of a set. It is specified the same way as the set with the
22
+ addition of a caret character (^) at the beginning of the test
23
+ string just inside the open square bracket.
24
+ * Backslash (\\) character to invalidate the special meaning of the
25
+ open square bracket ([), the asterisk, backslash or the question
26
+ mark. Two backslashes in sequence will result in the evaluation of
27
+ the backslash as a character with no special meaning.
28
+
29
+ ## SYNOPSIS
30
+
31
+ require 'wildmat'
32
+
33
+ regexp = Wildmat.to_regexp("[0-9]*")
34
+ match_data = regexp =~ "0123456"
35
+
36
+ ## REQUIREMENTS
37
+
38
+ * wildmat gem
39
+
40
+ ## INSTALL
41
+
42
+ * gem install wildmat
43
+
44
+ ## LICENSE
45
+
46
+ (The MIT License)
47
+
48
+ Copyright (c) 2012 Gene Hsu
49
+
50
+ Permission is hereby granted, free of charge, to any person obtaining
51
+ a copy of this software and associated documentation files (the
52
+ 'Software'), to deal in the Software without restriction, including
53
+ without limitation the rights to use, copy, modify, merge, publish,
54
+ distribute, sublicense, and/or sell copies of the Software, and to
55
+ permit persons to whom the Software is furnished to do so, subject to
56
+ the following conditions:
57
+
58
+ The above copyright notice and this permission notice shall be
59
+ included in all copies or substantial portions of the Software.
60
+
61
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
62
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
63
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
64
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
65
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
66
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
67
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,11 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new # Update options in spec/spec_helper.rb
7
+
8
+ desc "Run all the tests"
9
+ task :default => :spec
10
+
11
+ # vim: syntax=ruby
@@ -0,0 +1,70 @@
1
+ module Wildmat
2
+
3
+ VERSION = '0.9.0'
4
+
5
+ def self.to_regexp(pattern)
6
+ %r(^#{regexp_pattern(pattern)}$)
7
+ end
8
+
9
+ def self.regexp_pattern(pattern)
10
+ # * ? [ \ are treated specially
11
+ # all other non-alphanumeric will be escaped
12
+ literal(pattern, 0, '')
13
+ end
14
+
15
+ def self.literal(pattern, i, current)
16
+ return current if i >= pattern.size
17
+ next_char = pattern[i].chr
18
+ case next_char
19
+ when '*'
20
+ current << '.*'
21
+ when '?'
22
+ current << '.'
23
+ when '\\'
24
+ return backslash(pattern, i+1, current)
25
+ when '['
26
+ current << '['
27
+ return first_character_class(pattern, i+1, current)
28
+ else
29
+ current << Regexp.escape(next_char)
30
+ end
31
+ literal(pattern, i+1, current)
32
+ end
33
+
34
+ def self.backslash(pattern, i, current)
35
+ # TODO: raise error? if we reach the end of the pattern
36
+ # in a backslash context
37
+ # return ??? if i > pattern.size
38
+ next_char = pattern[i].chr
39
+ case next_char
40
+ when /\w/
41
+ current << next_char
42
+ else
43
+ current << '\\' << next_char
44
+ end
45
+ literal(pattern, i+1, current)
46
+ end
47
+
48
+ def self.first_character_class(pattern, i, current)
49
+ character_class(pattern, i, current, true)
50
+ end
51
+
52
+ def self.character_class(pattern, i, current, first=false, assertion=true)
53
+ # TODO: raise error? if we reach the end of the pattern
54
+ # in a character class context
55
+ # return ??? if i > pattern.size
56
+ next_char = pattern[i].chr
57
+ case next_char
58
+ when ']'
59
+ current << (first ? '\\]' : ']')
60
+ return literal(pattern, i+1, current) unless first
61
+ when '^'
62
+ current << next_char
63
+ return character_class(pattern, i+1, current, true, false) if first && assertion
64
+ else
65
+ current << next_char
66
+ end
67
+ character_class(pattern, i+1, current)
68
+ end
69
+
70
+ end
@@ -0,0 +1,11 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper.rb"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.color = true
11
+ end
@@ -0,0 +1,142 @@
1
+ require "spec_helper"
2
+ require "wildmat"
3
+
4
+ describe Wildmat do
5
+
6
+ context 'word characters' do
7
+ bad_string = "foo bar baz"
8
+ %w[
9
+ asdf
10
+ _
11
+ 123
12
+ a1
13
+ foo_bar_baz
14
+ _under
15
+ after_
16
+ ].each do |input|
17
+ it "should not change word characters: #{input}" do
18
+ Wildmat.regexp_pattern(input).should eq input
19
+ end
20
+ it "should match itself: #{input} =~ #{input}" do
21
+ input.should match Wildmat.to_regexp(input)
22
+ end
23
+ it "should not match an arbitrary string: #{input} !~ #{bad_string}" do
24
+ bad_string.should_not match Wildmat.to_regexp(input)
25
+ end
26
+ end
27
+ end
28
+
29
+ context 'question mark' do
30
+ # Negative cases
31
+ negatives = %w[ abc nice caba abcde ]
32
+ # Positive cases
33
+ [
34
+ [ '?abc', %w[ 1abc aabc _abc .abc ] ],
35
+ [ 'abc?', %w[ abc1 abcc abc_ abc. ] ],
36
+ [ 'a??c', %w[ abzc a12c a_?c a.!c ] ],
37
+ ].each do |wildmat,inputs|
38
+ regexp = Wildmat.to_regexp(wildmat)
39
+ it "should match a single character: #{wildmat} =~ #{inputs}" do
40
+ inputs.each do |input|
41
+ input.should match regexp
42
+ end
43
+ end
44
+ it "should not match the negatives: #{wildmat} !~ #{negatives}" do
45
+ negatives.each do |input|
46
+ input.should_not match regexp
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ context 'backslash' do
53
+ # Negative cases
54
+ negatives = %w[ ab abd abcd ]
55
+ # Positive cases
56
+ [
57
+ [ '\a\b\c', %w[ abc ] ],
58
+ [ '\a\b\?', %w[ ab? ] ],
59
+ [ '\a\b\[', %w[ ab\[ ] ],
60
+ ].each do |wildmat,inputs|
61
+ regexp = Wildmat.to_regexp(wildmat)
62
+ it "should match: #{wildmat} =~ #{inputs}" do
63
+ inputs.each do |input|
64
+ input.should match regexp
65
+ end
66
+ end
67
+ it "should not match: #{wildmat} != #{negatives}" do
68
+ negatives.each do |input|
69
+ input.should_not match regexp
70
+ end
71
+ end
72
+ end
73
+ end
74
+
75
+ context 'asterisk' do
76
+ # Test cases
77
+ [
78
+ [ 'a*b', %w[ ab aab abb a123b a_:-?=b ], %w[ ba aba ab123 123ab ] ],
79
+ [ 'ab*', %w[ ab aba abb ab123 ab_:-?= ], %w[ ba bab 123ab a123b ] ],
80
+ [ '*ab', %w[ ab aab bab 123ab _:-?=ab ], %w[ ba aba a123b ab123 ] ],
81
+ ].each do |wildmat,inputs,negatives|
82
+ regexp = Wildmat.to_regexp(wildmat)
83
+ it "should match a single character: #{wildmat} =~ #{inputs}" do
84
+ inputs.each do |input|
85
+ input.should match regexp
86
+ end
87
+ end
88
+ it "should not match the negatives: #{wildmat} != #{negatives}" do
89
+ negatives.each do |input|
90
+ input.should_not match regexp
91
+ end
92
+ end
93
+ end
94
+ end
95
+
96
+ context 'character class' do
97
+ # Test cases
98
+ [
99
+ [ '[a-z]', %w[ a d z ], %w[ ab 0 - * ] ],
100
+ [ '[0-9]', %w[ 0 5 9 ], %w[ a 00 - * ] ],
101
+ [ '[a-]', %w[ a - ], %w[ b 0 a- * ] ],
102
+ [ '[-a]', %w[ a - ], %w[ b 0 -a * ] ],
103
+ [ '[]0-9]', %w[ \] 5 ], %w[ \]0 a - * ] ],
104
+ ].each do |wildmat,inputs,negatives|
105
+ regexp = Wildmat.to_regexp(wildmat)
106
+ it "should match: #{wildmat} =~ #{inputs}" do
107
+ inputs.each do |input|
108
+ input.should match regexp
109
+ end
110
+ end
111
+ it "should not match: #{wildmat} != #{negatives}" do
112
+ negatives.each do |input|
113
+ input.should_not match regexp
114
+ end
115
+ end
116
+ end
117
+ end
118
+
119
+ context 'negative character class' do
120
+ # Test cases
121
+ [
122
+ [ '[^a-z]', %w[ 0 - * ], %w[ a d z ab ] ],
123
+ [ '[^0-9]', %w[ a - * ], %w[ 0 5 9 00 ] ],
124
+ [ '[^a-]', %w[ b 0 * ], %w[ a - a- ] ],
125
+ [ '[^-a]', %w[ b 0 * ], %w[ a - -a ] ],
126
+ [ '[^]0-9]', %w[ a - * ], %w[ \] 5 \]0 ] ],
127
+ ].each do |wildmat,inputs,negatives|
128
+ regexp = Wildmat.to_regexp(wildmat)
129
+ it "should match: #{wildmat} =~ #{inputs}" do
130
+ inputs.each do |input|
131
+ input.should match regexp
132
+ end
133
+ end
134
+ it "should not match: #{wildmat} != #{negatives}" do
135
+ negatives.each do |input|
136
+ input.should_not match regexp
137
+ end
138
+ end
139
+ end
140
+ end
141
+
142
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ require 'wildmat'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "wildmat"
8
+ s.version = Wildmat::VERSION
9
+ s.authors = ["Gene Hsu"]
10
+ s.email = ["gene@hsufarm.com"]
11
+ s.homepage = "https://github.com/genehsu/wildmat"
12
+ s.summary = %q{A wildmat expression library}
13
+ s.description = %q{A wildmat expression library}
14
+
15
+ s.rubyforge_project = "wildmat"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_development_dependency "rake"
23
+ s.add_development_dependency "yard"
24
+ s.add_development_dependency "spec"
25
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wildmat
3
+ version: !ruby/object:Gem::Version
4
+ hash: 1509009585894205680
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 9
9
+ - 0
10
+ version: 0.9.0
11
+ platform: ruby
12
+ authors:
13
+ - Gene Hsu
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-02-23 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ requirement: &id001 !ruby/object:Gem::Requirement
22
+ none: false
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ hash: 2002549777813010636
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ prerelease: false
31
+ type: :development
32
+ version_requirements: *id001
33
+ name: rake
34
+ - !ruby/object:Gem::Dependency
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ hash: 2002549777813010636
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ prerelease: false
45
+ type: :development
46
+ version_requirements: *id002
47
+ name: yard
48
+ - !ruby/object:Gem::Dependency
49
+ requirement: &id003 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ hash: 2002549777813010636
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ prerelease: false
59
+ type: :development
60
+ version_requirements: *id003
61
+ name: spec
62
+ description: A wildmat expression library
63
+ email:
64
+ - gene@hsufarm.com
65
+ executables: []
66
+
67
+ extensions: []
68
+
69
+ extra_rdoc_files: []
70
+
71
+ files:
72
+ - Gemfile
73
+ - Gemfile.lock
74
+ - README.md
75
+ - Rakefile
76
+ - lib/wildmat.rb
77
+ - spec/spec_helper.rb
78
+ - spec/wildmat_spec.rb
79
+ - wildmat.gemspec
80
+ homepage: https://github.com/genehsu/wildmat
81
+ licenses: []
82
+
83
+ post_install_message:
84
+ rdoc_options: []
85
+
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ hash: 2002549777813010636
94
+ segments:
95
+ - 0
96
+ version: "0"
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ hash: 2002549777813010636
103
+ segments:
104
+ - 0
105
+ version: "0"
106
+ requirements: []
107
+
108
+ rubyforge_project: wildmat
109
+ rubygems_version: 1.8.12
110
+ signing_key:
111
+ specification_version: 3
112
+ summary: A wildmat expression library
113
+ test_files:
114
+ - spec/spec_helper.rb
115
+ - spec/wildmat_spec.rb
116
+ has_rdoc: