| 1 |
C API for RUst's REgex engine |
| 2 |
============================= |
| 3 |
rure is a C API to Rust's regex library, which guarantees linear time |
| 4 |
searching using finite automata. In exchange, it must give up some common |
| 5 |
regex features such as backreferences and arbitrary lookaround. It does |
| 6 |
however include capturing groups, lazy matching, Unicode support and word |
| 7 |
boundary assertions. Its matching semantics generally correspond to Perl's, |
| 8 |
or "leftmost first." Namely, the match locations reported correspond to the |
| 9 |
first match that would be found by a backtracking engine. |
| 10 |
|
| 11 |
The header file (`includes/rure.h`) serves as the primary API documentation of |
| 12 |
this library. Types and flags are documented first, and functions follow. |
| 13 |
|
| 14 |
The syntax and possibly other useful things are documented in the Rust |
| 15 |
API documentation: https://docs.rs/regex |
| 16 |
|
| 17 |
|
| 18 |
Examples |
| 19 |
-------- |
| 20 |
There are readable examples in the `ctest` and `examples` sub-directories. |
| 21 |
|
| 22 |
Assuming you have |
| 23 |
[Rust and Cargo installed](https://www.rust-lang.org/downloads.html) |
| 24 |
(and a C compiler), then this should work to run the `iter` example: |
| 25 |
|
| 26 |
``` |
| 27 |
$ git clone git://github.com/rust-lang/regex |
| 28 |
$ cd regex/regex-capi/examples |
| 29 |
$ ./compile |
| 30 |
$ LD_LIBRARY_PATH=../target/release ./iter |
| 31 |
``` |
| 32 |
|
| 33 |
|
| 34 |
Performance |
| 35 |
----------- |
| 36 |
It's fast. Its core matching engine is a lazy DFA, which is what GNU grep |
| 37 |
and RE2 use. Like GNU grep, this regex engine can detect multi byte literals |
| 38 |
in the regex and will use fast literal string searching to quickly skip |
| 39 |
through the input to find possible match locations. |
| 40 |
|
| 41 |
All memory usage is bounded and all searching takes linear time with respect |
| 42 |
to the input string. |
| 43 |
|
| 44 |
For more details, see the PERFORMANCE guide: |
| 45 |
https://github.com/rust-lang/regex/blob/master/PERFORMANCE.md |
| 46 |
|
| 47 |
|
| 48 |
Text encoding |
| 49 |
------------- |
| 50 |
All regular expressions must be valid UTF-8. |
| 51 |
|
| 52 |
The text encoding of haystacks is more complicated. To a first |
| 53 |
approximation, haystacks should be UTF-8. In fact, UTF-8 (and, one |
| 54 |
supposes, ASCII) is the only well defined text encoding supported by this |
| 55 |
library. It is impossible to match UTF-16, UTF-32 or any other encoding |
| 56 |
without first transcoding it to UTF-8. |
| 57 |
|
| 58 |
With that said, haystacks do not need to be valid UTF-8, and if they aren't |
| 59 |
valid UTF-8, no performance penalty is paid. Whether invalid UTF-8 is |
| 60 |
matched or not depends on the regular expression. For example, with the |
| 61 |
`RURE_FLAG_UNICODE` flag enabled, the regex `.` is guaranteed to match a |
| 62 |
single UTF-8 encoding of a Unicode codepoint (sans LF). In particular, |
| 63 |
it will not match invalid UTF-8 such as `\xFF`, nor will it match surrogate |
| 64 |
codepoints or "alternate" (i.e., non-minimal) encodings of codepoints. |
| 65 |
However, with the `RURE_FLAG_UNICODE` flag disabled, the regex `.` will match |
| 66 |
any *single* arbitrary byte (sans LF), including `\xFF`. |
| 67 |
|
| 68 |
This provides a useful invariant: wherever `RURE_FLAG_UNICODE` is set, the |
| 69 |
corresponding regex is guaranteed to match valid UTF-8. Invalid UTF-8 will |
| 70 |
always prevent a match from happening when the flag is set. Since flags can be |
| 71 |
toggled in the regular expression itself, this allows one to pick and choose |
| 72 |
which parts of the regular expression must match UTF-8 or not. |
| 73 |
|
| 74 |
Some good advice is to always enable the `RURE_FLAG_UNICODE` flag (which is |
| 75 |
enabled when using `rure_compile_must`) and selectively disable the flag when |
| 76 |
one wants to match arbitrary bytes. The flag can be disabled in a regular |
| 77 |
expression with `(?-u)`. |
| 78 |
|
| 79 |
Finally, if one wants to match specific invalid UTF-8 bytes, then you can |
| 80 |
use escape sequences. e.g., `(?-u)\\xFF` will match `\xFF`. It's not |
| 81 |
possible to use C literal escape sequences in this case since regular |
| 82 |
expressions must be valid UTF-8. |
| 83 |
|
| 84 |
|
| 85 |
Aborts |
| 86 |
------ |
| 87 |
This library will abort your process if an unwinding panic is caught in the |
| 88 |
Rust code. Generally, a panic occurs when there is a bug in the program or |
| 89 |
if allocation failed. It is possible to cause this behavior by passing |
| 90 |
invalid inputs to some functions. For example, giving an invalid capture |
| 91 |
group index to `rure_captures_at` will cause Rust's bounds checks to fail, |
| 92 |
which will cause a panic, which will be caught and printed to stderr. The |
| 93 |
process will then `abort`. |
| 94 |
|
| 95 |
|
| 96 |
Missing |
| 97 |
------- |
| 98 |
There are a few things missing from the C API that are present in the Rust API. |
| 99 |
There's no particular (known) reason why they don't, they just haven't been |
| 100 |
implemented yet. |
| 101 |
|
| 102 |
* Splitting a string by a regex. |
| 103 |
* Replacing regex matches in a string with some other text. |