Example #1
0
static void trim(STRING& str)
{
    static const WCHAR Spaces[] = L" \t\r\n";
    size_t i = str.find_first_not_of(Spaces);
    size_t j = str.find_last_not_of(Spaces);
    if (i == STRING::npos || j == STRING::npos)
    {
        str.clear();
    }
    else
    {
        str = str.substr(i, j - i + 1);
    }
}
 /*!
  * Add escaping to each sequence of backslashes that immediately precedes
  * a quote character.  Each backslash in such a sequence must be escaped
  * with another backslash.  Note that backslashes that are not part of a
  * sequence that immediately precedes a quote are NOT escaped.  E.g.:
  *
  *  \a" -> \a"
  *  foo\bar"baz\\"fum -> foo\bar"baz\\\\"fum
  */
 void EscapeBackSlashes()
 {
     size_t quote = _quoted.find_first_of('"', 1);
     while (quote != STRING::npos)
     {
         size_t numSlashes = 0;
         size_t notSlash = _quoted.find_last_not_of('\\', quote-1);
         if (notSlash != quote-1)
         {
             if (notSlash == STRING::npos)
                 numSlashes = quote;
             else
                 numSlashes = quote - notSlash - 1;
             _quoted.insert(quote, numSlashes, '\\');
         }
         quote = _quoted.find_first_of('"', quote + numSlashes + 1);
     }
 }
    /*!
     * Add quotes around the string if it contains any whitespace.
     */
    void AddQuotes(const T *whitespace)
    {
        if (_quoted.find_first_of(whitespace) != STRING::npos)
        {
            _quoted.insert(0, 1, '"');
            _quoted.append(1, '"');

            // If the last character (prior to adding the quotes) was a backslash,
            // it needs to be escaped now because it precedes a quote.
            //
            size_t quote = _quoted.size() - 1;
            if (_quoted[quote-1] == '\\')
            {
                size_t notSlash = _quoted.find_last_not_of('\\', quote-2);
                size_t numSlashes = quote - notSlash - 1;
                _quoted.insert(quote, numSlashes, '\\');
            }
        }
    }