Repository: microsoft/terminal
File: src/cascadia/TerminalApp/Jumplist.cpp (C++)

Code before:
// - S_OK or HRESULT failure code.
winrt::com_ptr<IShellLinkW> Jumplist::_createShellLink(const std::wstring_view name, const std::wstring_view path, const std::wstring_view args)
{
    auto sh = winrt::create_instance<IShellLinkW>(CLSID_ShellLink, CLSCTX_ALL);

    const auto module{ GetWtExePath() };
    THROW_IF_FAILED(sh->SetPath(module.data()));
    THROW_IF_FAILED(sh->SetArguments(args.data()));
    auto propStore{ sh.as<IPropertyStore>() };

    PROPVARIANT titleProp;
    titleProp.vt = VT_LPWSTR;
    titleProp.pwszVal = const_cast<wchar_t*>(name.data());

    // Check for a comma in the path. If we find one we have an indirect icon. Parse the path into a file path and index/id.
    auto commaPosition = path.find(L",");
    if (commaPosition != std::wstring_view::npos)
    {
        const std::wstring iconPath{ path.substr(0, commaPosition) };

        // We dont want the comma included so add 1 to its position
        const std::wstring iconIndexString{ path.substr(commaPosition + 1) };
        std::wstringstream stringStream;
        stringStream << iconIndexString;
        int iconIndex = 0;
        stringStream >> iconIndex;
        if (iconIndex != 0)
        {
            THROW_IF_FAILED(sh->SetIconLocation(iconPath.data(), iconIndex));
        }
    }
    else
    {
        PROPVARIANT iconProp;
        iconProp.vt = VT_LPWSTR;
        iconProp.pwszVal = const_cast<wchar_t*>(path.data());

        THROW_IF_FAILED(propStore->SetValue(PKEY_AppUserModel_DestListLogoUri, iconProp));
    }

    THROW_IF_FAILED(propStore->SetValue(PKEY_Title, titleProp));
    THROW_IF_FAILED(propStore->Commit());

    return sh;
}


Review comment (performance):
I'd prefer we avoid stringstreams here - it's not a hot path, but it does establish a precedent. Is `til::to_ulong` insufficient? Should we fix it?

Code after:
// - S_OK or HRESULT failure code.
winrt::com_ptr<IShellLinkW> Jumplist::_createShellLink(const std::wstring_view name, const std::wstring_view path, const std::wstring_view args)
{
    auto sh = winrt::create_instance<IShellLinkW>(CLSID_ShellLink, CLSCTX_ALL);

    const auto module{ GetWtExePath() };
    THROW_IF_FAILED(sh->SetPath(module.data()));
    THROW_IF_FAILED(sh->SetArguments(args.data()));
    auto propStore{ sh.as<IPropertyStore>() };

    PROPVARIANT titleProp;
    titleProp.vt = VT_LPWSTR;
    titleProp.pwszVal = const_cast<wchar_t*>(name.data());

    // Check for a comma in the path. If we find one we have an indirect icon. Parse the path into a file path and index/id.
    auto commaPosition = path.find(L",");
    if (commaPosition != std::wstring_view::npos)
    {
        const std::wstring iconPath{ path.substr(0, commaPosition) };

        // We dont want the comma included so add 1 to its position
        int iconIndex = til::to_int(path.substr(commaPosition + 1));
        if (iconIndex != til::to_int_error)
        {
            THROW_IF_FAILED(sh->SetIconLocation(iconPath.data(), iconIndex));
        }
    }
    else if (til::ends_with(path, L"exe") || til::ends_with(path, L"dll"))
    {
        // We have a binary path but no index/id. Default to 0
        THROW_IF_FAILED(sh->SetIconLocation(path.data(), 0));
    }
    else
    {
        PROPVARIANT iconProp;
        iconProp.vt = VT_LPWSTR;
        iconProp.pwszVal = const_cast<wchar_t*>(path.data());

        THROW_IF_FAILED(propStore->SetValue(PKEY_AppUserModel_DestListLogoUri, iconProp));
    }

    THROW_IF_FAILED(propStore->SetValue(PKEY_Title, titleProp));
    THROW_IF_FAILED(propStore->Commit());

    return sh;
}