Skip to content
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

Expand Regex to allow querying for metacharacters #5962

Merged
merged 4 commits into from Apr 20, 2018

Conversation

Sija
Copy link
Contributor

@Sija Sija commented Apr 18, 2018

Would accommodate the need for such query from outside of Regex.escape.

@sdogruyol
Copy link
Member

I hate to deal with special characters...especially manually. So, this gets a big 👍 from me.

src/regex.cr Outdated Show resolved Hide resolved
src/regex.cr Outdated Show resolved Hide resolved
@straight-shoota
Copy link
Member

What exactly is a "need for such query from outside of Regex.escape"?

A use case like string = Regex.escape(string) if Regex.needs_escape?(string) is probably not worth it because you can just Regex.escape every time with minimal overhead.

Regex.escape can even be improved to copy unescaped bytes in slices for more performance.

@Sija
Copy link
Contributor Author

Sija commented Apr 19, 2018

What exactly is a "need for such query from outside of Regex.escape"?

@straight-shoota When you'd want to validate strings in terms are they regex-like, without resorting to manual checks. Escaping them is another step which sometimes could even be omitted.

@straight-shoota
Copy link
Member

But foo is also a valid regex, yet it does not need escape.

@sdogruyol
Copy link
Member

sdogruyol commented Apr 19, 2018

On second thought, how about escape? instead of needs_escape? 🤔

@Sija
Copy link
Contributor Author

Sija commented Apr 19, 2018

@straight-shoota I misspoke, what I meant was checking whether a string needs to be escaped - for validation purposes only.

@Sija
Copy link
Contributor Author

Sija commented Apr 19, 2018

@sdogruyol For me such name would imply non-raising version of Regex.escape.

@sdogruyol
Copy link
Member

@Sija yeah, you're right (looking at you Ruby) 👍

src/regex.cr Outdated Show resolved Hide resolved
Since previous, although cleaner, code results in ~60% performance penalty.
src/regex.cr Outdated Show resolved Hide resolved
src/regex.cr Outdated Show resolved Hide resolved
@RX14 RX14 added this to the Next milestone Apr 20, 2018
@RX14 RX14 merged commit 980492c into crystal-lang:master Apr 20, 2018
chris-huxtable pushed a commit to chris-huxtable/crystal that referenced this pull request Jun 6, 2018
* Add Regex.needs_escape?

* Revert to using case statement with hard-coded character list

Since previous, although cleaner, code results in ~60% performance penalty.

* Use macro expansion instead of hard-coded list, props to @RX14

* Revert to the original implementation

Since this one incurs ~10% performance penalty due to optimizer heuristics (as per crystal-lang#5962 (comment))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants