indentLines()

this will add a line-separator at the end of the last line because if it was
empty it is not any longer and deserves one.

@param string $lines
@param string $indent (optional)

@return string
334575
public static function indentLines($lines, $indent = '  ')
    {
        $lineSeparator = "\n";
        $buffer        = '';
        $line          = strtok($lines, $lineSeparator);
        while ($line) {
            $buffer .= $indent . $line . $lineSeparator;
            $line = strtok($lineSeparator);
        }
        strtok(null, null);

        return $buffer;
    }
1181236
def _set_line_indent(src, line, indent):
    '''
    
    '''
    if not indent:
        return line

    idt = []
    for c in src:
        if c not in ['\t', ' ']:
            break
        idt.append(c)

    return ''.join(idt) + line.lstrip()
1033306
public function writeLineIndented($string, $spaces = 2, $options = null) {
        return $this->writeLine($this->indent($string, $spaces), $options);
    }
414108
private function indentationModeBlock(string $line): int
  {
    $mode = 0;

    if (substr($line, -1, 1)=='{')
    {
      $mode |= self::C_INDENT_INCREMENT_AFTER;

      $this->defaultLevelIncrement();
    }

    if (substr($line, 0, 1)=='}')
    {
      $this->defaultLevelDecrement();

      if ($this->defaultLevelIsZero())
      {
        $mode |= self::C_INDENT_DECREMENT_BEFORE_DOUBLE;

        array_pop($this->defaultLevel);
      }
      else
      {
        $mode |= self::C_INDENT_DECREMENT_BEFORE;
      }
    }

    return $mode;
  }
627390
function raiseIndent(lines, offset) {
    offset = offset || '    ';

    return lines.map((line) => offset + line);
}
151861
def get_indented_lines(indent)
      buffer = ''

      # Gather multiple blank lines between lines of text
      blank_lines = proc do
        while lookahead_next_line :line_whitespace
          @line = @code[(@i += 1)]
          @offset = 0

          buffer += accept :line_whitespace
        end
      end

      # Get blank lines until we match something
      blank_lines[]

      # Get the next indentation after the parent line
      # and set it as primary indent
      first_indent = (lookahead_next_line(:indent).to_s || '').size
      next_indent = first_indent

      # While the indentation is smaller, add the line feed  to our buffer
      while next_indent > indent
        # Advance current line and reset offset
        @line = @code[(@i += 1)]
        @offset = 0

        # Get leading whitespace trimmed with first_indent's size
        next_line_indent = accept(:indent)[first_indent..-1] || ''
        next_line_indent = next_line_indent.size

        # Add next line feed, prepend the indent and append the newline
        buffer += ' ' * next_line_indent if next_line_indent > 0
        buffer += accept_stripped(:line_feed) || ''
        buffer += accept(:newline) || ''

        # Get blank lines until we match something
        blank_lines[]

        # Check the indentation on the following line. When we reach EOF,
        # set the indentation to 0 and cause the loop to stop
        if (next_indent = lookahead_next_line :indent)
          next_indent = next_indent[0].size
        else
          next_indent = 0
        end
      end

      buffer
    end
905226
public function setIndent($indent)
    {
        if (!$this->isNumber($indent)) {
            throw new ConfigException(ConfigException::MSG_INVALID_ARG, [
                                                                          '$indent',
                                                                          'integer'
                                                                      ]
            );
        }
        $this->indent = $indent;

        return $this;
    }
647052
public function getLines($begin, $end, $indent, $keepLastLF)
    {
        
        $line = $begin;

        if ($begin >= $end) {
            return '';
        }

        $queue =array_fill(0, $end - $begin, '');

        for ($i = 0; $line < $end; $line++, $i++) {
            $lineIndent = 0;
            $lineStart = $first = $this->bMarks[$line];

            if ($line + 1 < $end || $keepLastLF) {
                // No need for bounds check because we have fake entry on tail.
                $last = $this->eMarks[$line] + 1;
            } else {
                $last = $this->eMarks[$line];
            }

            while ($first < $last && $lineIndent < $indent) {
                $ch = $this->src[$first];

                if ($this->md->utils->isSpace($ch)) {
                    if ($ch === "\t") {
                        $lineIndent += 4 - ($lineIndent + $this->bsCount[$line]) % 4;
                    } else {
                        $lineIndent++;
                    }
                } else if ($first - $lineStart < $this->tShift[$line]) {
                    // patched tShift masked characters to look like spaces (blockquotes, list markers)
                    $lineIndent++;
                } else {
                    break;
                }

                $first++;
            }

            if ($lineIndent > $indent) {
                // partially expanding tabs in code blocks, e.g '\t\tfoobar'
                // with indent=2 becomes '  \tfoobar'
                $queue[$i] = str_repeat(' ', $lineIndent - $indent) . substr($this->src, $first, $last-$first);
            } else {
                $queue[$i] = substr($this->src, $first, $last-$first);
            }
        }

        return join('',$queue);
    }
163975
function indent(str, prefix) {
  prefix = prefix || '   ';
  var lines = str.split("\n");
  lines.forEach(function(line, i) {
    lines[i] = prefix + line;
  });
  return lines.join("\n");
}
882059
def indent_lines(text, indent)
      text ||= ''
      text.lines.map { |line| indent + line }.join
    end
1094754
def _indent_line(self, line, stripspace=''):
        """"""

        return re.sub(r"^%s" % stripspace, self.indentstring
                      * self.indent, line)
1002992
public static function prepend_each_line($string, $with)
    {
        $lines = explode("\n", $string);
        $append = '';
        if ("\n" === substr($string, -1) && '' === end($lines)) {
            // Last line might be empty because $string was terminated
            // with a newline, remove it from the $lines array,
            // we'll restore state by re-terminating the string at the end
            array_pop($lines);
            $append = "\n";
        }
        foreach ($lines as &$line) {
            $line = $with . $line;
        }
        unset($line);

        return implode("\n", $lines) . $append;
    }
437839
public function indent($indent = null)
    {
        if ($indent === null) {
            return $this->_indent;
        }
        return $this->_indent = $indent;
    }
84526
private function getIndent(array $lines)
    {
        $detector = new IndentationDetector(
            LineCollection::createFromArray($lines)
        );

        return str_repeat(' ', $detector->getFirstLineIndentation());
    }
1159762
protected static function prefixLength(array $lines)
    {
        // find only lines with interesting things
        $lines = \array_filter($lines, function ($line) {
            return \substr($line, \strspn($line, "* \t\n\r\0\x0B"));
        });

        // if we sort the lines, we only have to compare two items
        \sort($lines);

        $first = \reset($lines);
        $last  = \end($lines);

        // find the longest common substring
        $count = \min(\strlen($first), \strlen($last));
        for ($i = 0; $i < $count; $i++) {
            if ($first[$i] !== $last[$i]) {
                return $i;
            }
        }

        return $count;
    }
78351
private function getNextEmbedBlock($indentation = null)
    {
        $this->moveToNextLine();

        if (null === $indentation) {
            $newIndent = $this->getCurrentLineIndentation();

            $unindentedEmbedBlock = $this->isStringUnIndentedCollectionItem($this->currentLine);

            if (!$this->isCurrentLineEmpty() && 0 === $newIndent && !$unindentedEmbedBlock) {
                throw new ParseException('Indentation problem.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
            }
        } else {
            $newIndent = $indentation;
        }

        $data = array(substr($this->currentLine, $newIndent));

        $isItUnindentedCollection = $this->isStringUnIndentedCollectionItem($this->currentLine);

        while ($this->moveToNextLine()) {

            if ($isItUnindentedCollection && !$this->isStringUnIndentedCollectionItem($this->currentLine)) {
                $this->moveToPreviousLine();
                break;
            }

            if ($this->isCurrentLineEmpty()) {
                if ($this->isCurrentLineBlank()) {
                    $data[] = substr($this->currentLine, $newIndent);
                }

                continue;
            }

            $indent = $this->getCurrentLineIndentation();

            if (preg_match('#^(?P<text> *)$#', $this->currentLine, $match)) {
                // empty line
                $data[] = $match['text'];
            } elseif ($indent >= $newIndent) {
                $data[] = substr($this->currentLine, $newIndent);
            } elseif (0 == $indent) {
                $this->moveToPreviousLine();

                break;
            } else {
                throw new ParseException('Indentation problem.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
            }
        }

        return implode("\n", $data);
    }
1093808
def add_indent_lines(prefix, s):
    
    if not s:
        return prefix
    prefix_len = str_visible_len(prefix)
    lines = s.splitlines(True)
    return "".join([prefix + lines[0]] + [" " * prefix_len + l for l in lines[1:]])