Repository: microsoft/terminal
File: src/cascadia/TerminalSettingsModel/Profile.cpp (C++)

Code before:
winrt::hstring Profile::EvaluatedIcon()
{
    // We cache the result here, so we don't search the path for the exe every time.
    if (!_evaluatedIcon.has_value())
    {
        _evaluatedIcon = _evaluateIcon();
    }
    return *_evaluatedIcon;
}

winrt::hstring Profile::_evaluateIcon() const
{
    // If the profile has an icon, return it.
    if (!Icon().empty())
    {
        return Icon();
    }

    // Otherwise, use NormalizeCommandLine to find the actual exe name. This
    // will actually search for the exe, including spaces, in the same way that
    // CreateProcess does.
    std::wstring cmdline{ NormalizeCommandLine(Commandline().c_str()) };
    if (cmdline.empty())
    {
        return {};
    }

    // NormalizeCommandLine will return a string with embedded nulls after each
    // arg. We just want the first one.
    const size_t beforeNull{ ::wcslen(cmdline.c_str()) };
    const std::wstring_view exe{ cmdline.c_str(), beforeNull };
    return winrt::hstring{ exe };
}

// Given a commandLine like the following:
// * "C:\WINDOWS\System32\cmd.exe"
// * "pwsh -WorkingDirectory ~"
// * "C:\Program Files\PowerShell\7\pwsh.exe"
// * "C:\Program Files\PowerShell\7\pwsh.exe -WorkingDirectory ~"
//
// This function returns:
// * "C:\Windows\System32\cmd.exe"
// * "C:\Program Files\PowerShell\7\pwsh.exe\0-WorkingDirectory\0~"
// * "C:\Program Files\PowerShell\7\pwsh.exe"
// * "C:\Program Files\PowerShell\7\pwsh.exe\0-WorkingDirectory\0~"
//
// The resulting strings are then used for comparisons in _getProfileForCommandLine().
// For instance a resulting string of
//   "C:\Program Files\PowerShell\7\pwsh.exe"
// is considered a compatible profile with
//   "C:\Program Files\PowerShell\7\pwsh.exe -WorkingDirectory ~"

Review comment (refactor):
FWIW you can remove this check. An empty `wstring` still returns a valid string via `.c_str()` (`""`) and so all the code below should still work.

Code after:
    }

    return json;
}

// This is the implementation for and INHERITABLE_SETTING, but with one addition
// in the setter. We want to make sure to clear out our cached icon, so that we
// can re-evaluate it as it changes in the SUI.
void Profile::Icon(const winrt::hstring& value)
{
    _evaluatedIcon = std::nullopt;
    _Icon = value;
}
winrt::hstring Profile::Icon() const
{
    const auto val{ _getIconImpl() };
    return val ? *val : hstring{ L"\uE756" };
}

winrt::hstring Profile::EvaluatedIcon()
{
    // We cache the result here, so we don't search the path for the exe every time.
    if (!_evaluatedIcon.has_value())
    {
        _evaluatedIcon = _evaluateIcon();
    }
    return *_evaluatedIcon;
}

winrt::hstring Profile::_evaluateIcon() const
{
    // If the profile has an icon, return it.
    if (!Icon().empty())
    {
        return Icon();
    }

    // Otherwise, use NormalizeCommandLine to find the actual exe name. This
    // will actually search for the exe, including spaces, in the same way that
    // CreateProcess does.
    std::wstring cmdline{ NormalizeCommandLine(Commandline().c_str()) };
    // NormalizeCommandLine will return a string with embedded nulls after each
    // arg. We just want the first one.
    return winrt::hstring{ cmdline.c_str() };
}

// Given a commandLine like the following:
// * "C:\WINDOWS\System32\cmd.exe"
// * "pwsh -WorkingDirectory ~"
// * "C:\Program Files\PowerShell\7\pwsh.exe"
// * "C:\Program Files\PowerShell\7\pwsh.exe -WorkingDirectory ~"