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

Overlapping value replacement #365

Merged
merged 2 commits into from Nov 13, 2017
Merged

Overlapping value replacement #365

merged 2 commits into from Nov 13, 2017

Conversation

@tivac
Copy link
Owner

@tivac tivac commented Nov 12, 2017

Fixes #363 by sorting values by length so that longer replacements match first.

A little hacky-feeling, but seems ok after thinking about it for a bit.

This commit also solves the recurring travis failures I've been seeing thanks to a tip from @darthmaim in gitter.

So that values with the same prefixes don't stomp over each other. This feels a bit hacky but works for now.

Fixes #363
pygy
Copy link

pygy commented on 2c66d62 Nov 11, 2017

Choose a reason for hiding this comment

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

If it can help you feel better about the fix, that was my first idea as well. I think it is optimal as it guarantees that potential prefixes are replaced last, without resorting to string content comparisons that would be costlier.

Loading

pygy
Copy link

pygy commented on 2c66d62 Nov 11, 2017

Choose a reason for hiding this comment

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

Looking at the broader context, you may get a speedier matcher by sorting first alphabetically then by length, allowing the regex engine to build an efficient trie (if it can do such a thing). Otherwise you could build a trie-like regex manually.

Loading

pygy
Copy link

pygy commented on 2c66d62 Nov 11, 2017

Choose a reason for hiding this comment

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

I was offline this afternoon and whipped up this, only to find out that this lib existed. It has been vetted by Mathias Bynens, I'd rather go with it.

new RegExp('\b' + new RegExpTrie().add(Object.keys(values)).toString() + '\b') will give you an optimized matcher.

One caveat with \b: /\bx\b/ will match -x-, so there may be false positives, depending on what your values can contain.

You may want to go with '([^\w-]|^)(' + new RegExpTrie()... + ')([^\w-]|$), and restore the first/last char when returning.

Loading

tivac
Copy link
Owner

tivac commented on 2c66d62 Nov 12, 2017

Choose a reason for hiding this comment

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

regex-trie is an interesting approach, I wonder if there'd be much benefit to processing time.

I've never been thrilled about my chosen approach to replacing @value instances, it feels really brittle to me currently.

Loading

pygy
Copy link

pygy commented on 2c66d62 Nov 12, 2017

Choose a reason for hiding this comment

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

What's the shape of the node.value fields that are to be replaced? Could one of these hold either a prefix or a suffix of one of the values items (node.value not being a values item)? If so, the use of \b is indeed problematic ('([^\w-]|^)(' + valuesMatcher + ')([^\w-]|$) being a solution).

Explicitly, if values is ['foo'], and node.value is 'hi-foo-bar', you'll get a false positive match.

Otherwise sorting the values by length before matching should be robust AFAICT.

Loading

- Directly write files instead of copying
- Use travis in VM mode instead of container
@tivac tivac self-assigned this Nov 12, 2017
@tivac tivac added the bugfix label Nov 12, 2017
@tivac tivac merged commit 1f6fdb5 into master Nov 13, 2017
2 checks passed
Loading
@tivac tivac deleted the overlapping-value-replacement branch Nov 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants