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

Code before:
    const winrt::hstring newName{ fmt::format(L"Profile {}", _allProfiles.Size()) };
    newProfile->Name(newName);

    return *newProfile;
}

// Method Description:
// - Duplicate a new profile based off another profile's settings
// Arguments:
// - source: the Profile object we are duplicating (must not be null)
// Return Value:
// - a reference to the new profile
winrt::Microsoft::Terminal::Settings::Model::Profile CascadiaSettings::DuplicateProfile(Model::Profile source)
{
    THROW_HR_IF_NULL(E_INVALIDARG, source);

    auto duplicated{ _userDefaultProfileSettings->CreateChild() };
    _allProfiles.Append(*duplicated);

    if (!source.Hidden())
    {
        _activeProfiles.Append(*duplicated);
    }

    const auto newName{ fmt::format(L"{} ({})", source.Name(), RS_(L"CopySuffix")) };
    duplicated->Name(winrt::hstring(newName));

#define DUPLICATE_MACRO(settingName)                                                                                                       \
    if (source.Has##settingName() ||                                                                                                       \
        (source.##settingName##OverrideSource() != nullptr && source.##settingName##OverrideSource().Origin() != OriginTag::UserDefaults)) \
    {                                                                                                                                      \
        duplicated->##settingName(source.##settingName());                                                                                 \
    }

    DUPLICATE_MACRO(Hidden);
    DUPLICATE_MACRO(Icon);
    DUPLICATE_MACRO(CloseOnExit);
    DUPLICATE_MACRO(TabTitle);
    DUPLICATE_MACRO(TabColor);
    DUPLICATE_MACRO(SuppressApplicationTitle);
    DUPLICATE_MACRO(UseAcrylic);
    DUPLICATE_MACRO(AcrylicOpacity);
    DUPLICATE_MACRO(ScrollState);
    DUPLICATE_MACRO(FontFace);
    DUPLICATE_MACRO(FontSize);
    DUPLICATE_MACRO(FontWeight);
    DUPLICATE_MACRO(Padding);
    DUPLICATE_MACRO(Commandline);
    DUPLICATE_MACRO(StartingDirectory);
    DUPLICATE_MACRO(BackgroundImagePath);
    DUPLICATE_MACRO(BackgroundImageOpacity);

Review comment (refactor):
```suggestion
    const winrt::hstring newName{ fmt::format(L"{} ({})", source.Name(), RS_(L"CopySuffix")) };
    duplicated->Name(newName);
```

this saves you a needless cast.

Code after:
{
    if (_allProfiles.Size() == std::numeric_limits<uint32_t>::max())
    {
        // Shouldn't really happen
        return nullptr;
    }

    winrt::hstring newName{};
    for (uint32_t candidateIndex = 0; candidateIndex < _allProfiles.Size() + 1; candidateIndex++)
    {
        // There is a theoretical unsigned integer wraparound, which is OK
        newName = fmt::format(L"Profile {}", _allProfiles.Size() + 1 + candidateIndex);
        if (std::none_of(begin(_allProfiles), end(_allProfiles), [&](auto&& profile) { return profile.Name() == newName; }))
        {
            break;
        }
    }

    auto newProfile{ _userDefaultProfileSettings->CreateChild() };
    newProfile->Name(newName);
    _allProfiles.Append(*newProfile);
    return *newProfile;
}

// Method Description:
// - Duplicate a new profile based off another profile's settings
// - This differs from Profile::Copy because it also copies over settings
//   that were not defined in the json (for example, settings that were
//   defined in one of the parents)
// - This will not duplicate settings that were defined in profiles.defaults
//   however, because we do not want the json blob generated from the new profile
//   to contain those settings
// Arguments:
// - source: the Profile object we are duplicating (must not be null)
// Return Value:
// - a reference to the new profile
winrt::Microsoft::Terminal::Settings::Model::Profile CascadiaSettings::DuplicateProfile(Model::Profile source)
{
    THROW_HR_IF_NULL(E_INVALIDARG, source);

    winrt::com_ptr<Profile> duplicated;
    if (_userDefaultProfileSettings)
    {
        duplicated = _userDefaultProfileSettings->CreateChild();
    }
    else
    {
        duplicated = winrt::make_self<Profile>();
    }
    _allProfiles.Append(*duplicated);