Skip to content

Commit

Permalink
Util: Adapt ConvertListToRegexpWithAlternations to accept regexp elem…
Browse files Browse the repository at this point in the history
…ents

- Added optional "literal" argument, that is by default 1 (behavior as before)
when literal is set to 0 then the list elements can be regular expressions
themselves
- Since regexp can contain semicolons added a second optional argument for the
separator character(s) of list that defaults to ";"
- Added some tests
  • Loading branch information
MichaelHuth committed May 7, 2024
1 parent 7523dc3 commit 28d09b6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
27 changes: 21 additions & 6 deletions Packages/MIES/MIES_Utilities.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -5467,16 +5467,31 @@ End
/// @brief Turn a list of entries into a regular expression with alternations.
///
/// Can be used for GetListOfObjects() if you know in advance which entries to filter out.
Function/S ConvertListToRegexpWithAlternations(list)
string list
///
/// @param list semicolon separated list of strings to match
/// @param literal [optional, default = 1] when this flag is cleared the string elements of the list are treated as regular expressions
/// @param sep [optional, default = ";"] separator for list
Function/S ConvertListToRegexpWithAlternations(string list, [variable literal, string sep])

variable i, numEntries
string entry
string regexpList = ""
string regexpList = ""
string literalPrefix = "\\Q"
string literalSuffix = "\\E"

numEntries = ItemsInList(list)
literal = ParamIsDefault(literal) ? 1 : !!literal
if(ParamIsDefault(sep))
sep = ";"
else
ASSERT(!IsEmpty(sep), "separator can not be empty.")
endif

if(!literal)
literalPrefix = ""
literalSuffix = ""
endif
numEntries = ItemsInList(list, sep)
for(i = 0; i < numEntries; i += 1)
regexpList = AddListItem("\\Q" + StringFromList(i, list) + "\\E", regexpList, "|", Inf)
regexpList = AddListItem(literalPrefix + StringFromList(i, list, sep) + literalSuffix, regexpList, "|", Inf)
endfor

regexpList = "(?:" + RemoveEnding(regexpList, "|") + ")"
Expand Down
24 changes: 24 additions & 0 deletions Packages/tests/Basic/UTF_Utils.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -8077,3 +8077,27 @@ static Function HWS_Works()
CHECK_EQUAL_VAR(HasWildcardSyntax("!a"), 1)
CHECK_EQUAL_VAR(HasWildcardSyntax("a*b"), 1)
End

static Function ConvertListToRegexpWithAlternations_Test()

string str, ref

str = ConvertListToRegexpWithAlternations("1;2;")
ref = "(?:\\Q1\\E|\\Q2\\E)"
CHECK_EQUAL_STR(ref, str)

str = ConvertListToRegexpWithAlternations("1;2;", literal = 0)
ref = "(?:1|2)"
CHECK_EQUAL_STR(ref, str)

str = ConvertListToRegexpWithAlternations("1#2#", literal = 0, sep = "#")
ref = "(?:1|2)"
CHECK_EQUAL_STR(ref, str)

try
str = ConvertListToRegexpWithAlternations("1#2#", sep = "")
FAIL()
catch
PASS()
endtry
End

0 comments on commit 28d09b6

Please sign in to comment.