mirror of
https://github.com/Kitware/CMake.git
synced 2026-01-08 06:40:48 -06:00
string: Allow references to unmatched groups in REGEX REPLACE
References to unmatched groups will be replaced with empty strings. Issue: #26629 Fixes: #19012
This commit is contained in:
@@ -122,6 +122,9 @@ Search and Replace With Regular Expressions
|
|||||||
string instead of the beginning of each repeated search.
|
string instead of the beginning of each repeated search.
|
||||||
See policy :policy:`CMP0186`.
|
See policy :policy:`CMP0186`.
|
||||||
|
|
||||||
|
The replacement expression may contain references to subexpressions that
|
||||||
|
didn't match anything. Previously, such references triggered an error.
|
||||||
|
|
||||||
.. _`Regex Specification`:
|
.. _`Regex Specification`:
|
||||||
|
|
||||||
Regex Specification
|
Regex Specification
|
||||||
|
|||||||
@@ -3,3 +3,6 @@ regex-fixes
|
|||||||
|
|
||||||
* Regular expressions match the ``^`` anchor at most once in repeated
|
* Regular expressions match the ``^`` anchor at most once in repeated
|
||||||
searches, at the start of the input. See policy :policy:`CMP0186`.
|
searches, at the start of the input. See policy :policy:`CMP0186`.
|
||||||
|
|
||||||
|
* References to unmatched groups are allowed, they are replaced with empty
|
||||||
|
strings.
|
||||||
|
|||||||
@@ -61,10 +61,7 @@ bool cmStringReplaceHelper::Replace(std::string const& input,
|
|||||||
} else {
|
} else {
|
||||||
// Replace with part of the match.
|
// Replace with part of the match.
|
||||||
auto n = replacement.Number;
|
auto n = replacement.Number;
|
||||||
auto start = this->RegularExpression.start(n);
|
if (n > this->RegularExpression.num_groups()) {
|
||||||
if (start != std::string::npos) {
|
|
||||||
output += this->RegularExpression.match(n);
|
|
||||||
} else {
|
|
||||||
std::ostringstream error;
|
std::ostringstream error;
|
||||||
error << "replace expression \"" << this->ReplaceExpression
|
error << "replace expression \"" << this->ReplaceExpression
|
||||||
<< "\" contains an out-of-range escape for regex \""
|
<< "\" contains an out-of-range escape for regex \""
|
||||||
@@ -72,6 +69,7 @@ bool cmStringReplaceHelper::Replace(std::string const& input,
|
|||||||
this->ErrorString = error.str();
|
this->ErrorString = error.str();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
output += this->RegularExpression.match(n);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ check_cmake_test(String
|
|||||||
# Execute each test listed in StringTestScript.cmake:
|
# Execute each test listed in StringTestScript.cmake:
|
||||||
#
|
#
|
||||||
set(scriptname "@CMAKE_CURRENT_SOURCE_DIR@/StringTestScript.cmake")
|
set(scriptname "@CMAKE_CURRENT_SOURCE_DIR@/StringTestScript.cmake")
|
||||||
set(number_of_tests_expected 72)
|
set(number_of_tests_expected 73)
|
||||||
|
|
||||||
include("@CMAKE_CURRENT_SOURCE_DIR@/ExecuteScriptTests.cmake")
|
include("@CMAKE_CURRENT_SOURCE_DIR@/ExecuteScriptTests.cmake")
|
||||||
execute_all_script_tests(${scriptname} number_of_tests_executed)
|
execute_all_script_tests(${scriptname} number_of_tests_executed)
|
||||||
|
|||||||
@@ -116,6 +116,10 @@ elseif(testname STREQUAL regex_replace_index_too_small) # fail
|
|||||||
elseif(testname STREQUAL regex_replace_index_too_large) # fail
|
elseif(testname STREQUAL regex_replace_index_too_large) # fail
|
||||||
string(REGEX REPLACE "^this (.*)$" "with \\1 \\2" v "this input")
|
string(REGEX REPLACE "^this (.*)$" "with \\1 \\2" v "this input")
|
||||||
|
|
||||||
|
elseif(testname STREQUAL regex_replace_index_no_match) # pass
|
||||||
|
string(REGEX REPLACE "^(this (.*)|(that .*))$" "with \\1 \\2 \\3" v "this input")
|
||||||
|
message(STATUS "v='${v}'")
|
||||||
|
|
||||||
elseif(testname STREQUAL compare_no_mode) # fail
|
elseif(testname STREQUAL compare_no_mode) # fail
|
||||||
string(COMPARE)
|
string(COMPARE)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user