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

Breadcrumb

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

function Tokenizer::replaceTabsInToken

Replaces tabs in original token content with spaces.

Each tab can represent between 1 and $config->tabWidth spaces, so this cannot be a straight string replace. The original content is placed into an orig_content index and the new token length is also set in the length index.

Parameters

array $token The token to replace tabs inside.:

string $prefix The character to use to represent the start of a tab.:

string $padding The character to use to represent the end of a tab.:

int $tabWidth The number of spaces each tab represents.:

Return value

void

1 call to Tokenizer::replaceTabsInToken()
Tokenizer::createPositionMap in vendor/squizlabs/php_codesniffer/src/Tokenizers/Tokenizer.php
Sets token position information.

File

vendor/squizlabs/php_codesniffer/src/Tokenizers/Tokenizer.php, line 589

Class

Tokenizer

Namespace

PHP_CodeSniffer\Tokenizers

Code

public function replaceTabsInToken(&$token, $prefix = ' ', $padding = ' ', $tabWidth = null) {
    $checkEncoding = false;
    if (function_exists('iconv_strlen') === true) {
        $checkEncoding = true;
    }
    $currColumn = $token['column'];
    if ($tabWidth === null) {
        $tabWidth = $this->config->tabWidth;
        if ($tabWidth === 0) {
            $tabWidth = 1;
        }
    }
    if (rtrim($token['content'], "\t") === '') {
        // String only contains tabs, so we can shortcut the process.
        $numTabs = strlen($token['content']);
        $firstTabSize = $tabWidth - ($currColumn - 1) % $tabWidth;
        $length = $firstTabSize + $tabWidth * ($numTabs - 1);
        $newContent = $prefix . str_repeat($padding, $length - 1);
    }
    else {
        // We need to determine the length of each tab.
        $tabs = explode("\t", $token['content']);
        $numTabs = count($tabs) - 1;
        $tabNum = 0;
        $newContent = '';
        $length = 0;
        foreach ($tabs as $content) {
            if ($content !== '') {
                $newContent .= $content;
                if ($checkEncoding === true) {
                    // Not using ASCII encoding, so take a bit more care.
                    $oldLevel = error_reporting();
                    error_reporting(0);
                    $contentLength = iconv_strlen($content, $this->config->encoding);
                    error_reporting($oldLevel);
                    if ($contentLength === false) {
                        // String contained invalid characters, so revert to default.
                        $contentLength = strlen($content);
                    }
                }
                else {
                    $contentLength = strlen($content);
                }
                $currColumn += $contentLength;
                $length += $contentLength;
            }
            // The last piece of content does not have a tab after it.
            if ($tabNum === $numTabs) {
                break;
            }
            // Process the tab that comes after the content.
            $tabNum++;
            // Move the pointer to the next tab stop.
            $pad = $tabWidth - ($currColumn + $tabWidth - 1) % $tabWidth;
            $currColumn += $pad;
            $length += $pad;
            $newContent .= $prefix . str_repeat($padding, $pad - 1);
        }
        
        //end foreach
    }
    
    //end if
    $token['orig_content'] = $token['content'];
    $token['content'] = $newContent;
    $token['length'] = $length;
}
RSS feed
Powered by Drupal