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

Disallow certain non-filename characters in Export Text #13693

Merged
3 commits merged into from
Aug 11, 2022

Conversation

Eschivo
Copy link
Contributor

@Eschivo Eschivo commented Aug 5, 2022

Added control in code for not allowed characters in the filename when saving a
tab.

Closes #13664

@Eschivo
Copy link
Contributor Author

Eschivo commented Aug 5, 2022

@zadjii-msft not tried it yet, but something like this could work. Maybe it can be wrapped in a function str_to_filename and put somewhere else.

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 :)

src/cascadia/TerminalApp/TabManagement.cpp Outdated Show resolved Hide resolved
src/cascadia/TerminalApp/TabManagement.cpp Outdated Show resolved Hide resolved
src/cascadia/TerminalApp/TabManagement.cpp Outdated Show resolved Hide resolved
@lhecker
Copy link
Member

lhecker commented Aug 5, 2022

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.

@Eschivo
Copy link
Contributor Author

Eschivo commented Aug 6, 2022

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

@lhecker
Copy link
Member

lhecker commented Aug 7, 2022

Do you think that this file is a good place to define such a table and function?

If you choose to use the table based approach, then I'd suggest putting it into our <til/string.h> header: https://github.com/microsoft/terminal/blob/ffe9a0f09bc7b3cb96908d265c93bcd1a4e75fa2/src/inc/til/string.h
It already contains similar string-related functions like visualize_control_codes. But I'd approve this PR definitely even without it. Such a function would have no measurable benefit for this use case after all - it would just look nice (which can also be neat in a way 😅). I've adjusted my pseudo-code above to reflect the different code style in that file.

@xtremevipper
Copy link

Sorry, i am missing

@Eschivo
Copy link
Contributor Author

Eschivo commented Aug 8, 2022

Thank you again @lhecker ! I'm going to work on this these days

@Eschivo
Copy link
Contributor Author

Eschivo commented Aug 9, 2022

I added the table in <til/string.h> but have not tested it yet since I had some problems with my installation of C++ with VS. I have VS2019 16.11 but when I try to build I get this error:

C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Microsoft\VC\v160\Microsoft.CppBuild.targets(439,5): error MSB8020: The build tools for v143 (Platform Toolset = 'v143') cannot be found. To build using the v143 build tools, please install v143 build tools. Alternatively, you may upgrade to the current Visual Studio tools by selecting the Project menu or right-click the solution, and then selecting "Retarget solution". [C: \Users\eliaa\Documents\GitHub\terminal\src\tools\closetest\CloseTest.vcxproj]

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

@DHowett
Copy link
Member

DHowett commented Aug 9, 2022

Thanks for bearing with us! Yeah, unfortunately as of pretty recently we require VS 2022 😦

@lhecker
Copy link
Member

lhecker commented Aug 9, 2022

Thanks for bearing with us! Yeah, unfortunately as of pretty recently we require VS 2022 😦

I'll submit a PR to restore support for VS 2019 soon. We can't officially support it for this project though since only VS 2022 fully supports C++20 unfortunately.

Edit: After some deliberation we've decided to not restore support for VS 2019. It just wouldn't work correctly.

src/inc/til/string.h Outdated Show resolved Hide resolved
Co-authored-by: Leonard Hecker <leonard@hecker.io>
@Eschivo
Copy link
Contributor Author

Eschivo commented Aug 9, 2022

Edit: After some deliberation we've decided to not restore support for VS 2019. It just wouldn't work correctly.

If your decision is definitive, consider updating the readme here:

terminal/README.md

Lines 295 to 296 in b55bcb5

* You must have at least [VS
2019](https://visualstudio.microsoft.com/downloads/) installed

I can do that little edit in a PR if the project will be supported on VS2022 only from now on

@lhecker
Copy link
Member

lhecker commented Aug 9, 2022

I've opened a PR a few hours ago actually: #13709
I'm extremely sorry about that! When I upgraded the project to C++20 and VS 2022 I totally forgot to update the readme. 😥

Copy link
Member

@zadjii-msft zadjii-msft left a 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!

@Eschivo
Copy link
Contributor Author

Eschivo commented Aug 11, 2022 via email

@zadjii-msft zadjii-msft marked this pull request as ready for review August 11, 2022 16:54
@ghost ghost added Area-UserInterface Issues pertaining to the user interface of the Console or Terminal Issue-Bug It either shouldn't be doing this or needs an investigation. Priority-3 A description (P3) Product-Terminal The new Windows Terminal. labels Aug 11, 2022
Copy link
Member

@DHowett DHowett left a 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).
Copy link
Member

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.

@DHowett DHowett changed the title Added control in code for not allowed characters in filename Disallow certain non-filename characters in Export Text Aug 11, 2022
@DHowett DHowett added the AutoMerge Marked for automatic merge by the bot when requirements are met label Aug 11, 2022
@ghost
Copy link

ghost commented Aug 11, 2022

Hello @DHowett!

Because this pull request has the AutoMerge label, I will be glad to assist with helping to merge this pull request once all check-in policies pass.

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 (@msftbot) and give me an instruction to get started! Learn more here.

@ghost ghost merged commit 8721573 into microsoft:main Aug 11, 2022
@Eschivo
Copy link
Contributor Author

Eschivo commented Aug 14, 2022

Thank you guys! Hope to work with you on other issues in the future!

@Eschivo Eschivo deleted the filename_correction_tabsave branch August 14, 2022 12:40
DHowett pushed a commit that referenced this pull request Sep 6, 2022
Added control in code for not allowed characters in the filename when saving a
tab.

Closes #13664

(cherry picked from commit 8721573)
Service-Card-Id: 85487287
Service-Version: 1.15
DHowett added a commit that referenced this pull request Sep 9, 2022
@ghost
Copy link

ghost commented Sep 13, 2022

🎉Windows Terminal Preview v1.16.252 has been released which incorporates this pull request.:tada:

Handy links:

DHowett pushed a commit that referenced this pull request Sep 27, 2022
Added control in code for not allowed characters in the filename when saving a
tab.

Closes #13664

(cherry picked from commit 8721573)
Service-Card-Id: 85487287
Service-Version: 1.15
@ghost
Copy link

ghost commented Oct 18, 2022

🎉Windows Terminal v1.15.2874 has been released which incorporates this pull request.:tada:

Handy links:

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-UserInterface Issues pertaining to the user interface of the Console or Terminal AutoMerge Marked for automatic merge by the bot when requirements are met Issue-Bug It either shouldn't be doing this or needs an investigation. Priority-3 A description (P3) Product-Terminal The new Windows Terminal.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Export Text when in Admin mode filename suggestion has invalid character ":"
6 participants