Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 1 | function(escape_string_as_regex _str_out _str_in) |
Austin Schuh | c55b017 | 2022-02-20 17:52:35 -0800 | [diff] [blame] | 2 | string(REGEX REPLACE "\\\\" "\\\\\\\\" FILETEST2 "${_str_in}") |
| 3 | string(REGEX REPLACE "([.$+*?|-])" "\\\\\\1" FILETEST2 "${FILETEST2}") |
| 4 | string(REGEX REPLACE "\\^" "\\\\^" FILETEST2 "${FILETEST2}") |
| 5 | string(REGEX REPLACE "\\(" "\\\\(" FILETEST2 "${FILETEST2}") |
| 6 | string(REGEX REPLACE "\\)" "\\\\)" FILETEST2 "${FILETEST2}") |
| 7 | string(REGEX REPLACE "\\[" "\\\\[" FILETEST2 "${FILETEST2}") |
| 8 | string(REGEX REPLACE "\\]" "\\\\]" FILETEST2 "${FILETEST2}") |
| 9 | set(${_str_out} "${FILETEST2}" PARENT_SCOPE) |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 10 | endfunction() |
| 11 | |
| 12 | function(test_escape_string_as_regex) |
Austin Schuh | c55b017 | 2022-02-20 17:52:35 -0800 | [diff] [blame] | 13 | set(test1 "\\.^$-+*()[]?|") |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 14 | escape_string_as_regex(test2 "${test1}") |
Austin Schuh | c55b017 | 2022-02-20 17:52:35 -0800 | [diff] [blame] | 15 | set(testRef "\\\\\\.\\^\\$\\-\\+\\*\\(\\)\\[\\]\\?\\|") |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 16 | if(NOT test2 STREQUAL testRef) |
| 17 | message("Error in the escape_string_for_regex function : \n ${test1} was escaped as ${test2}, should be ${testRef}") |
Austin Schuh | c55b017 | 2022-02-20 17:52:35 -0800 | [diff] [blame] | 18 | endif() |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 19 | endfunction() |