1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
3 #include "cmPlaceholderExpander.h"
7 std::string& cmPlaceholderExpander::ExpandVariables(std::string& s)
9 std::string::size_type start = s.find('<');
10 // no variables to expand
11 if (start == std::string::npos) {
14 std::string::size_type pos = 0;
15 std::string expandedInput;
16 while (start != std::string::npos && start < s.size() - 2) {
17 std::string::size_type end = s.find('>', start);
18 // if we find a < with no > we are done
19 if (end == std::string::npos) {
23 char c = s[start + 1];
24 // if the next char after the < is not A-Za-z then
25 // skip it and try to find the next < in the string
27 start = s.find('<', start + 1);
30 std::string var = s.substr(start + 1, end - start - 1);
31 std::string replace = this->ExpandVariable(var);
32 expandedInput += s.substr(pos, start - pos);
34 // Prevent consecutive whitespace in the output if the rule variable
35 // expands to an empty string.
36 bool consecutive = replace.empty() && start > 0 && s[start - 1] == ' ' &&
37 end + 1 < s.size() && s[end + 1] == ' ';
39 expandedInput.pop_back();
42 expandedInput += replace;
45 start = s.find('<', start + var.size() + 2);
49 // add the rest of the input
50 expandedInput += s.substr(pos, s.size() - pos);