Skip to content

Commit

Permalink
search: add SearchFlag::RegularExpression
Browse files Browse the repository at this point in the history
If this flag is set, we will allow ICU to treat the needle as a regex
  • Loading branch information
DHowett committed May 30, 2024
1 parent fc41d3a commit c4390c6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/buffer/out/search.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ enum class SearchFlag : unsigned int
None = 0,

CaseInsensitive = 1 << 0,
RegularExpression = 1 << 1,
};

DEFINE_ENUM_FLAG_OPERATORS(SearchFlag);
Expand Down
11 changes: 10 additions & 1 deletion src/buffer/out/textBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3206,9 +3206,18 @@ std::vector<til::point_span> TextBuffer::SearchText(const std::wstring_view& nee

auto text = ICU::UTextFromTextBuffer(*this, rowBeg, rowEnd);

uint32_t icuFlags{ UREGEX_LITERAL };
uint32_t icuFlags{ 0 };
WI_SetFlagIf(icuFlags, UREGEX_CASE_INSENSITIVE, WI_IsFlagSet(flags, SearchFlag::CaseInsensitive));

if (WI_IsFlagSet(flags, SearchFlag::RegularExpression))
{
WI_SetFlag(icuFlags, UREGEX_MULTILINE);
}
else
{
WI_SetFlag(icuFlags, UREGEX_LITERAL);
}

UErrorCode status = U_ZERO_ERROR;
const auto re = ICU::CreateRegex(needle, icuFlags, &status);
uregex_setUText(re.get(), &text, &status);
Expand Down
45 changes: 45 additions & 0 deletions src/host/ut_host/SearchTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,49 @@ class SearchTests
s.Reset(gci.renderData, L"\x304b", SearchFlag::CaseInsensitive, true);
DoFoundChecks(s, { 2, 3 }, -1, true);
}

TEST_METHOD(ForwardCaseSensitiveRegex)
{
auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();

Search s;
s.Reset(gci.renderData, L"[BA]{2}", SearchFlag::RegularExpression, false);
DoFoundChecks(s, {}, 1, false);
}

TEST_METHOD(ForwardCaseSensitiveRegexJapanese)
{
auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
Search s;
// N.B. this is not a literal U+30xx, but a regex escape sequence \x{30xx}
s.Reset(gci.renderData, LR"-([\x{3041}-\x{304c}])-", SearchFlag::RegularExpression, false);
DoFoundChecks(s, { 2, 0 }, 1, false);
}

TEST_METHOD(ForwardCaseInsensitiveRegex)
{
auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();

Search s;
s.Reset(gci.renderData, L"ab", SearchFlag::CaseInsensitive | SearchFlag::RegularExpression, false);
DoFoundChecks(s, {}, 1, false);
}

TEST_METHOD(ForwardCaseInsensitiveRegexJapanese)
{
auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
Search s;
// N.B. this is not a literal U+30xx, but a regex escape sequence \x{30xx}
s.Reset(gci.renderData, LR"-([\x{3041}-\x{304c}])-", SearchFlag::CaseInsensitive | SearchFlag::RegularExpression, false);
DoFoundChecks(s, { 2, 0 }, 1, false);
}

TEST_METHOD(ForwardCaseSensitiveRegexWithCaseInsensitiveFlag)
{
auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();

Search s;
s.Reset(gci.renderData, L"(?i)ab", SearchFlag::RegularExpression, false);
DoFoundChecks(s, {}, 1, false);
}
};

0 comments on commit c4390c6

Please sign in to comment.