fast_blank 1.0.1-java
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/MIT-LICENSE +21 -0
- data/README.md +146 -0
- data/benchmark +166 -0
- data/ext/java/com/headius/jruby/fast_blank/FastBlankLibrary.java +119 -0
- data/lib/.gemkeep +0 -0
- data/lib/fast_blank.jar +0 -0
- data/lib/fast_blank.rb +12 -0
- data/spec/fast_blank_spec.rb +57 -0
- metadata +93 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 1c72ae742348e8648feb70ef9fecb367c7e1586764b3708e89621938ec7ec88c
|
|
4
|
+
data.tar.gz: 85226b0fdb4c4e5b124e2292d32f0826d1d116b2d9b324fff3ce5bde53496c35
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 0655db9256f773dad6d67c5741c20a35b32892c165e19f89e3550c8fc21d66f93be21640bcdacfb73bac30b09c89edf5851bf1c98c5c815d30a465fc72b439cf
|
|
7
|
+
data.tar.gz: e798ec0bdda4880cc50d9950a0b3e9928eba66e2084cba329b8293dc8170eacf0d2a31b74f5ee66c1cfa65c78e3d0b6a3ddfe39c99a208b2743a260f8a21d48d
|
data/MIT-LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
Copyright (c) 2006-2009 Steve Sloan
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
21
|
+
|
data/README.md
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
### `String#blank?` Ruby Extension
|
|
2
|
+
|
|
3
|
+
[](http://badge.fury.io/rb/fast_blank) [](https://travis-ci.org/SamSaffron/fast_blank)
|
|
4
|
+
|
|
5
|
+
`fast_blank` is a simple C extension which provides a fast implementation of [Active Support's `String#blank?` method](http://api.rubyonrails.org/classes/String.html#method-i-blank-3F).
|
|
6
|
+
|
|
7
|
+
### How do you use it?
|
|
8
|
+
|
|
9
|
+
require 'fast_blank'
|
|
10
|
+
|
|
11
|
+
or add it to your Bundler Gemfile
|
|
12
|
+
|
|
13
|
+
gem 'fast_blank'
|
|
14
|
+
|
|
15
|
+
### How fast is "Fast"?
|
|
16
|
+
|
|
17
|
+
About 1.2–20x faster than Active Support on my machine (your mileage my vary, depends on string length):
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
$ bundle exec ./benchmark
|
|
21
|
+
|
|
22
|
+
================== Test String Length: 0 ==================
|
|
23
|
+
Calculating -------------------------------------
|
|
24
|
+
Fast Blank 225.251k i/100ms
|
|
25
|
+
Fast ActiveSupport 225.676k i/100ms
|
|
26
|
+
Slow Blank 110.934k i/100ms
|
|
27
|
+
New Slow Blank 221.792k i/100ms
|
|
28
|
+
-------------------------------------------------
|
|
29
|
+
Fast Blank 29.673M (± 2.7%) i/s - 148.215M
|
|
30
|
+
Fast ActiveSupport 28.249M (± 3.5%) i/s - 141.048M
|
|
31
|
+
Slow Blank 2.158M (± 3.3%) i/s - 10.872M
|
|
32
|
+
New Slow Blank 23.558M (± 3.2%) i/s - 117.772M
|
|
33
|
+
|
|
34
|
+
Comparison:
|
|
35
|
+
Fast Blank: 29673200.1 i/s
|
|
36
|
+
Fast ActiveSupport: 28248894.5 i/s - 1.05x slower
|
|
37
|
+
New Slow Blank: 23557900.0 i/s - 1.26x slower
|
|
38
|
+
Slow Blank: 2157787.7 i/s - 13.75x slower
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
================== Test String Length: 6 ==================
|
|
42
|
+
Calculating -------------------------------------
|
|
43
|
+
Fast Blank 201.185k i/100ms
|
|
44
|
+
Fast ActiveSupport 205.076k i/100ms
|
|
45
|
+
Slow Blank 102.061k i/100ms
|
|
46
|
+
New Slow Blank 123.087k i/100ms
|
|
47
|
+
-------------------------------------------------
|
|
48
|
+
Fast Blank 13.894M (± 2.3%) i/s - 69.409M
|
|
49
|
+
Fast ActiveSupport 14.627M (± 3.5%) i/s - 73.212M
|
|
50
|
+
Slow Blank 1.943M (± 2.3%) i/s - 9.798M
|
|
51
|
+
New Slow Blank 2.796M (± 1.8%) i/s - 14.032M
|
|
52
|
+
|
|
53
|
+
Comparison:
|
|
54
|
+
Fast ActiveSupport: 14627063.7 i/s
|
|
55
|
+
Fast Blank: 13893631.2 i/s - 1.05x slower
|
|
56
|
+
New Slow Blank: 2795783.3 i/s - 5.23x slower
|
|
57
|
+
Slow Blank: 1943025.9 i/s - 7.53x slower
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
================== Test String Length: 14 ==================
|
|
61
|
+
Calculating -------------------------------------
|
|
62
|
+
Fast Blank 220.004k i/100ms
|
|
63
|
+
Fast ActiveSupport 219.716k i/100ms
|
|
64
|
+
Slow Blank 147.399k i/100ms
|
|
65
|
+
New Slow Blank 106.651k i/100ms
|
|
66
|
+
-------------------------------------------------
|
|
67
|
+
Fast Blank 24.949M (± 3.0%) i/s - 124.742M
|
|
68
|
+
Fast ActiveSupport 24.491M (± 3.3%) i/s - 122.382M
|
|
69
|
+
Slow Blank 4.292M (± 1.6%) i/s - 21.520M
|
|
70
|
+
New Slow Blank 2.115M (± 2.4%) i/s - 10.665M
|
|
71
|
+
|
|
72
|
+
Comparison:
|
|
73
|
+
Fast Blank: 24948558.8 i/s
|
|
74
|
+
Fast ActiveSupport: 24491245.1 i/s - 1.02x slower
|
|
75
|
+
Slow Blank: 4292490.5 i/s - 5.81x slower
|
|
76
|
+
New Slow Blank: 2115097.6 i/s - 11.80x slower
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
================== Test String Length: 24 ==================
|
|
80
|
+
Calculating -------------------------------------
|
|
81
|
+
Fast Blank 206.555k i/100ms
|
|
82
|
+
Fast ActiveSupport 208.513k i/100ms
|
|
83
|
+
Slow Blank 137.733k i/100ms
|
|
84
|
+
New Slow Blank 101.215k i/100ms
|
|
85
|
+
-------------------------------------------------
|
|
86
|
+
Fast Blank 16.761M (± 2.7%) i/s - 83.861M
|
|
87
|
+
Fast ActiveSupport 17.710M (± 3.2%) i/s - 88.618M
|
|
88
|
+
Slow Blank 3.744M (± 2.0%) i/s - 18.732M
|
|
89
|
+
New Slow Blank 1.962M (± 2.7%) i/s - 9.818M
|
|
90
|
+
|
|
91
|
+
Comparison:
|
|
92
|
+
Fast ActiveSupport: 17709936.5 i/s
|
|
93
|
+
Fast Blank: 16760839.7 i/s - 1.06x slower
|
|
94
|
+
Slow Blank: 3744048.4 i/s - 4.73x slower
|
|
95
|
+
New Slow Blank: 1961831.1 i/s - 9.03x slower
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
================== Test String Length: 136 ==================
|
|
99
|
+
Calculating -------------------------------------
|
|
100
|
+
Fast Blank 201.772k i/100ms
|
|
101
|
+
Fast ActiveSupport 189.120k i/100ms
|
|
102
|
+
Slow Blank 129.439k i/100ms
|
|
103
|
+
New Slow Blank 90.677k i/100ms
|
|
104
|
+
-------------------------------------------------
|
|
105
|
+
Fast Blank 16.718M (± 2.8%) i/s - 83.534M
|
|
106
|
+
Fast ActiveSupport 17.617M (± 3.6%) i/s - 87.941M
|
|
107
|
+
Slow Blank 3.725M (± 3.0%) i/s - 18.639M
|
|
108
|
+
New Slow Blank 1.940M (± 4.8%) i/s - 9.702M
|
|
109
|
+
|
|
110
|
+
Comparison:
|
|
111
|
+
Fast ActiveSupport: 17616782.1 i/s
|
|
112
|
+
Fast Blank: 16718307.8 i/s - 1.05x slower
|
|
113
|
+
Slow Blank: 3725097.6 i/s - 4.73x slower
|
|
114
|
+
New Slow Blank: 1940271.2 i/s - 9.08x slower
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
Additionally, this gem allocates no strings during the test, making it less of a GC burden.
|
|
120
|
+
|
|
121
|
+
### Compatibility note:
|
|
122
|
+
|
|
123
|
+
`fast_blank` supports MRI Ruby 1.9.3, 2.0, 2.1, and 2.2, as well as Rubinius 2.x. Earlier versions of MRI are untested.
|
|
124
|
+
|
|
125
|
+
`fast_blank` implements `String#blank?` as MRI would have implemented it, meaning it has 100% parity with `String#strip.length == 0`.
|
|
126
|
+
|
|
127
|
+
Active Support's version also considers Unicode spaces. For example, `"\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000".blank?` is true in Active Support even though `fast_blank` would treat it as *not* blank. Therefore, `fast_blank` also provides `blank_as?` which is a 100%-compatible Active Support `blank?` replacement.
|
|
128
|
+
|
|
129
|
+
### Credits
|
|
130
|
+
|
|
131
|
+
* Author: Sam Saffron (sam.saffron@gmail.com)
|
|
132
|
+
* https://github.com/SamSaffron/fast_blank
|
|
133
|
+
* License: MIT
|
|
134
|
+
* Gem template based on [CodeMonkeySteve/fast_xor](https://github.com/CodeMonkeySteve/fast_xor)
|
|
135
|
+
|
|
136
|
+
### Change log:
|
|
137
|
+
|
|
138
|
+
1.0.1:
|
|
139
|
+
- Minor, avoid warnings if redefining blank?
|
|
140
|
+
|
|
141
|
+
1.0.0:
|
|
142
|
+
- Adds Ruby 2.2 support ([@tjschuck](https://github.com/tjschuck) — [#9](https://github.com/SamSaffron/fast_blank/pull/9))
|
|
143
|
+
|
|
144
|
+
0.0.2:
|
|
145
|
+
- Removed rake dependency ([@tmm1](https://github.com/tmm1) — [#2](https://github.com/SamSaffron/fast_blank/pull/2))
|
|
146
|
+
- Unrolled internal loop to improve perf ([@tmm1](https://github.com/tmm1) — [#2](https://github.com/SamSaffron/fast_blank/pull/2))
|
data/benchmark
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
$: << File.dirname(__FILE__)+'/lib'
|
|
3
|
+
require 'benchmark/ips'
|
|
4
|
+
require 'fast_blank'
|
|
5
|
+
|
|
6
|
+
class String
|
|
7
|
+
# active support implementation
|
|
8
|
+
def slow_blank?
|
|
9
|
+
/\A[[:space:]]*\z/ === self
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def new_slow_blank?
|
|
13
|
+
empty? || !(/[[:^space:]]/ === self)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
test_strings = [
|
|
18
|
+
"",
|
|
19
|
+
"\r\n\r\n ",
|
|
20
|
+
"this is a test",
|
|
21
|
+
" this is a longer test",
|
|
22
|
+
" this is a longer test
|
|
23
|
+
this is a longer test
|
|
24
|
+
this is a longer test
|
|
25
|
+
this is a longer test
|
|
26
|
+
this is a longer test"
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
test_strings.each do |s|
|
|
30
|
+
raise "failed on #{s.inspect}" if s.blank? != s.slow_blank?
|
|
31
|
+
raise "failed on #{s.inspect}" if s.blank? != s.new_slow_blank?
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
test_strings.each do |s|
|
|
35
|
+
puts "\n================== Test String Length: #{s.length} =================="
|
|
36
|
+
Benchmark.ips do |x|
|
|
37
|
+
x.report("Fast Blank") do |times|
|
|
38
|
+
i = 0
|
|
39
|
+
while i < times
|
|
40
|
+
s.blank?
|
|
41
|
+
i += 1
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
x.report("Fast ActiveSupport") do |times|
|
|
46
|
+
i = 0
|
|
47
|
+
while i < times
|
|
48
|
+
s.blank_as?
|
|
49
|
+
i += 1
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
x.report("Slow Blank") do |times|
|
|
54
|
+
i = 0
|
|
55
|
+
while i < times
|
|
56
|
+
s.slow_blank?
|
|
57
|
+
i += 1
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
x.report("New Slow Blank") do |times|
|
|
62
|
+
i = 0
|
|
63
|
+
while i < times
|
|
64
|
+
s.new_slow_blank?
|
|
65
|
+
i += 1
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
x.compare!
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# ================== Test String Length: 0 ==================
|
|
74
|
+
# Calculating -------------------------------------
|
|
75
|
+
# Fast Blank 225.251k i/100ms
|
|
76
|
+
# Fast ActiveSupport 225.676k i/100ms
|
|
77
|
+
# Slow Blank 110.934k i/100ms
|
|
78
|
+
# New Slow Blank 221.792k i/100ms
|
|
79
|
+
# -------------------------------------------------
|
|
80
|
+
# Fast Blank 29.673M (± 2.7%) i/s - 148.215M
|
|
81
|
+
# Fast ActiveSupport 28.249M (± 3.5%) i/s - 141.048M
|
|
82
|
+
# Slow Blank 2.158M (± 3.3%) i/s - 10.872M
|
|
83
|
+
# New Slow Blank 23.558M (± 3.2%) i/s - 117.772M
|
|
84
|
+
#
|
|
85
|
+
# Comparison:
|
|
86
|
+
# Fast Blank: 29673200.1 i/s
|
|
87
|
+
# Fast ActiveSupport: 28248894.5 i/s - 1.05x slower
|
|
88
|
+
# New Slow Blank: 23557900.0 i/s - 1.26x slower
|
|
89
|
+
# Slow Blank: 2157787.7 i/s - 13.75x slower
|
|
90
|
+
#
|
|
91
|
+
#
|
|
92
|
+
# ================== Test String Length: 6 ==================
|
|
93
|
+
# Calculating -------------------------------------
|
|
94
|
+
# Fast Blank 201.185k i/100ms
|
|
95
|
+
# Fast ActiveSupport 205.076k i/100ms
|
|
96
|
+
# Slow Blank 102.061k i/100ms
|
|
97
|
+
# New Slow Blank 123.087k i/100ms
|
|
98
|
+
# -------------------------------------------------
|
|
99
|
+
# Fast Blank 13.894M (± 2.3%) i/s - 69.409M
|
|
100
|
+
# Fast ActiveSupport 14.627M (± 3.5%) i/s - 73.212M
|
|
101
|
+
# Slow Blank 1.943M (± 2.3%) i/s - 9.798M
|
|
102
|
+
# New Slow Blank 2.796M (± 1.8%) i/s - 14.032M
|
|
103
|
+
#
|
|
104
|
+
# Comparison:
|
|
105
|
+
# Fast ActiveSupport: 14627063.7 i/s
|
|
106
|
+
# Fast Blank: 13893631.2 i/s - 1.05x slower
|
|
107
|
+
# New Slow Blank: 2795783.3 i/s - 5.23x slower
|
|
108
|
+
# Slow Blank: 1943025.9 i/s - 7.53x slower
|
|
109
|
+
#
|
|
110
|
+
#
|
|
111
|
+
# ================== Test String Length: 14 ==================
|
|
112
|
+
# Calculating -------------------------------------
|
|
113
|
+
# Fast Blank 220.004k i/100ms
|
|
114
|
+
# Fast ActiveSupport 219.716k i/100ms
|
|
115
|
+
# Slow Blank 147.399k i/100ms
|
|
116
|
+
# New Slow Blank 106.651k i/100ms
|
|
117
|
+
# -------------------------------------------------
|
|
118
|
+
# Fast Blank 24.949M (± 3.0%) i/s - 124.742M
|
|
119
|
+
# Fast ActiveSupport 24.491M (± 3.3%) i/s - 122.382M
|
|
120
|
+
# Slow Blank 4.292M (± 1.6%) i/s - 21.520M
|
|
121
|
+
# New Slow Blank 2.115M (± 2.4%) i/s - 10.665M
|
|
122
|
+
#
|
|
123
|
+
# Comparison:
|
|
124
|
+
# Fast Blank: 24948558.8 i/s
|
|
125
|
+
# Fast ActiveSupport: 24491245.1 i/s - 1.02x slower
|
|
126
|
+
# Slow Blank: 4292490.5 i/s - 5.81x slower
|
|
127
|
+
# New Slow Blank: 2115097.6 i/s - 11.80x slower
|
|
128
|
+
#
|
|
129
|
+
#
|
|
130
|
+
# ================== Test String Length: 24 ==================
|
|
131
|
+
# Calculating -------------------------------------
|
|
132
|
+
# Fast Blank 206.555k i/100ms
|
|
133
|
+
# Fast ActiveSupport 208.513k i/100ms
|
|
134
|
+
# Slow Blank 137.733k i/100ms
|
|
135
|
+
# New Slow Blank 101.215k i/100ms
|
|
136
|
+
# -------------------------------------------------
|
|
137
|
+
# Fast Blank 16.761M (± 2.7%) i/s - 83.861M
|
|
138
|
+
# Fast ActiveSupport 17.710M (± 3.2%) i/s - 88.618M
|
|
139
|
+
# Slow Blank 3.744M (± 2.0%) i/s - 18.732M
|
|
140
|
+
# New Slow Blank 1.962M (± 2.7%) i/s - 9.818M
|
|
141
|
+
#
|
|
142
|
+
# Comparison:
|
|
143
|
+
# Fast ActiveSupport: 17709936.5 i/s
|
|
144
|
+
# Fast Blank: 16760839.7 i/s - 1.06x slower
|
|
145
|
+
# Slow Blank: 3744048.4 i/s - 4.73x slower
|
|
146
|
+
# New Slow Blank: 1961831.1 i/s - 9.03x slower
|
|
147
|
+
#
|
|
148
|
+
#
|
|
149
|
+
# ================== Test String Length: 136 ==================
|
|
150
|
+
# Calculating -------------------------------------
|
|
151
|
+
# Fast Blank 201.772k i/100ms
|
|
152
|
+
# Fast ActiveSupport 189.120k i/100ms
|
|
153
|
+
# Slow Blank 129.439k i/100ms
|
|
154
|
+
# New Slow Blank 90.677k i/100ms
|
|
155
|
+
# -------------------------------------------------
|
|
156
|
+
# Fast Blank 16.718M (± 2.8%) i/s - 83.534M
|
|
157
|
+
# Fast ActiveSupport 17.617M (± 3.6%) i/s - 87.941M
|
|
158
|
+
# Slow Blank 3.725M (± 3.0%) i/s - 18.639M
|
|
159
|
+
# New Slow Blank 1.940M (± 4.8%) i/s - 9.702M
|
|
160
|
+
#
|
|
161
|
+
# Comparison:
|
|
162
|
+
# Fast ActiveSupport: 17616782.1 i/s
|
|
163
|
+
# Fast Blank: 16718307.8 i/s - 1.05x slower
|
|
164
|
+
# Slow Blank: 3725097.6 i/s - 4.73x slower
|
|
165
|
+
# New Slow Blank: 1940271.2 i/s - 9.08x slower
|
|
166
|
+
#
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
package com.headius.jruby.fast_blank;
|
|
2
|
+
|
|
3
|
+
import org.jcodings.Encoding;
|
|
4
|
+
import org.jruby.Ruby;
|
|
5
|
+
import org.jruby.RubyString;
|
|
6
|
+
import org.jruby.anno.JRubyMethod;
|
|
7
|
+
import org.jruby.runtime.ThreadContext;
|
|
8
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
|
9
|
+
import org.jruby.runtime.load.Library;
|
|
10
|
+
import org.jruby.util.ByteList;
|
|
11
|
+
import org.jruby.util.StringSupport;
|
|
12
|
+
import org.jruby.util.io.EncodingUtils;
|
|
13
|
+
|
|
14
|
+
public class FastBlankLibrary implements Library {
|
|
15
|
+
public void load(Ruby runtime, boolean wrap) {
|
|
16
|
+
runtime.getString().defineAnnotatedMethods(FastBlankLibrary.class);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
@JRubyMethod(name = "blank_as?")
|
|
20
|
+
public static IRubyObject blank_as_p(ThreadContext context, IRubyObject self) {
|
|
21
|
+
Encoding enc;
|
|
22
|
+
int s, e;
|
|
23
|
+
byte[] sBytes;
|
|
24
|
+
|
|
25
|
+
Ruby runtime = context.runtime;
|
|
26
|
+
|
|
27
|
+
RubyString str = (RubyString) self;
|
|
28
|
+
enc = str.getEncoding();
|
|
29
|
+
ByteList sByteList = str.getByteList();
|
|
30
|
+
sBytes = sByteList.unsafeBytes();
|
|
31
|
+
s = sByteList.begin();
|
|
32
|
+
if (str.size() == 0) return context.tru;
|
|
33
|
+
|
|
34
|
+
e = s + sByteList.realSize();
|
|
35
|
+
int[] n = {0};
|
|
36
|
+
while (s < e) {
|
|
37
|
+
int cc = EncodingUtils.encCodepointLength(runtime, sBytes, s, e, n, enc);
|
|
38
|
+
|
|
39
|
+
switch (cc) {
|
|
40
|
+
case 9:
|
|
41
|
+
case 0xa:
|
|
42
|
+
case 0xb:
|
|
43
|
+
case 0xc:
|
|
44
|
+
case 0xd:
|
|
45
|
+
case 0x20:
|
|
46
|
+
case 0x85:
|
|
47
|
+
case 0xa0:
|
|
48
|
+
case 0x1680:
|
|
49
|
+
case 0x2000:
|
|
50
|
+
case 0x2001:
|
|
51
|
+
case 0x2002:
|
|
52
|
+
case 0x2003:
|
|
53
|
+
case 0x2004:
|
|
54
|
+
case 0x2005:
|
|
55
|
+
case 0x2006:
|
|
56
|
+
case 0x2007:
|
|
57
|
+
case 0x2008:
|
|
58
|
+
case 0x2009:
|
|
59
|
+
case 0x200a:
|
|
60
|
+
case 0x2028:
|
|
61
|
+
case 0x2029:
|
|
62
|
+
case 0x202f:
|
|
63
|
+
case 0x205f:
|
|
64
|
+
case 0x3000:
|
|
65
|
+
/* found */
|
|
66
|
+
break;
|
|
67
|
+
default:
|
|
68
|
+
return context.fals;
|
|
69
|
+
}
|
|
70
|
+
s += n[0];
|
|
71
|
+
}
|
|
72
|
+
return context.tru;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
@JRubyMethod(name = "blank?")
|
|
76
|
+
public static IRubyObject blank_p(ThreadContext context, IRubyObject self) {
|
|
77
|
+
RubyString str = (RubyString) self;
|
|
78
|
+
|
|
79
|
+
if (str.size() == 0) return context.tru;
|
|
80
|
+
|
|
81
|
+
ByteList sByteList = str.getByteList();
|
|
82
|
+
byte[] sBytes = sByteList.unsafeBytes();
|
|
83
|
+
int s = sByteList.begin();
|
|
84
|
+
int e = s + sByteList.realSize();
|
|
85
|
+
|
|
86
|
+
// Move to slower path if the string contains non 7-bit ASCII.
|
|
87
|
+
if (str.getCodeRange() != StringSupport.CR_7BIT) return blankSlow(context, sBytes, s, e, str.getEncoding());
|
|
88
|
+
|
|
89
|
+
for (int i = s; i < e; i++) {
|
|
90
|
+
if (!isSpace(sBytes[i])) return context.fals;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return context.tru;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
private static boolean isSpace(byte c) {
|
|
97
|
+
return c == ' ' || ('\t' <= c && c <= '\r') || c == '\0';
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
private static IRubyObject blankSlow(ThreadContext context, byte[] bytes, int s, int e, Encoding enc) {
|
|
101
|
+
Ruby runtime = context.runtime;
|
|
102
|
+
int[] n = {0};
|
|
103
|
+
|
|
104
|
+
while (s < e) {
|
|
105
|
+
int cc = EncodingUtils.encCodepointLength(runtime, bytes, s, e, n, enc);
|
|
106
|
+
|
|
107
|
+
if (!isSpaceCodepoint(cc) && cc != 0) return context.fals;
|
|
108
|
+
s += n[0];
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return context.tru;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// MRI: rb_isspace
|
|
115
|
+
private static boolean isSpaceCodepoint(int codepoint) {
|
|
116
|
+
long c = codepoint & 0xFFFFFFFF;
|
|
117
|
+
return c == ' ' || ('\t' <= c && c <= '\r');
|
|
118
|
+
}
|
|
119
|
+
}
|
data/lib/.gemkeep
ADDED
|
File without changes
|
data/lib/fast_blank.jar
ADDED
|
Binary file
|
data/lib/fast_blank.rb
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
class ::String
|
|
2
|
+
# Explicitly undefine method before redefining to avoid Ruby warnings.
|
|
3
|
+
undef_method(:blank?) if method_defined?(:blank?)
|
|
4
|
+
end
|
|
5
|
+
|
|
6
|
+
case RUBY_ENGINE
|
|
7
|
+
when 'jruby'
|
|
8
|
+
require 'fast_blank.jar'
|
|
9
|
+
JRuby::Util.load_ext("com.headius.jruby.fast_blank.FastBlankLibrary")
|
|
10
|
+
else
|
|
11
|
+
require 'fast_blank.so'
|
|
12
|
+
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
$VERBOSE = true
|
|
2
|
+
|
|
3
|
+
class ::String
|
|
4
|
+
# Stub the original method to make sure it is redefined correctly.
|
|
5
|
+
def blank?
|
|
6
|
+
raise NotImplementedError
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def blank2?
|
|
10
|
+
/\A[[:space:]]*\z/ === self
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
require 'fast_blank'
|
|
15
|
+
|
|
16
|
+
describe String do
|
|
17
|
+
it "works" do
|
|
18
|
+
expect("".blank?).to eq(true)
|
|
19
|
+
expect(" ".blank?).to eq(true)
|
|
20
|
+
expect("\r\n".blank?).to eq(true)
|
|
21
|
+
"\r\n\v\f\r\s\u0085".blank? == true
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
it "provides a parity with active support function" do
|
|
25
|
+
(16*16*16*16).times do |i|
|
|
26
|
+
c = i.chr('UTF-8') rescue nil
|
|
27
|
+
unless c.nil?
|
|
28
|
+
expect("#{i.to_s(16)} #{c.blank_as?}").to eq("#{i.to_s(16)} #{c.blank2?}")
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
(256).times do |i|
|
|
34
|
+
c = i.chr('ASCII') rescue nil
|
|
35
|
+
unless c.nil?
|
|
36
|
+
expect("#{i.to_s(16)} #{c.blank_as?}").to eq("#{i.to_s(16)} #{c.blank2?}")
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it "has parity with strip.length" do
|
|
42
|
+
(256).times do |i|
|
|
43
|
+
c = i.chr('ASCII') rescue nil
|
|
44
|
+
unless c.nil?
|
|
45
|
+
expect("#{i.to_s(16)} #{c.strip.length == 0}").to eq("#{i.to_s(16)} #{c.blank?}")
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
it "treats \u0000 correctly" do
|
|
51
|
+
# odd I know
|
|
52
|
+
expect("\u0000".strip.length).to eq(0)
|
|
53
|
+
expect("\u0000".blank_as?).to be_falsey
|
|
54
|
+
expect("\u0000".blank?).to be_truthy
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: fast_blank
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.1
|
|
5
|
+
platform: java
|
|
6
|
+
authors:
|
|
7
|
+
- Sam Saffron
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2021-08-17 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0'
|
|
19
|
+
name: rake-compiler
|
|
20
|
+
prerelease: false
|
|
21
|
+
type: :development
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ">="
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0'
|
|
33
|
+
name: rspec
|
|
34
|
+
prerelease: false
|
|
35
|
+
type: :development
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ">="
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '0'
|
|
47
|
+
name: benchmark-ips
|
|
48
|
+
prerelease: false
|
|
49
|
+
type: :development
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - ">="
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0'
|
|
55
|
+
description: Provides a C-optimized method for determining if a string is blank
|
|
56
|
+
email: sam.saffron@gmail.com
|
|
57
|
+
executables: []
|
|
58
|
+
extensions: []
|
|
59
|
+
extra_rdoc_files: []
|
|
60
|
+
files:
|
|
61
|
+
- MIT-LICENSE
|
|
62
|
+
- README.md
|
|
63
|
+
- benchmark
|
|
64
|
+
- ext/java/com/headius/jruby/fast_blank/FastBlankLibrary.java
|
|
65
|
+
- lib/.gemkeep
|
|
66
|
+
- lib/fast_blank.jar
|
|
67
|
+
- lib/fast_blank.rb
|
|
68
|
+
- spec/fast_blank_spec.rb
|
|
69
|
+
homepage: https://github.com/SamSaffron/fast_blank
|
|
70
|
+
licenses:
|
|
71
|
+
- MIT
|
|
72
|
+
metadata: {}
|
|
73
|
+
post_install_message:
|
|
74
|
+
rdoc_options: []
|
|
75
|
+
require_paths:
|
|
76
|
+
- lib
|
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
78
|
+
requirements:
|
|
79
|
+
- - ">="
|
|
80
|
+
- !ruby/object:Gem::Version
|
|
81
|
+
version: '0'
|
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
83
|
+
requirements:
|
|
84
|
+
- - ">="
|
|
85
|
+
- !ruby/object:Gem::Version
|
|
86
|
+
version: '0'
|
|
87
|
+
requirements: []
|
|
88
|
+
rubygems_version: 3.2.29
|
|
89
|
+
signing_key:
|
|
90
|
+
specification_version: 4
|
|
91
|
+
summary: Fast String blank? implementation
|
|
92
|
+
test_files:
|
|
93
|
+
- spec/fast_blank_spec.rb
|