Ejemplo n.º 1
0
void
TextOutputStream::writeNewline()
{
  for (size_t i = 0; i < newline.size(); ++i)
  {
    indentAppend(newline[i]);
  }
}
Ejemplo n.º 2
0
void TextOutput::wordWrapIndentAppend(const std::string& str) {
    // TODO: keep track of the last space character we saw so we don't
    // have to always search.

    if ((option.wordWrap == Settings::WRAP_NONE) ||
        (currentColumn + (int)str.size() <= option.numColumns)) {
        // No word-wrapping is needed

        // Add one character at a time.
        // TODO: optimize for strings without newlines to add multiple
        // characters.
        for (uint32 i = 0; i < str.size(); ++i) {
            indentAppend(str[i]);
        }
        return;
    }

    // Number of columns to wrap against
    int cols = option.numColumns - indentSpaces;
    
    // Copy forward until we exceed the column size, 
    // and then back up and try to insert newlines as needed.
    for (uint32 i = 0; i < str.size(); ++i) {

        indentAppend(str[i]);
        if ((str[i] == '\r') && (i + 1 < str.size()) && (str[i + 1] == '\n')) {
            // \r\n, we need to hit the \n to enter word wrapping.
            ++i;
            indentAppend(str[i]);
        }

        if (currentColumn >= cols) {
            debugAssertM(str[i] != '\n' && str[i] != '\r',
                "Should never enter word-wrapping on a newline character");            

            // True when we're allowed to treat a space as a space.
            bool unquotedSpace = option.allowWordWrapInsideDoubleQuotes || ! inDQuote;

            // Cases:
            //
            // 1. Currently in a series of spaces that ends with a newline
            //     strip all spaces and let the newline
            //     flow through.
            //
            // 2. Currently in a series of spaces that does not end with a newline
            //     strip all spaces and replace them with single newline
            //
            // 3. Not in a series of spaces
            //     search backwards for a space, then execute case 2.

            // Index of most recent space
            uint32 lastSpace = data.size() - 1;

            // How far back we had to look for a space
            uint32 k = 0;
            uint32 maxLookBackward = currentColumn - indentSpaces;

            // Search backwards (from current character), looking for a space.
            while ((k < maxLookBackward) &&
                (lastSpace > 0) &&
                (! ((data[lastSpace] == ' ') && unquotedSpace))) {
                --lastSpace;
                ++k;

                if ((data[lastSpace] == '\"') && !option.allowWordWrapInsideDoubleQuotes) {
                    unquotedSpace = ! unquotedSpace;
                }
            }

            if (k == maxLookBackward) {
                // We couldn't find a series of spaces

                if (option.wordWrap == Settings::WRAP_ALWAYS) {
                    // Strip the last character we wrote, force a newline,
                    // and replace the last character;
                    data.pop();
                    writeNewline();
                    indentAppend(str[i]);
                } else {
                    // Must be Settings::WRAP_WITHOUT_BREAKING
                    //
                    // Don't write the newline; we'll come back to
                    // the word wrap code after writing another character
                }
            } else {
                // We found a series of spaces.  If they continue 
                // to the new string, strip spaces off both.  Otherwise
                // strip spaces from data only and insert a newline.                

                // Find the start of the spaces.  firstSpace is the index of the
                // first non-space, looking backwards from lastSpace.
                uint32 firstSpace = lastSpace;
                while ((k < maxLookBackward) &&
                    (firstSpace > 0) &&
                    (data[firstSpace] == ' ')) {
                    --firstSpace;
                    ++k;
                }

                if (k == maxLookBackward) {
                    ++firstSpace;
                }

                if (lastSpace == (uint32)data.size() - 1) {
                    // Spaces continued up to the new string
                    data.resize(firstSpace + 1);
                    writeNewline();

                    // Delete the spaces from the new string
                    while ((i < str.size() - 1) && (str[i + 1] == ' ')) {
                        ++i;
                    }
                } else {
                    // Spaces were somewhere in the middle of the old string.
                    // replace them with a newline.

                    // Copy over the characters that should be saved
                    Array<char> temp;
                    for (uint32 j = lastSpace + 1; j < (uint32)data.size(); ++j) {
                        char c = data[j];

                        if (c == '\"') {
                            // Undo changes to quoting (they will be re-done
                            // when we paste these characters back on).
                            inDQuote = !inDQuote;
                        }
                        temp.append(c);
                    }

                    // Remove those characters and replace with a newline.
                    data.resize(firstSpace + 1);
                    writeNewline();

                    // Write them back
                    for (uint32 j = 0; j < (uint32)temp.size(); ++j) {
                        indentAppend(temp[j]);
                    }

                    // We are now free to continue adding from the
                    // new string, which may or may not begin with spaces.

                } // if spaces included new string
            } // if hit indent
        } // if line exceeded
    } // iterate over str
}
Ejemplo n.º 3
0
void TextOutput::writeNewline() {
    for (uint32 i = 0; i < newline.size(); ++i) {
        indentAppend(newline[i]);
    }
}