fast_underscore 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 92b7e9dcd681e04772d2ebc6c19cb4b81754c30c15f2c5b11ea70f480b60de94
4
+ data.tar.gz: 1dae29981eb5c68f39fe9f0d0840a4d2066c3a6e123ae2f68190e7244aae93c0
5
+ SHA512:
6
+ metadata.gz: e30d177a4285fe0486a995f83b7eba7e5d4bd11aa3b7c0ee25954364b28b57a9e02066e746271a3f4643cdbe6989e22a6b3587492f2820902eac062b1e490dc0
7
+ data.tar.gz: ba43f899c1055930bc7a9e3ad9a4c7973fefd03668428e9d3ae48de6f7bcdb53abbf3beec1f50b89c317a17f1b4400f5abd664744e4eff526d0591694213b61b
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /lib/fast_underscore/fast_underscore.bundle
data/.rubocop.yml ADDED
@@ -0,0 +1,11 @@
1
+ AllCops:
2
+ DisplayCopNames: true
3
+ DisplayStyleGuide: true
4
+ TargetRubyVersion: 2.4
5
+ Exclude:
6
+ - 'bin/**/*'
7
+ - 'vendor/**/*'
8
+ - 'tmp/**/*'
9
+
10
+ Style/PerlBackrefs:
11
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm: 2.5.0
3
+ branches:
4
+ only: master
5
+ cache: bundler
6
+ script: bin/rubocop && bin/rake
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
6
+
7
+ gem 'activesupport', '~> 5.1'
data/Gemfile.lock ADDED
@@ -0,0 +1,55 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ fast_underscore (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ activesupport (5.1.4)
10
+ concurrent-ruby (~> 1.0, >= 1.0.2)
11
+ i18n (~> 0.7)
12
+ minitest (~> 5.1)
13
+ tzinfo (~> 1.1)
14
+ ast (2.3.0)
15
+ benchmark-ips (2.7.2)
16
+ concurrent-ruby (1.0.5)
17
+ i18n (0.9.1)
18
+ concurrent-ruby (~> 1.0)
19
+ minitest (5.10.3)
20
+ parallel (1.12.1)
21
+ parser (2.4.0.2)
22
+ ast (~> 2.3)
23
+ powerpack (0.1.1)
24
+ rainbow (3.0.0)
25
+ rake (12.3.0)
26
+ rake-compiler (0.9.9)
27
+ rake
28
+ rubocop (0.52.0)
29
+ parallel (~> 1.10)
30
+ parser (>= 2.4.0.2, < 3.0)
31
+ powerpack (~> 0.1)
32
+ rainbow (>= 2.2.2, < 4.0)
33
+ ruby-progressbar (~> 1.7)
34
+ unicode-display_width (~> 1.0, >= 1.0.1)
35
+ ruby-progressbar (1.9.0)
36
+ thread_safe (0.3.6)
37
+ tzinfo (1.2.4)
38
+ thread_safe (~> 0.1)
39
+ unicode-display_width (1.3.0)
40
+
41
+ PLATFORMS
42
+ ruby
43
+
44
+ DEPENDENCIES
45
+ activesupport (~> 5.1)
46
+ benchmark-ips (~> 2)
47
+ bundler (~> 1)
48
+ fast_underscore!
49
+ minitest (~> 5)
50
+ rake (~> 12)
51
+ rake-compiler (~> 0)
52
+ rubocop (~> 0.52)
53
+
54
+ BUNDLED WITH
55
+ 1.16.1.pre1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Kevin Deisz
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,54 @@
1
+ # `String#underscore` Ruby Extension
2
+
3
+ `fast_underscore` is a simple C extension which provides a fast implementation of [Active Support's `String#underscore` method](http://api.rubyonrails.org/classes/String.html#method-i-underscore).
4
+
5
+ ## Do I need this?
6
+
7
+ Maybe! Run a stack profiler like [`ruby-prof`](https://github.com/ruby-prof/ruby-prof). If `String#underscore` is coming up near the top of the list, this gem might be for you! If not, you probably don't need it. Either way, your milage may vary depending on the type of strings on which you're calling `underscore`.
8
+
9
+ ## Is it fast?
10
+
11
+ At last check, these were the benchmarks (obtained by running `bin/benchmark`):
12
+
13
+ ```
14
+ Warming up --------------------------------------
15
+ ActiveSupport 2.000 i/100ms
16
+ FastUnderscore 64.000 i/100ms
17
+ Calculating -------------------------------------
18
+ ActiveSupport 28.839 (± 6.9%) i/s - 144.000 in 5.019140s
19
+ FastUnderscore 650.151 (± 7.7%) i/s - 3.264k in 5.056094s
20
+
21
+ Comparison:
22
+ FastUnderscore: 650.2 i/s
23
+ ActiveSupport: 28.8 i/s - 22.54x slower
24
+ ```
25
+
26
+ ## Installation
27
+
28
+ Add this line to your application's Gemfile:
29
+
30
+ ```ruby
31
+ gem 'fast_underscore'
32
+ ```
33
+
34
+ And then execute:
35
+
36
+ $ bundle
37
+
38
+ Or install it yourself as:
39
+
40
+ $ gem install fast_underscore
41
+
42
+ ## Development
43
+
44
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
45
+
46
+ 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).
47
+
48
+ ## Contributing
49
+
50
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kddeisz/fast_underscore.
51
+
52
+ ## License
53
+
54
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+ require 'rake/extensiontask'
6
+
7
+ Rake::ExtensionTask.new(:compile) do |ext|
8
+ ext.name = 'fast_underscore'
9
+ ext.ext_dir = 'ext/fast_underscore'
10
+ ext.lib_dir = 'lib/fast_underscore'
11
+ ext.gem_spec = Gem::Specification.load('fast_underscore.gemspec')
12
+ end
13
+
14
+ Rake::TestTask.new(:test) do |t|
15
+ t.libs << 'test'
16
+ t.libs << 'lib'
17
+ t.test_files = FileList['test/**/*_test.rb']
18
+ end
19
+
20
+ Rake::Task[:test].prerequisites << :compile
21
+
22
+ task default: :test
data/bin/benchmark ADDED
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'benchmark/ips'
5
+
6
+ require 'active_support'
7
+ require 'fast_underscore'
8
+
9
+ source =
10
+ %w[_ - : :: / 漢字 😊🎉] + ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
11
+ words = 500.times.map { Array.new(100) { source.sample }.join }
12
+
13
+ Benchmark.ips do |x|
14
+ x.report('ActiveSupport') do
15
+ words.each { |word| ActiveSupport::Inflector.underscore(word) }
16
+ end
17
+
18
+ x.report('FastUnderscore') do
19
+ words.each { |word| FastUnderscore.underscore(word) }
20
+ end
21
+
22
+ x.compare!
23
+ end
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'fast_underscore'
5
+
6
+ require 'irb'
7
+ IRB.start(__FILE__)
data/bin/rake ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rake' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 150) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rake", "rake")
data/bin/rubocop ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rubocop' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 150) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rubocop", "rubocop")
data/bin/setup ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mkmf'
4
+ create_makefile('fast_underscore/fast_underscore')
@@ -0,0 +1,376 @@
1
+ #include <ruby.h>
2
+ #include <ruby/encoding.h>
3
+ #include <stdlib.h>
4
+
5
+ /**
6
+ * true if the given codepoint is a lowercase ascii character.
7
+ */
8
+ static int
9
+ character_is_lower(unsigned int character) {
10
+ return character >= 'a' && character <= 'z';
11
+ }
12
+
13
+ /**
14
+ * true if the given codepoint is a uppercase ascii character.
15
+ */
16
+ static int
17
+ character_is_upper(unsigned int character) {
18
+ return character >= 'A' && character <= 'Z';
19
+ }
20
+
21
+ /**
22
+ * true if the given codepoint is an ascii digit.
23
+ */
24
+ static int
25
+ character_is_digit(unsigned int character) {
26
+ return character >= '0' && character <= '9';
27
+ }
28
+
29
+ /**
30
+ * Macros for extracting the character out of the `codepoint_t` struct.
31
+ */
32
+ #define codepoint_is_lower(codepoint) character_is_lower(codepoint->character)
33
+ #define codepoint_is_upper(codepoint) character_is_upper(codepoint->character)
34
+ #define codepoint_is_digit(codepoint) character_is_digit(codepoint->character)
35
+
36
+ /**
37
+ * Macros for shortcuts to call into the `builder_result_push_char` function.
38
+ */
39
+ #define builder_result_push(builder, codepoint) \
40
+ builder_result_push_char(builder, codepoint->character, codepoint->size, \
41
+ codepoint->encoding)
42
+ #define builder_result_push_literal(builder, character) \
43
+ builder_result_push_char(builder, character, 1, NULL)
44
+
45
+ /**
46
+ * A struct for keeping track of a codepoint from the original string. Maintains
47
+ * the original encoding, the actual character codepoint, and the size of the
48
+ * codepoint.
49
+ */
50
+ typedef struct codepoint {
51
+ // The encoding of the character
52
+ rb_encoding *encoding;
53
+
54
+ // Representation of a character, regardless of encoding
55
+ unsigned int character;
56
+
57
+ // The size that this character actually represents
58
+ int size;
59
+ } codepoint_t;
60
+
61
+ /**
62
+ * A struct for tracking the built string as it gets converted. Maintains an
63
+ * internal DFA for transitioning through various inputs to match certain
64
+ * patterns that need to be separated with underscores.
65
+ */
66
+ typedef struct builder {
67
+ // The state of the DFA in which is the builder
68
+ enum state {
69
+ STATE_DEFAULT,
70
+ STATE_COLON,
71
+ STATE_UPPER_END,
72
+ STATE_UPPER_START
73
+ } state;
74
+
75
+ // The current segment of text that we're analyzing
76
+ char *segment;
77
+ long segment_size;
78
+
79
+ // The resultant text from the underscore operation
80
+ char *result;
81
+ long result_size;
82
+
83
+ // Whether or not the last pushed result character should cause the following
84
+ // one to be spaced by an underscore
85
+ int pushNext;
86
+ } builder_t;
87
+
88
+ /**
89
+ * Allocate and initialize a `codepoint_t` struct.
90
+ */
91
+ static codepoint_t*
92
+ codepoint_build(rb_encoding *encoding) {
93
+ codepoint_t *codepoint = (codepoint_t *) malloc(sizeof(codepoint_t));
94
+ if (codepoint == NULL) {
95
+ return NULL;
96
+ }
97
+
98
+ codepoint->encoding = encoding;
99
+ return codepoint;
100
+ }
101
+
102
+ /**
103
+ * Free a previously allocated `codepoint_t` struct.
104
+ */
105
+ static void
106
+ codepoint_free(codepoint_t *codepoint) {
107
+ free(codepoint);
108
+ }
109
+
110
+ /**
111
+ * Allocate and initialize a `builder_t` struct.
112
+ */
113
+ static builder_t*
114
+ builder_build(long str_len) {
115
+ builder_t *builder = (builder_t *) malloc(sizeof(builder_t));
116
+ if (builder == NULL) {
117
+ return NULL;
118
+ }
119
+
120
+ builder->state = STATE_DEFAULT;
121
+ builder->segment = (char *) malloc(str_len * sizeof(unsigned int) * 2);
122
+
123
+ if (builder->segment == NULL) {
124
+ free(builder);
125
+ return NULL;
126
+ }
127
+
128
+ builder->result = (char *) malloc(str_len * sizeof(unsigned int) * 2);
129
+
130
+ if (builder->result == NULL) {
131
+ free(builder->segment);
132
+ free(builder);
133
+ return NULL;
134
+ }
135
+
136
+ builder->segment_size = 0;
137
+ builder->result_size = 0;
138
+ builder->pushNext = 0;
139
+
140
+ return builder;
141
+ }
142
+
143
+ /**
144
+ * Push a character onto the resultant string using the given codepoint and
145
+ * encoding.
146
+ */
147
+ static void
148
+ builder_result_push_char(builder_t *builder, unsigned int character, int size,
149
+ rb_encoding *encoding) {
150
+ if (character_is_upper(character)) {
151
+ if (builder->pushNext == 1) {
152
+ builder->pushNext = 0;
153
+ builder_result_push_literal(builder, '_');
154
+ }
155
+
156
+ builder->result[builder->result_size++] = (char) character - 'A' + 'a';
157
+ return;
158
+ }
159
+
160
+ builder->pushNext = (character_is_lower(character) || character_is_digit(character));
161
+
162
+ if (encoding == NULL) {
163
+ builder->result[builder->result_size++] = (char) character;
164
+ } else {
165
+ rb_enc_mbcput(character, &builder->result[builder->result_size], encoding);
166
+ builder->result_size += size;
167
+ }
168
+ }
169
+
170
+ /**
171
+ * Push the given codepoint onto the builder.
172
+ */
173
+ static void
174
+ builder_segment_push(builder_t *builder, codepoint_t *codepoint) {
175
+ builder->segment[builder->segment_size++] = (char) codepoint->character;
176
+ }
177
+
178
+ /**
179
+ * Copy the given number of characters out of the segment cache onto the result
180
+ * string.
181
+ */
182
+ static void
183
+ builder_segment_copy(builder_t *builder, long size) {
184
+ for (long idx = 0; idx < size; idx++) {
185
+ builder_result_push_literal(builder, builder->segment[idx]);
186
+ }
187
+ }
188
+
189
+ /**
190
+ * Restart the `builder_t` back at the default state (because we've hit a
191
+ * character for which we have no allowed transitions).
192
+ */
193
+ static void
194
+ builder_restart(builder_t *builder) {
195
+ builder->state = STATE_DEFAULT;
196
+ builder->segment_size = 0;
197
+ }
198
+
199
+ static void builder_next(builder_t *builder, codepoint_t *codepoint);
200
+
201
+ /**
202
+ * Pull the remaining content out of the cached segment in case we don't end
203
+ * parsing while not in the default state.
204
+ */
205
+ static void
206
+ builder_flush(builder_t *builder) {
207
+ switch (builder->state) {
208
+ case STATE_DEFAULT: return;
209
+ case STATE_COLON:
210
+ builder_result_push_literal(builder, ':');
211
+ return;
212
+ case STATE_UPPER_END:
213
+ case STATE_UPPER_START:
214
+ builder_segment_copy(builder, builder->segment_size);
215
+ return;
216
+ }
217
+ }
218
+
219
+ /**
220
+ * Perform transitions from the STATE_DEFAULT state.
221
+ */
222
+ static inline void
223
+ builder_default_transition(builder_t *builder, codepoint_t *codepoint) {
224
+ if (codepoint->character == '-') {
225
+ builder_result_push_literal(builder, '_');
226
+ return;
227
+ }
228
+ if (codepoint->character == ':') {
229
+ builder->state = STATE_COLON;
230
+ return;
231
+ }
232
+ if (codepoint_is_digit(codepoint) || codepoint_is_upper(codepoint)) {
233
+ builder->segment[0] = (char) codepoint->character;
234
+ builder->segment_size = 1;
235
+ builder->state = STATE_UPPER_START;
236
+ return;
237
+ }
238
+ builder_result_push(builder, codepoint);
239
+ }
240
+
241
+ /**
242
+ * Perform transitions from the STATE_COLON state.
243
+ */
244
+ static inline void
245
+ builder_colon_transition(builder_t *builder, codepoint_t *codepoint) {
246
+ if (codepoint->character == ':') {
247
+ builder_result_push_literal(builder, '/');
248
+ builder_restart(builder);
249
+ return;
250
+ }
251
+
252
+ builder_result_push_literal(builder, ':');
253
+ builder_restart(builder);
254
+ builder_next(builder, codepoint);
255
+ }
256
+
257
+ /**
258
+ * Perform transitions from the STATE_UPPER_START state.
259
+ */
260
+ static inline void
261
+ builder_upper_start_transition(builder_t *builder, codepoint_t *codepoint) {
262
+ if (codepoint_is_digit(codepoint)) {
263
+ builder_segment_push(builder, codepoint);
264
+ return;
265
+ }
266
+ if (codepoint_is_upper(codepoint)) {
267
+ builder_segment_push(builder, codepoint);
268
+ builder->state = STATE_UPPER_END;
269
+ return;
270
+ }
271
+
272
+ builder_segment_copy(builder, builder->segment_size);
273
+ builder_restart(builder);
274
+ builder_next(builder, codepoint);
275
+ }
276
+
277
+ /**
278
+ * Perform transitions from the STATE_UPPER_END state.
279
+ */
280
+ static inline void
281
+ builder_upper_end_transition(builder_t *builder, codepoint_t *codepoint) {
282
+ if (codepoint_is_digit(codepoint)) {
283
+ builder_segment_push(builder, codepoint);
284
+ builder->state = STATE_UPPER_START;
285
+ return;
286
+ }
287
+ if (codepoint_is_upper(codepoint)) {
288
+ builder_segment_push(builder, codepoint);
289
+ return;
290
+ }
291
+ if (codepoint_is_lower(codepoint)) {
292
+ builder_segment_copy(builder, builder->segment_size - 1);
293
+ builder_result_push_literal(builder, '_');
294
+ builder_result_push_literal(builder, builder->segment[builder->segment_size - 1]);
295
+ builder_restart(builder);
296
+ builder_next(builder, codepoint);
297
+ return;
298
+ }
299
+
300
+ builder_segment_copy(builder, builder->segment_size);
301
+ builder_restart(builder);
302
+ builder_next(builder, codepoint);
303
+ }
304
+
305
+ /**
306
+ * Accept the next codepoint, which will move the `builder_t` struct into the
307
+ * next state.
308
+ */
309
+ static void
310
+ builder_next(builder_t *builder, codepoint_t *codepoint) {
311
+ switch (builder->state) {
312
+ case STATE_DEFAULT:
313
+ return builder_default_transition(builder, codepoint);
314
+ case STATE_COLON:
315
+ return builder_colon_transition(builder, codepoint);
316
+ case STATE_UPPER_START:
317
+ return builder_upper_start_transition(builder, codepoint);
318
+ case STATE_UPPER_END:
319
+ return builder_upper_end_transition(builder, codepoint);
320
+ }
321
+ }
322
+
323
+ /**
324
+ * Frees a previously allocated `builder_t` struct.
325
+ */
326
+ static void
327
+ builder_free(builder_t *builder) {
328
+ free(builder->segment);
329
+ free(builder->result);
330
+ free(builder);
331
+ }
332
+
333
+ /**
334
+ * Makes an underscored, lowercase form from the expression in the string.
335
+ *
336
+ * Changes '::' to '/' to convert namespaces to paths.
337
+ *
338
+ * underscore('ActiveModel') # => "active_model"
339
+ * underscore('ActiveModel::Errors') # => "active_model/errors"
340
+ *
341
+ * As a rule of thumb you can think of +underscore+ as the inverse of
342
+ * #camelize, though there are cases where that does not hold:
343
+ *
344
+ * camelize(underscore('SSLError')) # => "SslError"
345
+ */
346
+ static VALUE
347
+ rb_str_underscore(VALUE self, VALUE rb_string) {
348
+ rb_encoding *encoding = rb_enc_from_index(ENCODING_GET(rb_string));
349
+
350
+ char *string = RSTRING_PTR(rb_string);
351
+ char *end = RSTRING_END(rb_string);
352
+
353
+ builder_t *builder = builder_build(RSTRING_LEN(rb_string) * 2);
354
+ codepoint_t *codepoint = codepoint_build(encoding);
355
+
356
+ while (string < end) {
357
+ codepoint->character = rb_enc_codepoint_len(string, end, &codepoint->size, encoding);
358
+ builder_next(builder, codepoint);
359
+ string += codepoint->size;
360
+ }
361
+ builder_flush(builder);
362
+
363
+ VALUE resultant = rb_enc_str_new(builder->result, builder->result_size, encoding);
364
+ builder_free(builder);
365
+
366
+ return resultant;
367
+ }
368
+
369
+ /**
370
+ * Hook into Ruby and define the `FastUnderscore::underscore`.
371
+ */
372
+ void
373
+ Init_fast_underscore(void) {
374
+ VALUE rb_cFastUnderscore = rb_define_module("FastUnderscore");
375
+ rb_define_singleton_method(rb_cFastUnderscore, "underscore", rb_str_underscore, 1);
376
+ }
@@ -0,0 +1,3 @@
1
+ #ifndef FAST_UNDERSCORE_H
2
+ #define FAST_UNDERSCORE_H
3
+ #endif
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'fast_underscore/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'fast_underscore'
9
+ spec.version = FastUnderscore::VERSION
10
+ spec.authors = ['Kevin Deisz']
11
+ spec.email = ['kevin.deisz@gmail.com']
12
+
13
+ spec.summary = 'Fast String#underscore implementation'
14
+ spec.description = 'Provides a C-optimized method for underscoring a string'
15
+ spec.homepage = 'https://github.com/kddeisz/fast_underscore'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+ spec.extensions = ['ext/fast_underscore/extconf.rb']
25
+
26
+ spec.add_development_dependency 'benchmark-ips', '~> 2'
27
+ spec.add_development_dependency 'bundler', '~> 1'
28
+ spec.add_development_dependency 'minitest', '~> 5'
29
+ spec.add_development_dependency 'rake', '~> 12'
30
+ spec.add_development_dependency 'rake-compiler', '~> 0'
31
+ spec.add_development_dependency 'rubocop', '~> 0.52'
32
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fast_underscore/version'
4
+ require 'fast_underscore/fast_underscore'
5
+
6
+ if defined?(ActiveSupport)
7
+ String.prepend(Module.new do
8
+ def underscore
9
+ return self unless /[A-Z-]|::/.match?(self)
10
+
11
+ acronyms = ActiveSupport::Inflector.inflections.acronym_regex
12
+ dup.gsub!(/(?:(?<=([A-Za-z\d]))|\b)(#{acronyms})(?=\b|[^a-z])/) do
13
+ "#{$1 && '_'}#{$2.downcase}"
14
+ end
15
+
16
+ FastUnderscore.underscore(self)
17
+ end
18
+ end)
19
+ else
20
+ String.prepend(Module.new do
21
+ def underscore
22
+ FastUnderscore.underscore(self)
23
+ end
24
+ end)
25
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FastUnderscore
4
+ VERSION = '0.0.1'
5
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fast_underscore
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kevin Deisz
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-12-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: benchmark-ips
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '12'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake-compiler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.52'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.52'
97
+ description: Provides a C-optimized method for underscoring a string
98
+ email:
99
+ - kevin.deisz@gmail.com
100
+ executables: []
101
+ extensions:
102
+ - ext/fast_underscore/extconf.rb
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rubocop.yml"
107
+ - ".travis.yml"
108
+ - Gemfile
109
+ - Gemfile.lock
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - bin/benchmark
114
+ - bin/console
115
+ - bin/rake
116
+ - bin/rubocop
117
+ - bin/setup
118
+ - ext/fast_underscore/extconf.rb
119
+ - ext/fast_underscore/fast_underscore.c
120
+ - ext/fast_underscore/fast_underscore.h
121
+ - fast_underscore.gemspec
122
+ - lib/fast_underscore.rb
123
+ - lib/fast_underscore/version.rb
124
+ homepage: https://github.com/kddeisz/fast_underscore
125
+ licenses:
126
+ - MIT
127
+ metadata: {}
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 2.7.3
145
+ signing_key:
146
+ specification_version: 4
147
+ summary: Fast String#underscore implementation
148
+ test_files: []