fast_parameterize 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/.github/dependabot.yml +6 -0
- data/.github/workflows/main.yml +35 -0
- data/.gitignore +10 -0
- data/.rubocop.yml +34 -0
- data/CHANGELOG.md +16 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +15 -0
- data/Gemfile.lock +90 -0
- data/LICENSE +21 -0
- data/README.md +53 -0
- data/Rakefile +22 -0
- data/Steepfile +5 -0
- data/bin/bench +24 -0
- data/bin/console +8 -0
- data/bin/setup +6 -0
- data/ext/fast_parameterize/extconf.rb +4 -0
- data/ext/fast_parameterize/fast_parameterize.c +140 -0
- data/fast_parameterize.gemspec +26 -0
- data/lib/fast_parameterize.rb +61 -0
- data/lib/fast_parameterize/version.rb +5 -0
- data/sig/fast_parameterize.rbs +10 -0
- metadata +65 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 3f2ea6fd1830774b3e605fc7d7edcc5d3fb946c125992b61367c900b979cf4ac
|
|
4
|
+
data.tar.gz: 98d7e79bd0c62d33332a24d45eafb724fea935a99761b56697155bb345e6f83d
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: ee18b8d230eae73b416ce9fab9d5090e12891bdc28fb11e0e64a90eccf79d6d621d68bdafa0046a22ff301c9e811f8acfaf65c7d3c6cc70a66f783e1dd74df99
|
|
7
|
+
data.tar.gz: c2555d0f44e75257a91b87303d31765ec103fee7c55c780a5c98f3dc9bc63945d15c29001d272a78aa96ae6d3489a289da721450e41a8efe0b587ce524813bc2
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
name: Main
|
|
2
|
+
on:
|
|
3
|
+
- push
|
|
4
|
+
- pull_request_target
|
|
5
|
+
jobs:
|
|
6
|
+
ci:
|
|
7
|
+
name: CI
|
|
8
|
+
runs-on: ubuntu-latest
|
|
9
|
+
env:
|
|
10
|
+
CI: true
|
|
11
|
+
steps:
|
|
12
|
+
- uses: actions/checkout@master
|
|
13
|
+
- uses: ruby/setup-ruby@v1
|
|
14
|
+
with:
|
|
15
|
+
bundler-cache: true
|
|
16
|
+
ruby-version: 3.0
|
|
17
|
+
- name: Lint and test
|
|
18
|
+
run: |
|
|
19
|
+
bundle exec rubocop --parallel
|
|
20
|
+
bundle exec rake test
|
|
21
|
+
bundle exec steep check
|
|
22
|
+
automerge:
|
|
23
|
+
name: AutoMerge
|
|
24
|
+
needs: ci
|
|
25
|
+
runs-on: ubuntu-latest
|
|
26
|
+
if: github.event_name == 'pull_request_target' && github.actor == 'dependabot[bot]'
|
|
27
|
+
steps:
|
|
28
|
+
- uses: actions/github-script@v3
|
|
29
|
+
with:
|
|
30
|
+
script: |
|
|
31
|
+
github.pulls.merge({
|
|
32
|
+
owner: context.payload.repository.owner.login,
|
|
33
|
+
repo: context.payload.repository.name,
|
|
34
|
+
pull_number: context.payload.pull_request.number
|
|
35
|
+
})
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
AllCops:
|
|
2
|
+
DisplayCopNames: true
|
|
3
|
+
DisplayStyleGuide: true
|
|
4
|
+
SuggestExtensions: false
|
|
5
|
+
TargetRubyVersion: 3.0
|
|
6
|
+
NewCops: enable
|
|
7
|
+
Exclude:
|
|
8
|
+
- 'vendor/**/*'
|
|
9
|
+
- 'yard/**/*'
|
|
10
|
+
- 'tmp/**/*'
|
|
11
|
+
|
|
12
|
+
Gemspec/RequiredRubyVersion:
|
|
13
|
+
Enabled: false
|
|
14
|
+
|
|
15
|
+
Layout/LineLength:
|
|
16
|
+
Max: 80
|
|
17
|
+
|
|
18
|
+
Lint/AmbiguousBlockAssociation:
|
|
19
|
+
Enabled: false
|
|
20
|
+
|
|
21
|
+
Naming/RescuedExceptionsVariableName:
|
|
22
|
+
Enabled: false
|
|
23
|
+
|
|
24
|
+
Style/Documentation:
|
|
25
|
+
Enabled: false
|
|
26
|
+
|
|
27
|
+
Style/FormatString:
|
|
28
|
+
EnforcedStyle: percent
|
|
29
|
+
|
|
30
|
+
Style/FormatStringToken:
|
|
31
|
+
Enabled: false
|
|
32
|
+
|
|
33
|
+
Style/PerlBackrefs:
|
|
34
|
+
Enabled: false
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
|
|
6
|
+
|
|
7
|
+
## [Unreleased]
|
|
8
|
+
|
|
9
|
+
## [0.1.0] - 2020-10-09
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
|
|
13
|
+
- 🎉 Initial release! 🎉
|
|
14
|
+
|
|
15
|
+
[unreleased]: https://github.com/kddeisz/fast_parameterize/compare/v0.1.0...HEAD
|
|
16
|
+
[0.1.0]: https://github.com/kddeisz/fast_parameterize/compare/39c5a7...v0.1.0
|
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 kevin.deisz@gmail.com. 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 [https://contributor-covenant.org/version/1/4][version]
|
|
72
|
+
|
|
73
|
+
[homepage]: https://contributor-covenant.org
|
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
fast_parameterize (0.1.0)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: https://rubygems.org/
|
|
8
|
+
specs:
|
|
9
|
+
activesupport (6.1.3.1)
|
|
10
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
11
|
+
i18n (>= 1.6, < 2)
|
|
12
|
+
minitest (>= 5.1)
|
|
13
|
+
tzinfo (~> 2.0)
|
|
14
|
+
zeitwerk (~> 2.3)
|
|
15
|
+
ast (2.4.2)
|
|
16
|
+
ast_utils (0.4.0)
|
|
17
|
+
parser (>= 2.7.0)
|
|
18
|
+
benchmark-ips (2.8.4)
|
|
19
|
+
concurrent-ruby (1.1.8)
|
|
20
|
+
docile (1.3.5)
|
|
21
|
+
ffi (1.15.0)
|
|
22
|
+
i18n (1.8.10)
|
|
23
|
+
concurrent-ruby (~> 1.0)
|
|
24
|
+
language_server-protocol (3.15.0.2)
|
|
25
|
+
listen (3.5.1)
|
|
26
|
+
rb-fsevent (~> 0.10, >= 0.10.3)
|
|
27
|
+
rb-inotify (~> 0.9, >= 0.9.10)
|
|
28
|
+
minitest (5.14.4)
|
|
29
|
+
parallel (1.20.1)
|
|
30
|
+
parser (3.0.0.0)
|
|
31
|
+
ast (~> 2.4.1)
|
|
32
|
+
rainbow (3.0.0)
|
|
33
|
+
rake (13.0.3)
|
|
34
|
+
rake-compiler (1.1.1)
|
|
35
|
+
rake
|
|
36
|
+
rb-fsevent (0.10.4)
|
|
37
|
+
rb-inotify (0.10.1)
|
|
38
|
+
ffi (~> 1.0)
|
|
39
|
+
rbs (1.1.1)
|
|
40
|
+
regexp_parser (2.1.1)
|
|
41
|
+
rexml (3.2.4)
|
|
42
|
+
rubocop (1.12.0)
|
|
43
|
+
parallel (~> 1.10)
|
|
44
|
+
parser (>= 3.0.0.0)
|
|
45
|
+
rainbow (>= 2.2.2, < 4.0)
|
|
46
|
+
regexp_parser (>= 1.8, < 3.0)
|
|
47
|
+
rexml
|
|
48
|
+
rubocop-ast (>= 1.2.0, < 2.0)
|
|
49
|
+
ruby-progressbar (~> 1.7)
|
|
50
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
|
51
|
+
rubocop-ast (1.4.1)
|
|
52
|
+
parser (>= 2.7.1.5)
|
|
53
|
+
ruby-progressbar (1.11.0)
|
|
54
|
+
simplecov (0.21.2)
|
|
55
|
+
docile (~> 1.1)
|
|
56
|
+
simplecov-html (~> 0.11)
|
|
57
|
+
simplecov_json_formatter (~> 0.1)
|
|
58
|
+
simplecov-html (0.12.3)
|
|
59
|
+
simplecov_json_formatter (0.1.2)
|
|
60
|
+
steep (0.42.0)
|
|
61
|
+
activesupport (>= 5.1)
|
|
62
|
+
ast_utils (>= 0.4.0)
|
|
63
|
+
language_server-protocol (~> 3.15.0.1)
|
|
64
|
+
listen (~> 3.0)
|
|
65
|
+
parallel (>= 1.0.0)
|
|
66
|
+
parser (>= 2.7)
|
|
67
|
+
rainbow (>= 2.2.2, < 4.0)
|
|
68
|
+
rbs (~> 1.1.0)
|
|
69
|
+
tzinfo (2.0.4)
|
|
70
|
+
concurrent-ruby (~> 1.0)
|
|
71
|
+
unicode-display_width (2.0.0)
|
|
72
|
+
zeitwerk (2.4.2)
|
|
73
|
+
|
|
74
|
+
PLATFORMS
|
|
75
|
+
ruby
|
|
76
|
+
|
|
77
|
+
DEPENDENCIES
|
|
78
|
+
activesupport
|
|
79
|
+
benchmark-ips
|
|
80
|
+
bundler
|
|
81
|
+
fast_parameterize!
|
|
82
|
+
minitest
|
|
83
|
+
rake
|
|
84
|
+
rake-compiler
|
|
85
|
+
rubocop
|
|
86
|
+
simplecov
|
|
87
|
+
steep
|
|
88
|
+
|
|
89
|
+
BUNDLED WITH
|
|
90
|
+
2.2.3
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021-present 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,53 @@
|
|
|
1
|
+
# `String#parameterize` Ruby Extension
|
|
2
|
+
|
|
3
|
+
[](https://github.com/kddeisz/fast_parameterize/actions)
|
|
4
|
+
[](https://rubygems.org/gems/fast_parameterize)
|
|
5
|
+
|
|
6
|
+
`fast_parameterize` is a C extension that provides a fast implementation of [ActiveSupport's `String#parameterize` method](http://api.rubyonrails.org/classes/String.html#method-i-parameterize).
|
|
7
|
+
|
|
8
|
+
## Is it fast?
|
|
9
|
+
|
|
10
|
+
At last check, these were the benchmarks (obtained by running `bin/bench` with ActiveSupport 6.1.3):
|
|
11
|
+
|
|
12
|
+
```
|
|
13
|
+
Warming up --------------------------------------
|
|
14
|
+
ActiveSupport 8.000 i/100ms
|
|
15
|
+
FastParameterize 131.000 i/100ms
|
|
16
|
+
Calculating -------------------------------------
|
|
17
|
+
ActiveSupport 82.566 (± 2.4%) i/s - 416.000 in 5.042230s
|
|
18
|
+
FastParameterize 1.308k (± 3.4%) i/s - 6.550k in 5.014582s
|
|
19
|
+
|
|
20
|
+
Comparison:
|
|
21
|
+
FastParameterize: 1308.0 i/s
|
|
22
|
+
ActiveSupport: 82.6 i/s - 15.84x (± 0.00) slower
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Installation
|
|
26
|
+
|
|
27
|
+
Add this line to your application's Gemfile:
|
|
28
|
+
|
|
29
|
+
```ruby
|
|
30
|
+
gem 'fast_parameterize'
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
And then execute:
|
|
34
|
+
|
|
35
|
+
$ bundle
|
|
36
|
+
|
|
37
|
+
Or install it yourself as:
|
|
38
|
+
|
|
39
|
+
$ gem install fast_parameterize
|
|
40
|
+
|
|
41
|
+
## Development
|
|
42
|
+
|
|
43
|
+
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.
|
|
44
|
+
|
|
45
|
+
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).
|
|
46
|
+
|
|
47
|
+
## Contributing
|
|
48
|
+
|
|
49
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/kddeisz/fast_parameterize.
|
|
50
|
+
|
|
51
|
+
## License
|
|
52
|
+
|
|
53
|
+
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_parameterize'
|
|
9
|
+
ext.ext_dir = 'ext/fast_parameterize'
|
|
10
|
+
ext.lib_dir = 'lib/fast_parameterize'
|
|
11
|
+
ext.gem_spec = Gem::Specification.load('fast_parameterize.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/Steepfile
ADDED
data/bin/bench
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require 'bundler/setup'
|
|
5
|
+
require 'benchmark/ips'
|
|
6
|
+
|
|
7
|
+
require 'fast_parameterize'
|
|
8
|
+
require 'active_support/inflector'
|
|
9
|
+
|
|
10
|
+
source =
|
|
11
|
+
%w[_ - : :: / 漢字 😊🎉] + ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
|
|
12
|
+
words = 500.times.map { Array.new(100) { source.sample }.join }
|
|
13
|
+
|
|
14
|
+
Benchmark.ips do |x|
|
|
15
|
+
x.report('ActiveSupport') do
|
|
16
|
+
words.each { |word| ActiveSupport::Inflector.as_parameterize(word) }
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
x.report('FastParameterize') do
|
|
20
|
+
words.each { |word| FastParameterize.parameterize(word) }
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
x.compare!
|
|
24
|
+
end
|
data/bin/console
ADDED
data/bin/setup
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
#include <ruby.h>
|
|
2
|
+
#include <ruby/encoding.h>
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* This function works by maintaining a state machine that tracks which
|
|
6
|
+
* characters are allowed at any given state, and reacting appropriately. The
|
|
7
|
+
* state machine looks like:
|
|
8
|
+
*
|
|
9
|
+
* ┌ N ┐ ┌ C ┐
|
|
10
|
+
* │ v │ v
|
|
11
|
+
* ┌─────────────┐ ┌─────────────┐
|
|
12
|
+
* │ │ │ │
|
|
13
|
+
* ──>│ START │──── C ───>│ CHAR │
|
|
14
|
+
* │ │ │ │
|
|
15
|
+
* └─────────────┘ └─────────────┘
|
|
16
|
+
* │ ^
|
|
17
|
+
* ┌ N ┐ │ │
|
|
18
|
+
* │ v │ │
|
|
19
|
+
* ┌─────────────┐ │ |
|
|
20
|
+
* │ │<─── N ────────┘ |
|
|
21
|
+
* │ SEP │ |
|
|
22
|
+
* │ │──── C ──────────────┘
|
|
23
|
+
* └─────────────┘
|
|
24
|
+
*
|
|
25
|
+
* Where C represents a character that is allowed in the output and N represents
|
|
26
|
+
* a character that is not allowed in the output.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
// This is the current state of the state machine being used to compile the
|
|
30
|
+
// parameterized string.
|
|
31
|
+
enum state {
|
|
32
|
+
STATE_START,
|
|
33
|
+
STATE_CHAR,
|
|
34
|
+
STATE_SEP
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// This is used to equivalent to the characters that are allowed to be part of
|
|
38
|
+
// the output string as opposed to the ones that get replaced by the separator.
|
|
39
|
+
static inline int ischar(int character) {
|
|
40
|
+
return (
|
|
41
|
+
(character >= 'a' && character <= 'z') ||
|
|
42
|
+
(character >= 'A' && character <= 'Z') ||
|
|
43
|
+
(character >= '0' && character <= '9') ||
|
|
44
|
+
character == '-' || character == '_'
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// This is a function that preserves the same interface as tolower that can be
|
|
49
|
+
// swapped in its place in case the preserve_case option was set to true.
|
|
50
|
+
static int tosame(int character) {
|
|
51
|
+
return character;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Makes a parameterized, lowercase form from the expression in the string.
|
|
55
|
+
static VALUE parameterize(VALUE string, VALUE kwargs) {
|
|
56
|
+
static ID keywords[2];
|
|
57
|
+
CONST_ID(keywords[0], "separator");
|
|
58
|
+
CONST_ID(keywords[1], "preserve_case");
|
|
59
|
+
|
|
60
|
+
VALUE options[2] = { Qundef, Qundef };
|
|
61
|
+
rb_get_kwargs(kwargs, keywords, 0, 2, options);
|
|
62
|
+
|
|
63
|
+
// Configure the options based on the inputted keyword arguments
|
|
64
|
+
const char *separator = options[0] == Qundef ? "-" : StringValueCStr(options[0]);
|
|
65
|
+
size_t separator_size = strlen(separator);
|
|
66
|
+
int(*caser)(int) = options[1] == Qtrue ? tosame : tolower;
|
|
67
|
+
|
|
68
|
+
// Set the initial state and build a buffer for the resulting string
|
|
69
|
+
enum state state = STATE_START;
|
|
70
|
+
char result[RSTRING_LEN(string) * separator_size];
|
|
71
|
+
size_t size = 0;
|
|
72
|
+
|
|
73
|
+
// Set up a couple of variables necessary for iterating through the string
|
|
74
|
+
rb_encoding *encoding = rb_enc_from_index(ENCODING_GET(string));
|
|
75
|
+
int codepoint_size;
|
|
76
|
+
int character;
|
|
77
|
+
|
|
78
|
+
// Determine the bounds of the string for our loop
|
|
79
|
+
char *pointer = RSTRING_PTR(string);
|
|
80
|
+
char *end = RSTRING_END(string);
|
|
81
|
+
|
|
82
|
+
while (pointer < end) {
|
|
83
|
+
character = rb_enc_codepoint_len(pointer, end, &codepoint_size, encoding);
|
|
84
|
+
pointer += codepoint_size;
|
|
85
|
+
|
|
86
|
+
switch (state) {
|
|
87
|
+
case STATE_START:
|
|
88
|
+
if (ischar(character)) {
|
|
89
|
+
result[size++] = caser(character);
|
|
90
|
+
state = STATE_CHAR;
|
|
91
|
+
}
|
|
92
|
+
break;
|
|
93
|
+
case STATE_CHAR:
|
|
94
|
+
if (ischar(character)) {
|
|
95
|
+
result[size++] = caser(character);
|
|
96
|
+
} else {
|
|
97
|
+
state = STATE_SEP;
|
|
98
|
+
}
|
|
99
|
+
break;
|
|
100
|
+
case STATE_SEP:
|
|
101
|
+
if (ischar(character)) {
|
|
102
|
+
strncpy(result + size, separator, separator_size);
|
|
103
|
+
size += separator_size;
|
|
104
|
+
|
|
105
|
+
result[size++] = caser(character);
|
|
106
|
+
state = STATE_CHAR;
|
|
107
|
+
} else {
|
|
108
|
+
state = STATE_SEP;
|
|
109
|
+
}
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return rb_enc_str_new(result, size, encoding);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// FastUnderscore::parameterize
|
|
118
|
+
static VALUE fast_parameterize(int argc, VALUE* argv, VALUE self) {
|
|
119
|
+
VALUE string = Qnil;
|
|
120
|
+
VALUE kwargs = Qnil;
|
|
121
|
+
rb_scan_args(argc, argv, "10:", &string, &kwargs);
|
|
122
|
+
|
|
123
|
+
return RB_TYPE_P(string, T_STRING) ? parameterize(string, kwargs) : Qnil;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// String#parameterize
|
|
127
|
+
static VALUE string_parameterize(int argc, VALUE* argv, VALUE self) {
|
|
128
|
+
VALUE kwargs = Qnil;
|
|
129
|
+
rb_scan_args(argc, argv, "00:", &kwargs);
|
|
130
|
+
|
|
131
|
+
return parameterize(self, kwargs);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Hook into Ruby and define the FastParameterize::parameterize and
|
|
135
|
+
// String#parameterize.
|
|
136
|
+
void Init_fast_parameterize(void) {
|
|
137
|
+
VALUE rb_cFastParameterize = rb_define_module("FastParameterize");
|
|
138
|
+
rb_define_singleton_method(rb_cFastParameterize, "parameterize", fast_parameterize, -1);
|
|
139
|
+
rb_define_method(rb_cString, "parameterize", string_parameterize, -1);
|
|
140
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'lib/fast_parameterize/version'
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = 'fast_parameterize'
|
|
7
|
+
spec.version = FastParameterize::VERSION
|
|
8
|
+
spec.authors = ['Kevin Deisz']
|
|
9
|
+
spec.email = ['kevin.deisz@gmail.com']
|
|
10
|
+
|
|
11
|
+
spec.summary = 'Fast String#parameterize implementation'
|
|
12
|
+
spec.description = 'Provides a C-optimized method for parameterizing a string'
|
|
13
|
+
spec.homepage = 'https://github.com/kddeisz/fast_parameterize'
|
|
14
|
+
spec.license = 'MIT'
|
|
15
|
+
|
|
16
|
+
spec.files = Dir.chdir(__dir__) do
|
|
17
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
spec.bindir = 'exe'
|
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
24
|
+
spec.require_paths = %w[lib]
|
|
25
|
+
spec.extensions = ['ext/fast_parameterize/extconf.rb']
|
|
26
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'fast_parameterize/version'
|
|
4
|
+
require 'fast_parameterize/fast_parameterize'
|
|
5
|
+
|
|
6
|
+
# By default, FastParameterize already defines String#parameterize and
|
|
7
|
+
# FastParameterize::parameterize. In the case that we're using ActiveSupport,
|
|
8
|
+
# however, there is the additional functionality of transliteration that we need
|
|
9
|
+
# to account for. In this case, we're going to first allow ActiveSupport to do
|
|
10
|
+
# that work, then we're going to do the actual parameterization.
|
|
11
|
+
module FastParameterize
|
|
12
|
+
# Override ActiveSupport::Inflector::parameterize to use
|
|
13
|
+
# FastParameterize::parameterize.
|
|
14
|
+
module ActiveSupportInflectorPatch
|
|
15
|
+
def parameterize(string, separator: '-', preserve_case: false, locale: nil)
|
|
16
|
+
FastParameterize.parameterize(
|
|
17
|
+
ActiveSupport::Inflector.transliterate(string, locale: locale),
|
|
18
|
+
separator: separator,
|
|
19
|
+
preserve_case: preserve_case
|
|
20
|
+
)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Override String#parameterize to use FastParameterize::parameterize.
|
|
25
|
+
module ActiveSupportStringPatch
|
|
26
|
+
def parameterize(separator: '-', preserve_case: false, locale: nil)
|
|
27
|
+
FastParameterize.parameterize(
|
|
28
|
+
ActiveSupport::Inflector.transliterate(self, locale: locale),
|
|
29
|
+
separator: separator,
|
|
30
|
+
preserve_case: preserve_case
|
|
31
|
+
)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Override ActiveSupport::Inflector::method_added so that if and when the
|
|
36
|
+
# parameterize method gets defined, we can immediately redefine it.
|
|
37
|
+
module ActiveSupportDelayedPatch
|
|
38
|
+
def method_added(method)
|
|
39
|
+
FastParameterize.active_support if method == :parameterize
|
|
40
|
+
super
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Hook into ActiveSupport::Inflector and String to take advantage of
|
|
45
|
+
# FastParameterize.
|
|
46
|
+
def self.active_support
|
|
47
|
+
ActiveSupport::Inflector.alias_method(:as_parameterize, :parameterize)
|
|
48
|
+
ActiveSupport::Inflector.extend(ActiveSupportInflectorPatch)
|
|
49
|
+
String.include(ActiveSupportStringPatch)
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
if defined?(ActiveSupport)
|
|
54
|
+
FastParameterize.active_support
|
|
55
|
+
else
|
|
56
|
+
module ActiveSupport
|
|
57
|
+
module Inflector
|
|
58
|
+
prepend(FastParameterize::ActiveSupportDelayedPatch)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
module FastParameterize
|
|
2
|
+
VERSION: String
|
|
3
|
+
|
|
4
|
+
def self.active_support: () -> void
|
|
5
|
+
def self.parameterize: (String string, ?separator: String, ?preserve_case: bool) -> String
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
class String
|
|
9
|
+
def parameterize: (?separator: String, ?preserve_case: bool) -> String
|
|
10
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: fast_parameterize
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Kevin Deisz
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2021-04-16 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Provides a C-optimized method for parameterizing a string
|
|
14
|
+
email:
|
|
15
|
+
- kevin.deisz@gmail.com
|
|
16
|
+
executables: []
|
|
17
|
+
extensions:
|
|
18
|
+
- ext/fast_parameterize/extconf.rb
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- ".github/dependabot.yml"
|
|
22
|
+
- ".github/workflows/main.yml"
|
|
23
|
+
- ".gitignore"
|
|
24
|
+
- ".rubocop.yml"
|
|
25
|
+
- CHANGELOG.md
|
|
26
|
+
- CODE_OF_CONDUCT.md
|
|
27
|
+
- Gemfile
|
|
28
|
+
- Gemfile.lock
|
|
29
|
+
- LICENSE
|
|
30
|
+
- README.md
|
|
31
|
+
- Rakefile
|
|
32
|
+
- Steepfile
|
|
33
|
+
- bin/bench
|
|
34
|
+
- bin/console
|
|
35
|
+
- bin/setup
|
|
36
|
+
- ext/fast_parameterize/extconf.rb
|
|
37
|
+
- ext/fast_parameterize/fast_parameterize.c
|
|
38
|
+
- fast_parameterize.gemspec
|
|
39
|
+
- lib/fast_parameterize.rb
|
|
40
|
+
- lib/fast_parameterize/version.rb
|
|
41
|
+
- sig/fast_parameterize.rbs
|
|
42
|
+
homepage: https://github.com/kddeisz/fast_parameterize
|
|
43
|
+
licenses:
|
|
44
|
+
- MIT
|
|
45
|
+
metadata: {}
|
|
46
|
+
post_install_message:
|
|
47
|
+
rdoc_options: []
|
|
48
|
+
require_paths:
|
|
49
|
+
- lib
|
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - ">="
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0'
|
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
56
|
+
requirements:
|
|
57
|
+
- - ">="
|
|
58
|
+
- !ruby/object:Gem::Version
|
|
59
|
+
version: '0'
|
|
60
|
+
requirements: []
|
|
61
|
+
rubygems_version: 3.2.3
|
|
62
|
+
signing_key:
|
|
63
|
+
specification_version: 4
|
|
64
|
+
summary: Fast String#parameterize implementation
|
|
65
|
+
test_files: []
|