Repository: microsoft/terminal
File: src/cascadia/TerminalControl/TermControl.cpp (C++)

Code before:
                            // Cc/foo/bar -> /mnt/c/foo/bar
                            fullPath.replace(0, 1, L"/mnt/");
                        }
                        else
                        {
                            static constexpr std::wstring_view wslPathPrefixes[] = { L"//wsl.localhost/", L"//wsl$/" };
                            for (auto prefix : wslPathPrefixes)
                            {
                                if (til::starts_with(fullPath, prefix))
                                {
                                    if (const auto idx = fullPath.find(L'/', prefix.size()); idx != std::wstring::npos)
                                    {
                                        // //wsl.localhost/Ubuntu-18.04/foo/bar -> /foo/bar
                                        fullPath.erase(0, idx);
                                    }
                                    else
                                    {
                                        // //wsl.localhost/Ubuntu-18.04 -> /
                                        fullPath = L"/";
                                    }
                                    break;
                                }
                            }
                        }
                        fullPath.insert(0, L"\'");
                        fullPath += L"\'";
                    }

                    const auto containsSpaces = std::find(fullPath.begin(),
                                                          fullPath.end(),
                                                          L' ') != fullPath.end();

                    if (containsSpaces && !_interactivity.ManglePathsForWsl())
                    {
                        fullPath.insert(0, L"\"");
                        fullPath += L"\"";
                    }

                    allPathsString += fullPath;
                }

                _pasteTextWithBroadcast(winrt::hstring{ allPathsString });
            }
        }
    }

    // Method Description:
    // - Paste this text, and raise a StringSent, to potentially broadcast this
    //   text to other controls in the app. For certain interactions, like
    //   drag/dropping a file, we want to act like we "pasted" the text (even if
    //   the text didn't come from the clipboard). This lets those interactions

Review comment (performance):
If you don't mind doing it in this PR, the below code is kinda whack and could need an improvement. `.insert(0, L"\"");` is an O(n) operation that is entirely avoidable, because the `fullPath` is being appended to `allPathsString` anyways, so we can instead just append the quotes to `allPathsString` directly. Here's what I mean:

```cpp
const auto isWSL = _interactivity.ManglePathsForWsl();

if (isWSL)
{
    // ...
    // the above WSL specific code block but without your new addition
    // ...
}

const auto quotesNeeded = isWSL || fullPath.find(L' ') != decltype(fullPath)::npos;
const auto quotesChar = isWSL ? L'\'' : L'"';

// Append fullPath and also wrap it in quotes if needed
if (needsQuotes)
{
    allPathsString.push_back(quotesChar);
}
allPathsString.append(fullPath);
if (needsQuotes)
{
    allPathsString.push_back(quotesChar);
}
```

Code after:
                            // C:/foo/bar -> Cc/foo/bar
                            fullPath.at(1) = til::tolower_ascii(fullPath.at(0));
                            // Cc/foo/bar -> /mnt/c/foo/bar
                            fullPath.replace(0, 1, L"/mnt/");
                        }
                        else
                        {
                            static constexpr std::wstring_view wslPathPrefixes[] = { L"//wsl.localhost/", L"//wsl$/" };
                            for (auto prefix : wslPathPrefixes)
                            {
                                if (til::starts_with(fullPath, prefix))
                                {
                                    if (const auto idx = fullPath.find(L'/', prefix.size()); idx != std::wstring::npos)
                                    {
                                        // //wsl.localhost/Ubuntu-18.04/foo/bar -> /foo/bar
                                        fullPath.erase(0, idx);
                                    }
                                    else
                                    {
                                        // //wsl.localhost/Ubuntu-18.04 -> /
                                        fullPath = L"/";
                                    }
                                    break;
                                }
                            }
                        }
                    }

                    const auto quotesNeeded = isWSL || fullPath.find(L' ') != std::wstring::npos;
                    const auto quotesChar = isWSL ? L'\'' : L'"';

                    // Append fullPath and also wrap it in quotes if needed
                    if (quotesNeeded)
                    {
                        allPathsString.push_back(quotesChar);
                    }
                    allPathsString.append(fullPath);
                    if (quotesNeeded)
                    {
                        allPathsString.push_back(quotesChar);
                    }
                }

                _pasteTextWithBroadcast(winrt::hstring{ allPathsString });
            }
        }
    }

    // Method Description:
    // - Paste this text, and raise a StringSent, to potentially broadcast this
    //   text to other controls in the app. For certain interactions, like