multi_string_replace 2.0.0 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +10 -0
- data/ext/multi_string_replace/ahocorasick.c +12 -4
- data/ext/multi_string_replace/multi_string_replace.c +11 -3
- data/lib/multi_string_replace/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1485ddac5f7139a2f9f5161625aa14a2c375f08f995c9d996022a1c7b2c57384
|
4
|
+
data.tar.gz: 94943343da93436f1be137e7ceb38f68a53ff81bacc8b5954997ad0c4d25b777
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 522e1a14c9430326bfa7aeffab8a484f334eed80123361cd039152ba47ada0bc362a10b42953b25abab4987ebdc73f4ae7c9964972390d5c2b01c601bb935239
|
7
|
+
data.tar.gz: dd9d77326cc78330a021294a1364d270f22f4aee96d5621ca09df2476e06feca3c9207a6965a9b775db117585b4f53d6a8d4bda6052c3f9d1f7b2549fdc8d0e7
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -40,8 +40,18 @@ You can also pass in a Proc, these will only get evaluated when the token is enc
|
|
40
40
|
|
41
41
|
```ruby
|
42
42
|
MultiStringReplace.replace("The quick brown fox jumps over the lazy dog brown", {'brown' => 'black', 'fox' => ->(s, e) { "cat" }})
|
43
|
+
# => "The quick black cat jumps over the lazy dog black"
|
44
|
+
|
45
|
+
# returning nil will cause the substitution to be ignored.
|
46
|
+
MultiStringReplace.replace("The quick brown fox jumps over the lazy dog brown", {'brown' => 'black', 'fox' => ->(s, e) { nil }})
|
47
|
+
# => "The quick black fox jumps over the lazy dog black"
|
48
|
+
|
49
|
+
MultiStringReplace.replace("The quick brown fox jumps over the lazy dog brown", {'brown' => 'black', 'fox' => ->(s, e) { "" }})
|
50
|
+
# => "The quick black jumps over the lazy dog black"
|
43
51
|
```
|
44
52
|
|
53
|
+
This should allow for very fast and simple templating systems.
|
54
|
+
|
45
55
|
Also adds a mreplace method to String which does the same thing:
|
46
56
|
|
47
57
|
```ruby
|
@@ -218,13 +218,21 @@ VALUE aho_replace_text(struct ahocorasick * restrict aho, const char* data,
|
|
218
218
|
rb_str_cat(main_result, &data[last_concat_pos], pos - last_concat_pos);
|
219
219
|
}
|
220
220
|
|
221
|
-
|
222
|
-
|
223
221
|
// concatenate replace
|
224
222
|
if (values[result->id] == NULL) {
|
225
223
|
VALUE proc_result = rb_funcall(ruby_values[result->id], rb_intern("call"), 2, LONG2NUM(pos), LONG2NUM(pos + result->len));
|
226
|
-
|
227
|
-
|
224
|
+
if (RB_TYPE_P(proc_result, T_NIL)) {
|
225
|
+
rb_str_cat(main_result, &data[pos], result->len);
|
226
|
+
last_concat_pos = i + 1;
|
227
|
+
continue;
|
228
|
+
} else if (RB_TYPE_P(proc_result, T_STRING)) {
|
229
|
+
value_sizes[result->id] = RSTRING_LEN(proc_result);
|
230
|
+
values[result->id] = StringValuePtr(proc_result);
|
231
|
+
} else {
|
232
|
+
VALUE string_result = rb_funcall(proc_result, rb_intern("to_s"), 0);
|
233
|
+
value_sizes[result->id] = RSTRING_LEN(string_result);
|
234
|
+
values[result->id] = StringValuePtr(string_result);
|
235
|
+
}
|
228
236
|
}
|
229
237
|
|
230
238
|
rb_str_cat(main_result, values[result->id], value_sizes[result->id]);
|
@@ -93,10 +93,18 @@ VALUE multi_string_replace(VALUE self, VALUE body, VALUE replace)
|
|
93
93
|
values[idx] = StringValuePtr(value);
|
94
94
|
value_sizes[idx] = RSTRING_LEN(value);
|
95
95
|
} else {
|
96
|
-
|
97
|
-
|
96
|
+
|
97
|
+
VALUE responds_value = rb_funcall(value, rb_intern("respond_to?"), 1, rb_str_new_cstr("call"));
|
98
|
+
if (RB_TYPE_P(responds_value, T_TRUE)) {
|
99
|
+
values[idx] = NULL;
|
100
|
+
ruby_val[idx] = value;
|
101
|
+
} else {
|
102
|
+
VALUE value_as_string = rb_funcall(value, rb_intern("to_s"), 0);
|
103
|
+
values[idx] = StringValuePtr(value_as_string);
|
104
|
+
value_sizes[idx] = RSTRING_LEN(value_as_string);
|
105
|
+
}
|
98
106
|
}
|
99
|
-
|
107
|
+
|
100
108
|
aho_add_match_text(&aho, StringValuePtr(entry), RSTRING_LEN(entry));
|
101
109
|
}
|
102
110
|
aho_create_trie(&aho);
|
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: 2.0.
|
4
|
+
version: 2.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joseph Dayo
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-12-
|
11
|
+
date: 2022-12-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|