corvus_regex_ecma262 0.1.1-x86_64-linux-gnu
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/LICENSE +201 -0
- data/README.md +100 -0
- data/lib/corvus_regex_ecma262/3.3/corvus_regex_ecma262.so +0 -0
- data/lib/corvus_regex_ecma262/3.4/corvus_regex_ecma262.so +0 -0
- data/lib/corvus_regex_ecma262/4.0/corvus_regex_ecma262.so +0 -0
- data/lib/corvus_regex_ecma262/version.rb +5 -0
- data/lib/corvus_regex_ecma262.rb +297 -0
- metadata +58 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 5b367e7b382030f1053ab030ff79bbf0a5d28d46e6192a87f6c6cce67b600fc9
|
|
4
|
+
data.tar.gz: b833e7969f5d9741f94756cbc6d6418dd2dac9be14d4f558d7315db590472fab
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 9c52447221dc4c558802a860ec178ff3e172d5cf43e6fcf64ba9b6ea6eff21d89ee3ad4e11c10f0b298ffac6c60d5588edf86a66b15016f8dae5c90a423cb735
|
|
7
|
+
data.tar.gz: 21290d0e7c5465e85dd40f759eb9c56bfc1da00527261d5627d08c4c99ee5e0ff12ef12f5b446cd12bd903e89520c2602f9acdefbf0d9dbcdf85cd103ae32534
|
data/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
data/README.md
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# corvus-regex-ecma262 for Ruby
|
|
2
|
+
|
|
3
|
+
Regular expressions with the grammar and the answers of ECMA-262, the `RegExp` of JavaScript, for Ruby: the
|
|
4
|
+
[corvus-regex-ecma262](https://github.com/corvus-dotnet/corvus-regex-ecma262-rs) Rust crate, as a gem with native
|
|
5
|
+
builds for Linux (glibc and musl), macOS and Windows, x64 and arm64, for Ruby 3.3 and later.
|
|
6
|
+
|
|
7
|
+
- Every flag: `d`, `g`, `i`, `m`, `s`, `u`, `v` and `y`, with the grammar of each. Without `u` or `v` a pattern is
|
|
8
|
+
read by the Annex B grammar and matched by UTF-16 code unit, as ECMA-262 matches it; with them, by code point. The
|
|
9
|
+
`v` flag has nested classes, the difference and the intersection of classes, strings in a class and the properties
|
|
10
|
+
of strings.
|
|
11
|
+
- ECMAScript 2025: lookbehind, named groups (and a name shared between alternatives), modifier groups such as
|
|
12
|
+
`(?i:...)`, and every Unicode property ECMA-262 lists, from Unicode 17 tables the crate carries.
|
|
13
|
+
- UTF-8 strings (and US-ASCII, and binary strings holding UTF-8) and UTF-16LE strings, each matched as it is held.
|
|
14
|
+
A pattern answers as it does in a browser or Node, which Ruby's `Regexp` does not: `^` and `$`, `.`, `\d`, `\w`,
|
|
15
|
+
`\b`, case folding, `\p{...}` and the rest read as JavaScript reads them.
|
|
16
|
+
- A pattern without a backreference is matched in time proportional to its size times the text, and `match` and
|
|
17
|
+
`match?` take a timeout, so neither a pattern nor a text from outside can make a program hang.
|
|
18
|
+
|
|
19
|
+
**Compliance.** Over the [ECMA-262 test data](https://github.com/corvus-dotnet/ecma262-regex-test-data) (what V8 says
|
|
20
|
+
about 109,547 patterns and 5,325,026 texts, `exec`, the global search, `replace` and `split` among them), through this
|
|
21
|
+
gem over UTF-16LE and UTF-8 strings, it gives 23,647,077 right answers and none wrong, refuses no valid pattern,
|
|
22
|
+
accepts no invalid one and reaches no limit. CI runs the whole data through each native gem on its platform.
|
|
23
|
+
|
|
24
|
+
## Install
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
gem install corvus_regex_ecma262
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Getting started
|
|
31
|
+
|
|
32
|
+
```ruby
|
|
33
|
+
require "corvus_regex_ecma262"
|
|
34
|
+
|
|
35
|
+
# The flags are those of new RegExp(pattern, flags).
|
|
36
|
+
date = CorvusRegexEcma262::Regex.new('(?<year>\d{4})-(?<month>\d{2})', "gu")
|
|
37
|
+
|
|
38
|
+
date.match?("due 2026-10") # => true
|
|
39
|
+
m = date.match("due 2026-10")
|
|
40
|
+
m[:year] # => "2026"
|
|
41
|
+
m.offset # => [4, 11]
|
|
42
|
+
|
|
43
|
+
# Every match of a global search, as matchAll makes it.
|
|
44
|
+
date.each_match("2026-10, 2027-01").map(&:to_s) # => ["2026-10", "2027-01"]
|
|
45
|
+
|
|
46
|
+
# replace replaces every match with the g flag, and the first without it; a block may give each replacement.
|
|
47
|
+
date.replace("2026-10, 2027-01", "$<month>/$1") # => "10/2026, 01/2027"
|
|
48
|
+
date.replace("2026-10") { |m| m[:month] } # => "10"
|
|
49
|
+
|
|
50
|
+
# split splices in what the groups captured; a group that did not take part is nil.
|
|
51
|
+
CorvusRegexEcma262.compile('\s*(;)\s*').split("a ; b;c") # => ["a", ";", "b", ";", "c"]
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## The API
|
|
55
|
+
|
|
56
|
+
| | |
|
|
57
|
+
|---|---|
|
|
58
|
+
| `Regex.new(pattern, flags = "")`, `CorvusRegexEcma262.compile` | Compiles a pattern as `new RegExp(pattern, flags)` does. |
|
|
59
|
+
| `Regex.valid?(pattern, flags = "")` | Whether a pattern is valid, without compiling it. |
|
|
60
|
+
| `match?(string, pos = 0, timeout: nil)` | `RegExp.prototype.test` with `lastIndex` at `pos` (at `pos` alone with `y`). |
|
|
61
|
+
| `match(string, pos = 0, timeout: nil)` | The first match at or after `pos`, as `exec` finds it: a `Match`, or `nil`; with a block, the block's result. |
|
|
62
|
+
| `each_match(string)` | A global search as `matchAll` makes it: yields each `Match`, or returns an `Enumerator`. |
|
|
63
|
+
| `match_count(string)` | The number of matches of a global search, without making them. |
|
|
64
|
+
| `replace(string, replacement)`, `replace(string) { \|m\| ... }` | `String.prototype.replace`: the first match, or every match with `g`. |
|
|
65
|
+
| `replace_all(string, replacement)` | `String.prototype.replaceAll`: every match. |
|
|
66
|
+
| `split(string, limit = nil)` | `String.prototype.split`: an Array of Strings, `nil` for a group that did not take part. |
|
|
67
|
+
| `source`, `flags`, `group_count`, `names` | What the pattern is. |
|
|
68
|
+
|
|
69
|
+
A `Match` is read as a `MatchData` is: `m[g]` (a number, a name or a Symbol), `captures`, `named_captures`, `to_a`,
|
|
70
|
+
`values_at`, `pre_match`, `post_match`, `begin(g)`, `end(g)` and `offset(g)` (nil for a group that did not take part),
|
|
71
|
+
`byte_begin`, `byte_end` and `byteoffset`, `string` and `regexp`.
|
|
72
|
+
|
|
73
|
+
**Positions.** Over a UTF-8 string, `begin`, `end` and `offset` are in characters, as Ruby counts them, and the
|
|
74
|
+
`byte_` forms in bytes; over a UTF-16LE string they are in UTF-16 code units, as JavaScript counts them. Without `u` or
|
|
75
|
+
`v` a pattern is matched by UTF-16 code unit, so a match can take half of a character beyond the Basic Multilingual
|
|
76
|
+
Plane: over UTF-8 its text holds that half as U+FFFD and its character positions are the whole character's (its byte
|
|
77
|
+
positions are the character's start and third byte); over UTF-16LE it is the lone half, as in JavaScript.
|
|
78
|
+
|
|
79
|
+
**Errors.** `InvalidPatternError`, `InvalidFlagsError`, `PatternTooLargeError` and `MatchLimitError` (a match that
|
|
80
|
+
reached its timeout or its cap on backtracking memory) are each a `CorvusRegexEcma262::Error`, a `StandardError`. A
|
|
81
|
+
string that is not valid in its encoding raises `ArgumentError`; one in another encoding, `EncodingError`.
|
|
82
|
+
|
|
83
|
+
**Threads.** A `Regex` is frozen and may be used from any number of threads. A string cannot change while a global
|
|
84
|
+
search walks it.
|
|
85
|
+
|
|
86
|
+
## Benchmarks
|
|
87
|
+
|
|
88
|
+
The Industry workloads of dotnet/performance, this gem against Ruby's `Regexp` and V8, are on the central page,
|
|
89
|
+
[corvus-oss.org/corvus-regex-ecma262-benchmarks](https://corvus-oss.org/corvus-regex-ecma262-benchmarks/), measured
|
|
90
|
+
nightly on one runner with every other Corvus ECMA-262 engine.
|
|
91
|
+
|
|
92
|
+
## Repository
|
|
93
|
+
|
|
94
|
+
- `ext/corvus_regex_ecma262`: the native extension (magnus, over the crate); `lib/`: the Ruby API.
|
|
95
|
+
- `test/`: the tests; `tools/dataset.rb`: the test-data runner (`ruby -Ilib tools/dataset.rb <data> wrong.tsv`).
|
|
96
|
+
- `tools/bench`: the benchmark harness and the script the image runs; `Dockerfile`: the image the central page runs.
|
|
97
|
+
|
|
98
|
+
## Licence
|
|
99
|
+
|
|
100
|
+
Apache-2.0. See [LICENSE](LICENSE).
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# ECMA-262 (JavaScript) regular expressions for Ruby, with the grammar and the answers of RegExp, every flag, backed by
|
|
4
|
+
# the corvus-regex-ecma262 Rust crate.
|
|
5
|
+
#
|
|
6
|
+
# date = CorvusRegexEcma262::Regex.new('(?<year>\d{4})-(?<month>\d{2})', "gu")
|
|
7
|
+
# date.match?("due 2026-10") # => true
|
|
8
|
+
# date.match("due 2026-10")[:year] # => "2026"
|
|
9
|
+
# date.each_match("2026-10, 2027-01").map(&:to_s) # => ["2026-10", "2027-01"]
|
|
10
|
+
# date.replace("2026-10, 2027-01", "$<month>/$1") # => "10/2026, 01/2027"
|
|
11
|
+
#
|
|
12
|
+
# A String is matched as it is held: UTF-8 (or US-ASCII, or binary holding UTF-8) as UTF-8, with positions in
|
|
13
|
+
# characters as Ruby counts them and byte positions beside them; UTF-16LE as UTF-16, with positions in code units.
|
|
14
|
+
# Without the u or v flag a pattern is matched by UTF-16 code unit, so a match can take half of a character beyond the
|
|
15
|
+
# Basic Multilingual Plane: over UTF-8 its text holds that half as U+FFFD, and its start and end in characters are the
|
|
16
|
+
# whole character's.
|
|
17
|
+
require_relative "corvus_regex_ecma262/version"
|
|
18
|
+
|
|
19
|
+
begin
|
|
20
|
+
RUBY_VERSION =~ /(\d+\.\d+)/
|
|
21
|
+
require_relative "corvus_regex_ecma262/#{Regexp.last_match(1)}/corvus_regex_ecma262"
|
|
22
|
+
rescue LoadError
|
|
23
|
+
require_relative "corvus_regex_ecma262/corvus_regex_ecma262"
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
module CorvusRegexEcma262
|
|
27
|
+
UTF16LE = Encoding::UTF_16LE
|
|
28
|
+
|
|
29
|
+
# A compiled ECMA-262 regular expression, as new RegExp(pattern, flags) compiles it. It is immutable, and may be
|
|
30
|
+
# used from any number of threads.
|
|
31
|
+
class Regex
|
|
32
|
+
attr_reader :names
|
|
33
|
+
|
|
34
|
+
# Compiles a pattern with the flags new RegExp(pattern, flags) takes, any of dgimsuvy. Raises InvalidPatternError,
|
|
35
|
+
# InvalidFlagsError or PatternTooLargeError (each an Error).
|
|
36
|
+
def initialize(pattern, flags = "")
|
|
37
|
+
@native = Native::Regex.new(CorvusRegexEcma262.utf8(pattern), flags.to_s)
|
|
38
|
+
@names = @native.names.freeze
|
|
39
|
+
freeze
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Whether a pattern is valid with the flags, without compiling it.
|
|
43
|
+
def self.valid?(pattern, flags = "")
|
|
44
|
+
Native::Regex.valid?(CorvusRegexEcma262.utf8(pattern), flags.to_s)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# The pattern as it was given, as UTF-8.
|
|
48
|
+
def source = @native.source
|
|
49
|
+
|
|
50
|
+
# The flags as RegExp.prototype.flags writes them, in the order dgimsuvy.
|
|
51
|
+
def flags = @native.flags
|
|
52
|
+
|
|
53
|
+
# The number of capturing groups.
|
|
54
|
+
def group_count = @native.group_count
|
|
55
|
+
|
|
56
|
+
# Whether the pattern matches at or after pos (at pos alone with the y flag), as RegExp.prototype.test with
|
|
57
|
+
# lastIndex set to pos. With timeout: (seconds), a match that reaches it raises MatchLimitError.
|
|
58
|
+
def match?(string, pos = 0, timeout: nil)
|
|
59
|
+
@native.test(string, CorvusRegexEcma262.unit_of(string, pos), timeout&.to_f)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# The first match at or after pos, as exec finds it with lastIndex set to pos: a Match, or nil. With a block, the
|
|
63
|
+
# block's result for the match, or nil. With timeout: (seconds), a match that reaches it raises MatchLimitError.
|
|
64
|
+
def match(string, pos = 0, timeout: nil)
|
|
65
|
+
groups = @native.exec(string, CorvusRegexEcma262.unit_of(string, pos), timeout&.to_f)
|
|
66
|
+
return nil unless groups
|
|
67
|
+
|
|
68
|
+
m = Match.new(self, string, groups, Positions.new(string))
|
|
69
|
+
block_given? ? yield(m) : m
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Every match of a global search of the string, as matchAll makes one: yields each Match, or returns an
|
|
73
|
+
# Enumerator. After an empty match the search goes on one character later, and with the y flag each match must
|
|
74
|
+
# start where the one before ended.
|
|
75
|
+
def each_match(string)
|
|
76
|
+
return enum_for(:each_match, string) unless block_given?
|
|
77
|
+
|
|
78
|
+
string = CorvusRegexEcma262.snapshot(string)
|
|
79
|
+
positions = Positions.new(string)
|
|
80
|
+
@native.each(string) { |groups| yield Match.new(self, string, groups, positions) }
|
|
81
|
+
self
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# The number of matches of a global search, without making them.
|
|
85
|
+
def match_count(string) = @native.count(string)
|
|
86
|
+
|
|
87
|
+
# The string with the first match replaced, or every match with the g flag, as String.prototype.replace replaces
|
|
88
|
+
# them. The replacement is a template ($& the match, $` the text before it, $' the text after it, $1 to $99 a
|
|
89
|
+
# group, $<name> a named group, $$ a dollar sign), or the block's result for each Match.
|
|
90
|
+
def replace(string, replacement = nil, &block)
|
|
91
|
+
substitute(string, replacement, @native.global?, &block)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# The string with every match replaced, whether or not the g flag is set (String.prototype.replaceAll).
|
|
95
|
+
def replace_all(string, replacement = nil, &block)
|
|
96
|
+
substitute(string, replacement, true, &block)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# The string split around the matches, as String.prototype.split(separator, limit) splits it: what each group
|
|
100
|
+
# captured is spliced in after the piece before it (nil for a group that did not take part), an empty match at
|
|
101
|
+
# the start or the end splits nothing, and there are at most limit pieces.
|
|
102
|
+
def split(string, limit = nil)
|
|
103
|
+
@native.split(string, limit).map { |piece| piece&.force_encoding(CorvusRegexEcma262.result_encoding(string)) }
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def inspect = "#<CorvusRegexEcma262::Regex /#{source}/#{flags}>"
|
|
107
|
+
alias to_s inspect
|
|
108
|
+
|
|
109
|
+
def ==(other) = other.is_a?(Regex) && other.source == source && other.flags == flags
|
|
110
|
+
alias eql? ==
|
|
111
|
+
def hash = [source, flags].hash
|
|
112
|
+
|
|
113
|
+
private
|
|
114
|
+
|
|
115
|
+
def substitute(string, replacement, all)
|
|
116
|
+
unless block_given?
|
|
117
|
+
raise ArgumentError, "a replacement or a block" if replacement.nil?
|
|
118
|
+
|
|
119
|
+
template = string.encoding == UTF16LE ? replacement.encode(UTF16LE) : CorvusRegexEcma262.utf8(replacement)
|
|
120
|
+
return @native.replace(string, template, all).force_encoding(CorvusRegexEcma262.result_encoding(string))
|
|
121
|
+
end
|
|
122
|
+
raise ArgumentError, "a replacement or a block, not both" unless replacement.nil?
|
|
123
|
+
|
|
124
|
+
string = CorvusRegexEcma262.snapshot(string)
|
|
125
|
+
out = +""
|
|
126
|
+
out.force_encoding(CorvusRegexEcma262.result_encoding(string))
|
|
127
|
+
last = 0
|
|
128
|
+
unit = string.encoding == UTF16LE ? 2 : 1
|
|
129
|
+
positions = Positions.new(string)
|
|
130
|
+
@native.each(string) do |groups|
|
|
131
|
+
m = Match.new(self, string, groups, positions)
|
|
132
|
+
out << string.byteslice(last * unit, (groups[0] - last) * unit) << yield(m).to_s
|
|
133
|
+
last = groups[1]
|
|
134
|
+
break unless all
|
|
135
|
+
end
|
|
136
|
+
out << string.byteslice(last * unit, string.bytesize - last * unit)
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# Where a match and its groups are, read as a MatchData is: by number, by name (String or Symbol), with begin, end
|
|
141
|
+
# and offset in characters (in code units for a UTF-16LE string), and byte_begin, byte_end and byteoffset in bytes.
|
|
142
|
+
class Match
|
|
143
|
+
attr_reader :string, :regexp
|
|
144
|
+
|
|
145
|
+
def initialize(regexp, string, groups, positions)
|
|
146
|
+
@regexp = regexp
|
|
147
|
+
@string = string
|
|
148
|
+
@groups = groups
|
|
149
|
+
@positions = positions
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# The number of groups, the whole match among them.
|
|
153
|
+
def size = @regexp.group_count + 1
|
|
154
|
+
alias length size
|
|
155
|
+
|
|
156
|
+
def names = @regexp.names
|
|
157
|
+
|
|
158
|
+
# The text of a group, by number, name or Symbol: nil when it did not take part.
|
|
159
|
+
def [](group)
|
|
160
|
+
s, e = span(group)
|
|
161
|
+
return nil if s.negative?
|
|
162
|
+
|
|
163
|
+
text(s, e)
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
# The start of the group in characters (code units for UTF-16LE), or nil when it did not take part.
|
|
167
|
+
def begin(group = 0)
|
|
168
|
+
s, = span(group)
|
|
169
|
+
s.negative? ? nil : @positions.char(s, false)
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
# The end of the group in characters (code units for UTF-16LE), or nil when it did not take part.
|
|
173
|
+
def end(group = 0)
|
|
174
|
+
_, e = span(group)
|
|
175
|
+
e.negative? ? nil : @positions.char(e, true)
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# [begin, end] of the group, or [nil, nil] when it did not take part.
|
|
179
|
+
def offset(group = 0) = [self.begin(group), self.end(group)]
|
|
180
|
+
|
|
181
|
+
def byte_begin(group = 0)
|
|
182
|
+
s, = span(group)
|
|
183
|
+
s.negative? ? nil : s * unit
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
def byte_end(group = 0)
|
|
187
|
+
_, e = span(group)
|
|
188
|
+
e.negative? ? nil : e * unit
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
def byteoffset(group = 0) = [byte_begin(group), byte_end(group)]
|
|
192
|
+
|
|
193
|
+
# The text of every capturing group, nil for one that did not take part.
|
|
194
|
+
def captures = (1...size).map { |g| self[g] }
|
|
195
|
+
|
|
196
|
+
# The text of each name's group.
|
|
197
|
+
def named_captures = names.to_h { |n| [n, self[n]] }
|
|
198
|
+
|
|
199
|
+
def to_a = (0...size).map { |g| self[g] }
|
|
200
|
+
def values_at(*groups) = groups.map { |g| self[g] }
|
|
201
|
+
def to_s = self[0]
|
|
202
|
+
def pre_match = text(0, @groups[0])
|
|
203
|
+
def post_match = @string.byteslice(@groups[1] * unit, @string.bytesize - (@groups[1] * unit))
|
|
204
|
+
|
|
205
|
+
def inspect = "#<CorvusRegexEcma262::Match #{to_s.inspect} #{offset.inspect}>"
|
|
206
|
+
|
|
207
|
+
private
|
|
208
|
+
|
|
209
|
+
def unit = @string.encoding == UTF16LE ? 2 : 1
|
|
210
|
+
|
|
211
|
+
# [start, end] of a group in engine positions (-1 for one that did not take part).
|
|
212
|
+
def span(group)
|
|
213
|
+
i = case group
|
|
214
|
+
when Integer
|
|
215
|
+
raise IndexError, "index #{group} out of matches" if group.negative? || group >= size
|
|
216
|
+
|
|
217
|
+
group
|
|
218
|
+
when String, Symbol
|
|
219
|
+
k = names.index(group.to_s)
|
|
220
|
+
raise IndexError, "undefined group name reference: #{group}" unless k
|
|
221
|
+
|
|
222
|
+
size + k
|
|
223
|
+
else
|
|
224
|
+
raise TypeError, "a group is an Integer, a String or a Symbol"
|
|
225
|
+
end
|
|
226
|
+
[@groups[2 * i], @groups[(2 * i) + 1]]
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
def text(s, e)
|
|
230
|
+
piece = @string.byteslice(s * unit, (e - s) * unit)
|
|
231
|
+
# A half of a character (without u or v, over UTF-8) is no UTF-8: written as U+FFFD, as the crate writes it.
|
|
232
|
+
piece.valid_encoding? ? piece : piece.scrub("�")
|
|
233
|
+
end
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
# Byte positions to character positions, going on from where it was last asked, since a search asks in order.
|
|
237
|
+
class Positions
|
|
238
|
+
def initialize(string)
|
|
239
|
+
@string = string
|
|
240
|
+
@identity = string.encoding == UTF16LE || string.ascii_only?
|
|
241
|
+
@byte = 0
|
|
242
|
+
@char = 0
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
# The character index of a byte position; a position inside a character (between the halves of one beyond the
|
|
246
|
+
# Basic Multilingual Plane) is its start, or (for an end) past it.
|
|
247
|
+
def char(byte, at_end)
|
|
248
|
+
return byte if @identity
|
|
249
|
+
|
|
250
|
+
@byte = @char = 0 if byte < @byte
|
|
251
|
+
inside = byte < @string.bytesize && (@string.getbyte(byte) & 0xC0) == 0x80
|
|
252
|
+
target = byte
|
|
253
|
+
target -= 1 while inside && target.positive? && (@string.getbyte(target) & 0xC0) == 0x80
|
|
254
|
+
@char += @string.byteslice(@byte, target - @byte).length
|
|
255
|
+
@byte = target
|
|
256
|
+
inside && at_end ? @char + 1 : @char
|
|
257
|
+
end
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
class << self
|
|
261
|
+
# Compiles a pattern: Regex.new(pattern, flags).
|
|
262
|
+
def compile(pattern, flags = "") = Regex.new(pattern, flags)
|
|
263
|
+
|
|
264
|
+
# Whether a pattern is valid with the flags, without compiling it.
|
|
265
|
+
def valid?(pattern, flags = "") = Regex.valid?(pattern, flags)
|
|
266
|
+
|
|
267
|
+
# The version of the crate the extension was built with.
|
|
268
|
+
def crate_version = Native.crate_version
|
|
269
|
+
|
|
270
|
+
# @api private
|
|
271
|
+
def utf8(s)
|
|
272
|
+
s = s.to_s
|
|
273
|
+
return s if s.encoding == Encoding::UTF_8 || (s.encoding == Encoding::US_ASCII && s.ascii_only?)
|
|
274
|
+
|
|
275
|
+
# A lone surrogate (in a UTF-16 pattern) is read as U+FFFD, as the crate's UTF-8 patterns have none.
|
|
276
|
+
s.encode(Encoding::UTF_8, invalid: :replace, undef: :replace, replace: "�")
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
# @api private: the string as a search that runs a block walks it, frozen, so that it cannot change under the
|
|
280
|
+
# search and any number of searches may walk it at once. A string that is not frozen is copied as Ruby copies a
|
|
281
|
+
# String, sharing its bytes until either is changed.
|
|
282
|
+
def snapshot(string) = string.frozen? ? string : string.dup.freeze
|
|
283
|
+
|
|
284
|
+
# @api private
|
|
285
|
+
def result_encoding(string) = string.encoding == UTF16LE ? UTF16LE : Encoding::UTF_8
|
|
286
|
+
|
|
287
|
+
# @api private: the engine position of a character position.
|
|
288
|
+
def unit_of(string, pos)
|
|
289
|
+
raise ArgumentError, "pos must not be negative" if pos.negative?
|
|
290
|
+
return pos if string.encoding == UTF16LE || string.ascii_only? || pos.zero?
|
|
291
|
+
# Past the end, as lastIndex past a string's length: no match.
|
|
292
|
+
return string.bytesize + 1 if pos > string.length
|
|
293
|
+
|
|
294
|
+
string[0, pos].bytesize
|
|
295
|
+
end
|
|
296
|
+
end
|
|
297
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: corvus_regex_ecma262
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.1
|
|
5
|
+
platform: x86_64-linux-gnu
|
|
6
|
+
authors:
|
|
7
|
+
- endjin
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-10-21 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Regular expressions that answer as JavaScript's RegExp does, every flag
|
|
14
|
+
(dgimsuvy), over UTF-8 and UTF-16LE strings, with the corvus-regex-ecma262 Rust
|
|
15
|
+
crate.
|
|
16
|
+
email:
|
|
17
|
+
executables: []
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- LICENSE
|
|
22
|
+
- README.md
|
|
23
|
+
- lib/corvus_regex_ecma262.rb
|
|
24
|
+
- lib/corvus_regex_ecma262/3.3/corvus_regex_ecma262.so
|
|
25
|
+
- lib/corvus_regex_ecma262/3.4/corvus_regex_ecma262.so
|
|
26
|
+
- lib/corvus_regex_ecma262/4.0/corvus_regex_ecma262.so
|
|
27
|
+
- lib/corvus_regex_ecma262/version.rb
|
|
28
|
+
homepage: https://github.com/corvus-dotnet/corvus-regex-ecma262-ruby
|
|
29
|
+
licenses:
|
|
30
|
+
- Apache-2.0
|
|
31
|
+
metadata:
|
|
32
|
+
source_code_uri: https://github.com/corvus-dotnet/corvus-regex-ecma262-ruby
|
|
33
|
+
bug_tracker_uri: https://github.com/corvus-dotnet/corvus-regex-ecma262-ruby/issues
|
|
34
|
+
rubygems_mfa_required: 'true'
|
|
35
|
+
post_install_message:
|
|
36
|
+
rdoc_options: []
|
|
37
|
+
require_paths:
|
|
38
|
+
- lib
|
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
40
|
+
requirements:
|
|
41
|
+
- - ">="
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
version: '3.3'
|
|
44
|
+
- - "<"
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: 4.1.dev
|
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
48
|
+
requirements:
|
|
49
|
+
- - ">="
|
|
50
|
+
- !ruby/object:Gem::Version
|
|
51
|
+
version: 3.3.22
|
|
52
|
+
requirements: []
|
|
53
|
+
rubygems_version: 3.5.23
|
|
54
|
+
signing_key:
|
|
55
|
+
specification_version: 4
|
|
56
|
+
summary: 'ECMA-262 (JavaScript) regular expressions for Ruby: the grammar and the
|
|
57
|
+
answers of RegExp, backed by Rust.'
|
|
58
|
+
test_files: []
|