title: Replacing tokens inside string
body: <p>What i am trying to do is to replace all the tokens with a similiar syntax <code>[[VULKAN_SDK]] </code> inside a string. I want to take the contents of the brackes(in this case <code>VULKAN_SDK</code>) and replace the entire token with a value i get from <code>getenv</code> by passing the contents of the brackets. Example  <code>[[VULKAN_SDK]]</code> gets replaced by <code>C:\VulkanSDK\1.2.148.1</code>(a system enviroment variable).
  If the enviroment variable does not exist then the token should not be replaced.
  Here is my code:</p>
  <pre><code>void replaceSystemEnviromentTokens(std::string&amp; var) {
    static const char* tokenOpening = &quot;[[&quot;;
    static const char* tokenClosure = &quot;]]&quot;;
    auto getEnvVal = [](const char* var) { 
      char * val = std::getenv(var);
      return val == NULL ? std::string() : std::string(val);
    };
    
    std::size_t temp = var.find(tokenOpening, 0);
    while(temp != std::string::npos) {
      const std::size_t beginning = temp;
      const std::size_t end = var.find(tokenClosure, beginning);
      temp = var.find(tokenOpening, beginning + 1);
      
      if(end == std::string::npos) 
        break;
      
      if(temp != std::string::npos)
        if(end &gt; temp)
          continue;
      
      const std::size_t tokenSize = end - (beginning + strlen(tokenOpening));
      const std::string token = var.substr(beginning + strlen(tokenOpening), tokenSize);
      const std::string translatedToken = getEnvVal(token.c_str());
      
      if(!translatedToken.empty()) {
        const std::size_t currentSize = var.length();
        var.replace(beginning, end, translatedToken);
        if(temp != std::string::npos) {
          temp += currentSize - var.length();
          if(temp &gt; var.length())
            temp = std::string::npos;
        }
      }
    }
  }
  </code></pre>
  <p>Testing:</p>
  <pre><code>int main(int argc, char** argv) {
    std::string test = &quot;this should be converted: [[VULKAN_SDK]] this should not be converted: VULKAN_SDK]] this: [[VULKAN_SDK]]&quot;;
    replaceSystemEnviromentTokens(test);
    std::cout &lt;&lt; test &lt;&lt; std::endl;
  }
  </code></pre>
  <p>current behavior: <code>this should be converted: C:\VulkanSDK\1.2.148.1erted: VULKAN_SDK]] this: [[VULKAN_SDK]]</code></p>
  <p>expected behavior: <code>this should be converted: C:\VulkanSDK\1.2.148.1 this should not be converted: VULKAN_SDK]] this: C:\VulkanSDK\1.2.148.1</code></p>