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

Code before:
        // value of the function we consider the null value to be "false".
        auto isMatching = std::optional<bool>{};

        auto isMatch = [](std::wstring_view regex, std::wstring_view text) {
            if (text.empty())
            {
                return false;
            }

            std::wregex re;
            try
            {
                re = { regex.cbegin(), regex.cend() };
            }
            catch (std::regex_error)
            {
                // invalid regex
                return false;
            }

            return std::regex_match(text.cbegin(), text.cend(), re);
        };

        if (!_Name.empty())
        {
            isMatching = { isMatching.value_or(true) && isMatch(_Name, profile.Name()) };
        }

        if (!_Source.empty())
        {
            isMatching = { isMatching.value_or(true) && isMatch(_Source, profile.Source()) };
        }

        if (!_Commandline.empty())
        {
            isMatching = { isMatching.value_or(true) && isMatch(_Commandline, profile.Commandline()) };
        }

        return isMatching.value_or(false);
    }

    Model::NewTabMenuEntry MatchProfilesEntry::Copy() const
    {
        auto entry = winrt::make_self<MatchProfilesEntry>();
        entry->_Name = _Name;
        entry->_Commandline = _Commandline;
        entry->_Source = _Source;
        return *entry;
    }
}


Review comment (suggestion):
This optional-bool code seems like a roundabout way to say "return early if true". 😅

```cpp
if (!field.empty() && isMatch(field, ...)) {
    return true;
}
if (!field.empty() && isMatch(field, ...)) {
    return true;
}
if (!field.empty() && isMatch(field, ...)) {
    return true;
}
return false;
```

Code after:

#define DEFINE_VALIDATE_FUNCTION(name)                               \
    void MatchProfilesEntry::_validate##name() noexcept              \
    {                                                                \
        _invalid##name = false;                                      \
        if (_##name.empty())                                         \
        {                                                            \
            /* empty field is valid*/                                \
            return;                                                  \
        }                                                            \
        UErrorCode status = U_ZERO_ERROR;                            \
        _##name##Regex = til::ICU::CreateRegex(_##name, 0, &status); \
        if (U_FAILURE(status))                                       \
        {                                                            \
            _invalid##name = true;                                   \
            _##name##Regex.reset();                                  \
        }                                                            \
    }

    DEFINE_VALIDATE_FUNCTION(Name);
    DEFINE_VALIDATE_FUNCTION(Commandline);
    DEFINE_VALIDATE_FUNCTION(Source);

    bool MatchProfilesEntry::MatchesProfile(const Model::Profile& profile)
    {
        auto isMatch = [](const til::ICU::unique_uregex& regex, std::wstring_view text) {
            if (text.empty())
            {
                return false;
            }
            UErrorCode status = U_ZERO_ERROR;
            uregex_setText(regex.get(), reinterpret_cast<const UChar*>(text.data()), static_cast<int32_t>(text.size()), &status);
            const auto match = uregex_matches(regex.get(), 0, &status);
            return status == U_ZERO_ERROR && match;
        };

        if (!_Name.empty() && isMatch(_NameRegex, profile.Name()))
        {
            return true;
        }
        else if (!_Source.empty() && isMatch(_SourceRegex, profile.Source()))
        {
            return true;
        }
        else if (!_Commandline.empty() && isMatch(_CommandlineRegex, profile.Commandline()))
        {
            return true;
        }
        return false;
    }