PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/system/utf8/ord.php

https://bitbucket.org/chrispiechowicz/zepto
PHP | 72 lines | 47 code | 16 blank | 9 comment | 12 complexity | bcfe1662f979c8e2ebc570b8fd5e0564 MD5 | raw file
Possible License(s): LGPL-2.1, MIT, BSD-3-Clause
  1. <?php defined('SYSPATH') OR die('No direct script access.');
  2. /**
  3. * UTF8::ord
  4. *
  5. * @package Kohana
  6. * @author Kohana Team
  7. * @copyright (c) 2007-2012 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. return $ord0;
  16. if ( ! isset($chr[1]))
  17. {
  18. throw new UTF8_Exception('Short sequence - at least 2 bytes expected, only 1 seen');
  19. }
  20. $ord1 = ord($chr[1]);
  21. if ($ord0 >= 192 AND $ord0 <= 223)
  22. return ($ord0 - 192) * 64 + ($ord1 - 128);
  23. if ( ! isset($chr[2]))
  24. {
  25. throw new UTF8_Exception('Short sequence - at least 3 bytes expected, only 2 seen');
  26. }
  27. $ord2 = ord($chr[2]);
  28. if ($ord0 >= 224 AND $ord0 <= 239)
  29. return ($ord0 - 224) * 4096 + ($ord1 - 128) * 64 + ($ord2 - 128);
  30. if ( ! isset($chr[3]))
  31. {
  32. throw new UTF8_Exception('Short sequence - at least 4 bytes expected, only 3 seen');
  33. }
  34. $ord3 = ord($chr[3]);
  35. if ($ord0 >= 240 AND $ord0 <= 247)
  36. return ($ord0 - 240) * 262144 + ($ord1 - 128) * 4096 + ($ord2-128) * 64 + ($ord3 - 128);
  37. if ( ! isset($chr[4]))
  38. {
  39. throw new UTF8_Exception('Short sequence - at least 5 bytes expected, only 4 seen');
  40. }
  41. $ord4 = ord($chr[4]);
  42. if ($ord0 >= 248 AND $ord0 <= 251)
  43. return ($ord0 - 248) * 16777216 + ($ord1-128) * 262144 + ($ord2 - 128) * 4096 + ($ord3 - 128) * 64 + ($ord4 - 128);
  44. if ( ! isset($chr[5]))
  45. {
  46. throw new UTF8_Exception('Short sequence - at least 6 bytes expected, only 5 seen');
  47. }
  48. if ($ord0 >= 252 AND $ord0 <= 253)
  49. return ($ord0 - 252) * 1073741824 + ($ord1 - 128) * 16777216 + ($ord2 - 128) * 262144 + ($ord3 - 128) * 4096 + ($ord4 - 128) * 64 + (ord($chr[5]) - 128);
  50. if ($ord0 >= 254 AND $ord0 <= 255)
  51. {
  52. throw new UTF8_Exception("Invalid UTF-8 with surrogate ordinal ':ordinal'", array(
  53. ':ordinal' => $ord0,
  54. ));
  55. }
  56. }