League\CommonMark\Cursor::advanceBy PHP Method

advanceBy() public method

Move the cursor forwards
public advanceBy ( integer $characters, $advanceByColumns = false )
$characters integer Number of characters to advance by
    public function advanceBy($characters, $advanceByColumns = false)
    {
        $this->previousPosition = $this->currentPosition;
        $this->firstNonSpaceCache = null;
        $nextFewChars = mb_substr($this->line, $this->currentPosition, $characters, 'utf-8');
        if ($characters === 1 && !empty($nextFewChars)) {
            $asArray = [$nextFewChars];
        } else {
            $asArray = preg_split('//u', $nextFewChars, null, PREG_SPLIT_NO_EMPTY);
        }
        foreach ($asArray as $relPos => $c) {
            if ($c === "\t") {
                $charsToTab = 4 - $this->column % 4;
                $this->partiallyConsumedTab = $advanceByColumns && $charsToTab > $characters;
                $charsToAdvance = $charsToTab > $characters ? $characters : $charsToTab;
                $this->column += $charsToAdvance;
                $this->currentPosition += $this->partiallyConsumedTab ? 0 : 1;
                $characters -= $charsToAdvance;
            } else {
                $this->partiallyConsumedTab = false;
                $this->currentPosition++;
                $this->column++;
                $characters--;
            }
            if ($characters <= 0) {
                break;
            }
        }
    }

Usage Example

 public static function parse(Cursor $cursor)
 {
     if (null === self::$regexp) {
         $regex = RegexHelper::getInstance();
         self::$regexp = sprintf('/^\\s*([.#][_a-z0-9-]+|%s%s)(?<!})\\s*/i', $regex->getPartialRegex(RegexHelper::ATTRIBUTENAME), $regex->getPartialRegex(RegexHelper::ATTRIBUTEVALUESPEC));
     }
     $state = $cursor->saveState();
     $cursor->advanceToFirstNonSpace();
     if ('{' !== $cursor->getCharacter()) {
         $cursor->restoreState($state);
         return [];
     }
     $cursor->advanceBy(1);
     if (':' === $cursor->getCharacter()) {
         $cursor->advanceBy(1);
     }
     $attributes = [];
     while ($attribute = trim($cursor->match(self::$regexp))) {
         if ('#' === $attribute[0]) {
             $attributes['id'] = substr($attribute, 1);
             continue;
         }
         if ('.' === $attribute[0]) {
             $attributes['class'][] = substr($attribute, 1);
             continue;
         }
         list($name, $value) = explode('=', $attribute, 2);
         $first = $value[0];
         $last = substr($value, -1);
         if (('"' === $first && '"' === $last || "'" === $first && "'" === $last) && strlen($value) > 1) {
             $value = substr($value, 1, -1);
         }
         if ('class' === strtolower(trim($name))) {
             foreach (array_filter(explode(' ', trim($value))) as $class) {
                 $attributes['class'][] = $class;
             }
         } else {
             $attributes[trim($name)] = trim($value);
         }
     }
     if (0 === $cursor->advanceWhileMatches('}')) {
         $cursor->restoreState($state);
         return [];
     }
     if (isset($attributes['class'])) {
         $attributes['class'] = implode(' ', $attributes['class']);
     }
     return $attributes;
 }
All Usage Examples Of League\CommonMark\Cursor::advanceBy