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

Is it necessary to use String for Text(String) variant #1

Open
alexpyattaev opened this issue Mar 22, 2024 · 6 comments
Open

Is it necessary to use String for Text(String) variant #1

alexpyattaev opened this issue Mar 22, 2024 · 6 comments

Comments

@alexpyattaev
Copy link

Great idea to rid the world of regex=) Small performance question - when creating the match pattern, for Text it is necessary to allocate memory for every small bit of pattern, which is a lot of allocations. Is it possible for Text to take a string reference instead?

@Heiss
Copy link
Owner

Heiss commented May 29, 2024

Maybe not, but this was a first idea.

Text(String),

If you know a way to store a reference in enums, it would be an easy fix.

@Heiss Heiss closed this as completed in 3bd373f May 29, 2024
@Heiss
Copy link
Owner

Heiss commented May 29, 2024

I implemented it now like you suggested. Was easy and gives some more insides about rust. :)

Takes now &str everywhere possible. Only in some cases it needs allocations and a dirty hack with Box::leak. But i think, this are the rare use cases, where it is okay. Maybe there could be more things to be done, when it comes to compile-time optimization. But this is right now far over my head. :)

Thank you for your input.

@alexpyattaev
Copy link
Author

I am glad that you are improving your rust skills while also maintaining this! I'll look into the Box::leak trick that you are using now to check if it is sound, if not there are some mechanisms for string manipulation at compile time I can try. Stay tuned for a PR.

@alexpyattaev
Copy link
Author

Ok just checked the not(Text("something")), and it would appear that while it indeed does not leak memory, it does not actually work (or I am missing what it is supposed to be doing). Can you clarify? Regex does not appear to have a notion of negative match on text that makes sense in general case. Thanks!

@Heiss Heiss reopened this May 31, 2024
@Heiss
Copy link
Owner

Heiss commented May 31, 2024

Okay sad. Then i need another workaround and more tests to catch this case. :) Thank you for your testing.

Regex does not appear to have a notion of negative match on text that makes sense in general case

Nah. My intuition would say: The equivilant of Not from a Text would be: "I assume, that this text is not here". So it would make sense to me that there is an opposite of text. :)

Given example should clarify this.

let input = Not(Exactly(Text("foo"))).and(Exactly(Text("bar")));
let re = create_reg_exp(input).unwrap();

assert!(!re.is_match("foobar"));  // Attention: Exclamation mark!
assert!(re.is_match("barbar"));
assert!(re.is_match("fooShouldNotBeAProblemAsLongAsItIsNotInFrontOFbar"));

But this is a different statement as it is right now implemented.

If we confirm, that this would be the expected behaivour, then yes you are right, this does not work right now. :)

@alexpyattaev
Copy link
Author

alexpyattaev commented Jun 1, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants