Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. LSM.php

function LSM::match

Executes the match. It returns an array where the first element is the number of consumed characters and the second element is the match. If no match is found it returns null.

Parameters

Scanner $scanner Scanner instance:

int $index Current index:

string $char Current character:

Return value

array|null

File

vendor/mck89/peast/lib/Peast/Syntax/LSM.php, line 123

Class

LSM
Longest Sequence Matcher. Utility class used by the scanner to consume the longest sequence of character given a set of allowed characters sequences.

Namespace

Peast\Syntax

Code

public function match($scanner, $index, $char) {
    $consumed = 1;
    $bestMatch = null;
    if (isset($this->map[$char])) {
        
        //If the character is present in the map and it has a max length of
        
        //1, match immediately
        if ($this->map[$char]["maxLen"] === 1) {
            $bestMatch = array(
                $consumed,
                $char,
            );
        }
        else {
            
            //Otherwise consume a number of characters equal to the max
            
            //length and find the longest match
            $buffer = $char;
            $map = $this->map[$char]["map"];
            $maxLen = $this->map[$char]["maxLen"];
            do {
                if (in_array($buffer, $map)) {
                    $bestMatch = array(
                        $consumed,
                        $buffer,
                    );
                }
                $nextChar = $scanner->charAt($index + $consumed);
                if ($nextChar === null) {
                    break;
                }
                $buffer .= $nextChar;
                $consumed++;
            } while ($consumed <= $maxLen);
        }
    }
    return $bestMatch;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal