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

Breadcrumb

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

function AbstractUnicodeString::wcswidth

Based on https://github.com/jquast/wcwidth, a Python implementation of https://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c.

1 call to AbstractUnicodeString::wcswidth()
AbstractUnicodeString::width in vendor/symfony/string/AbstractUnicodeString.php
Returns the printable length on a terminal.

File

vendor/symfony/string/AbstractUnicodeString.php, line 569

Class

AbstractUnicodeString
Represents a string of abstract Unicode characters.

Namespace

Symfony\Component\String

Code

private function wcswidth(string $string) : int {
    $width = 0;
    foreach (preg_split('//u', $string, -1, \PREG_SPLIT_NO_EMPTY) as $c) {
        $codePoint = mb_ord($c, 'UTF-8');
        if (0 === $codePoint || 0x34f === $codePoint || 0x200b <= $codePoint && 0x200f >= $codePoint || 0x2028 === $codePoint || 0x2029 === $codePoint || 0x202a <= $codePoint && 0x202e >= $codePoint || 0x2060 <= $codePoint && 0x2063 >= $codePoint) {
            continue;
        }
        // Non printable characters
        if (32 > $codePoint || 0x7f <= $codePoint && 0xa0 > $codePoint) {
            return -1;
        }
        self::$tableZero ??= (require __DIR__ . '/Resources/data/wcswidth_table_zero.php');
        if ($codePoint >= self::$tableZero[0][0] && $codePoint <= self::$tableZero[$ubound = \count(self::$tableZero) - 1][1]) {
            $lbound = 0;
            while ($ubound >= $lbound) {
                $mid = floor(($lbound + $ubound) / 2);
                if ($codePoint > self::$tableZero[$mid][1]) {
                    $lbound = $mid + 1;
                }
                elseif ($codePoint < self::$tableZero[$mid][0]) {
                    $ubound = $mid - 1;
                }
                else {
                    continue 2;
                }
            }
        }
        self::$tableWide ??= (require __DIR__ . '/Resources/data/wcswidth_table_wide.php');
        if ($codePoint >= self::$tableWide[0][0] && $codePoint <= self::$tableWide[$ubound = \count(self::$tableWide) - 1][1]) {
            $lbound = 0;
            while ($ubound >= $lbound) {
                $mid = floor(($lbound + $ubound) / 2);
                if ($codePoint > self::$tableWide[$mid][1]) {
                    $lbound = $mid + 1;
                }
                elseif ($codePoint < self::$tableWide[$mid][0]) {
                    $ubound = $mid - 1;
                }
                else {
                    $width += 2;
                    continue 2;
                }
            }
        }
        ++$width;
    }
    return $width;
}

API Navigation

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