Skip to content

parallels: simplify regex for vm files#108

Merged
nywilken merged 1 commit intomainfrom
parallels_regex_cleanup
Jan 16, 2024
Merged

parallels: simplify regex for vm files#108
nywilken merged 1 commit intomainfrom
parallels_regex_cleanup

Conversation

@lbajolet-hashicorp
Copy link
Contributor

@lbajolet-hashicorp lbajolet-hashicorp commented Jan 9, 2024

The original regex had multiple occurrences of a .+? pattern, which is essentially the same as a .*, so we replace the former by the latter for clarity.

Additionally, since the beginning of the path does not interest us, we can ignore it from the regex, and only start matching once we have found what's interesting for us, further simplifying the expression.

@lbajolet-hashicorp lbajolet-hashicorp requested a review from a team as a code owner January 9, 2024 19:45

tmpPath := filepath.ToSlash(path)
pathRe := regexp.MustCompile(`^(.+?)([^/]+\.(pvm|macvm)/.+?)$`)
pathRe := regexp.MustCompile(`[^/]+\.(pvm|macvm)/.+$`)
Copy link
Contributor

Choose a reason for hiding this comment

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

is pvmPath = filepath.FromSlash(matches[2]) still correct since there is only one capture would this be at index 1?

I think it would be great to add a test for this. It wasn't clear to me what the actual file paths can be.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is a good question, I tested this with the updated test that I added to #107, and it did not fail fwiw, but I'll check what happens here. I would think there's two matches only indeed, the global one and the pvm/macvm.
I'll investigate, thanks for the callout

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright nevermind, I guess I didn't do my test right and it does crash now, I'll fix that now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Rerolled, since we don't capture the whole path now, but instead only the .+.pvm|macvm/..., we don't need to get a submatch and instead we can use FindString to return the whole path, excluding the parent directories.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: I based this PR on #107 for tests to run, we can rebase it on main before merging

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for making the update. I rebased onto the latest main and will merge once all goes green.

@lbajolet-hashicorp lbajolet-hashicorp force-pushed the parallels_regex_cleanup branch 2 times, most recently from 10ed84f to c5a2cf4 Compare January 9, 2024 20:52
The original regex had multiple occurrences of a `.+?' pattern, which is
essentially the same as a `.*', so we replace the former by the latter
for clarity.

Additionally, since the beginning of the path does not interest us, we
can ignore it from the regex, and only start matching once we have found
what's interesting for us, further simplifying the expression.
@nywilken nywilken force-pushed the parallels_regex_cleanup branch from c5a2cf4 to 8f85b4b Compare January 16, 2024 21:58

tmpPath := filepath.ToSlash(path)
pathRe := regexp.MustCompile(`^(.+?)([^/]+\.(pvm|macvm)/.+?)$`)
pathRe := regexp.MustCompile(`[^/]+\.(pvm|macvm)/.+$`)
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for making the update. I rebased onto the latest main and will merge once all goes green.

@nywilken nywilken merged commit 308d101 into main Jan 16, 2024
@nywilken nywilken deleted the parallels_regex_cleanup branch January 16, 2024 22:13
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.

2 participants