예제 #1
0
std::vector<std::string> findStringsWithCode(std::string code) {
  if (code.length() == 0) {
    return std::vector<std::string>();
  }

  std::vector<std::string> prevStrings, prevMinus1Strings;
  prevStrings.push_back("");

  for (int length = 1 ; length <= code.length() ; ++length) {
    std::vector<std::string> newStrings;
    int nextDigitPosition = code.length() - length;

    std::string nextDigit = code.substr(nextDigitPosition, 1);
    if (isValidLetterCode(nextDigit)) {
      appendPrefix(newStrings, letterForCode(nextDigit), prevStrings);
    }

    if (length > 1) {
      std::string nextTwoDigits = code.substr(nextDigitPosition, 2);
      if (isValidLetterCode(nextTwoDigits)) {
        appendPrefix(newStrings, letterForCode(nextTwoDigits), prevMinus1Strings);
      }
    }

    prevMinus1Strings = prevStrings;
    prevStrings = newStrings;
  }

  return prevStrings;
}
예제 #2
0
파일: WordWrapper.cpp 프로젝트: alewisw/aq
//------------------------------------------------------------------------------
void WordWrapper::appendWordWrapped(const AQLogStringBuilder& src,
    AQLogStringBuilder& dst, uint32_t wrapCols, uint32_t prefixSpaces, 
    Newline newline)
{
    uint32_t basePrefixOffset;

    switch (newline)
    {
    default:
        newline = Newline::NEWLINE_LF;
        // falls through

    case Newline::NEWLINE_LF:
        basePrefixOffset = 1;
        break;

    case Newline::NEWLINE_CRLF:
        basePrefixOffset = 0;
        break;
    }


    AQLogStringBuilder::iterator lineStart = src.begin();
    AQLogStringBuilder::iterator it = lineStart;
    AQLogStringBuilder::iterator firstSpace = src.end();
    AQLogStringBuilder::iterator lastSpace = firstSpace;
    uint32_t cols = 0;
    while (it != src.end())
    {
        // Any of "\n", "\r\n", or "\n\r" result in a newline being inserted.
        char ch = *it;
        if (ch == '\r' || ch == '\n')
        {
            // Append the line.
            if (lastSpace == it)
            {
                dst.appendPointer(lineStart, firstSpace);
            }
            else
            {
                dst.appendPointer(lineStart, it);
            }

            // Check for a following '\r' or '\n' and skip over it for the
            // next line.
            it++;
            if (it != src.end())
            {
                char ch2 = *it;
                if (ch2 != ch && (ch2 == '\r' || ch2 == '\n'))
                {
                    it++;
                }
            }
            lineStart = it;
            firstSpace = lastSpace = src.end();
            cols = 0;

            // Attach the prefix for the next line unless we reached the end.
            if (it != src.end())
            {
                appendPrefix(dst, prefixSpaces, basePrefixOffset);
            }
        }
        else 
        {
            if (isspace(ch))
            {
                // Find the range of spaces until the end of the line or a non-space
                // character.
                firstSpace = it;
                do
                {
                    it++;
                    cols++;
                } while (it != src.end() && isspace(*it) && *it != '\r' && *it != '\n');
                lastSpace = it;
            }
            else
            {
                it++;
                cols++;
            }

            // Word wrap.
            if (cols >= wrapCols && firstSpace != src.end())
            {
                dst.appendPointer(lineStart, firstSpace);
                if (lastSpace != src.end())
                {
                    appendPrefix(dst, prefixSpaces, basePrefixOffset);
                }
                lineStart = lastSpace;
                firstSpace = lastSpace = src.end();
                cols = 0;
            }
        }
    }

    // Append the remainder of the string.
    if (lineStart < it)
    {
        if (lastSpace == it)
        {
            dst.appendPointer(lineStart, firstSpace);
        }
        else
        {
            dst.appendPointer(lineStart, it);
        }
    }
}