1212#include " ../../types/inc/GlyphWidth.hpp"
1313#include " ../renderer/base/renderer.hpp"
1414#include " ../types/inc/utils.hpp"
15+ #include " search.h"
1516
1617using namespace Microsoft ::Console;
1718using namespace Microsoft ::Console::Types;
@@ -3193,14 +3194,15 @@ void TextBuffer::CopyHyperlinkMaps(const TextBuffer& other)
31933194
31943195// Searches through the entire (committed) text buffer for `needle` and returns the coordinates in absolute coordinates.
31953196// The end coordinates of the returned ranges are considered inclusive.
3196- std::vector<til::point_span> TextBuffer::SearchText (const std::wstring_view& needle, bool caseInsensitive ) const
3197+ std::optional<std:: vector<til::point_span>> TextBuffer::SearchText (const std::wstring_view& needle, SearchFlag flags ) const
31973198{
3198- return SearchText (needle, caseInsensitive , 0 , til::CoordTypeMax);
3199+ return SearchText (needle, flags , 0 , til::CoordTypeMax);
31993200}
32003201
32013202// Searches through the given rows [rowBeg,rowEnd) for `needle` and returns the coordinates in absolute coordinates.
32023203// While the end coordinates of the returned ranges are considered inclusive, the [rowBeg,rowEnd) range is half-open.
3203- std::vector<til::point_span> TextBuffer::SearchText (const std::wstring_view& needle, bool caseInsensitive, til::CoordType rowBeg, til::CoordType rowEnd) const
3204+ // Returns nullopt if the parameters were invalid (e.g. regex search was requested with an invalid regex)
3205+ std::optional<std::vector<til::point_span>> TextBuffer::SearchText (const std::wstring_view& needle, SearchFlag flags, til::CoordType rowBeg, til::CoordType rowEnd) const
32043206{
32053207 rowEnd = std::min (rowEnd, _estimateOffsetOfLastCommittedRow () + 1 );
32063208
@@ -3214,11 +3216,25 @@ std::vector<til::point_span> TextBuffer::SearchText(const std::wstring_view& nee
32143216
32153217 auto text = ICU::UTextFromTextBuffer (*this , rowBeg, rowEnd);
32163218
3217- uint32_t flags = UREGEX_LITERAL;
3218- WI_SetFlagIf (flags, UREGEX_CASE_INSENSITIVE, caseInsensitive);
3219+ uint32_t icuFlags{ 0 };
3220+ WI_SetFlagIf (icuFlags, UREGEX_CASE_INSENSITIVE, WI_IsFlagSet (flags, SearchFlag::CaseInsensitive));
3221+
3222+ if (WI_IsFlagSet (flags, SearchFlag::RegularExpression))
3223+ {
3224+ WI_SetFlag (icuFlags, UREGEX_MULTILINE);
3225+ }
3226+ else
3227+ {
3228+ WI_SetFlag (icuFlags, UREGEX_LITERAL);
3229+ }
32193230
32203231 UErrorCode status = U_ZERO_ERROR;
3221- const auto re = ICU::CreateRegex (needle, flags, &status);
3232+ const auto re = ICU::CreateRegex (needle, icuFlags, &status);
3233+ if (status > U_ZERO_ERROR)
3234+ {
3235+ return std::nullopt ;
3236+ }
3237+
32223238 uregex_setUText (re.get (), &text, &status);
32233239
32243240 if (uregex_find (re.get (), -1 , &status))
0 commit comments