Repository: microsoft/terminal
File: src/cascadia/TerminalSettingsEditor/GlobalAppearance.cpp (C++)

Code before:
            {
                const auto size = v.Size();
                v.GetMany(0, winrt::array_view(data, size));
                data += size;
            }
        }

        // NOTE: The size of tags is always >0, due to tags[0] being hardcoded to "und".
        const auto tagsBegin = ++tags.begin();
        const auto tagsEnd = tags.end();

        // [2]:
        std::sort(tagsBegin, tagsEnd);

        // I'd love for both, std::unique and std::remove_if, to occur in a single loop,
        // but the code turned out to be complex and even less maintainable, so I gave up.
        {
            // [3] part 1:
            auto it = std::unique(tagsBegin, tagsEnd);

            // The qps- languages are useful for testing ("pseudo-localization").
            // --> Leave them in if debug features are enabled.
            if (!_State.Globals().DebugFeaturesEnabled())
            {
                // [4] part 1:
                it = std::remove_if(it, tagsEnd, [](const winrt::hstring& tag) mutable -> bool {
                    return til::starts_with(tag, L"qps-");
                });
            }

            // [3], [4] part 2 (completing the so called "erase-remove idiom"):
            tags.erase(it, tagsEnd);
        }

        _languageList = winrt::single_threaded_observable_vector(std::move(tags));
        return _languageList;
    }

    winrt::Windows::Foundation::IInspectable GlobalAppearance::CurrentLanguage()
    {
        if (_currentLanguage.empty())
        {
            _currentLanguage = winrt::Windows::Globalization::ApplicationLanguages::PrimaryLanguageOverride();
            if (_currentLanguage.empty())
            {
                _currentLanguage = systemLanguageTag;
            }
        }

        return winrt::box_value(_currentLanguage);
    }

Review comment (suggestion):
@DHowett FYI I just realized this is a programming error and should be `std::remove_if(tagsBegin, it, ...)` instead. `std::unique` returns an iterator for the new end of the range, which I have to use instead of `tagsEnd` here.

Code after:
            {
                const auto size = v.Size();
                v.GetMany(0, winrt::array_view(data, size));
                data += size;
            }
        }

        // NOTE: The size of tags is always >0, due to tags[0] being hardcoded to "und".
        const auto tagsBegin = ++tags.begin();
        const auto tagsEnd = tags.end();

        // [2]:
        std::sort(tagsBegin, tagsEnd);

        // I'd love for both, std::unique and std::remove_if, to occur in a single loop,
        // but the code turned out to be complex and even less maintainable, so I gave up.
        {
            // [3] part 1:
            auto it = std::unique(tagsBegin, tagsEnd);

            // The qps- languages are useful for testing ("pseudo-localization").
            // --> Leave them in if debug features are enabled.
            if (!_State.Globals().DebugFeaturesEnabled())
            {
                // [4] part 1:
                it = std::remove_if(tagsBegin, it, [](const winrt::hstring& tag) -> bool {
                    return til::starts_with(tag, L"qps-");
                });
            }

            // [3], [4] part 2 (completing the so called "erase-remove idiom"):
            tags.erase(it, tagsEnd);
        }

        _languageList = winrt::single_threaded_observable_vector(std::move(tags));
        return _languageList;
    }

    winrt::Windows::Foundation::IInspectable GlobalAppearance::CurrentLanguage()
    {
        if (_currentLanguage.empty())
        {
            _currentLanguage = winrt::Windows::Globalization::ApplicationLanguages::PrimaryLanguageOverride();
            if (_currentLanguage.empty())
            {
                _currentLanguage = systemLanguageTag;
            }
        }

        return winrt::box_value(_currentLanguage);
    }