-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
nu-cli/completions: better fix for files with special characters #5254
nu-cli/completions: better fix for files with special characters #5254
Conversation
I'm not sure if we should match other cases:
Let me know what you guys think about it. |
19115e4
to
8ec1f6e
Compare
Since you are already splitting the function into a check and a separate escape replacement step I'd just iterate over over the string once and check against all characters that trigger quoting in one go rather than iterating over the string separately for each character like the current code does: let needs_escape = path.chars().fold( false, |acc, x|
acc
|| " \\\"'`".contains(x) // or write out the comparisons against each possible char
); In fact that can still be sped up without compromising anything:
*not sure how non-breaking, zero-width and fullwidth spaces are handled in nu So let's iterate & compare individual bytes instead: let needs_escape = path.bytes().fold( false, |acc, x|
acc
|| b" \\\"'`".contains(x)
); However if we don't mind being a bit more zealous with triggering the escape than perhaps necessary (may or may not be a useful thing, I'm not sure how much string interpolation etc happens after this part) we can further optimize this: ASCII is quite deliberate with its character layout in terms of bit patterns. If you look closely you'll notice that a lot of the usual special characters in shells have their top bits fixed to let needs_escape = path.bytes().fold(false, |acc, x|
acc
|| (x >> 4) == 0b0010
|| x == '\\' as u8 // 0x5c
|| x == '`' as u8 // 0x60
); Or simply go with the lazy shotgun approach: let needs_escape = path.bytes().fold( false, |acc, x| acc || x.is_ascii_punctuation() ); For this particular case that is not the most efficient solution as it appears that simply checks each individual punctuation character instead of shortening the check based on the bit patterns. Not that it should really matter in this day and age. 😆 |
In case you are not familiar with
|
Could you check now @jansol? |
The tests are failing, looks like it's escaping more than it should:
This case shouldn't be escaped, right? It's due to the |
Yeah that case does not really need quoting. This is what I meant with the comment about being unnecessarily trigger-happy with quoting. |
If you still want to quote the rest of the characters in that column, forward slash is easy to special case out of that condition: |
wow, nice one @jansol ! |
a8ba019
to
95e689c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This trashes non-ascii characters in its current form.
@jansol @herlon214 we may want to take a step back. let's try this. let's just get something working, land the pr, then think more about optimizing it. so, this pr should be for fixing the current situation. I'm also not very interested in unsafe code. |
@fdncred I'm not sure iif we can reproduce the tests on windows, the CI is showing error:
|
I'm on windows and I can't check out this pr. I get this
|
ah, double quotes are forbidden in file names (and paths i assume) so we can't have a folder with "" in it like it is above. |
I would also add test cases for a path with multibyte characters that needs escaping and one that does not. Let's say For the sake of cross-platform testing the escaping function should probably just get a unit test that just works with hardcoded paths instead of touching any files so we don't need to worry about the limitations of windows. |
@jansol that's exactly what I'm doing right now. |
@fdncred I don't think it will be possible to create the test for windows. Once the path will always be parsed AND the path string from |
@herlon214 if you're trying to create a test to parse an path in windows that has illegal characters like double quotes, then a test isn't necessary because the OS will prevent that from happening and it'll never get to nushell to figure out what to do. so, in that case, it doesn't matter if there's a test. |
How does that interact with things like WSL? Linux allows almost all characters in filenames (except forward slash '/') after all, provided that the underlying filesystem supports them. |
it would work exactly the same way it work on any linux installation since WSL is, in fact, running in a linux vm on the ext4 file system. |
@fdncred no, no. So we have:
Therefore, the same will apply to all the other tests. I think for windows it should only escape when the
And for other OS it doesn't need to be escaped, is that correct? |
If '\' is the control character used for escaping special characters in nu strings it should always be escaped. |
83b74d9
to
c20aa31
Compare
@herlon214 can we make the test that's failing just work on *nix and bypass windows. i know we do that on a few tests. |
The test that’s failing isn’t failing when it’s called, but when it’s compiled. It’s trying to call a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should resolve the errors.
@merelymyself yeah that's the latest error, this is easy to solve, but isn't the error we've been discussing. I was just trying to test some ideas and made that compile error |
@fdncred yeah that should be doable, we can proceed with that at least for now. |
@fdncred tests passing now, ignoring windows. |
ok, let's land this then. thanks to the team for all the hard work and effort. |
…hell#5254) * nu-cli/completions: fix paths with special chars * add backticks * fix replace * added single quotes to check list * check escape using fold * fix clippy errors * fix comment line * fix conflicts * change to vec * skip sort checking * removed invalid windows path * remove comment * added tests for escape function * fix fn import * fix fn import error * test windows issue fix * fix windows backslash path in the tests * show expected path on error * skip test for windows
…ers (nushell#5254)" (nushell#5372) This reverts commit 3cf3329.
Description
Creating this PR for for discussing a better implementation for handling paths with special characters.
Related:
Also, created a function so that logic can be reused.
Tests
Make sure you've run and fixed any issues with these commands:
cargo fmt --all -- --check
to check standard code formatting (cargo fmt --all
applies these changes)cargo clippy --all --all-features -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect
to check that you're using the standard code stylecargo build; cargo test --all --all-features
to check that all the tests pass