boyer_moore 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/README.md +36 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/boyer_moore.gemspec +31 -0
- data/lib/boyer_moore.rb +108 -0
- data/lib/boyer_moore/version.rb +3 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: febf5fb6612c4d0940bf38e1c049bd1ecb2e6995
|
4
|
+
data.tar.gz: e893c4c88b3ba12e0620c1a2fdba9ffe13f4ca7b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bdeb9a0af899395f80f7100b9e77aaa568a931d8facd9bf47cf18780d60db77aff5a1b11978fe54e2bdef9de4ce7f2b1ae03b8ce42085be094495beed3f11b9e
|
7
|
+
data.tar.gz: 015ee98265c8e787a355fac0845424f148a9afbc954d50311dffb00718584a5b470dfad16dedacf89ba0c0bec41d12dfe9db97d94ca224fb7cf1f06cc2f117f3
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# BoyerMoore
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/boyer_moore`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'boyer_moore'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install boyer_moore
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/boyer_moore.
|
36
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "boyer_moore"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/boyer_moore.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'boyer_moore/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "boyer_moore"
|
8
|
+
spec.version = BoyerMoore::VERSION
|
9
|
+
spec.authors = ["Sri Harsha Kappala"]
|
10
|
+
spec.email = ["sriharsha.kappala@hotmail.com"]
|
11
|
+
spec.licenses = ['MIT']
|
12
|
+
spec.summary = "Ruby wrapper for BoyerMoore algorithm - The fastest search strategy, ever!"
|
13
|
+
spec.description = "Ruby wrapper for Boyer-Moore - The fastest search strategy, ever!"
|
14
|
+
spec.homepage = "https://github.com/sriharshakappala/boyer_moore"
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
17
|
+
# delete this section to allow pushing this gem to any host.
|
18
|
+
if spec.respond_to?(:metadata)
|
19
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
20
|
+
else
|
21
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
22
|
+
end
|
23
|
+
|
24
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
|
29
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
30
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
31
|
+
end
|
data/lib/boyer_moore.rb
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
require "boyer_moore/version"
|
2
|
+
|
3
|
+
class RichHash
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@regexps = {}
|
7
|
+
@regular = {}
|
8
|
+
end
|
9
|
+
|
10
|
+
def [](k)
|
11
|
+
regular = @regular[k]
|
12
|
+
return regular if regular
|
13
|
+
if @regexps.size > 0
|
14
|
+
@regexps.each do |regex,v|
|
15
|
+
return v if regex.match(k)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
nil
|
19
|
+
end
|
20
|
+
|
21
|
+
def []=(k,v)
|
22
|
+
if k.kind_of?(Regexp)
|
23
|
+
@regexps[k] = v
|
24
|
+
else
|
25
|
+
@regular[k] = v
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
module BoyerMoore
|
32
|
+
|
33
|
+
def self.search(haystack, needle)
|
34
|
+
needle_len = needle.size
|
35
|
+
haystack_len = haystack.size
|
36
|
+
return nil if haystack_len == 0
|
37
|
+
return haystack if needle_len == 0
|
38
|
+
badcharacter = self.prepare_badcharacter_heuristic(needle)
|
39
|
+
goodsuffix = self.prepare_goodsuffix_heuristic(needle)
|
40
|
+
s = 0
|
41
|
+
while s <= haystack_len - needle_len
|
42
|
+
j = needle_len
|
43
|
+
while (j > 0) && self.needle_matches?(needle[j-1], haystack[s+j-1])
|
44
|
+
j -= 1
|
45
|
+
end
|
46
|
+
if(j > 0)
|
47
|
+
k = badcharacter[haystack[s+j-1]]
|
48
|
+
k = -1 unless k
|
49
|
+
if (k < j) && (m = j-k-1) > goodsuffix[j]
|
50
|
+
s += m
|
51
|
+
else
|
52
|
+
s += goodsuffix[j]
|
53
|
+
end
|
54
|
+
else
|
55
|
+
return s
|
56
|
+
end
|
57
|
+
end
|
58
|
+
return nil
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.prepare_badcharacter_heuristic(str)
|
62
|
+
result = RichHash.new
|
63
|
+
0.upto(str.length - 1) do |i|
|
64
|
+
result[str[i]] = i
|
65
|
+
end
|
66
|
+
result
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.prepare_goodsuffix_heuristic(normal)
|
70
|
+
size = normal.size
|
71
|
+
result = []
|
72
|
+
reversed = normal.dup.reverse
|
73
|
+
prefix_normal = compute_prefix(normal)
|
74
|
+
prefix_reversed = compute_prefix(reversed)
|
75
|
+
0.upto(size) do |i|
|
76
|
+
result[i] = size - prefix_normal[size-1]
|
77
|
+
end
|
78
|
+
0.upto(size-1) do |i|
|
79
|
+
j = size - prefix_reversed[i]
|
80
|
+
k = i - prefix_reversed[i]+1
|
81
|
+
result[j] = k if result[j] > k
|
82
|
+
end
|
83
|
+
result
|
84
|
+
end
|
85
|
+
|
86
|
+
def self.needle_matches?(needle, haystack)
|
87
|
+
if needle.kind_of?(Regexp)
|
88
|
+
needle.match(haystack) ? true : false
|
89
|
+
else
|
90
|
+
needle == haystack
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def self.compute_prefix(str)
|
95
|
+
size = str.length
|
96
|
+
k = 0
|
97
|
+
result = [0]
|
98
|
+
1.upto(size - 1) do |q|
|
99
|
+
while (k > 0) && (str[k] != str[q])
|
100
|
+
k = result[k-1]
|
101
|
+
end
|
102
|
+
k += 1 if(str[k] == str[q])
|
103
|
+
result[q] = k
|
104
|
+
end
|
105
|
+
result
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: boyer_moore
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sri Harsha Kappala
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: Ruby wrapper for Boyer-Moore - The fastest search strategy, ever!
|
42
|
+
email:
|
43
|
+
- sriharsha.kappala@hotmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".travis.yml"
|
50
|
+
- Gemfile
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- bin/console
|
54
|
+
- bin/setup
|
55
|
+
- boyer_moore.gemspec
|
56
|
+
- lib/boyer_moore.rb
|
57
|
+
- lib/boyer_moore/version.rb
|
58
|
+
homepage: https://github.com/sriharshakappala/boyer_moore
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata:
|
62
|
+
allowed_push_host: https://rubygems.org
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.4.8
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: Ruby wrapper for BoyerMoore algorithm - The fastest search strategy, ever!
|
83
|
+
test_files: []
|