function SearchTokenizerTest::code2utf

Same name and namespace in other branches
  1. 9 core/modules/search/tests/src/Kernel/SearchTokenizerTest.php \Drupal\Tests\search\Kernel\SearchTokenizerTest::code2utf()
  2. 10 core/modules/search/tests/src/Kernel/SearchTokenizerTest.php \Drupal\Tests\search\Kernel\SearchTokenizerTest::code2utf()
  3. 11.x core/modules/search/tests/src/Kernel/SearchTokenizerTest.php \Drupal\Tests\search\Kernel\SearchTokenizerTest::code2utf()

Like PHP chr() function, but for unicode characters.

Function chr() only works for ASCII characters up to character 255. This function converts a number to the corresponding unicode character. Adapted from functions supplied in comments on several functions on php.net.

1 call to SearchTokenizerTest::code2utf()
SearchTokenizerTest::testTokenizer in core/modules/search/tests/src/Kernel/SearchTokenizerTest.php
Verifies that strings of CJK characters are tokenized.

File

core/modules/search/tests/src/Kernel/SearchTokenizerTest.php, line 135

Class

SearchTokenizerTest
Tests that CJK tokenizer works as intended.

Namespace

Drupal\Tests\search\Kernel

Code

public function code2utf($num) {
    if ($num < 128) {
        return chr($num);
    }
    if ($num < 2048) {
        return chr(($num >> 6) + 192) . chr(($num & 63) + 128);
    }
    if ($num < 65536) {
        return chr(($num >> 12) + 224) . chr(($num >> 6 & 63) + 128) . chr(($num & 63) + 128);
    }
    if ($num < 2097152) {
        return chr(($num >> 18) + 240) . chr(($num >> 12 & 63) + 128) . chr(($num >> 6 & 63) + 128) . chr(($num & 63) + 128);
    }
    return '';
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.