fzy 0.1.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.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +25 -0
- data/LICENSE.txt +21 -0
- data/README.md +60 -0
- data/Rakefile +17 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/ext/config.h +8 -0
- data/ext/fzy/bonus.h +108 -0
- data/ext/fzy/extconf.rb +3 -0
- data/ext/fzy/fzy.c +50 -0
- data/ext/fzy/fzy.h +6 -0
- data/ext/fzy/match.c +181 -0
- data/ext/fzy/match.h +14 -0
- data/fzy.gemspec +31 -0
- data/lib/fzy.rb +27 -0
- data/lib/fzy/version.rb +3 -0
- metadata +119 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3d9b0ca699907c027e4cc3196971d3bb6cadd90e84d7cbb3e715ab6646560c0f
|
4
|
+
data.tar.gz: 52efaf9f44e76b5c15cd71e2b7b26aaa7364e290de28d0658bf149e282bed2d3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6199c4ec774da59b321ca878d7bc60cd9109fd3ceb7b56b785a74e6ba43471dd65a972bb09705e93dc7612d750e2363171265cb4c57bde4d0b9db5ce2a5359db
|
7
|
+
data.tar.gz: bf7d4f1f30ad6fe4050dfed9407fc8e3ebb08f11e4d190dab69476c714819bd4212d9798b32f720a1ef9f1a6f50a418a8e73304d33441627d59a6aa6e49e530b
|
data/.gitignore
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at john@hawthorn.email. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
fzy (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
minitest (5.13.0)
|
10
|
+
rake (10.5.0)
|
11
|
+
rake-compiler (1.0.8)
|
12
|
+
rake
|
13
|
+
|
14
|
+
PLATFORMS
|
15
|
+
ruby
|
16
|
+
|
17
|
+
DEPENDENCIES
|
18
|
+
bundler (~> 1.17)
|
19
|
+
fzy!
|
20
|
+
minitest
|
21
|
+
rake (~> 10.0)
|
22
|
+
rake-compiler
|
23
|
+
|
24
|
+
BUNDLED WITH
|
25
|
+
1.17.3
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 John Hawthorn
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# fzy.rb
|
2
|
+
|
3
|
+
A Ruby port of [fzy](https://github.com/jhawthorn/fzy)'s fuzzy finder scoring algorithm
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'fzy'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install fzy
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
`Fzy.has_match?(needle, haystack)`
|
24
|
+
|
25
|
+
``` ruby
|
26
|
+
Fzy.has_match?("rb", "ruby") # => true
|
27
|
+
Fzy.has_match?("rb", "bar") # => false
|
28
|
+
```
|
29
|
+
|
30
|
+
`Fzy.score(needle, haystack)`
|
31
|
+
|
32
|
+
``` ruby
|
33
|
+
Fzy.score("amuser", "app/models/user.rb") # => 5.595
|
34
|
+
Fzy.score("amuser", "app/models/customer.rb") # => 3.655
|
35
|
+
```
|
36
|
+
|
37
|
+
`Fzy.positions(needle, haystack)`
|
38
|
+
|
39
|
+
``` ruby
|
40
|
+
Fzy.positions("amuser", "app/models/user.rb") # => [ 0, 4, 11, 12, 13, 14 ]
|
41
|
+
Fzy.positions("amuser", "app/models/customer.rb") # => [ 0, 4, 12, 13, 17, 18 ]
|
42
|
+
```
|
43
|
+
|
44
|
+
## Development
|
45
|
+
|
46
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
47
|
+
|
48
|
+
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).
|
49
|
+
|
50
|
+
## Contributing
|
51
|
+
|
52
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/jhawthorn/fzy. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
53
|
+
|
54
|
+
## License
|
55
|
+
|
56
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
57
|
+
|
58
|
+
## Code of Conduct
|
59
|
+
|
60
|
+
Everyone interacting in the Fzy project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/jhawthorn/fzy/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rake/extensiontask"
|
3
|
+
require "rake/testtask"
|
4
|
+
|
5
|
+
task :build => :compile
|
6
|
+
|
7
|
+
Rake::ExtensionTask.new("fzy") do |ext|
|
8
|
+
ext.lib_dir = "lib/fzy"
|
9
|
+
end
|
10
|
+
|
11
|
+
Rake::TestTask.new(:test) do |t|
|
12
|
+
t.libs << "test"
|
13
|
+
t.libs << "lib"
|
14
|
+
t.test_files = p(FileList["test/**/*_test.rb"])
|
15
|
+
end
|
16
|
+
|
17
|
+
task :default => [:clobber, :compile, :test]
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "fzy"
|
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(__FILE__)
|
data/bin/setup
ADDED
data/ext/config.h
ADDED
data/ext/fzy/bonus.h
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
#ifndef BONUS_H
|
2
|
+
#define BONUS_H BONUS_H
|
3
|
+
|
4
|
+
#include "../config.h"
|
5
|
+
|
6
|
+
#define ASSIGN_LOWER(v) \
|
7
|
+
['a'] = (v), \
|
8
|
+
['b'] = (v), \
|
9
|
+
['c'] = (v), \
|
10
|
+
['d'] = (v), \
|
11
|
+
['e'] = (v), \
|
12
|
+
['f'] = (v), \
|
13
|
+
['g'] = (v), \
|
14
|
+
['h'] = (v), \
|
15
|
+
['i'] = (v), \
|
16
|
+
['j'] = (v), \
|
17
|
+
['k'] = (v), \
|
18
|
+
['l'] = (v), \
|
19
|
+
['m'] = (v), \
|
20
|
+
['n'] = (v), \
|
21
|
+
['o'] = (v), \
|
22
|
+
['p'] = (v), \
|
23
|
+
['q'] = (v), \
|
24
|
+
['r'] = (v), \
|
25
|
+
['s'] = (v), \
|
26
|
+
['t'] = (v), \
|
27
|
+
['u'] = (v), \
|
28
|
+
['v'] = (v), \
|
29
|
+
['w'] = (v), \
|
30
|
+
['x'] = (v), \
|
31
|
+
['y'] = (v), \
|
32
|
+
['z'] = (v)
|
33
|
+
|
34
|
+
#define ASSIGN_UPPER(v) \
|
35
|
+
['A'] = (v), \
|
36
|
+
['B'] = (v), \
|
37
|
+
['C'] = (v), \
|
38
|
+
['D'] = (v), \
|
39
|
+
['E'] = (v), \
|
40
|
+
['F'] = (v), \
|
41
|
+
['G'] = (v), \
|
42
|
+
['H'] = (v), \
|
43
|
+
['I'] = (v), \
|
44
|
+
['J'] = (v), \
|
45
|
+
['K'] = (v), \
|
46
|
+
['L'] = (v), \
|
47
|
+
['M'] = (v), \
|
48
|
+
['N'] = (v), \
|
49
|
+
['O'] = (v), \
|
50
|
+
['P'] = (v), \
|
51
|
+
['Q'] = (v), \
|
52
|
+
['R'] = (v), \
|
53
|
+
['S'] = (v), \
|
54
|
+
['T'] = (v), \
|
55
|
+
['U'] = (v), \
|
56
|
+
['V'] = (v), \
|
57
|
+
['W'] = (v), \
|
58
|
+
['X'] = (v), \
|
59
|
+
['Y'] = (v), \
|
60
|
+
['Z'] = (v)
|
61
|
+
|
62
|
+
#define ASSIGN_DIGIT(v) \
|
63
|
+
['0'] = (v), \
|
64
|
+
['1'] = (v), \
|
65
|
+
['2'] = (v), \
|
66
|
+
['3'] = (v), \
|
67
|
+
['4'] = (v), \
|
68
|
+
['5'] = (v), \
|
69
|
+
['6'] = (v), \
|
70
|
+
['7'] = (v), \
|
71
|
+
['8'] = (v), \
|
72
|
+
['9'] = (v)
|
73
|
+
|
74
|
+
const score_t bonus_states[3][256] = {
|
75
|
+
{ 0 },
|
76
|
+
{
|
77
|
+
['/'] = SCORE_MATCH_SLASH,
|
78
|
+
['-'] = SCORE_MATCH_WORD,
|
79
|
+
['_'] = SCORE_MATCH_WORD,
|
80
|
+
[' '] = SCORE_MATCH_WORD,
|
81
|
+
['.'] = SCORE_MATCH_DOT,
|
82
|
+
},
|
83
|
+
{
|
84
|
+
['/'] = SCORE_MATCH_SLASH,
|
85
|
+
['-'] = SCORE_MATCH_WORD,
|
86
|
+
['_'] = SCORE_MATCH_WORD,
|
87
|
+
[' '] = SCORE_MATCH_WORD,
|
88
|
+
['.'] = SCORE_MATCH_DOT,
|
89
|
+
|
90
|
+
/* ['a' ... 'z'] = SCORE_MATCH_CAPITAL, */
|
91
|
+
ASSIGN_LOWER(SCORE_MATCH_CAPITAL)
|
92
|
+
}
|
93
|
+
};
|
94
|
+
|
95
|
+
const size_t bonus_index[256] = {
|
96
|
+
/* ['A' ... 'Z'] = 2 */
|
97
|
+
ASSIGN_UPPER(2),
|
98
|
+
|
99
|
+
/* ['a' ... 'z'] = 1 */
|
100
|
+
ASSIGN_LOWER(1),
|
101
|
+
|
102
|
+
/* ['0' ... '9'] = 1 */
|
103
|
+
ASSIGN_DIGIT(1)
|
104
|
+
};
|
105
|
+
|
106
|
+
#define COMPUTE_BONUS(last_ch, ch) (bonus_states[bonus_index[(unsigned char)(ch)]][(unsigned char)(last_ch)])
|
107
|
+
|
108
|
+
#endif
|
data/ext/fzy/extconf.rb
ADDED
data/ext/fzy/fzy.c
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
#include "fzy.h"
|
2
|
+
#include "match.h"
|
3
|
+
|
4
|
+
static VALUE rb_mFzy;
|
5
|
+
|
6
|
+
static VALUE
|
7
|
+
rb_has_match(VALUE _, VALUE needle, VALUE haystack) {
|
8
|
+
const char *needle_str = StringValueCStr(needle);
|
9
|
+
const char *haystack_str = StringValueCStr(haystack);
|
10
|
+
int match = has_match(needle_str, haystack_str);
|
11
|
+
|
12
|
+
return match ? Qtrue : Qfalse;
|
13
|
+
}
|
14
|
+
|
15
|
+
static VALUE
|
16
|
+
rb_score(VALUE _, VALUE needle, VALUE haystack) {
|
17
|
+
const char *needle_str = StringValueCStr(needle);
|
18
|
+
const char *haystack_str = StringValueCStr(haystack);
|
19
|
+
double score = match(needle_str, haystack_str);
|
20
|
+
|
21
|
+
return DBL2NUM(score);
|
22
|
+
}
|
23
|
+
|
24
|
+
static VALUE
|
25
|
+
rb_positions(VALUE _, VALUE needle, VALUE haystack) {
|
26
|
+
const char *needle_str = StringValueCStr(needle);
|
27
|
+
const char *haystack_str = StringValueCStr(haystack);
|
28
|
+
size_t size = RSTRING_LEN(needle);
|
29
|
+
VALUE arr = rb_ary_new2(size);
|
30
|
+
|
31
|
+
size_t *positions = xcalloc(size, sizeof(size_t));
|
32
|
+
match_positions(needle_str, haystack_str, positions);
|
33
|
+
|
34
|
+
for (size_t i = 0; i < size; i++) {
|
35
|
+
rb_ary_push(arr, ULL2NUM(positions[i]));
|
36
|
+
}
|
37
|
+
|
38
|
+
xfree(positions);
|
39
|
+
return arr;
|
40
|
+
}
|
41
|
+
|
42
|
+
void
|
43
|
+
Init_fzy(void)
|
44
|
+
{
|
45
|
+
rb_mFzy = rb_define_module("Fzy");
|
46
|
+
|
47
|
+
rb_define_module_function(rb_mFzy, "has_match?", rb_has_match, 2);
|
48
|
+
rb_define_module_function(rb_mFzy, "score", rb_score, 2);
|
49
|
+
rb_define_module_function(rb_mFzy, "positions", rb_positions, 2);
|
50
|
+
}
|
data/ext/fzy/fzy.h
ADDED
data/ext/fzy/match.c
ADDED
@@ -0,0 +1,181 @@
|
|
1
|
+
#include <ctype.h>
|
2
|
+
#include <string.h>
|
3
|
+
#include <strings.h>
|
4
|
+
#include <stdio.h>
|
5
|
+
#include <float.h>
|
6
|
+
#include <math.h>
|
7
|
+
|
8
|
+
#include "match.h"
|
9
|
+
#include "bonus.h"
|
10
|
+
|
11
|
+
#include "../config.h"
|
12
|
+
|
13
|
+
char *strcasechr(const char *s, char c) {
|
14
|
+
const char accept[3] = {c, toupper(c), 0};
|
15
|
+
return strpbrk(s, accept);
|
16
|
+
}
|
17
|
+
|
18
|
+
int has_match(const char *needle, const char *haystack) {
|
19
|
+
while (*needle) {
|
20
|
+
char nch = *needle++;
|
21
|
+
|
22
|
+
if (!(haystack = strcasechr(haystack, nch))) {
|
23
|
+
return 0;
|
24
|
+
}
|
25
|
+
haystack++;
|
26
|
+
}
|
27
|
+
return 1;
|
28
|
+
}
|
29
|
+
|
30
|
+
#define max(a, b) (((a) > (b)) ? (a) : (b))
|
31
|
+
|
32
|
+
#ifdef DEBUG_VERBOSE
|
33
|
+
/* print one of the internal matrices */
|
34
|
+
void mat_print(score_t *mat, char name, const char *needle, const char *haystack) {
|
35
|
+
int n = strlen(needle);
|
36
|
+
int m = strlen(haystack);
|
37
|
+
int i, j;
|
38
|
+
fprintf(stderr, "%c ", name);
|
39
|
+
for (j = 0; j < m; j++) {
|
40
|
+
fprintf(stderr, " %c", haystack[j]);
|
41
|
+
}
|
42
|
+
fprintf(stderr, "\n");
|
43
|
+
for (i = 0; i < n; i++) {
|
44
|
+
fprintf(stderr, " %c |", needle[i]);
|
45
|
+
for (j = 0; j < m; j++) {
|
46
|
+
score_t val = mat[i * m + j];
|
47
|
+
if (val == SCORE_MIN) {
|
48
|
+
fprintf(stderr, " -\u221E");
|
49
|
+
} else {
|
50
|
+
fprintf(stderr, " %.3f", val);
|
51
|
+
}
|
52
|
+
}
|
53
|
+
fprintf(stderr, "\n");
|
54
|
+
}
|
55
|
+
fprintf(stderr, "\n\n");
|
56
|
+
}
|
57
|
+
#endif
|
58
|
+
|
59
|
+
static void precompute_bonus(const char *haystack, score_t *match_bonus) {
|
60
|
+
/* Which positions are beginning of words */
|
61
|
+
int m = strlen(haystack);
|
62
|
+
char last_ch = '/';
|
63
|
+
for (int i = 0; i < m; i++) {
|
64
|
+
char ch = haystack[i];
|
65
|
+
match_bonus[i] = COMPUTE_BONUS(last_ch, ch);
|
66
|
+
last_ch = ch;
|
67
|
+
}
|
68
|
+
}
|
69
|
+
|
70
|
+
score_t match_positions(const char *needle, const char *haystack, size_t *positions) {
|
71
|
+
if (!*needle)
|
72
|
+
return SCORE_MIN;
|
73
|
+
|
74
|
+
int n = strlen(needle);
|
75
|
+
int m = strlen(haystack);
|
76
|
+
|
77
|
+
if (n == m) {
|
78
|
+
/* Since this method can only be called with a haystack which
|
79
|
+
* matches needle. If the lengths of the strings are equal the
|
80
|
+
* strings themselves must also be equal (ignoring case).
|
81
|
+
*/
|
82
|
+
if (positions)
|
83
|
+
for (int i = 0; i < n; i++)
|
84
|
+
positions[i] = i;
|
85
|
+
return SCORE_MAX;
|
86
|
+
}
|
87
|
+
|
88
|
+
if (m > 1024) {
|
89
|
+
/*
|
90
|
+
* Unreasonably large candidate: return no score
|
91
|
+
* If it is a valid match it will still be returned, it will
|
92
|
+
* just be ranked below any reasonably sized candidates
|
93
|
+
*/
|
94
|
+
return SCORE_MIN;
|
95
|
+
}
|
96
|
+
|
97
|
+
char lower_needle[n];
|
98
|
+
char lower_haystack[m];
|
99
|
+
|
100
|
+
for (int i = 0; i < n; i++)
|
101
|
+
lower_needle[i] = tolower(needle[i]);
|
102
|
+
|
103
|
+
for (int i = 0; i < m; i++)
|
104
|
+
lower_haystack[i] = tolower(haystack[i]);
|
105
|
+
|
106
|
+
score_t match_bonus[m];
|
107
|
+
score_t D[n][m], M[n][m];
|
108
|
+
|
109
|
+
/*
|
110
|
+
* D[][] Stores the best score for this position ending with a match.
|
111
|
+
* M[][] Stores the best possible score at this position.
|
112
|
+
*/
|
113
|
+
precompute_bonus(haystack, match_bonus);
|
114
|
+
|
115
|
+
for (int i = 0; i < n; i++) {
|
116
|
+
score_t prev_score = SCORE_MIN;
|
117
|
+
score_t gap_score = i == n - 1 ? SCORE_GAP_TRAILING : SCORE_GAP_INNER;
|
118
|
+
|
119
|
+
for (int j = 0; j < m; j++) {
|
120
|
+
if (lower_needle[i] == lower_haystack[j]) {
|
121
|
+
score_t score = SCORE_MIN;
|
122
|
+
if (!i) {
|
123
|
+
score = (j * SCORE_GAP_LEADING) + match_bonus[j];
|
124
|
+
} else if (j) { /* i > 0 && j > 0*/
|
125
|
+
score = max(
|
126
|
+
M[i - 1][j - 1] + match_bonus[j],
|
127
|
+
|
128
|
+
/* consecutive match, doesn't stack with match_bonus */
|
129
|
+
D[i - 1][j - 1] + SCORE_MATCH_CONSECUTIVE);
|
130
|
+
}
|
131
|
+
D[i][j] = score;
|
132
|
+
M[i][j] = prev_score = max(score, prev_score + gap_score);
|
133
|
+
} else {
|
134
|
+
D[i][j] = SCORE_MIN;
|
135
|
+
M[i][j] = prev_score = prev_score + gap_score;
|
136
|
+
}
|
137
|
+
}
|
138
|
+
}
|
139
|
+
|
140
|
+
#ifdef DEBUG_VERBOSE
|
141
|
+
fprintf(stderr, "\"%s\" =~ \"%s\"\n", needle, haystack);
|
142
|
+
mat_print(&D[0][0], 'D', needle, haystack);
|
143
|
+
mat_print(&M[0][0], 'M', needle, haystack);
|
144
|
+
fprintf(stderr, "\n");
|
145
|
+
#endif
|
146
|
+
|
147
|
+
/* backtrace to find the positions of optimal matching */
|
148
|
+
if (positions) {
|
149
|
+
int match_required = 0;
|
150
|
+
for (int i = n - 1, j = m - 1; i >= 0; i--) {
|
151
|
+
for (; j >= 0; j--) {
|
152
|
+
/*
|
153
|
+
* There may be multiple paths which result in
|
154
|
+
* the optimal weight.
|
155
|
+
*
|
156
|
+
* For simplicity, we will pick the first one
|
157
|
+
* we encounter, the latest in the candidate
|
158
|
+
* string.
|
159
|
+
*/
|
160
|
+
if (D[i][j] != SCORE_MIN &&
|
161
|
+
(match_required || D[i][j] == M[i][j])) {
|
162
|
+
/* If this score was determined using
|
163
|
+
* SCORE_MATCH_CONSECUTIVE, the
|
164
|
+
* previous character MUST be a match
|
165
|
+
*/
|
166
|
+
match_required =
|
167
|
+
i && j &&
|
168
|
+
M[i][j] == D[i - 1][j - 1] + SCORE_MATCH_CONSECUTIVE;
|
169
|
+
positions[i] = j--;
|
170
|
+
break;
|
171
|
+
}
|
172
|
+
}
|
173
|
+
}
|
174
|
+
}
|
175
|
+
|
176
|
+
return M[n - 1][m - 1];
|
177
|
+
}
|
178
|
+
|
179
|
+
score_t match(const char *needle, const char *haystack) {
|
180
|
+
return match_positions(needle, haystack, NULL);
|
181
|
+
}
|
data/ext/fzy/match.h
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#ifndef MATCH_H
|
2
|
+
#define MATCH_H MATCH_H
|
3
|
+
|
4
|
+
#include <math.h>
|
5
|
+
|
6
|
+
typedef double score_t;
|
7
|
+
#define SCORE_MAX INFINITY
|
8
|
+
#define SCORE_MIN -INFINITY
|
9
|
+
|
10
|
+
int has_match(const char *needle, const char *haystack);
|
11
|
+
score_t match_positions(const char *needle, const char *haystack, size_t *positions);
|
12
|
+
score_t match(const char *needle, const char *haystack);
|
13
|
+
|
14
|
+
#endif
|
data/fzy.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "fzy/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "fzy"
|
8
|
+
spec.version = Fzy::VERSION
|
9
|
+
spec.authors = ["John Hawthorn"]
|
10
|
+
spec.email = ["john@hawthorn.email"]
|
11
|
+
|
12
|
+
spec.summary = %q{A better fuzzy finder}
|
13
|
+
spec.description = %q{fzy is a fast fuzzy text selector with an advanced scoring algorithm}
|
14
|
+
spec.homepage = "https://github.com/jhawthorn/fzy.js"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
spec.extensions = ["ext/fzy/extconf.rb"]
|
26
|
+
|
27
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
28
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
29
|
+
spec.add_development_dependency "rake-compiler"
|
30
|
+
spec.add_development_dependency "minitest"
|
31
|
+
end
|
data/lib/fzy.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require "fzy/version"
|
2
|
+
require "fzy/fzy"
|
3
|
+
|
4
|
+
module Fzy
|
5
|
+
class Error < StandardError; end
|
6
|
+
|
7
|
+
class Match
|
8
|
+
def initialize(needle, haystack)
|
9
|
+
@needle = needle
|
10
|
+
@haystack = haystack
|
11
|
+
end
|
12
|
+
|
13
|
+
def score
|
14
|
+
@score ||= Fzy.score(@needle, @haystack)
|
15
|
+
end
|
16
|
+
|
17
|
+
def positions
|
18
|
+
@positions ||= Fzy.positions(@needle, @haystack)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.match(needle, haystack)
|
23
|
+
if has_match?(needle, haystack)
|
24
|
+
Match.new(needle, haystack)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/fzy/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fzy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- John Hawthorn
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-11-09 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.17'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.17'
|
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
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake-compiler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: fzy is a fast fuzzy text selector with an advanced scoring algorithm
|
70
|
+
email:
|
71
|
+
- john@hawthorn.email
|
72
|
+
executables: []
|
73
|
+
extensions:
|
74
|
+
- ext/fzy/extconf.rb
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- CODE_OF_CONDUCT.md
|
79
|
+
- Gemfile
|
80
|
+
- Gemfile.lock
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- bin/console
|
85
|
+
- bin/setup
|
86
|
+
- ext/config.h
|
87
|
+
- ext/fzy/bonus.h
|
88
|
+
- ext/fzy/extconf.rb
|
89
|
+
- ext/fzy/fzy.c
|
90
|
+
- ext/fzy/fzy.h
|
91
|
+
- ext/fzy/match.c
|
92
|
+
- ext/fzy/match.h
|
93
|
+
- fzy.gemspec
|
94
|
+
- lib/fzy.rb
|
95
|
+
- lib/fzy/version.rb
|
96
|
+
homepage: https://github.com/jhawthorn/fzy.js
|
97
|
+
licenses:
|
98
|
+
- MIT
|
99
|
+
metadata: {}
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
requirements: []
|
115
|
+
rubygems_version: 3.0.3
|
116
|
+
signing_key:
|
117
|
+
specification_version: 4
|
118
|
+
summary: A better fuzzy finder
|
119
|
+
test_files: []
|