[Feature] Add command to escape special regex characters
Hey,
I've a feature proposition and I can implement it, but I'd rather ask first if it feels needed.
I'd love to have an command to escape all regex characters in string. I guess the syntax could be:
string(REGEX_ESCAPE <string-var> [<input>...])
It would simply replace all regex characters C
with backlashed ones \C
.
Its logic can be implemented with simple function (simplified for one arg case):
function(escape_regex OUT_NAME INPUT)
for(CHAR ^ $ . [ ] - + * \( \) )
string(REPLACE "${CHAR}" "\\${CHAR}" INPUT ${INPUT}
endfor()
set(${OUT_NAME} ${INPUT} PARENT_SCOPE)
endfunction()
Background: recently I had an issue where I had to detect whenever file was generated one (under ${CMAKE_CURRENT_BUILD_DIR}) and my naive approach to check if ${FILEPATH} MATCHES ${CMAKE_CURRENT_BUILD_DIR} failed for directory containing +
inside its name. As if(STR MATCHES XXX)
supports only REGEX syntax I've escaped all the characters in directory path with function as above (more or like).