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

Code before:
    }
    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 ~"
// as it shares the same (normalized) prefix.
std::wstring Profile::NormalizeCommandLine(LPCWSTR commandLine)
{
    // Turn "%SystemRoot%\System32\cmd.exe" into "C:\WINDOWS\System32\cmd.exe".
    // We do this early, as environment variables might occur anywhere in the commandLine.
    std::wstring normalized;

Review comment (suggestion):
Just passing `cmdline.c_str()` to the `hstring` constructor will be fine. It has a `hstring(wchar_t const* c)` constructor, which calls `wcslen` for you. With the above comment this turns into:
```cpp
const auto 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() };
```

Code after:
// 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 ~"
//
// 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~"