multi_string_replace 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/Gemfile.lock +1 -1
- data/README.md +6 -0
- data/ext/multi_string_replace/aho_trie.c +4 -5
- data/ext/multi_string_replace/ahocorasick.c +11 -1
- data/ext/multi_string_replace/multi_string_replace.c +3 -3
- data/lib/multi_string_replace/version.rb +1 -1
- data/multi_string_replace.gemspec +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3c5074c0fbcc2384a188df631c14c47759bda65a435c3414095068fa77681952
|
4
|
+
data.tar.gz: 8c93d10f3b3e4f04a5316cc39a784933d307e7ebec29f624efc19c0d06a59db6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2e94c9f113f6bbdaac2b1e0d4e94d7a21cd0cfe95ec88e26c0bad4aa277560c0df3ec109e5426316d7dff46e651ac652fa69f87c09a1a6a46c257d8c4654e948
|
7
|
+
data.tar.gz: 47f44d101bc6885d24ef617b073d85204540c8c3e2edbdf0e08d1e041bf32fc435f051d6e12b154565422f2f32575c91692b93a073899180d727a52d2cefacfc
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
|
2
|
+
[![Gem](https://img.shields.io/gem/v/multi_string_replace.svg)](https://rubygems.org/gems/multi_string_replace)
|
3
|
+
|
4
|
+
|
1
5
|
# MultiStringReplace
|
2
6
|
|
3
7
|
A fast multiple string replace library for ruby. Uses a C implementation of the Aho–Corasick Algorithm based
|
@@ -53,6 +57,8 @@ MultiStringReplace 0.260000 0.010000 0.270000 ( 0.277135)
|
|
53
57
|
mreplace 0.260000 0.020000 0.280000 ( 0.281935)
|
54
58
|
```
|
55
59
|
|
60
|
+
Benchmark sources can be found here: <https://github.com/jedld/multi_word_replace/blob/master/bin/benchmark.rb>
|
61
|
+
|
56
62
|
## Development
|
57
63
|
|
58
64
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -43,7 +43,7 @@ bool aho_add_trie_node(struct aho_trie * restrict t, struct aho_text_t * restric
|
|
43
43
|
__aho_trie_node_init(travasal_node->child_list[0]);
|
44
44
|
travasal_node->child_list[0]->text = node_text;
|
45
45
|
travasal_node->child_list[0]->parent = travasal_node;
|
46
|
-
|
46
|
+
travasal_node->child_list[0]->failure_link = &(t->root);
|
47
47
|
travasal_node = travasal_node->child_list[0];
|
48
48
|
continue;
|
49
49
|
}
|
@@ -81,7 +81,7 @@ bool aho_add_trie_node(struct aho_trie * restrict t, struct aho_text_t * restric
|
|
81
81
|
__aho_trie_node_init(child_node);
|
82
82
|
child_node->text = node_text;
|
83
83
|
child_node->parent = travasal_node;
|
84
|
-
|
84
|
+
child_node->failure_link = &(t->root);
|
85
85
|
travasal_node = child_node;
|
86
86
|
}
|
87
87
|
}
|
@@ -99,7 +99,6 @@ bool __aho_connect_link(struct aho_trie_node* p, struct aho_trie_node* q)
|
|
99
99
|
{
|
100
100
|
struct aho_trie_node *pf = NULL;
|
101
101
|
int i = 0;
|
102
|
-
|
103
102
|
/* is root node */
|
104
103
|
if (p->parent == NULL)
|
105
104
|
{
|
@@ -108,6 +107,7 @@ bool __aho_connect_link(struct aho_trie_node* p, struct aho_trie_node* q)
|
|
108
107
|
}
|
109
108
|
|
110
109
|
pf = p->failure_link;
|
110
|
+
|
111
111
|
for (i=0; i < pf->child_count; i++)
|
112
112
|
{
|
113
113
|
/* check child node of failure link(p) */
|
@@ -161,7 +161,6 @@ void aho_connect_link(struct aho_trie * restrict t)
|
|
161
161
|
for (i=0; i < p->child_count; i++)
|
162
162
|
{
|
163
163
|
struct aho_trie_node *pf = p;
|
164
|
-
|
165
164
|
aho_queue_enqueue(&queue, p->child_list[i]);
|
166
165
|
q = p->child_list[i];
|
167
166
|
|
@@ -169,9 +168,9 @@ void aho_connect_link(struct aho_trie * restrict t)
|
|
169
168
|
{
|
170
169
|
pf = pf->failure_link;
|
171
170
|
}
|
171
|
+
|
172
172
|
}
|
173
173
|
}
|
174
|
-
|
175
174
|
aho_queue_destroy(&queue);
|
176
175
|
}
|
177
176
|
|
@@ -32,6 +32,7 @@ void aho_destroy(struct ahocorasick * restrict aho)
|
|
32
32
|
int aho_add_match_text(struct ahocorasick * restrict aho, const char* text, unsigned int len)
|
33
33
|
{
|
34
34
|
struct aho_text_t* a_text = NULL;
|
35
|
+
|
35
36
|
if (aho->accumulate_text_id == AHO_MAX_TEXT_ID)
|
36
37
|
{
|
37
38
|
return -1;
|
@@ -42,11 +43,14 @@ int aho_add_match_text(struct ahocorasick * restrict aho, const char* text, unsi
|
|
42
43
|
goto lack_free_mem;
|
43
44
|
|
44
45
|
a_text->text = (char*) malloc(sizeof(char)*len);
|
46
|
+
|
45
47
|
if (!a_text->text)
|
46
48
|
goto lack_free_mem;
|
47
49
|
|
48
50
|
a_text->id = aho->accumulate_text_id++;
|
51
|
+
|
49
52
|
memcpy(a_text->text, text, len);
|
53
|
+
|
50
54
|
a_text->len = len;
|
51
55
|
a_text->prev = NULL;
|
52
56
|
a_text->next = NULL;
|
@@ -66,6 +70,7 @@ int aho_add_match_text(struct ahocorasick * restrict aho, const char* text, unsi
|
|
66
70
|
return a_text->id;
|
67
71
|
|
68
72
|
lack_free_mem:
|
73
|
+
|
69
74
|
return -1;
|
70
75
|
}
|
71
76
|
|
@@ -122,7 +127,9 @@ void aho_create_trie(struct ahocorasick * restrict aho)
|
|
122
127
|
|
123
128
|
for (iter = aho->text_list_head; iter != NULL; iter = iter->next)
|
124
129
|
{
|
130
|
+
|
125
131
|
aho_add_trie_node(&(aho->trie), iter);
|
132
|
+
|
126
133
|
}
|
127
134
|
|
128
135
|
aho_connect_link(&(aho->trie));
|
@@ -204,7 +211,10 @@ VALUE aho_replace_text(struct ahocorasick * restrict aho, const char* data,
|
|
204
211
|
}
|
205
212
|
|
206
213
|
// concatenate from last_concat_pos
|
207
|
-
|
214
|
+
if (pos > last_concat_pos) {
|
215
|
+
rb_str_cat(main_result, &data[last_concat_pos], pos - last_concat_pos);
|
216
|
+
}
|
217
|
+
|
208
218
|
// concatenate replace
|
209
219
|
if (values[result->id] == NULL) {
|
210
220
|
VALUE proc_result = rb_funcall(ruby_values[result->id], rb_intern("call"), 0);
|
@@ -65,7 +65,6 @@ VALUE multi_string_replace(VALUE self, VALUE body, VALUE replace)
|
|
65
65
|
int state;
|
66
66
|
|
67
67
|
struct ahocorasick aho;
|
68
|
-
aho_init(&aho);
|
69
68
|
|
70
69
|
char *target = StringValuePtr(body);
|
71
70
|
VALUE keys = rb_funcall(replace, rb_intern("keys"), 0);
|
@@ -75,6 +74,8 @@ VALUE multi_string_replace(VALUE self, VALUE body, VALUE replace)
|
|
75
74
|
long value_sizes[size];
|
76
75
|
char *values[size];
|
77
76
|
VALUE ruby_val[size];
|
77
|
+
|
78
|
+
aho_init(&aho);
|
78
79
|
|
79
80
|
for(long idx = 0; idx < size; idx++) {
|
80
81
|
VALUE entry = rb_ary_entry(keys, idx);
|
@@ -94,9 +95,8 @@ VALUE multi_string_replace(VALUE self, VALUE body, VALUE replace)
|
|
94
95
|
ruby_val[idx] = value;
|
95
96
|
}
|
96
97
|
|
97
|
-
|
98
|
+
aho_add_match_text(&aho, StringValuePtr(entry), RSTRING_LEN(entry));
|
98
99
|
}
|
99
|
-
|
100
100
|
aho_create_trie(&aho);
|
101
101
|
|
102
102
|
VALUE result = aho_replace_text(&aho, target, RSTRING_LEN(body), values, value_sizes, ruby_val);
|
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
|
|
11
11
|
|
12
12
|
spec.summary = %q{A fast multiple string replace library for ruby. Uses a C implementation of the Aho–Corasick Algorithm}
|
13
13
|
spec.description = %q{A fast multiple string replace library for ruby. Uses a C implementation of the Aho–Corasick Algorithm}
|
14
|
-
spec.homepage = "https://github.com/jedld/
|
14
|
+
spec.homepage = "https://github.com/jedld/multi_string_replace"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
17
17
|
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: multi_string_replace
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joseph Dayo
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-09-
|
11
|
+
date: 2018-09-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -101,7 +101,7 @@ files:
|
|
101
101
|
- multi_string_replace.gemspec
|
102
102
|
- replaced.txt
|
103
103
|
- replaced2.txt
|
104
|
-
homepage: https://github.com/jedld/
|
104
|
+
homepage: https://github.com/jedld/multi_string_replace
|
105
105
|
licenses:
|
106
106
|
- MIT
|
107
107
|
metadata:
|
@@ -122,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
122
122
|
version: '0'
|
123
123
|
requirements: []
|
124
124
|
rubyforge_project:
|
125
|
-
rubygems_version: 2.
|
125
|
+
rubygems_version: 2.7.7
|
126
126
|
signing_key:
|
127
127
|
specification_version: 4
|
128
128
|
summary: A fast multiple string replace library for ruby. Uses a C implementation
|