-
Notifications
You must be signed in to change notification settings - Fork 471
[RFC] regex-capi: expose regex::escape #537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that this would be a nice addition! I think there are a few small issues with the implementation in this PR that need to be addressed before merging though.
Sorry to use this PR as a way to learn more about rust but i don't quite understand something.
to
but i get an error on the last line saying that
Why suddenly the compiler is not able to infer the correct type ? |
Good question. I don't know off the top of my head. It might just be an inference failure. |
@marmeladema I can't seem to reproduce your problem: https://play.rust-lang.org/?version=stable&mode=debug&edition=2015&gist=964bf197a31f7342fcf79df66163a45d Which version of Rust are you using? |
rustc 1.30.0 (da5f414c2 2018-10-24) Same error with: And also with current nightly: |
Dunno then. Can you reproduce it on the playground? If not, it might be the |
1246545
to
b0a83b8
Compare
PR updated with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm. Apologies for not catching this earlier, but reviewing this again, I'm realizing that while rure_escape_must
probably looks OK, I think rure_escape
is not quite right since pattern
is actually allowed to contain a NUL
byte. It's a bit of a corner case, but it's technically legal, so we should get it right. That means returning a const uint8_t *
along with a length. So the function signature probably wants to be:
void rure_escape(const uint8_t *pattern, size_t length, const uint8_t **escaped, size_t *escaped_len, rure_error *error);
or something like that anyway.
With that said, I would be more than happy to accept a PR that dropped rure_escape
and just had rure_escape_must
. Sorry again for the run-around!
I have an updated version working with prototype
I can understand 1., but 2. sounds weird to me for an escaping function. I understand its not necessary to escape null byte because String handle it properly, but it make a lot of things hard to deal with, like printing etc. |
@BurntSushi any news about this? |
This commit exposes two new functions in regex's C API: rure_escape_must and rure_cstring_free. These permit escaping a pattern such that it contains no special regex meta characters. Currently, we only expose a routine that will abort the process if it fails, but we document the precise error conditions. A more flexible but less convenient routine should ideally be exposed in the future, but that needs a bit more API design than what's here. Closes #537
I merged this in #567, after doing a bit of cleanup. I removed the I'd be happy to accept another PR if you want to implement Thanks for this! |
I needed regex::escape available in the C API so i added it, but i have only done a few hours of rust in my life so this patch might be totally wrong. I have successfully been able to rust-regex-escape some strings from Python using ctypes to load the library though.
I followed this guide to handle String as returned type: http://jakegoulding.com/rust-ffi-omnibus/string_return/