Feature #306 - Internationalization API: Implementation of two new functions api_chr() and api_ord().

skala
Ivan Tcholakov 16 years ago
parent 4ec0433ddd
commit 942f562e44
  1. 25
      main/inc/lib/internationalization.lib.php
  2. 30
      tests/main/inc/lib/internationalization.lib.test.php

@ -1023,6 +1023,31 @@ function api_utf8_decode_xml($string, $to_encoding = null) {
* ----------------------------------------------------------------------------
*/
/**
* Takes the first character in a string and returns its Unicode codepoint.
* @param string $character The input string.
* @param string $encoding (optional) The encoding of the input string. If it is omitted, the platform character set will be used by default.
* @return int Returns: the codepoint of the first character; or 0xFFFD (unknown character) when the input string is empty.
* This is a multibyte aware version of the function ord().
* @link http://php.net/manual/en/function.ord.php
* Note the difference with the original funtion ord(): ord('') returns 0, api_ord('') returns 0xFFFD (unknown character).
*/
function api_ord($character, $encoding) {
return _api_utf8_ord(api_utf8_encode($character, $encoding));
}
/**
* Takes a Unicode codepoint and returns its correspondent character, encoded in given encoding.
* @param int $codepoint The Unicode codepoint.
* @param string $encoding (optional) The encoding of the returned character. If it is omitted, the platform character set will be used by default.
* @return string Returns the corresponding character, encoded as it has been requested.
* This is a multibyte aware version of the function chr().
* @link http://php.net/manual/en/function.chr.php
*/
function api_chr($codepoint, $encoding) {
return api_utf8_decode(_api_utf8_chr($codepoint), $encoding);
}
/**
* This function returns a string or an array with all occurrences of search in subject (ignoring case) replaced with the given replace value.
* @param mixed $search String or array of strings to be found.

@ -157,6 +157,30 @@ class TestInternationalization extends UnitTestCase {
* ----------------------------------------------------------------------------
*/
public function test_api_ord() {
$encoding = 'UTF-8';
$characters = array('И', 'в', 'а', 'н', ' ', 'I', 'v', 'a', 'n'); // UTF-8
$codepoints = array(1048, 1074, 1072, 1085, 32, 73, 118, 97, 110);
$res = array();
foreach ($characters as $character) {
$res[] = api_ord($character, $encoding);
}
$this->assertTrue($res == $codepoints);
//var_dump($res);
}
public function test_api_chr() {
$encoding = 'UTF-8';
$codepoints = array(1048, 1074, 1072, 1085, 32, 73, 118, 97, 110);
$characters = array('И', 'в', 'а', 'н', ' ', 'I', 'v', 'a', 'n'); // UTF-8
$res = array();
foreach ($codepoints as $codepoint) {
$res[] = api_chr($codepoint, $encoding);
}
$this->assertTrue($res == $characters);
//var_dump($res);
}
public function test_api_str_ireplace() {
$search = 'Á'; // UTF-8
$replace = 'a';
@ -526,7 +550,7 @@ class TestInternationalization extends UnitTestCase {
$res = api_asort($array, $sort_flag, $language, $encoding);
$keys = array_keys($array);
$this->assertTrue(is_bool($res));
$this->assertTrue($array[$keys[0]] == 'aíó');
$this->assertTrue($array[$keys[0]] == 'aíó' || $array[$keys[0]] == 'áed'); // The second result is given when intl php-extension is active.
//var_dump($array);
//var_dump($res);
}
@ -637,7 +661,7 @@ class TestInternationalization extends UnitTestCase {
$res = api_knatrsort($array, $language, $encoding);
$keys = array_keys($array);
$this->assertTrue(is_bool($res));
$this->assertTrue($array[$keys[0]] == 'úéo');
$this->assertTrue($array[$keys[0]] == 'úéo' || $array[$keys[0]] == 'áed'); // The second result is given when intl php-extension is active.
//var_dump($array);
//var_dump($res);
}
@ -673,7 +697,7 @@ class TestInternationalization extends UnitTestCase {
$encoding = 'UTF-8';
$res = api_sort($array, $sort_flag, $language, $encoding);
$this->assertTrue(is_bool($res));
$this->assertTrue($array[0] == 'aíó');
$this->assertTrue($array[0] == 'aíó' || $array[0] == 'áed'); // The second result is given when intl php-extension is active.
//var_dump($array);
//var_dump($res);
}

Loading…
Cancel
Save