Exemplo n.º 1
0
//++
// Details: Remove backslashes added by CMIUtilString::AddSlashes.
// Type:    Method.
// Args:    None.
// Return:  CMIUtilString - The initial version of wrapped string.
// Throws:  None.
//--
CMIUtilString CMIUtilString::StripSlashes() const {
  const char cBckSlash('\\');
  const size_t nLen(length());
  CMIUtilString strNew;
  strNew.reserve(nLen);

  size_t nOffset(0);
  while (nOffset < nLen) {
    const size_t nBckSlashPos(find(cBckSlash, nOffset));
    const bool bBckSlashNotFound(nBckSlashPos == std::string::npos);
    if (bBckSlashNotFound) {
      const size_t nAppendAll(std::string::npos);
      strNew.append(*this, nOffset, nAppendAll);
      break;
    }
    const size_t nAppendLen(nBckSlashPos - nOffset);
    strNew.append(*this, nOffset, nAppendLen);
    const bool bBckSlashIsLast(nBckSlashPos == nLen);
    if (bBckSlashIsLast) {
      strNew.push_back(cBckSlash);
      break;
    }
    const char cEscapedChar((*this)[nBckSlashPos + 1]);
    const size_t nEscapedCharPos(std::string("\"\\").find(cEscapedChar));
    const bool bEscapedCharNotFound(nEscapedCharPos == std::string::npos);
    if (bEscapedCharNotFound)
      strNew.push_back(cBckSlash);
    strNew.push_back(cEscapedChar);
    nOffset = nBckSlashPos + 2;
  }

  return strNew;
}
Exemplo n.º 2
0
//++ ------------------------------------------------------------------------------------
// Details: Get string with backslashes in front of double quote '"' and backslash '\\'
//          characters.
// Type:    Method.
// Args:    None.
// Return:  CMIUtilString - The wrapped version of the initial string.
// Throws:  None.
//--
CMIUtilString
CMIUtilString::AddSlashes(void) const
{
    const char cBckSlash('\\');
    const size_t nLen(length());
    CMIUtilString strNew;
    strNew.reserve(nLen);

    size_t nOffset(0);
    while (nOffset < nLen)
    {
        const size_t nUnescapedCharPos(find_first_of("\"\\", nOffset));
        const bool bUnescapedCharNotFound(nUnescapedCharPos == std::string::npos);
        if (bUnescapedCharNotFound)
        {
            const size_t nAppendAll(std::string::npos);
            strNew.append(*this, nOffset, nAppendAll);
            break;
        }
        const size_t nAppendLen(nUnescapedCharPos - nOffset);
        strNew.append(*this, nOffset, nAppendLen);
        strNew.push_back(cBckSlash);
        const char cUnescapedChar((*this)[nUnescapedCharPos]);
        strNew.push_back(cUnescapedChar);
        nOffset = nUnescapedCharPos + 1;
    }

    return strNew;
}
Exemplo n.º 3
0
//++
// Details: Get escaped string from *this string.
// Type:    Method.
// Args:    None.
// Return:  CMIUtilString - The escaped version of the initial string.
// Throws:  None.
//--
CMIUtilString CMIUtilString::Escape(bool vbEscapeQuotes /* = false */) const {
  const size_t nLen(length());
  CMIUtilString strNew;
  strNew.reserve(nLen);
  for (size_t nIndex(0); nIndex < nLen; ++nIndex) {
    const char cUnescapedChar((*this)[nIndex]);
    if (cUnescapedChar == '"' && vbEscapeQuotes)
      strNew.append("\\\"");
    else
      strNew.append(ConvertToPrintableASCII((char)cUnescapedChar));
  }
  return strNew;
}