1.20.x search.test SearchTokenizerTestCase::code2utf($num)

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.

File

modules/search/tests/search.test, line 1803
Tests for search.module.

Class

SearchTokenizerTestCase
Test the CJK tokenizer.

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