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

Ensure PowerShell Core profile commandline is quoted #12086

Merged
1 commit merged into from
Jan 4, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,15 @@ void PowershellCoreProfileGenerator::GenerateProfiles(std::vector<winrt::com_ptr
{
const auto name = psI.Name();
auto profile{ CreateDynamicProfile(name) };
profile->Commandline(winrt::hstring{ psI.executablePath.native() });

const auto& unquotedCommandline = psI.executablePath.native();
std::wstring quotedCommandline;
quotedCommandline.reserve(unquotedCommandline.size() + 2);
quotedCommandline.push_back(L'"');
quotedCommandline.append(unquotedCommandline);
quotedCommandline.push_back(L'"');
profile->Commandline(winrt::hstring{ quotedCommandline });
Comment on lines +313 to +319
Copy link
Member

Choose a reason for hiding this comment

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

for cheap tricks, we could also...

auto quotedCommandline = fmt::format(FMT_COMPILE(L"\"{}\""), psI.executablePath.native());

which should (?????) compile down to effectively optimal code (!)

Copy link
Contributor Author

@ianjoneill ianjoneill Jan 4, 2022

Choose a reason for hiding this comment

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

A micro-benchmark on my machine shows my code to be faster :)

        auto start = std::chrono::steady_clock::now();
        for (int i = 0; i < 1000000; i++)
        {
            const auto& unquotedCommandline = psI.executablePath.native();
            std::wstring quotedCommandline;
            quotedCommandline.reserve(unquotedCommandline.size() + 2);
            quotedCommandline.push_back(L'"');
            quotedCommandline.append(unquotedCommandline);
            quotedCommandline.push_back(L'"');
            profile->Commandline(winrt::hstring{ quotedCommandline });
        }
        auto end = std::chrono::steady_clock::now();
        std::wstringstream stream;
        stream << "Elapsed microseconds for native code " << std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() << std::endl;
        OutputDebugString(stream.str().c_str());

        start = std::chrono::steady_clock::now();
        for (int i = 0; i < 1000000; i++)
        {
            auto quotedCommandline = fmt::format(FMT_COMPILE(L"\"{}\""), psI.executablePath.native());
            profile->Commandline(winrt::hstring{ quotedCommandline });
        }
        end = std::chrono::steady_clock::now();
        std::wstringstream stream2;
        stream2 << "Elapsed microseconds for fmt code " << std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() << std::endl;
        OutputDebugString(stream2.str().c_str());

Output:

Elapsed microseconds for native code 234055
Elapsed microseconds for fmt code 258514

Edit: Run the loop a factor of 10 more times, and do something with the constructed string.

Copy link
Member

Choose a reason for hiding this comment

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

A micro-benchmark on my machine shows my code to be faster :)

Well, you'll make @lhecker 's day. And I chuckled. Good enough.

Copy link
Member

Choose a reason for hiding this comment

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

LOL. Fair enough 😁


profile->StartingDirectory(winrt::hstring{ DEFAULT_STARTING_DIRECTORY });
profile->DefaultAppearance().ColorSchemeName(L"Campbell");
profile->Icon(winrt::hstring{ WI_IsFlagSet(psI.flags, PowerShellFlags::Preview) ? POWERSHELL_PREVIEW_ICON : POWERSHELL_ICON });
Expand Down