to_regexp 0.0.3

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.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in to_regexp.gemspec
4
+ gemspec
@@ -0,0 +1,15 @@
1
+ =to_regexp
2
+
3
+ Basically a safe way to convert strings to regexps (with options).
4
+
5
+ str = "/finalis(é)/im"
6
+ old_way = eval(str) # not safe
7
+ new_way = str.to_regexp # provided by this gem
8
+ old_way == new_way # true
9
+
10
+ You can get the options passed to <tt>Regexp.new</tt> with
11
+
12
+ str.as_regexp # a hash of options passed to Regexp.new
13
+ str.to_regexp == Regexp.new(str.as_regexp) # true
14
+
15
+ Copyright 2011 Seamus Abshere
@@ -0,0 +1,25 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake'
5
+ require 'rake/testtask'
6
+ Rake::TestTask.new(:test) do |test|
7
+ test.libs << 'lib' << 'test'
8
+ test.pattern = 'test/**/test_*.rb'
9
+ test.verbose = true
10
+ end
11
+
12
+ task :default => :test
13
+
14
+ begin
15
+ require 'rake/rdoctask'
16
+ Rake::RDocTask.new do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'remote_table'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README*')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
23
+ rescue LoadError
24
+ puts "Rdoc is not available"
25
+ end
@@ -0,0 +1,55 @@
1
+ # encoding: utf-8
2
+ module ToRegexp
3
+ module Regexp
4
+ def to_regexp
5
+ self
6
+ end
7
+ end
8
+
9
+ module String
10
+ REGEXP_DELIMITERS = {
11
+ '%r{' => '}',
12
+ '/' => '/',
13
+ }
14
+
15
+ def to_regexp
16
+ if args = as_regexp
17
+ ::Regexp.new *args
18
+ end
19
+ end
20
+
21
+ def as_regexp
22
+ str = self.dup
23
+ unless delim_set = REGEXP_DELIMITERS.detect { |k, v| str.start_with? k }
24
+ # no starting delimiter found
25
+ return
26
+ end
27
+ delim_start, delim_end = delim_set.map { |delim| ::Regexp.escape delim }
28
+ /\A#{delim_start}(.*)#{delim_end}([^#{delim_end}]*)\z/u =~ str.strip
29
+ content = $1
30
+ options = $2
31
+ unless content.is_a?(::String) and options.is_a?(::String)
32
+ # maybe a missing end delimiter?
33
+ return
34
+ end
35
+ content.gsub! '\\/', '/'
36
+ ignore_case = options.include?('i') ? ::Regexp::IGNORECASE : 0
37
+ multiline = options.include?('m') ? ::Regexp::MULTILINE : 0
38
+ extended = options.include?('x') ? ::Regexp::EXTENDED : 0
39
+ # 'n', 'N' = none, 'e', 'E' = EUC, 's', 'S' = SJIS, 'u', 'U' = UTF-8
40
+ lang = options.scan(/[nesu]/i).join.downcase
41
+ if ::RUBY_VERSION > '1.9' and lang.include?('u')
42
+ lang.gsub! 'u', ''
43
+ end
44
+ if lang.empty?
45
+ [ content, (ignore_case|multiline|extended) ]
46
+ else
47
+ [ content, (ignore_case|multiline|extended), lang ]
48
+ end
49
+ end
50
+ end
51
+
52
+ end
53
+
54
+ ::String.send :include, ::ToRegexp::String
55
+ ::Regexp.send :include, ::ToRegexp::Regexp
@@ -0,0 +1,3 @@
1
+ module ToRegexp
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,11 @@
1
+ unless RUBY_VERSION >= '1.9'
2
+ require 'rubygems'
3
+ end
4
+ require 'bundler'
5
+ Bundler.setup
6
+ require 'test/unit'
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
9
+ require 'to_regexp'
10
+ class Test::Unit::TestCase
11
+ end
@@ -0,0 +1,80 @@
1
+ # encoding: UTF-8
2
+ require 'helper'
3
+
4
+ class TestToRegexp < Test::Unit::TestCase
5
+ def test_000_versus_eval_ascii
6
+ str = "/finalis(e)/im"
7
+ old_way = eval(str)
8
+ new_way = str.to_regexp
9
+ assert_equal old_way, new_way
10
+ end
11
+
12
+ def test_000a_versus_eval_utf8
13
+ str = "/finalis(é)/im"
14
+ old_way = eval(str)
15
+ new_way = str.to_regexp
16
+ assert_equal old_way, new_way
17
+ end
18
+
19
+ def test_001_utf8
20
+ assert_equal 'ë', '/(ë)/'.to_regexp.match('Citroën').captures[0]
21
+ end
22
+
23
+ def test_002_multiline
24
+ assert_equal nil, '/foo.*(bar)/'.to_regexp.match("foo\n\nbar")
25
+ assert_equal 'bar', '/foo.*(bar)/m'.to_regexp.match("foo\n\nbar").captures[0]
26
+ end
27
+
28
+ def test_003_ignore_case
29
+ assert_equal nil, '/(FOO)/'.to_regexp.match('foo')
30
+ assert_equal 'foo', '/(FOO)/i'.to_regexp.match('foo').captures[0]
31
+ end
32
+
33
+ def test_004_percentage_r_notation
34
+ assert_equal '/', '%r{(/)}'.to_regexp.match('/').captures[0]
35
+ end
36
+
37
+ def test_005_multiline_and_ignore_case
38
+ assert_equal 'bar', '/foo.*(bar)/mi'.to_regexp.match("foo\n\nbar").captures[0]
39
+ end
40
+
41
+ def test_006_cant_fix_garbled_input
42
+ if RUBY_VERSION >= '1.9'
43
+ garbled = 'finalisé'.force_encoding('ASCII-8BIT') # like if it was misinterpreted
44
+ assert_raises(Encoding::CompatibilityError) do
45
+ '/finalis(é)/'.to_regexp.match(garbled)
46
+ end
47
+ else # not applicable to ruby 1.8
48
+ garbled = 'finalisé'
49
+ assert_nothing_raised do
50
+ '/finalis(é)/'.to_regexp.match(garbled)
51
+ end
52
+ end
53
+ end
54
+
55
+ def test_007_possible_garbled_input_fix_using_manfreds_gem
56
+ if RUBY_VERSION >= '1.9'
57
+ require 'ensure/encoding'
58
+ garbled = 'finalisé'.force_encoding('ASCII-8BIT') # like if it was misinterpreted
59
+ assert_equal 'é', '/finalis(é)/'.to_regexp.match(garbled.ensure_encoding('UTF-8')).captures[0]
60
+ else # not applicable to ruby 1.8
61
+ garbled = 'finalisé'
62
+ assert_equal 'é', '/finalis(é)/'.to_regexp.match(garbled).captures[0]
63
+ end
64
+ end
65
+
66
+ def test_008_as_regexp
67
+ str = '/finalis(é)/in'
68
+ assert_equal ['finalis(é)', ::Regexp::IGNORECASE, 'n'], str.as_regexp
69
+ assert_equal Regexp.new(*str.as_regexp), str.to_regexp
70
+ end
71
+
72
+ def test_009_ruby_19_splat
73
+ assert_equal nil, 'hi'.to_regexp
74
+ end
75
+
76
+ def test_010_regexp_to_regexp
77
+ a = /foo/
78
+ assert_equal a, a.to_regexp
79
+ end
80
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "to_regexp/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "to_regexp"
7
+ s.version = ToRegexp::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Seamus Abshere"]
10
+ s.email = ["seamus@abshere.net"]
11
+ s.homepage = "https://github.com/seamusabshere/to_regexp"
12
+ s.summary = %q{Provides String#to_regexp}
13
+ s.description = %q{Provides String#to_regexp, for example if you want to make regexps out of a CSV you just imported.}
14
+
15
+ s.rubyforge_project = "to_regexp"
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 'ensure-encoding'
23
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: to_regexp
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 3
10
+ version: 0.0.3
11
+ platform: ruby
12
+ authors:
13
+ - Seamus Abshere
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-04-27 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: ensure-encoding
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :development
33
+ version_requirements: *id001
34
+ description: Provides String#to_regexp, for example if you want to make regexps out of a CSV you just imported.
35
+ email:
36
+ - seamus@abshere.net
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - .gitignore
45
+ - Gemfile
46
+ - README.rdoc
47
+ - Rakefile
48
+ - lib/to_regexp.rb
49
+ - lib/to_regexp/version.rb
50
+ - test/helper.rb
51
+ - test/test_to_regexp.rb
52
+ - to_regexp.gemspec
53
+ homepage: https://github.com/seamusabshere/to_regexp
54
+ licenses: []
55
+
56
+ post_install_message:
57
+ rdoc_options: []
58
+
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ hash: 3
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ requirements: []
80
+
81
+ rubyforge_project: to_regexp
82
+ rubygems_version: 1.7.2
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: Provides String#to_regexp
86
+ test_files:
87
+ - test/helper.rb
88
+ - test/test_to_regexp.rb