-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Disallow certain non-filename characters in Export Text #13693
Conversation
@zadjii-msft not tried it yet, but something like this could work. Maybe it can be wrapped in a function Side-note: I'm a beginner in C++, so maybe there's a better way of implementing this (maybe using stl algorithms?). Let me know any feedback :) |
The STL has no builtin way to efficiently filter a string, so for the (theoretically) best approach you have to build your own algorithm, usually done by using a table approach (pseudo code, might not work): std::wstring clean_filename(std::wstring str) noexcept {
static constexpr std::array<bool, 128> filter{{
// clang-format off
0 /* NUL */, 0 /* SOH */, 0 /* STX */, 0 /* ETX */, 0 /* EOT */, 0 /* ENQ */, 0 /* ACK */, 0 /* BEL */, 0 /* BS */, 0 /* HT */, 0 /* LF */, 0 /* VT */, 0 /* FF */, 0 /* CR */, 0 /* SO */, 0 /* SI */,
0 /* DLE */, 0 /* DC1 */, 0 /* DC2 */, 0 /* DC3 */, 0 /* DC4 */, 0 /* NAK */, 0 /* SYN */, 0 /* ETB */, 0 /* CAN */, 0 /* EM */, 0 /* SUB */, 0 /* ESC */, 0 /* FS */, 0 /* GS */, 0 /* RS */, 0 /* US */,
0 /* SP */, 0 /* ! */, 1 /* " */, 0 /* # */, 0 /* $ */, 0 /* % */, 0 /* & */, 0 /* ' */, 0 /* ( */, 0 /* ) */, 1 /* * */, 0 /* + */, 0 /* , */, 0 /* - */, 0 /* . */, 1 /* / */,
0 /* 0 */, 0 /* 1 */, 0 /* 2 */, 0 /* 3 */, 0 /* 4 */, 0 /* 5 */, 0 /* 6 */, 0 /* 7 */, 0 /* 8 */, 0 /* 9 */, 1 /* : */, 0 /* ; */, 1 /* < */, 0 /* = */, 1 /* > */, 1 /* ? */,
0 /* @ */, 0 /* A */, 0 /* B */, 0 /* C */, 0 /* D */, 0 /* E */, 0 /* F */, 0 /* G */, 0 /* H */, 0 /* I */, 0 /* J */, 0 /* K */, 0 /* L */, 0 /* M */, 0 /* N */, 0 /* O */,
0 /* P */, 0 /* Q */, 0 /* R */, 0 /* S */, 0 /* T */, 0 /* U */, 0 /* V */, 0 /* W */, 0 /* X */, 0 /* Y */, 0 /* Z */, 0 /* [ */, 1 /* \ */, 0 /* ] */, 0 /* ^ */, 0 /* _ */,
0 /* ` */, 0 /* a */, 0 /* b */, 0 /* c */, 0 /* d */, 0 /* e */, 0 /* f */, 0 /* g */, 0 /* h */, 0 /* i */, 0 /* j */, 0 /* k */, 0 /* l */, 0 /* m */, 0 /* n */, 0 /* o */,
0 /* p */, 0 /* q */, 0 /* r */, 0 /* s */, 0 /* t */, 0 /* u */, 0 /* v */, 0 /* w */, 0 /* x */, 0 /* y */, 0 /* z */, 0 /* { */, 1 /* | */, 0 /* } */, 0 /* ~ */, 0 /* DEL */,
// clang-format on
}};
std::erase_if(str, [](auto ch) {
// This lookup is branchless: It always checks the filter, but throws
// away the result if ch >= 128. This is faster than using `&&` (branchy).
return filter[ch & 127] & (ch < 128);
});
return str;
} Your approach is fine as well. That code isn't being called particularly often and the string isn't very long. |
Wow! Thank you for all your feedback! I think that for now I'm going to correct my code with your suggestions and test it and maybe then improve it using the table. Do you think that this file is a good place to define such a table and function? Or maybe is better to put them in another file? I think that maybe the second option is better since it could be called also by other parts of the code if needed |
If you choose to use the table based approach, then I'd suggest putting it into our |
Sorry, i am missing |
Thank you again @lhecker ! I'm going to work on this these days |
I added the table in
that searching should be resolved by installing VS2022, or otherwise, the target VS should be changed by the solution properties, is this correct? Anyway, I start to send the code since the installation of VS2022 is taking some time so I can get feedback on the work |
Thanks for bearing with us! Yeah, unfortunately as of pretty recently we require VS 2022 😦 |
Edit: After some deliberation we've decided to not restore support for VS 2019. It just wouldn't work correctly. |
Co-authored-by: Leonard Hecker <leonard@hecker.io>
If your decision is definitive, consider updating the readme here: Lines 295 to 296 in b55bcb5
I can do that little edit in a PR if the project will be supported on VS2022 only from now on |
I've opened a PR a few hours ago actually: #13709 |
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.
Any reason this is still in draft? This looks good to me!
No, forgive me! I forgot to check it as ready to merge
|
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 is excellent work, thank you for doing it!
|
||
std::erase_if(str, [](auto ch) { | ||
// This lookup is branchless: It always checks the filter, but throws | ||
// away the result if ch >= 128. This is faster than using `&&` (branchy). |
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 SCREAMS "leonard code". I love it.
Hello @DHowett! Because this pull request has the p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (
|
Thank you guys! Hope to work with you on other issues in the future! |
…)" This reverts commit 64c774e.
🎉 Handy links: |
🎉 Handy links: |
Added control in code for not allowed characters in the filename when saving a
tab.
Closes #13664