PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/system/utf8/ord.php

https://github.com/avidenie/Yet-Another-PHP-Framework
PHP | 89 lines | 51 code | 17 blank | 21 comment | 19 complexity | 78c52212e233c81ccc2b45d5e32a10fe MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of the Yet Another PHP Framework package.
  4. * (c) 2010 Adrian Videnie <{@link mailto:avidenie@gmail.com avidenie@gmail.com}>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. *
  9. * @package Framework
  10. * @subpackage I18n
  11. * @author Adrian Videnie <avidenie@gmail.com>
  12. * @copyright 2010 Adrian Videnie
  13. * @license http://www.opensource.org/licenses/mit-license.php The MIT license
  14. */
  15. /**
  16. * Returns the unicode ordinal for a character.
  17. *
  18. * @author Harry Fuecks <hfuecks@gmail.com>
  19. *
  20. * @param string A character.
  21. * @return integer The unicode ordinal for a character.
  22. */
  23. function _ord($chr)
  24. {
  25. $ord0 = ord($chr);
  26. if (0 <= $ord0 && $ord0 <= 127) {
  27. return $ord0;
  28. }
  29. if (!isset($chr[1])) {
  30. trigger_error('Short sequence - at least 2 bytes expected, only 1 seen', E_USER_WARNING);
  31. return false;
  32. }
  33. $ord1 = ord($chr[1]);
  34. if (192 <= $ord0 && $ord0 <= 223) {
  35. return ($ord0 - 192) * 64 + ($ord1 - 128);
  36. }
  37. if (!isset($chr[2])) {
  38. trigger_error('Short sequence - at least 3 bytes expected, only 2 seen', E_USER_WARNING);
  39. return false;
  40. }
  41. $ord2 = ord($chr[2]);
  42. if (224 <= $ord0 && $ord0 <= 239) {
  43. return ($ord0 - 224) * 4096 + ($ord1 - 128) * 64 + ($ord2 - 128);
  44. }
  45. if (!isset($chr[3])) {
  46. trigger_error('Short sequence - at least 4 bytes expected, only 3 seen', E_USER_WARNING);
  47. return false;
  48. }
  49. $ord3 = ord($chr[3]);
  50. if (240 <= $ord0 && $ord0 <= 247) {
  51. return ($ord0 - 240) * 262144 + ($ord1 - 128) * 4096 + ($ord2-128) * 64 + ($ord3 - 128);
  52. }
  53. if (!isset($chr[4])) {
  54. trigger_error('Short sequence - at least 5 bytes expected, only 4 seen', E_USER_WARNING);
  55. return false;
  56. }
  57. $ord4 = ord($chr[4]);
  58. if (248 <= $ord0 && $ord0 <= 251) {
  59. return ($ord0 - 248) * 16777216 + ($ord1-128) * 262144 + ($ord2 - 128) * 4096 + ($ord3 - 128) * 64 + ($ord4 - 128);
  60. }
  61. if (!isset($chr[5])) {
  62. trigger_error('Short sequence - at least 6 bytes expected, only 5 seen', E_USER_WARNING);
  63. return false;
  64. }
  65. if (252 <= $ord0 && $ord0 <= 253) {
  66. return ($ord0 - 252) * 1073741824 + ($ord1 - 128) * 16777216 + ($ord2 - 128) * 262144 + ($ord3 - 128) * 4096 + ($ord4 - 128) * 64 + (ord($chr[5]) - 128);
  67. }
  68. if (254 <= $ord0 && $ord0 <= 255) {
  69. trigger_error('Invalid UTF-8 with surrogate ordinal ' . $ord0, E_USER_WARNING);
  70. return false;
  71. }
  72. }