opal-onigmo 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.gitmodules +3 -0
- data/.rspec +3 -0
- data/.rspec-opal +3 -0
- data/.travis.yml +6 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +66 -0
- data/LICENSE.adoc +31 -0
- data/LICENSE.txt +21 -0
- data/README.adoc +57 -0
- data/Rakefile +35 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/opal/onigmo.rb +5 -0
- data/lib/opal/onigmo/version.rb +5 -0
- data/opal-onigmo.gemspec +31 -0
- data/opal/onigmo.rb +7 -0
- data/opal/onigmo/constants.rb +237 -0
- data/opal/onigmo/constants.rb.erb +16 -0
- data/opal/onigmo/core_ext.rb +21 -0
- data/opal/onigmo/ffi.rb +114 -0
- data/opal/onigmo/onigmo-wasm.wasm +0 -0
- data/opal/onigmo/regexp.rb +118 -0
- data/spec-opal/regexp_spec.rb +59 -0
- data/spec-opal/spec_helper.rb +17 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 42b7f0d79bf4c8caa956996c2c3e719e830e4b7d5cdc36a37f259bad0817d4be
|
4
|
+
data.tar.gz: 5d696a5c11e17b61aca28cefc0c275295dd9c5ffafd4e59cb173578e515d30db
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6183bcc0febb8a96b3380a91ba1a1073f77ed304acc19619adf2ba570108fc68085cd3517e32abd4339c647564d8ce8cc203639fb3ff8ee80a6411ccf5d53ad3
|
7
|
+
data.tar.gz: e45bb5cbccbb07eb87ee9de044769aada834639012fe0fdfc8cfa0115308aaa6ad5c8548c9b0698eef34c963cd6da6be0c6e389b52381d9c1a718fc0c7cb339c
|
data/.gitignore
ADDED
data/.gitmodules
ADDED
data/.rspec
ADDED
data/.rspec-opal
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
opal-onigmo (0.1.0)
|
5
|
+
opal-webassembly
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
ast (2.4.1)
|
11
|
+
coderay (1.1.3)
|
12
|
+
concurrent-ruby (1.1.7)
|
13
|
+
diff-lcs (1.4.4)
|
14
|
+
method_source (1.0.0)
|
15
|
+
opal (1.0.3)
|
16
|
+
ast (>= 2.3.0)
|
17
|
+
parser (~> 2.6)
|
18
|
+
opal-rspec (0.8.0.alpha2)
|
19
|
+
opal (>= 0.11, < 1.1)
|
20
|
+
opal-sprockets
|
21
|
+
rake (~> 12.0)
|
22
|
+
opal-sprockets (0.4.9.1.0.3.7)
|
23
|
+
opal (~> 1.0.0)
|
24
|
+
sprockets (~> 3.7)
|
25
|
+
tilt (>= 1.4)
|
26
|
+
opal-webassembly (0.1.0)
|
27
|
+
opal
|
28
|
+
parser (2.7.1.5)
|
29
|
+
ast (~> 2.4.1)
|
30
|
+
pry (0.13.1)
|
31
|
+
coderay (~> 1.1)
|
32
|
+
method_source (~> 1.0)
|
33
|
+
rack (2.2.3)
|
34
|
+
rake (12.3.3)
|
35
|
+
rspec (3.9.0)
|
36
|
+
rspec-core (~> 3.9.0)
|
37
|
+
rspec-expectations (~> 3.9.0)
|
38
|
+
rspec-mocks (~> 3.9.0)
|
39
|
+
rspec-core (3.9.3)
|
40
|
+
rspec-support (~> 3.9.3)
|
41
|
+
rspec-expectations (3.9.2)
|
42
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
43
|
+
rspec-support (~> 3.9.0)
|
44
|
+
rspec-mocks (3.9.1)
|
45
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
46
|
+
rspec-support (~> 3.9.0)
|
47
|
+
rspec-support (3.9.3)
|
48
|
+
sprockets (3.7.2)
|
49
|
+
concurrent-ruby (~> 1.0)
|
50
|
+
rack (> 1, < 3)
|
51
|
+
tilt (2.0.10)
|
52
|
+
|
53
|
+
PLATFORMS
|
54
|
+
ruby
|
55
|
+
|
56
|
+
DEPENDENCIES
|
57
|
+
opal (~> 1.0)
|
58
|
+
opal-onigmo!
|
59
|
+
opal-rspec
|
60
|
+
opal-webassembly
|
61
|
+
pry
|
62
|
+
rake (~> 12.0)
|
63
|
+
rspec (~> 3.0)
|
64
|
+
|
65
|
+
BUNDLED WITH
|
66
|
+
2.1.4
|
data/LICENSE.adoc
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
= Licenses & Copyright
|
2
|
+
|
3
|
+
This license file adheres to the formatting guidelines of
|
4
|
+
https://github.com/nevir/readable-licenses[readable-licenses].
|
5
|
+
|
6
|
+
|
7
|
+
== Ribose BSD 2-Clause License
|
8
|
+
|
9
|
+
Copyright (c) 2019-, https://www.ribose.com[Ribose Inc].
|
10
|
+
All rights reserved.
|
11
|
+
|
12
|
+
Redistribution and use in source and binary forms, with or without modification,
|
13
|
+
are permitted provided that the following conditions are met:
|
14
|
+
|
15
|
+
1. Redistributions of source code must retain the above copyright notice,
|
16
|
+
this list of conditions and the following disclaimer.
|
17
|
+
|
18
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
19
|
+
this list of conditions and the following disclaimer in the documentation
|
20
|
+
and/or other materials provided with the distribution.
|
21
|
+
|
22
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
23
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
24
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
25
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
26
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
27
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
28
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
29
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
30
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
31
|
+
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 TODO: Write your name
|
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.adoc
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
= Opal::Onigmo
|
2
|
+
|
3
|
+
Execute regular expressions in Opal with a native Ruby regexp engine - Onigmo - compiled using clang to WebAssembly.
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
[source,ruby]
|
10
|
+
----
|
11
|
+
gem 'opal-onigmo'
|
12
|
+
----
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
[source,sh]
|
17
|
+
----
|
18
|
+
$ bundle install
|
19
|
+
----
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
[source,sh]
|
24
|
+
----
|
25
|
+
$ gem install opal-onigmo
|
26
|
+
----
|
27
|
+
|
28
|
+
|
29
|
+
== Usage
|
30
|
+
|
31
|
+
[source,ruby]
|
32
|
+
----
|
33
|
+
# In your Opal code
|
34
|
+
|
35
|
+
require 'onigmo'
|
36
|
+
r = Onigmo::Regexp.new("^ab+")
|
37
|
+
"abbbcd".gsub(r, "dd") # => "ddcd"
|
38
|
+
r.match("test") # => false
|
39
|
+
|
40
|
+
require 'onigmo/core_ext'
|
41
|
+
"test".match(r) # => false
|
42
|
+
----
|
43
|
+
|
44
|
+
|
45
|
+
== Development
|
46
|
+
|
47
|
+
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.
|
48
|
+
|
49
|
+
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 https://rubygems.org[rubygems.org].
|
50
|
+
|
51
|
+
== Contributing
|
52
|
+
|
53
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/hmdne/opal-onigmo.
|
54
|
+
|
55
|
+
== License
|
56
|
+
|
57
|
+
The gem is available as open source under the terms of the BSD 2-clause license.
|
data/Rakefile
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
require "opal/rspec/rake_task"
|
4
|
+
require 'erb'
|
5
|
+
|
6
|
+
RSpec::Core::RakeTask.new(:spec)
|
7
|
+
|
8
|
+
Opal::RSpec::RakeTask.new("spec-opal") do |server, task|
|
9
|
+
require 'opal/onigmo'
|
10
|
+
end
|
11
|
+
|
12
|
+
task :generate => ['opal/onigmo/constants.rb', 'opal/onigmo/onigmo.wasm'] do
|
13
|
+
end
|
14
|
+
|
15
|
+
def render_template(template, output, vars)
|
16
|
+
tmpl = File.read(template)
|
17
|
+
erb = ERB.new(tmpl, 0, "<>")
|
18
|
+
File.open(output, "w") do |f|
|
19
|
+
f.puts erb.result_with_hash(vars)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
file 'opal/onigmo/constants.rb' => ['opal/onigmo/constants.rb.erb', 'Onigmo/onigmo.h'] do |task|
|
24
|
+
render_template(
|
25
|
+
File.expand_path(task.prerequisites.first, __dir__),
|
26
|
+
task.name,
|
27
|
+
{onigmo_h: File.read('Onigmo/onigmo.h')}
|
28
|
+
)
|
29
|
+
end
|
30
|
+
|
31
|
+
file 'opal/onigmo/onigmo.wasm' => ['Onigmo/build-wasm.sh'] do |task|
|
32
|
+
sh "cd Onigmo; sh ./build-wasm.sh; mv onigmo.wasm #{task.name}"
|
33
|
+
end
|
34
|
+
|
35
|
+
task :default => [:spec, "spec-opal"]
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "opal/onigmo"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/opal/onigmo.rb
ADDED
data/opal-onigmo.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require_relative 'lib/opal/onigmo/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "opal-onigmo"
|
5
|
+
spec.version = Opal::Onigmo::VERSION
|
6
|
+
spec.authors = ["Ribose Inc."]
|
7
|
+
spec.email = ["open.source@ribose.com"]
|
8
|
+
|
9
|
+
spec.summary = %q{Execute regexps with Opal using Onigmo}
|
10
|
+
spec.description = %q{Execute regexps in Opal with a native Ruby regexp engine - Onigmo - compiled using clang to WebAssembly}
|
11
|
+
spec.homepage = "https://github.com/interscript/opal-onigmo"
|
12
|
+
spec.license = "BSD-2-Clause"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
14
|
+
|
15
|
+
#spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
16
|
+
|
17
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
18
|
+
spec.metadata["source_code_uri"] = "https://github.com/interscript/opal-onigmo"
|
19
|
+
spec.metadata["changelog_uri"] = "https://github.com/interscript/opal-onigmo/commits/master"
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
24
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|Onigmo)/}) }
|
25
|
+
end
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_dependency "opal-webassembly"
|
31
|
+
end
|
data/opal/onigmo.rb
ADDED
@@ -0,0 +1,237 @@
|
|
1
|
+
# Copy constants from Onigmo/onigmo.h
|
2
|
+
|
3
|
+
module Onigmo
|
4
|
+
module Constants
|
5
|
+
|
6
|
+
ONIGMO_VERSION_MAJOR = 6
|
7
|
+
ONIGMO_VERSION_MINOR = 2
|
8
|
+
ONIGMO_VERSION_TEENY = 0
|
9
|
+
OnigCodePointMaskWidth = 3
|
10
|
+
OnigCodePointMask = ((1<<OnigCodePointMaskWidth)-1)
|
11
|
+
OnigSpecialIndexShift = 3
|
12
|
+
OnigSpecialIndexWidth = 10
|
13
|
+
ONIGENC_CASE_UPCASE = (1<<13)
|
14
|
+
ONIGENC_CASE_DOWNCASE = (1<<14)
|
15
|
+
ONIGENC_CASE_TITLECASE = (1<<15)
|
16
|
+
ONIGENC_CASE_SPECIAL_OFFSET = 3
|
17
|
+
ONIGENC_CASE_UP_SPECIAL = (1<<16)
|
18
|
+
ONIGENC_CASE_DOWN_SPECIAL = (1<<17)
|
19
|
+
ONIGENC_CASE_MODIFIED = (1<<18)
|
20
|
+
ONIGENC_CASE_FOLD = (1<<19)
|
21
|
+
ONIGENC_CASE_FOLD_TURKISH_AZERI = (1<<20)
|
22
|
+
ONIGENC_CASE_FOLD_LITHUANIAN = (1<<21)
|
23
|
+
ONIGENC_CASE_ASCII_ONLY = (1<<22)
|
24
|
+
ONIGENC_CASE_IS_TITLECASE = (1<<23)
|
25
|
+
INTERNAL_ONIGENC_CASE_FOLD_MULTI_CHAR = (1<<30)
|
26
|
+
ONIGENC_CASE_FOLD_MIN = INTERNAL_ONIGENC_CASE_FOLD_MULTI_CHAR
|
27
|
+
ONIGENC_MAX_COMP_CASE_FOLD_CODE_LEN = 3
|
28
|
+
ONIGENC_GET_CASE_FOLD_CODES_MAX_NUM = 13
|
29
|
+
ONIGENC_CODE_TO_MBC_MAXLEN = 7
|
30
|
+
ONIGENC_MBC_CASE_FOLD_MAXLEN = 18
|
31
|
+
ONIGENC_CTYPE_NEWLINE = 0
|
32
|
+
ONIGENC_CTYPE_ALPHA = 1
|
33
|
+
ONIGENC_CTYPE_BLANK = 2
|
34
|
+
ONIGENC_CTYPE_CNTRL = 3
|
35
|
+
ONIGENC_CTYPE_DIGIT = 4
|
36
|
+
ONIGENC_CTYPE_GRAPH = 5
|
37
|
+
ONIGENC_CTYPE_LOWER = 6
|
38
|
+
ONIGENC_CTYPE_PRINT = 7
|
39
|
+
ONIGENC_CTYPE_PUNCT = 8
|
40
|
+
ONIGENC_CTYPE_SPACE = 9
|
41
|
+
ONIGENC_CTYPE_UPPER = 10
|
42
|
+
ONIGENC_CTYPE_XDIGIT = 11
|
43
|
+
ONIGENC_CTYPE_WORD = 12
|
44
|
+
ONIGENC_CTYPE_ALNUM = 13
|
45
|
+
ONIGENC_CTYPE_ASCII = 14
|
46
|
+
ONIGENC_MAX_STD_CTYPE = ONIGENC_CTYPE_ASCII
|
47
|
+
ONIGENC_FLAG_NONE = 0
|
48
|
+
ONIGENC_FLAG_UNICODE = 1
|
49
|
+
ONIG_NREGION = 10
|
50
|
+
ONIG_MAX_CAPTURE_GROUP_NUM = 32767
|
51
|
+
ONIG_MAX_BACKREF_NUM = 1000
|
52
|
+
ONIG_MAX_REPEAT_NUM = 100000
|
53
|
+
ONIG_MAX_MULTI_BYTE_RANGES_NUM = 10000
|
54
|
+
ONIG_MAX_ERROR_MESSAGE_LEN = 90
|
55
|
+
ONIG_OPTION_NONE = 0
|
56
|
+
ONIG_OPTION_IGNORECASE = 1
|
57
|
+
ONIG_OPTION_EXTEND = (ONIG_OPTION_IGNORECASE << 1)
|
58
|
+
ONIG_OPTION_MULTILINE = (ONIG_OPTION_EXTEND << 1)
|
59
|
+
ONIG_OPTION_DOTALL = ONIG_OPTION_MULTILINE
|
60
|
+
ONIG_OPTION_SINGLELINE = (ONIG_OPTION_MULTILINE << 1)
|
61
|
+
ONIG_OPTION_FIND_LONGEST = (ONIG_OPTION_SINGLELINE << 1)
|
62
|
+
ONIG_OPTION_FIND_NOT_EMPTY = (ONIG_OPTION_FIND_LONGEST << 1)
|
63
|
+
ONIG_OPTION_NEGATE_SINGLELINE = (ONIG_OPTION_FIND_NOT_EMPTY << 1)
|
64
|
+
ONIG_OPTION_DONT_CAPTURE_GROUP = (ONIG_OPTION_NEGATE_SINGLELINE << 1)
|
65
|
+
ONIG_OPTION_CAPTURE_GROUP = (ONIG_OPTION_DONT_CAPTURE_GROUP << 1)
|
66
|
+
ONIG_OPTION_NOTBOL = (ONIG_OPTION_CAPTURE_GROUP << 1)
|
67
|
+
ONIG_OPTION_NOTEOL = (ONIG_OPTION_NOTBOL << 1)
|
68
|
+
ONIG_OPTION_NOTBOS = (ONIG_OPTION_NOTEOL << 1)
|
69
|
+
ONIG_OPTION_NOTEOS = (ONIG_OPTION_NOTBOS << 1)
|
70
|
+
ONIG_OPTION_ASCII_RANGE = (ONIG_OPTION_NOTEOS << 1)
|
71
|
+
ONIG_OPTION_POSIX_BRACKET_ALL_RANGE = (ONIG_OPTION_ASCII_RANGE << 1)
|
72
|
+
ONIG_OPTION_WORD_BOUND_ALL_RANGE = (ONIG_OPTION_POSIX_BRACKET_ALL_RANGE << 1)
|
73
|
+
ONIG_OPTION_NEWLINE_CRLF = (ONIG_OPTION_WORD_BOUND_ALL_RANGE << 1)
|
74
|
+
ONIG_OPTION_MAXBIT = ONIG_OPTION_NEWLINE_CRLF
|
75
|
+
ONIG_SYN_OP_VARIABLE_META_CHARACTERS = (1<<0)
|
76
|
+
ONIG_SYN_OP_DOT_ANYCHAR = (1<<1)
|
77
|
+
ONIG_SYN_OP_ASTERISK_ZERO_INF = (1<<2)
|
78
|
+
ONIG_SYN_OP_ESC_ASTERISK_ZERO_INF = (1<<3)
|
79
|
+
ONIG_SYN_OP_PLUS_ONE_INF = (1<<4)
|
80
|
+
ONIG_SYN_OP_ESC_PLUS_ONE_INF = (1<<5)
|
81
|
+
ONIG_SYN_OP_QMARK_ZERO_ONE = (1<<6)
|
82
|
+
ONIG_SYN_OP_ESC_QMARK_ZERO_ONE = (1<<7)
|
83
|
+
ONIG_SYN_OP_BRACE_INTERVAL = (1<<8)
|
84
|
+
ONIG_SYN_OP_ESC_BRACE_INTERVAL = (1<<9)
|
85
|
+
ONIG_SYN_OP_VBAR_ALT = (1<<10)
|
86
|
+
ONIG_SYN_OP_ESC_VBAR_ALT = (1<<11)
|
87
|
+
ONIG_SYN_OP_LPAREN_SUBEXP = (1<<12)
|
88
|
+
ONIG_SYN_OP_ESC_LPAREN_SUBEXP = (1<<13)
|
89
|
+
ONIG_SYN_OP_ESC_AZ_BUF_ANCHOR = (1<<14)
|
90
|
+
ONIG_SYN_OP_ESC_CAPITAL_G_BEGIN_ANCHOR = (1<<15)
|
91
|
+
ONIG_SYN_OP_DECIMAL_BACKREF = (1<<16)
|
92
|
+
ONIG_SYN_OP_BRACKET_CC = (1<<17)
|
93
|
+
ONIG_SYN_OP_ESC_W_WORD = (1<<18)
|
94
|
+
ONIG_SYN_OP_ESC_LTGT_WORD_BEGIN_END = (1<<19)
|
95
|
+
ONIG_SYN_OP_ESC_B_WORD_BOUND = (1<<20)
|
96
|
+
ONIG_SYN_OP_ESC_S_WHITE_SPACE = (1<<21)
|
97
|
+
ONIG_SYN_OP_ESC_D_DIGIT = (1<<22)
|
98
|
+
ONIG_SYN_OP_LINE_ANCHOR = (1<<23)
|
99
|
+
ONIG_SYN_OP_POSIX_BRACKET = (1<<24)
|
100
|
+
ONIG_SYN_OP_QMARK_NON_GREEDY = (1<<25)
|
101
|
+
ONIG_SYN_OP_ESC_CONTROL_CHARS = (1<<26)
|
102
|
+
ONIG_SYN_OP_ESC_C_CONTROL = (1<<27)
|
103
|
+
ONIG_SYN_OP_ESC_OCTAL3 = (1<<28)
|
104
|
+
ONIG_SYN_OP_ESC_X_HEX2 = (1<<29)
|
105
|
+
ONIG_SYN_OP_ESC_X_BRACE_HEX8 = (1<<30)
|
106
|
+
ONIG_SYN_OP_ESC_O_BRACE_OCTAL = (1<<31)
|
107
|
+
ONIG_SYN_OP2_ESC_CAPITAL_Q_QUOTE = (1<<0)
|
108
|
+
ONIG_SYN_OP2_QMARK_GROUP_EFFECT = (1<<1)
|
109
|
+
ONIG_SYN_OP2_OPTION_PERL = (1<<2)
|
110
|
+
ONIG_SYN_OP2_OPTION_RUBY = (1<<3)
|
111
|
+
ONIG_SYN_OP2_PLUS_POSSESSIVE_REPEAT = (1<<4)
|
112
|
+
ONIG_SYN_OP2_PLUS_POSSESSIVE_INTERVAL = (1<<5)
|
113
|
+
ONIG_SYN_OP2_CCLASS_SET_OP = (1<<6)
|
114
|
+
ONIG_SYN_OP2_QMARK_LT_NAMED_GROUP = (1<<7)
|
115
|
+
ONIG_SYN_OP2_ESC_K_NAMED_BACKREF = (1<<8)
|
116
|
+
ONIG_SYN_OP2_ESC_G_SUBEXP_CALL = (1<<9)
|
117
|
+
ONIG_SYN_OP2_ATMARK_CAPTURE_HISTORY = (1<<10)
|
118
|
+
ONIG_SYN_OP2_ESC_CAPITAL_C_BAR_CONTROL = (1<<11)
|
119
|
+
ONIG_SYN_OP2_ESC_CAPITAL_M_BAR_META = (1<<12)
|
120
|
+
ONIG_SYN_OP2_ESC_V_VTAB = (1<<13)
|
121
|
+
ONIG_SYN_OP2_ESC_U_HEX4 = (1<<14)
|
122
|
+
ONIG_SYN_OP2_ESC_GNU_BUF_ANCHOR = (1<<15)
|
123
|
+
ONIG_SYN_OP2_ESC_P_BRACE_CHAR_PROPERTY = (1<<16)
|
124
|
+
ONIG_SYN_OP2_ESC_P_BRACE_CIRCUMFLEX_NOT = (1<<17)
|
125
|
+
ONIG_SYN_OP2_ESC_H_XDIGIT = (1<<19)
|
126
|
+
ONIG_SYN_OP2_INEFFECTIVE_ESCAPE = (1<<20)
|
127
|
+
ONIG_SYN_OP2_ESC_CAPITAL_R_LINEBREAK = (1<<21)
|
128
|
+
ONIG_SYN_OP2_ESC_CAPITAL_X_EXTENDED_GRAPHEME_CLUSTER = (1<<22)
|
129
|
+
ONIG_SYN_OP2_ESC_V_VERTICAL_WHITESPACE = (1<<23)
|
130
|
+
ONIG_SYN_OP2_ESC_H_HORIZONTAL_WHITESPACE = (1<<24)
|
131
|
+
ONIG_SYN_OP2_ESC_CAPITAL_K_KEEP = (1<<25)
|
132
|
+
ONIG_SYN_OP2_ESC_G_BRACE_BACKREF = (1<<26)
|
133
|
+
ONIG_SYN_OP2_QMARK_SUBEXP_CALL = (1<<27)
|
134
|
+
ONIG_SYN_OP2_QMARK_VBAR_BRANCH_RESET = (1<<28)
|
135
|
+
ONIG_SYN_OP2_QMARK_LPAREN_CONDITION = (1<<29)
|
136
|
+
ONIG_SYN_OP2_QMARK_CAPITAL_P_NAMED_GROUP = (1<<30)
|
137
|
+
ONIG_SYN_OP2_QMARK_TILDE_ABSENT = (1<<31)
|
138
|
+
ONIG_SYN_CONTEXT_INDEP_ANCHORS = (1<<31)
|
139
|
+
ONIG_SYN_CONTEXT_INDEP_REPEAT_OPS = (1<<0)
|
140
|
+
ONIG_SYN_CONTEXT_INVALID_REPEAT_OPS = (1<<1)
|
141
|
+
ONIG_SYN_ALLOW_UNMATCHED_CLOSE_SUBEXP = (1<<2)
|
142
|
+
ONIG_SYN_ALLOW_INVALID_INTERVAL = (1<<3)
|
143
|
+
ONIG_SYN_ALLOW_INTERVAL_LOW_ABBREV = (1<<4)
|
144
|
+
ONIG_SYN_STRICT_CHECK_BACKREF = (1<<5)
|
145
|
+
ONIG_SYN_DIFFERENT_LEN_ALT_LOOK_BEHIND = (1<<6)
|
146
|
+
ONIG_SYN_CAPTURE_ONLY_NAMED_GROUP = (1<<7)
|
147
|
+
ONIG_SYN_ALLOW_MULTIPLEX_DEFINITION_NAME = (1<<8)
|
148
|
+
ONIG_SYN_FIXED_INTERVAL_IS_GREEDY_ONLY = (1<<9)
|
149
|
+
ONIG_SYN_ALLOW_MULTIPLEX_DEFINITION_NAME_CALL = (1<<10)
|
150
|
+
ONIG_SYN_USE_LEFT_MOST_NAMED_GROUP = (1<<11)
|
151
|
+
ONIG_SYN_NOT_NEWLINE_IN_NEGATIVE_CC = (1<<20)
|
152
|
+
ONIG_SYN_BACKSLASH_ESCAPE_IN_CC = (1<<21)
|
153
|
+
ONIG_SYN_ALLOW_EMPTY_RANGE_IN_CC = (1<<22)
|
154
|
+
ONIG_SYN_ALLOW_DOUBLE_RANGE_OP_IN_CC = (1<<23)
|
155
|
+
ONIG_SYN_WARN_CC_OP_NOT_ESCAPED = (1<<24)
|
156
|
+
ONIG_SYN_WARN_REDUNDANT_NESTED_REPEAT = (1<<25)
|
157
|
+
ONIG_SYN_WARN_CC_DUP = (1<<26)
|
158
|
+
ONIG_META_CHAR_ESCAPE = 0
|
159
|
+
ONIG_META_CHAR_ANYCHAR = 1
|
160
|
+
ONIG_META_CHAR_ANYTIME = 2
|
161
|
+
ONIG_META_CHAR_ZERO_OR_ONE_TIME = 3
|
162
|
+
ONIG_META_CHAR_ONE_OR_MORE_TIME = 4
|
163
|
+
ONIG_META_CHAR_ANYCHAR_ANYTIME = 5
|
164
|
+
ONIG_INEFFECTIVE_META_CHAR = 0
|
165
|
+
ONIG_NORMAL = 0
|
166
|
+
ONIG_MISMATCH = -1
|
167
|
+
ONIG_NO_SUPPORT_CONFIG = -2
|
168
|
+
ONIGERR_MEMORY = -5
|
169
|
+
ONIGERR_TYPE_BUG = -6
|
170
|
+
ONIGERR_PARSER_BUG = -11
|
171
|
+
ONIGERR_STACK_BUG = -12
|
172
|
+
ONIGERR_UNDEFINED_BYTECODE = -13
|
173
|
+
ONIGERR_UNEXPECTED_BYTECODE = -14
|
174
|
+
ONIGERR_MATCH_STACK_LIMIT_OVER = -15
|
175
|
+
ONIGERR_PARSE_DEPTH_LIMIT_OVER = -16
|
176
|
+
ONIGERR_DEFAULT_ENCODING_IS_NOT_SET = -21
|
177
|
+
ONIGERR_SPECIFIED_ENCODING_CANT_CONVERT_TO_WIDE_CHAR = -22
|
178
|
+
ONIGERR_INVALID_ARGUMENT = -30
|
179
|
+
ONIGERR_END_PATTERN_AT_LEFT_BRACE = -100
|
180
|
+
ONIGERR_END_PATTERN_AT_LEFT_BRACKET = -101
|
181
|
+
ONIGERR_EMPTY_CHAR_CLASS = -102
|
182
|
+
ONIGERR_PREMATURE_END_OF_CHAR_CLASS = -103
|
183
|
+
ONIGERR_END_PATTERN_AT_ESCAPE = -104
|
184
|
+
ONIGERR_END_PATTERN_AT_META = -105
|
185
|
+
ONIGERR_END_PATTERN_AT_CONTROL = -106
|
186
|
+
ONIGERR_META_CODE_SYNTAX = -108
|
187
|
+
ONIGERR_CONTROL_CODE_SYNTAX = -109
|
188
|
+
ONIGERR_CHAR_CLASS_VALUE_AT_END_OF_RANGE = -110
|
189
|
+
ONIGERR_CHAR_CLASS_VALUE_AT_START_OF_RANGE = -111
|
190
|
+
ONIGERR_UNMATCHED_RANGE_SPECIFIER_IN_CHAR_CLASS = -112
|
191
|
+
ONIGERR_TARGET_OF_REPEAT_OPERATOR_NOT_SPECIFIED = -113
|
192
|
+
ONIGERR_TARGET_OF_REPEAT_OPERATOR_INVALID = -114
|
193
|
+
ONIGERR_NESTED_REPEAT_OPERATOR = -115
|
194
|
+
ONIGERR_UNMATCHED_CLOSE_PARENTHESIS = -116
|
195
|
+
ONIGERR_END_PATTERN_WITH_UNMATCHED_PARENTHESIS = -117
|
196
|
+
ONIGERR_END_PATTERN_IN_GROUP = -118
|
197
|
+
ONIGERR_UNDEFINED_GROUP_OPTION = -119
|
198
|
+
ONIGERR_INVALID_POSIX_BRACKET_TYPE = -121
|
199
|
+
ONIGERR_INVALID_LOOK_BEHIND_PATTERN = -122
|
200
|
+
ONIGERR_INVALID_REPEAT_RANGE_PATTERN = -123
|
201
|
+
ONIGERR_INVALID_CONDITION_PATTERN = -124
|
202
|
+
ONIGERR_TOO_BIG_NUMBER = -200
|
203
|
+
ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE = -201
|
204
|
+
ONIGERR_UPPER_SMALLER_THAN_LOWER_IN_REPEAT_RANGE = -202
|
205
|
+
ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS = -203
|
206
|
+
ONIGERR_MISMATCH_CODE_LENGTH_IN_CLASS_RANGE = -204
|
207
|
+
ONIGERR_TOO_MANY_MULTI_BYTE_RANGES = -205
|
208
|
+
ONIGERR_TOO_SHORT_MULTI_BYTE_STRING = -206
|
209
|
+
ONIGERR_TOO_BIG_BACKREF_NUMBER = -207
|
210
|
+
ONIGERR_INVALID_BACKREF = -208
|
211
|
+
ONIGERR_NUMBERED_BACKREF_OR_CALL_NOT_ALLOWED = -209
|
212
|
+
ONIGERR_TOO_MANY_CAPTURE_GROUPS = -210
|
213
|
+
ONIGERR_TOO_SHORT_DIGITS = -211
|
214
|
+
ONIGERR_TOO_LONG_WIDE_CHAR_VALUE = -212
|
215
|
+
ONIGERR_EMPTY_GROUP_NAME = -214
|
216
|
+
ONIGERR_INVALID_GROUP_NAME = -215
|
217
|
+
ONIGERR_INVALID_CHAR_IN_GROUP_NAME = -216
|
218
|
+
ONIGERR_UNDEFINED_NAME_REFERENCE = -217
|
219
|
+
ONIGERR_UNDEFINED_GROUP_REFERENCE = -218
|
220
|
+
ONIGERR_MULTIPLEX_DEFINED_NAME = -219
|
221
|
+
ONIGERR_MULTIPLEX_DEFINITION_NAME_CALL = -220
|
222
|
+
ONIGERR_NEVER_ENDING_RECURSION = -221
|
223
|
+
ONIGERR_GROUP_NUMBER_OVER_FOR_CAPTURE_HISTORY = -222
|
224
|
+
ONIGERR_INVALID_CHAR_PROPERTY_NAME = -223
|
225
|
+
ONIGERR_INVALID_CODE_POINT_VALUE = -400
|
226
|
+
ONIGERR_INVALID_WIDE_CHAR_VALUE = -400
|
227
|
+
ONIGERR_TOO_BIG_WIDE_CHAR_VALUE = -401
|
228
|
+
ONIGERR_NOT_SUPPORTED_ENCODING_COMBINATION = -402
|
229
|
+
ONIGERR_INVALID_COMBINATION_OF_OPTIONS = -403
|
230
|
+
ONIG_MAX_CAPTURE_HISTORY_GROUP = 31
|
231
|
+
ONIG_TRAVERSE_CALLBACK_AT_FIRST = 1
|
232
|
+
ONIG_TRAVERSE_CALLBACK_AT_LAST = 2
|
233
|
+
ONIG_REGION_NOTPOS = -1
|
234
|
+
ONIG_CHAR_TABLE_SIZE = 256
|
235
|
+
ONIG_OPTION_DEFAULT = ONIG_OPTION_NONE
|
236
|
+
end
|
237
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# Copy constants from Onigmo/onigmo.h
|
2
|
+
|
3
|
+
module Onigmo
|
4
|
+
module Constants
|
5
|
+
<%
|
6
|
+
onigmo_h
|
7
|
+
.scan(/^#define\s+(\w+)\s+(\(?\s*[()<>0-9\w\t -]+)/)
|
8
|
+
.reject { |k,v| %w[
|
9
|
+
ONIG_NULL_WARN ONIG_SYNTAX_ ONIG_ENCODING_ ONIG_INFINITE_DISTANCE
|
10
|
+
ONIGENC_CASE_FOLD_DEFAULT ONIG_OPTION_DEFAULT].any? { |i| k.start_with? i } }
|
11
|
+
.each do |var, val|
|
12
|
+
%>
|
13
|
+
<%= var %> = <%= val.gsub(/U(<<|$)/,'\1') %><% end %>
|
14
|
+
ONIG_OPTION_DEFAULT = ONIG_OPTION_NONE
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'onigmo'
|
2
|
+
|
3
|
+
class String
|
4
|
+
alias match_before_onigmo match
|
5
|
+
def match(re, pos = undefined, &block)
|
6
|
+
if Onigmo::Regexp === re
|
7
|
+
re.match(self, pos, &block)
|
8
|
+
else
|
9
|
+
match_before_onigmo(re, pos, &block)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
alias match_before_onigmo? match?
|
14
|
+
def match?(re, pos = undefined, &block)
|
15
|
+
if Onigmo::Regexp === re
|
16
|
+
re.match?(self, pos, &block)
|
17
|
+
else
|
18
|
+
match_before_onigmo?(re, pos, &block)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/opal/onigmo/ffi.rb
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
require 'ffi'
|
2
|
+
require 'corelib/array/pack'
|
3
|
+
require 'corelib/string/unpack'
|
4
|
+
|
5
|
+
module Onigmo
|
6
|
+
module FFI
|
7
|
+
include Onigmo::Constants
|
8
|
+
extend ::FFI::Library
|
9
|
+
|
10
|
+
ffi_lib "onigmo/onigmo-wasm"
|
11
|
+
|
12
|
+
class Regexp < ::FFI::Struct
|
13
|
+
layout :p, :pointer,
|
14
|
+
:used, :uint,
|
15
|
+
:alloc, :uint,
|
16
|
+
|
17
|
+
:num_mem, :int,
|
18
|
+
:num_repeat, :int,
|
19
|
+
:num_null_check, :int,
|
20
|
+
:num_comb_exp_check, :int,
|
21
|
+
:num_call, :int,
|
22
|
+
:capture_history, :uint,
|
23
|
+
:bt_mem_start, :uint,
|
24
|
+
:bt_mem_end, :uint,
|
25
|
+
:stack_pop_level, :int,
|
26
|
+
:repeat_range_alloc, :int,
|
27
|
+
|
28
|
+
:options, :int,
|
29
|
+
|
30
|
+
:repeat_range, :pointer,
|
31
|
+
|
32
|
+
:encoding, :pointer,
|
33
|
+
:syntax, :pointer,
|
34
|
+
:name_table, :pointer,
|
35
|
+
:casefold, :int,
|
36
|
+
|
37
|
+
:optimize, :int,
|
38
|
+
:threshold_len, :int,
|
39
|
+
:anchor, :int,
|
40
|
+
:anchor_dmin, :long,
|
41
|
+
:anchor_dmax, :long,
|
42
|
+
:sub_anchor, :int,
|
43
|
+
:exact, :pointer,
|
44
|
+
:exact_end, :pointer,
|
45
|
+
:map, [:uchar, 256],
|
46
|
+
|
47
|
+
:reserved1, :pointer,
|
48
|
+
:reserved2, :pointer,
|
49
|
+
:dmin, :long,
|
50
|
+
:dmax, :long,
|
51
|
+
|
52
|
+
:chain, :pointer
|
53
|
+
end
|
54
|
+
|
55
|
+
class RegexpPtr < ::FFI::Struct
|
56
|
+
layout :value, Regexp.ptr
|
57
|
+
|
58
|
+
def value
|
59
|
+
self[:value]
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
class Region < ::FFI::Struct
|
64
|
+
layout :allocated, :int,
|
65
|
+
:num_regs, :int,
|
66
|
+
:beg, :pointer,
|
67
|
+
:end, :pointer
|
68
|
+
end
|
69
|
+
|
70
|
+
class CompileInfo < ::FFI::Struct
|
71
|
+
layout :num_of_elements, :int,
|
72
|
+
:pattern_enc, :pointer,
|
73
|
+
:target_enc, :pointer,
|
74
|
+
:syntax, :pointer,
|
75
|
+
:option, :uint,
|
76
|
+
:case_fold_flag, :uint
|
77
|
+
|
78
|
+
def self.default_compile_info
|
79
|
+
@default ||= Onigmo::FFI.context do
|
80
|
+
new.tap do |ci|
|
81
|
+
ci[:num_of_elements] = 5
|
82
|
+
ci[:pattern_enc] = Onigmo::FFI.library.exports[:OnigEncodingUTF_16LE]
|
83
|
+
ci[:target_enc] = Onigmo::FFI.library.exports[:OnigEncodingUTF_16LE]
|
84
|
+
ci[:syntax] = Onigmo::FFI.library.exports[:OnigSyntaxRuby]
|
85
|
+
ci[:option] = Onigmo::FFI::ONIG_OPTION_NONE # To be overwritten
|
86
|
+
ci[:case_fold_flag] = Onigmo::FFI.library.exports[:OnigDefaultCaseFoldFlag].value
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def options
|
92
|
+
self[:option]
|
93
|
+
end
|
94
|
+
|
95
|
+
def options= value
|
96
|
+
self[:option] = value
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
class ErrorInfo < ::FFI::Struct
|
101
|
+
layout :enc, :pointer,
|
102
|
+
:par, :pointer,
|
103
|
+
:par_end, :pointer
|
104
|
+
end
|
105
|
+
|
106
|
+
attach_function :onig_new_deluxe, [RegexpPtr, :pointer, :pointer, CompileInfo, ErrorInfo], :int
|
107
|
+
attach_function :onig_region_new, [], Region
|
108
|
+
attach_function :onig_match, [Regexp, :pointer, :pointer, :pointer, Region, :uint], :int
|
109
|
+
attach_function :onig_search, [Regexp, :pointer, :pointer, :pointer, :pointer, Region, :uint], :int
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
Onigmo::FFI.library.memory.grow(128)
|
114
|
+
end
|
Binary file
|
@@ -0,0 +1,118 @@
|
|
1
|
+
module Onigmo
|
2
|
+
class Regexp
|
3
|
+
`Opal.defineProperty(self.$$prototype, '$$is_regexp', true)`
|
4
|
+
# Hacks in order to make it more alike JS regexp that is perfectly compatible.
|
5
|
+
# This should make String#sub and friends work without a hassle.
|
6
|
+
`Opal.defineProperty(self.$$prototype, 'global', true)`
|
7
|
+
`Opal.defineProperty(self.$$prototype, 'multiline', true)`
|
8
|
+
|
9
|
+
def match(str, offset=nil, &block)
|
10
|
+
block ||= proc{|i| i}
|
11
|
+
evaluate(str, offset) && block.(MatchData.new(self, js_matches(str)))
|
12
|
+
end
|
13
|
+
|
14
|
+
def match?(str, offset=nil)
|
15
|
+
!!evaluate(str, offset)
|
16
|
+
end
|
17
|
+
|
18
|
+
def =~ str
|
19
|
+
match(str) && $~.begin(0)
|
20
|
+
end
|
21
|
+
|
22
|
+
def inspect
|
23
|
+
"/#{pattern.inspect[1..-2]}/O#{options}"
|
24
|
+
end
|
25
|
+
|
26
|
+
def initialize pattern, options = ''
|
27
|
+
@lastIndex = 0
|
28
|
+
@pattern = pattern.encode("UTF-16LE")
|
29
|
+
@options = options
|
30
|
+
|
31
|
+
out = Onigmo::FFI.context do
|
32
|
+
@ffi_regexpptr = Onigmo::FFI::RegexpPtr.new
|
33
|
+
ffi_options = Onigmo::FFI::CompileInfo.default_compile_info
|
34
|
+
ffi_errorinfo = Onigmo::FFI::ErrorInfo.new
|
35
|
+
|
36
|
+
ffi_pattern = ::FFI::Pointer.from_string(@pattern)
|
37
|
+
ffi_pattern_end = ffi_pattern + (@pattern.length * 2)
|
38
|
+
|
39
|
+
ffi_options.options = Onigmo::FFI::ONIG_OPTION_NONE
|
40
|
+
ffi_options.options |= Onigmo::FFI::ONIG_OPTION_IGNORECASE if options.include? "i"
|
41
|
+
ffi_options.options |= Onigmo::FFI::ONIG_OPTION_MULTILINE if options.include? "m"
|
42
|
+
ffi_options.options |= Onigmo::FFI::ONIG_OPTION_EXTEND if options.include? "x"
|
43
|
+
|
44
|
+
Onigmo::FFI.onig_new_deluxe(@ffi_regexpptr, ffi_pattern, ffi_pattern_end,
|
45
|
+
ffi_options, ffi_errorinfo)
|
46
|
+
end
|
47
|
+
|
48
|
+
@ffi_regexp = @ffi_regexpptr.value
|
49
|
+
|
50
|
+
@exec = proc { |re| js_exec(re) }
|
51
|
+
|
52
|
+
unless out == Onigmo::FFI::ONIG_NORMAL
|
53
|
+
raise RuntimeError, "Onigmo has failed with an error code '#{out}'"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def ffi_evaluate string, offset = 0
|
58
|
+
offset ||= 0
|
59
|
+
|
60
|
+
@ffi_region = Onigmo::FFI.onig_region_new
|
61
|
+
|
62
|
+
string = string.encode("UTF-16LE")
|
63
|
+
|
64
|
+
Onigmo::FFI.context do
|
65
|
+
ffi_string = ::FFI::Pointer.from_string(string)
|
66
|
+
ffi_string_end = ffi_string + (string.length * 2)
|
67
|
+
ffi_string_offset = ffi_string + offset * 2
|
68
|
+
|
69
|
+
Onigmo::FFI.onig_search(@ffi_regexp, ffi_string, ffi_string_end, ffi_string_offset,
|
70
|
+
ffi_string_end, @ffi_region, Onigmo::FFI::ONIG_OPTION_NONE)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def ffi_region
|
75
|
+
@ffi_region[:num_regs].times.map do |i|
|
76
|
+
[@ffi_region[:beg].get(:long, i*4),
|
77
|
+
@ffi_region[:end].get(:long, i*4) ]
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def evaluate string, offset = 0
|
82
|
+
out = ffi_evaluate(string, offset)
|
83
|
+
if out < 0
|
84
|
+
nil
|
85
|
+
else
|
86
|
+
out / 2
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def matches(str)
|
91
|
+
ffi_region.map { |b,e| str[b/2...e/2] }
|
92
|
+
end
|
93
|
+
|
94
|
+
def js_matches(str)
|
95
|
+
matches = matches(str)
|
96
|
+
region = ffi_region
|
97
|
+
out = {
|
98
|
+
input: str,
|
99
|
+
index: region[0][0]/2,
|
100
|
+
length: matches.length,
|
101
|
+
slice: proc { |i| matches[i..-1] }
|
102
|
+
}
|
103
|
+
out = out.to_n
|
104
|
+
matches.each_with_index { |m,i| `out[#{i}] = #{m}` }
|
105
|
+
@lastIndex = region[0][1]/2
|
106
|
+
out
|
107
|
+
end
|
108
|
+
|
109
|
+
def js_exec(str)
|
110
|
+
if evaluate(str, @lastIndex || 0)
|
111
|
+
js_matches(str)
|
112
|
+
else
|
113
|
+
@lastIndex = 0
|
114
|
+
nil.to_n
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Onigmo::Regexp do
|
4
|
+
it "can create a Regexp" do
|
5
|
+
Onigmo::Regexp.new('(?<=test)test!', 'i')
|
6
|
+
end
|
7
|
+
|
8
|
+
it "can evaluate a Regexp" do
|
9
|
+
re = Onigmo::Regexp.new('ll. wor', 'i')
|
10
|
+
out = re.ffi_evaluate("Hello World!")
|
11
|
+
expect(out).to eq(4)
|
12
|
+
regs = re.ffi_region
|
13
|
+
expect(regs).to eq([[4, 18]])
|
14
|
+
end
|
15
|
+
|
16
|
+
it "can find Hangul characters" do
|
17
|
+
re = Onigmo::Regexp.new('\\p{Hangul}')
|
18
|
+
out = re.ffi_evaluate("Some characters that are not Hangul and one that 봉is.")
|
19
|
+
expect(out).to eq(98)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should support String#gsub" do
|
23
|
+
re = Onigmo::Regexp.new('aa')
|
24
|
+
expect('aababaaaabbaaaba'.gsub(re, 'ZZ')).to eq('ZZbabZZZZbbZZaba')
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should support String#sub" do
|
28
|
+
re = Onigmo::Regexp.new('aa')
|
29
|
+
expect('aababaaaabbaaaba'.sub(re, 'ZZ')).to eq('ZZbabaaaabbaaaba')
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should support String#=~" do
|
33
|
+
re = Onigmo::Regexp.new('aa')
|
34
|
+
expect('haha' =~ re).to eq(nil)
|
35
|
+
expect('haaha' =~ re).to eq(1)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should support String#match?" do
|
39
|
+
require 'onigmo/core_ext'
|
40
|
+
re = Onigmo::Regexp.new('aa')
|
41
|
+
expect('haha'.match? re).to eq(false)
|
42
|
+
expect('haaha'.match? re).to eq(true)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should support String#match" do
|
46
|
+
require 'onigmo/core_ext'
|
47
|
+
re = Onigmo::Regexp.new('aa')
|
48
|
+
expect('haha'.match re).to eq(nil)
|
49
|
+
expect('haaha'.match re).to be_a(MatchData)
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should support $1, $2..." do
|
53
|
+
require 'onigmo/core_ext'
|
54
|
+
re = Onigmo::Regexp.new('(a).(a)')
|
55
|
+
expect(re.match "haha").to be_a(MatchData)
|
56
|
+
expect($1).to eq('a')
|
57
|
+
expect($2).to eq('a')
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'onigmo'
|
2
|
+
|
3
|
+
RSpec.configure do |config|
|
4
|
+
# Run specs in random order to surface order dependencies. If you find an
|
5
|
+
# order dependency and want to debug it, you can fix the order by providing
|
6
|
+
# the seed, which is printed after each run.
|
7
|
+
# --seed 1234
|
8
|
+
config.order = :random
|
9
|
+
|
10
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
11
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
12
|
+
# test failures related to randomization by passing the same `--seed` value
|
13
|
+
# as the one that triggered the failure.
|
14
|
+
Kernel.srand config.seed
|
15
|
+
|
16
|
+
config.full_backtrace = true
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: opal-onigmo
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ribose Inc.
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-10-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: opal-webassembly
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Execute regexps in Opal with a native Ruby regexp engine - Onigmo - compiled
|
28
|
+
using clang to WebAssembly
|
29
|
+
email:
|
30
|
+
- open.source@ribose.com
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- ".gitignore"
|
36
|
+
- ".gitmodules"
|
37
|
+
- ".rspec"
|
38
|
+
- ".rspec-opal"
|
39
|
+
- ".travis.yml"
|
40
|
+
- Gemfile
|
41
|
+
- Gemfile.lock
|
42
|
+
- LICENSE.adoc
|
43
|
+
- LICENSE.txt
|
44
|
+
- README.adoc
|
45
|
+
- Rakefile
|
46
|
+
- bin/console
|
47
|
+
- bin/setup
|
48
|
+
- lib/opal/onigmo.rb
|
49
|
+
- lib/opal/onigmo/version.rb
|
50
|
+
- opal-onigmo.gemspec
|
51
|
+
- opal/onigmo.rb
|
52
|
+
- opal/onigmo/constants.rb
|
53
|
+
- opal/onigmo/constants.rb.erb
|
54
|
+
- opal/onigmo/core_ext.rb
|
55
|
+
- opal/onigmo/ffi.rb
|
56
|
+
- opal/onigmo/onigmo-wasm.wasm
|
57
|
+
- opal/onigmo/regexp.rb
|
58
|
+
- spec-opal/regexp_spec.rb
|
59
|
+
- spec-opal/spec_helper.rb
|
60
|
+
homepage: https://github.com/interscript/opal-onigmo
|
61
|
+
licenses:
|
62
|
+
- BSD-2-Clause
|
63
|
+
metadata:
|
64
|
+
homepage_uri: https://github.com/interscript/opal-onigmo
|
65
|
+
source_code_uri: https://github.com/interscript/opal-onigmo
|
66
|
+
changelog_uri: https://github.com/interscript/opal-onigmo/commits/master
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 2.3.0
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubygems_version: 3.0.3
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: Execute regexps with Opal using Onigmo
|
86
|
+
test_files: []
|