title: Function to escape some characters for C++ string
body: <p>I need a function to escape some characters inside a <code>std::string</code> and so i made this:</p>
  
  <pre><code>static void escape(std::string&amp; source,const  std::vector&lt;std::string&gt; &amp; toEscape, const std::string&amp; escape){
      //for each position of the string
      for(auto i = 0; i &lt; source.size(); ++i){
          // for each substring to escape
          for(const auto &amp; cur_to_escape : toEscape){
              // if the current position + the size of the current "to_escape" string are less than the string size and it's equal to the substring next of the i'th position
              if(i + cur_to_escape.size() &lt; source.size() &amp;&amp; source.substr(i, cur_to_escape.size()) == cur_to_escape){
                  // then for each char of the current "to_escape", escape the current character with the "escape" string given as parameter
                  /*
                   *  source = asd
                   *  toEscape = {"asd"}
                   *  escape = \
                   *  -&gt; asd -&gt; \asd -&gt; \a\sd -&gt; \a\s\d 
                   * */
                  for(auto z = 0; z &lt; cur_to_escape.size(); ++z){
                      source.insert(i, escape);
                      i+=escape.size();
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>and to test it i've used this:</p>
  
  <pre><code>int main() {
      std::string s = "need to escape \" , \\ and \n .";
      std::cout&lt;&lt;s;
      escape(s, {"\n", "\\", "\""}, "\\");
      std::cout&lt;&lt;"\n\n final string: "&lt;&lt;s;
  }
  </code></pre>
  
  <p>and the output is</p>
  
  <pre><code>final string: need to escape \" , \\ and \
   .
  </code></pre>
  
  <p>and so the <code>\n</code> is not been escaped as intended... and i can't find the problem... any guesses?</p>