stringglob 0.0.1
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/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Rakefile +5 -0
- data/lib/stringglob/version.rb +3 -0
- data/lib/stringglob.rb +95 -0
- data/stringglob.gemspec +20 -0
- data/test/test_stringglob.rb +114 -0
- metadata +72 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
data/lib/stringglob.rb
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
## Ruby StringGlob: Generate a Regexp object from a glob(3) pattern
|
2
|
+
## (Port Text::Glob 0.08 from Perl to Ruby)
|
3
|
+
## Copyright (c) 2002, 2003, 2006, 2007 Richard Clamp. All Rights Reserved.
|
4
|
+
## Copyright (c) 2009-2011 SATOH Fumiyasu @ OSS Technology Inc.
|
5
|
+
## <http://www.osstech.co.jp/>
|
6
|
+
##
|
7
|
+
## License: This module is free software; you can redistribute it and/or
|
8
|
+
## modify it under the same terms as Ruby itself
|
9
|
+
## Date: 2011-06-21, since 2009-07-15
|
10
|
+
|
11
|
+
require "stringglob/version"
|
12
|
+
|
13
|
+
module StringGlob
|
14
|
+
IGNORE_CASE = 1 << 0
|
15
|
+
NO_STRICT_LEADING_DOT = 1 << 1
|
16
|
+
NO_STRICT_WILDCARD_SLASH = 1 << 2
|
17
|
+
|
18
|
+
def regexp(glob, opt = 0, code = nil)
|
19
|
+
re = regexp_string(glob, opt)
|
20
|
+
return Regexp.new("\\A#{re}\\z", nil, code)
|
21
|
+
end
|
22
|
+
module_function :regexp
|
23
|
+
|
24
|
+
def regexp_string(glob, opt = 0)
|
25
|
+
re_str = ''
|
26
|
+
in_curlies = 0
|
27
|
+
escaping = false
|
28
|
+
first_byte = true
|
29
|
+
asterisk = false
|
30
|
+
strict_leading_dot = (opt & NO_STRICT_LEADING_DOT == 0)
|
31
|
+
strict_wildcard_slash = (opt & NO_STRICT_WILDCARD_SLASH == 0)
|
32
|
+
glob.scan(/./m) do |glob_c|
|
33
|
+
if first_byte
|
34
|
+
if strict_leading_dot
|
35
|
+
re_str += '(?=[^\.])' unless glob_c == '.'
|
36
|
+
end
|
37
|
+
first_byte = false
|
38
|
+
end
|
39
|
+
if asterisk && glob_c != '*'
|
40
|
+
re_str += strict_wildcard_slash ? '[^/]*' : '.*'
|
41
|
+
asterisk = false
|
42
|
+
end
|
43
|
+
if glob_c == '/'
|
44
|
+
first_byte = true
|
45
|
+
end
|
46
|
+
if (glob_c == '.' || glob_c == '(' || glob_c == ')' || glob_c == '|' ||
|
47
|
+
glob_c == '+' || glob_c == '^' || glob_c == '$' || glob_c == '@' || glob_c == '%' )
|
48
|
+
re_str += '\\' + glob_c
|
49
|
+
elsif glob_c == '*'
|
50
|
+
if escaping
|
51
|
+
re_str += '\\*'
|
52
|
+
elsif asterisk
|
53
|
+
re_str += '.*'
|
54
|
+
asterisk = false
|
55
|
+
else
|
56
|
+
asterisk = true
|
57
|
+
end
|
58
|
+
elsif glob_c == '?'
|
59
|
+
re_str += escaping ? '\\?' :
|
60
|
+
strict_wildcard_slash ? '[^/]' : '.'
|
61
|
+
elsif glob_c == '{'
|
62
|
+
re_str += escaping ? '\\{' : '('
|
63
|
+
in_curlies += 1 unless escaping
|
64
|
+
elsif glob_c == '}' && in_curlies > 0
|
65
|
+
re_str += escaping ? '}' : ')'
|
66
|
+
in_curlies -= 1 unless escaping
|
67
|
+
elsif glob_c == ',' && in_curlies > 0
|
68
|
+
re_str += escaping ? ',' : '|'
|
69
|
+
elsif glob_c == '\\'
|
70
|
+
if escaping
|
71
|
+
re_str += '\\\\'
|
72
|
+
escaping = false
|
73
|
+
else
|
74
|
+
escaping = true
|
75
|
+
end
|
76
|
+
next
|
77
|
+
else
|
78
|
+
## Suppress warning: re_str has `}' without escape
|
79
|
+
re_str += '\\' if glob_c == '}'
|
80
|
+
re_str += glob_c
|
81
|
+
escaping = false
|
82
|
+
end
|
83
|
+
escaping = false
|
84
|
+
end
|
85
|
+
if asterisk
|
86
|
+
re_str += strict_wildcard_slash ? '[^/]*' : '.*'
|
87
|
+
end
|
88
|
+
|
89
|
+
re_str = "(?i:#{re_str})" if (opt & IGNORE_CASE != 0)
|
90
|
+
|
91
|
+
return re_str
|
92
|
+
end
|
93
|
+
module_function :regexp_string
|
94
|
+
end
|
95
|
+
|
data/stringglob.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "stringglob/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "stringglob"
|
7
|
+
s.version = StringGlob::VERSION
|
8
|
+
s.authors = ["SATOH Fumiyasu"]
|
9
|
+
s.email = ["fumiyas@osstech.co.jp"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Generate a Regexp object from a glob(3) pattern}
|
12
|
+
s.description = %q{Generate a Regexp object from a glob(3) pattern}
|
13
|
+
|
14
|
+
s.rubyforge_project = "stringglob"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
require 'stringglob'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
SG = StringGlob
|
5
|
+
|
6
|
+
class StringGlobTest < Test::Unit::TestCase
|
7
|
+
def test_all
|
8
|
+
regexp = SG.regexp('foo')
|
9
|
+
assert_not_nil regexp.match('foo')
|
10
|
+
assert_nil regexp.match('foobar')
|
11
|
+
|
12
|
+
## absolute string
|
13
|
+
assert_not_nil SG.regexp('foo').match('foo')
|
14
|
+
assert_nil SG.regexp('foo').match('foobar')
|
15
|
+
|
16
|
+
## * wildcard
|
17
|
+
assert_not_nil SG.regexp('foo.*').match('foo.')
|
18
|
+
assert_not_nil SG.regexp('foo.*').match('foo.bar')
|
19
|
+
assert_nil SG.regexp('foo.*').match('gfoo.bar')
|
20
|
+
|
21
|
+
## ? wildcard
|
22
|
+
assert_not_nil SG.regexp('foo.?p').match('foo.cp')
|
23
|
+
assert_nil SG.regexp('foo.?p').match('foo.cd')
|
24
|
+
|
25
|
+
## .{alternation,or,something}
|
26
|
+
assert_not_nil SG.regexp('foo.{c,h}').match('foo.h')
|
27
|
+
assert_not_nil SG.regexp('foo.{c,h}').match('foo.c')
|
28
|
+
assert_nil SG.regexp('foo.{c,h}').match('foo.o')
|
29
|
+
|
30
|
+
## \escaping
|
31
|
+
assert_not_nil SG.regexp('foo.\\{c,h}\\*').match('foo.{c,h}*')
|
32
|
+
assert_nil SG.regexp('foo.\\{c,h}\\*').match('foo.\\c')
|
33
|
+
|
34
|
+
## escape ()
|
35
|
+
assert_not_nil SG.regexp('foo.(bar)').match('foo.(bar)')
|
36
|
+
|
37
|
+
## strict . rule fail
|
38
|
+
assert_nil SG.regexp('*.foo').match('.file.foo')
|
39
|
+
## strict . rule match
|
40
|
+
assert_not_nil SG.regexp('.*.foo').match('.file.foo')
|
41
|
+
## relaxed . rule
|
42
|
+
assert_not_nil SG.regexp('*.foo', SG::NO_STRICT_LEADING_DOT).match('.file.foo')
|
43
|
+
|
44
|
+
## strict wildcard / fail
|
45
|
+
assert_nil SG.regexp('*.fo?').match('foo/file.fob')
|
46
|
+
## strict wildcard / match
|
47
|
+
assert_not_nil SG.regexp('*/*.fo?').match('foo/file.fob')
|
48
|
+
## relaxed wildcard /
|
49
|
+
assert_not_nil SG.regexp('*.fo?', SG::NO_STRICT_WILDCARD_SLASH).match('foo/file.fob')
|
50
|
+
|
51
|
+
## more strict wildcard / fail
|
52
|
+
assert_nil SG.regexp('foo/*.foo').match('foo/.foo')
|
53
|
+
## more strict wildcard / match
|
54
|
+
assert_not_nil SG.regexp('foo/.f*').match('foo/.foo')
|
55
|
+
## relaxed wildcard /
|
56
|
+
assert_not_nil SG.regexp('*.foo', SG::NO_STRICT_WILDCARD_SLASH).match('foo/.foo')
|
57
|
+
|
58
|
+
## properly escape +
|
59
|
+
assert_not_nil SG.regexp('f+.foo').match('f+.foo')
|
60
|
+
assert_nil SG.regexp('f+.foo').match('ffff.foo')
|
61
|
+
|
62
|
+
## handle embedded \\n
|
63
|
+
assert_not_nil SG.regexp("foo\nbar").match("foo\nbar")
|
64
|
+
assert_nil SG.regexp("foo\nbar").match("foobar")
|
65
|
+
|
66
|
+
## [abc]
|
67
|
+
assert_not_nil SG.regexp('test[abc]').match('testa')
|
68
|
+
assert_not_nil SG.regexp('test[abc]').match('testb')
|
69
|
+
assert_not_nil SG.regexp('test[abc]').match('testc')
|
70
|
+
assert_nil SG.regexp('test[abc]').match('testd')
|
71
|
+
|
72
|
+
## escaping \$
|
73
|
+
assert_not_nil SG.regexp('foo$bar.*').match('foo$bar.c')
|
74
|
+
|
75
|
+
## escaping ^
|
76
|
+
assert_not_nil SG.regexp('foo^bar.*').match('foo^bar.c')
|
77
|
+
|
78
|
+
## escaping |
|
79
|
+
assert_not_nil SG.regexp('foo|bar.*').match('foo|bar.c')
|
80
|
+
|
81
|
+
|
82
|
+
## {foo,{bar,baz}}
|
83
|
+
assert_not_nil SG.regexp('{foo,{bar,baz}}').match('foo')
|
84
|
+
assert_not_nil SG.regexp('{foo,{bar,baz}}').match('bar')
|
85
|
+
assert_not_nil SG.regexp('{foo,{bar,baz}}').match('baz')
|
86
|
+
assert_nil SG.regexp('{foo,{bar,baz}}').match('foz')
|
87
|
+
|
88
|
+
## @ character
|
89
|
+
assert_not_nil SG.regexp('foo@bar').match('foo@bar')
|
90
|
+
## $ character
|
91
|
+
assert_not_nil SG.regexp('foo$bar').match('foo$bar')
|
92
|
+
## % character
|
93
|
+
assert_not_nil SG.regexp('foo%bar').match('foo%bar')
|
94
|
+
|
95
|
+
## ** wildcard and path components
|
96
|
+
assert_not_nil SG.regexp('foo/*/baz').match('foo/bar/baz')
|
97
|
+
assert_nil SG.regexp('foo/*/baz').match('foo/bar/bar/baz')
|
98
|
+
assert_nil SG.regexp('foo/**/baz').match('foo/baz')
|
99
|
+
assert_not_nil SG.regexp('foo/**/baz').match('foo/bar/baz')
|
100
|
+
assert_not_nil SG.regexp('foo/**/baz').match('foo/bar/bar/baz')
|
101
|
+
assert_nil SG.regexp('foo/**').match('foo')
|
102
|
+
assert_not_nil SG.regexp('foo/**').match('foo/bar')
|
103
|
+
assert_not_nil SG.regexp('foo/**').match('foo/bar/baz')
|
104
|
+
assert_nil SG.regexp('**/foo').match('foo')
|
105
|
+
assert_not_nil SG.regexp('**/foo').match('bar/foo')
|
106
|
+
assert_not_nil SG.regexp('**/foo').match('baz/bar/foo')
|
107
|
+
## escaping ** wildcard
|
108
|
+
assert_nil SG.regexp('foo/\\**/baz').match('foo/bar/baz')
|
109
|
+
assert_not_nil SG.regexp('foo/\\**/baz').match('foo/*bar/baz')
|
110
|
+
assert_nil SG.regexp('foo/*\\*/baz').match('foo/bar/baz')
|
111
|
+
assert_not_nil SG.regexp('foo/*\\*/baz').match('foo/bar*/baz')
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: stringglob
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- SATOH Fumiyasu
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-09-26 00:00:00 Z
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Generate a Regexp object from a glob(3) pattern
|
22
|
+
email:
|
23
|
+
- fumiyas@osstech.co.jp
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- .gitignore
|
32
|
+
- Gemfile
|
33
|
+
- Rakefile
|
34
|
+
- lib/stringglob.rb
|
35
|
+
- lib/stringglob/version.rb
|
36
|
+
- stringglob.gemspec
|
37
|
+
- test/test_stringglob.rb
|
38
|
+
homepage: ""
|
39
|
+
licenses: []
|
40
|
+
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
hash: 3
|
52
|
+
segments:
|
53
|
+
- 0
|
54
|
+
version: "0"
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
hash: 3
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
version: "0"
|
64
|
+
requirements: []
|
65
|
+
|
66
|
+
rubyforge_project: stringglob
|
67
|
+
rubygems_version: 1.7.2
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: Generate a Regexp object from a glob(3) pattern
|
71
|
+
test_files:
|
72
|
+
- test/test_stringglob.rb
|