PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/libraries/utf8/ord.php

http://joostina.googlecode.com/
PHP | 88 lines | 63 code | 16 blank | 9 comment | 12 complexity | 7cb59e0d7ae04b8c240bd59b3d6515ec MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2. /**
  3. * utf8::ord
  4. *
  5. * @package Core
  6. * @author Kohana Team
  7. * @copyright (c) 2007 Kohana Team
  8. * @copyright (c) 2005 Harry Fuecks
  9. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
  10. */
  11. function _ord($chr)
  12. {
  13. $ord0 = ord($chr);
  14. if ($ord0 >= 0 AND $ord0 <= 127)
  15. {
  16. return $ord0;
  17. }
  18. if ( ! isset($chr[1]))
  19. {
  20. trigger_error('Short sequence - at least 2 bytes expected, only 1 seen', E_USER_WARNING);
  21. return FALSE;
  22. }
  23. $ord1 = ord($chr[1]);
  24. if ($ord0 >= 192 AND $ord0 <= 223)
  25. {
  26. return ($ord0 - 192) * 64 + ($ord1 - 128);
  27. }
  28. if ( ! isset($chr[2]))
  29. {
  30. trigger_error('Short sequence - at least 3 bytes expected, only 2 seen', E_USER_WARNING);
  31. return FALSE;
  32. }
  33. $ord2 = ord($chr[2]);
  34. if ($ord0 >= 224 AND $ord0 <= 239)
  35. {
  36. return ($ord0 - 224) * 4096 + ($ord1 - 128) * 64 + ($ord2 - 128);
  37. }
  38. if ( ! isset($chr[3]))
  39. {
  40. trigger_error('Short sequence - at least 4 bytes expected, only 3 seen', E_USER_WARNING);
  41. return FALSE;
  42. }
  43. $ord3 = ord($chr[3]);
  44. if ($ord0 >= 240 AND $ord0 <= 247)
  45. {
  46. return ($ord0 - 240) * 262144 + ($ord1 - 128) * 4096 + ($ord2-128) * 64 + ($ord3 - 128);
  47. }
  48. if ( ! isset($chr[4]))
  49. {
  50. trigger_error('Short sequence - at least 5 bytes expected, only 4 seen', E_USER_WARNING);
  51. return FALSE;
  52. }
  53. $ord4 = ord($chr[4]);
  54. if ($ord0 >= 248 AND $ord0 <= 251)
  55. {
  56. return ($ord0 - 248) * 16777216 + ($ord1-128) * 262144 + ($ord2 - 128) * 4096 + ($ord3 - 128) * 64 + ($ord4 - 128);
  57. }
  58. if ( ! isset($chr[5]))
  59. {
  60. trigger_error('Short sequence - at least 6 bytes expected, only 5 seen', E_USER_WARNING);
  61. return FALSE;
  62. }
  63. if ($ord0 >= 252 AND $ord0 <= 253)
  64. {
  65. return ($ord0 - 252) * 1073741824 + ($ord1 - 128) * 16777216 + ($ord2 - 128) * 262144 + ($ord3 - 128) * 4096 + ($ord4 - 128) * 64 + (ord($chr[5]) - 128);
  66. }
  67. if ($ord0 >= 254 AND $ord0 <= 255)
  68. {
  69. trigger_error('Invalid UTF-8 with surrogate ordinal '.$ord0, E_USER_WARNING);
  70. return FALSE;
  71. }
  72. }