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

Improvement to greedy-flag #967

Merged
merged 1 commit into from
Jun 11, 2016

Conversation

zeitgeist87
Copy link
Collaborator

This patch expands the idea of the greedy flag and significantly
improves it, by matching against the whole text instead of just
the next couple of tokens. This does not only improve the
results, but it should also slightly improve performance.

This patch expands the idea of the greedy flag and significantly
improves it, by matching against the whole text instead of just
the next couple of tokens. This does not only improve the
results, but it should also slightly improve performance.
@zeitgeist87
Copy link
Collaborator Author

The issue #963 inspired me to look into improving the greedy-flag feature and I am quite excited about the result.

The idea is essentially the same as before, but instead of matching a greedy-pattern against the concatenated string of the next few tokens, it matches against the whole text. With this patch the following code matches correctly:

/ '1' '2' '3' '4' '5' /
"foo `a` `b` `c` `d` bar"

@Golmote
Copy link
Contributor

Golmote commented Jun 4, 2016

Sounds awesome! I'll try to get some time to review this later this week.

@LeaVerou
Copy link
Member

LeaVerou commented Jun 7, 2016

Thank you @zeitgeist87!
I'm rally swamped with preparing talks right now, so if @Golmote feels it's ok, feel free to merge!
From a quick look it looks good!

@Golmote
Copy link
Contributor

Golmote commented Jun 10, 2016

This looks good to me, @zeitgeist87. It seems to works really well, kudos! Merge it whenever you want.

@zeitgeist87 zeitgeist87 merged commit 500121b into PrismJS:gh-pages Jun 11, 2016
@@ -280,9 +280,15 @@ var _ = _self.Prism = {
lookbehindLength = 0,
alias = pattern.alias;

if (greedy && !pattern.pattern.global) {
// Without the global flag, lastIndex won't work
pattern.pattern = RegExp(pattern.pattern.source, pattern.pattern.flags + "g");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zeitgeist87: How recent is the flags property? It looks like it's not supported very well... I just ran into errors running npm test and I think they are due to this.

Maybe we could refactor like so:

var flags = pattern.pattern.toString().match(/[imuy]*$/)[0];
pattern.pattern = RegExp(pattern.pattern.source, flags + "g");

What do you think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Golmote Yes you are right it is not supported well at all. Good catch. Your solution is perfect and we should add it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Golmote Shall I add this fix or do you?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zeitgeist87 I just did, thanks. ;)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather we added a separate polyfill for flags instead of refactoring our code not to use it. That way, once support is more broad, we can just ditch the polyfill.

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

Successfully merging this pull request may close these issues.

3 participants