You are here

function SearchTokenizerTest::code2utf in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/search/src/Tests/SearchTokenizerTest.php \Drupal\search\Tests\SearchTokenizerTest::code2utf()

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

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/src/Tests/SearchTokenizerTest.php
Verifies that strings of CJK characters are tokenized.

File

core/modules/search/src/Tests/SearchTokenizerTest.php, line 136
Contains \Drupal\search\Tests\SearchTokenizerTest.

Class

SearchTokenizerTest
Tests that CJK tokenizer works as intended.

Namespace

Drupal\search\Tests

Code

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 '';
}