PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/bower_components/joomla-platform/libraries/phputf8/ord.php

https://github.com/dprotopopov/tritumana
PHP | 92 lines | 65 code | 13 blank | 14 comment | 19 complexity | 3164e567745e089911692daabadee515 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause, LGPL-2.0
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @package utf8
  5. * @subpackage strings
  6. */
  7. //---------------------------------------------------------------
  8. /**
  9. * UTF-8 aware alternative to ord
  10. * Returns the unicode ordinal for a character
  11. * @param string UTF-8 encoded character
  12. * @return int unicode ordinal for the character
  13. * @see http://www.php.net/ord
  14. * @see http://www.php.net/manual/en/function.ord.php#46267
  15. */
  16. function utf8_ord($chr) {
  17. $ord0 = ord($chr);
  18. if ( $ord0 >= 0 && $ord0 <= 127 ) {
  19. return $ord0;
  20. }
  21. if ( !isset($chr{1}) ) {
  22. trigger_error('Short sequence - at least 2 bytes expected, only 1 seen');
  23. return FALSE;
  24. }
  25. $ord1 = ord($chr{1});
  26. if ( $ord0 >= 192 && $ord0 <= 223 ) {
  27. return ( $ord0 - 192 ) * 64
  28. + ( $ord1 - 128 );
  29. }
  30. if ( !isset($chr{2}) ) {
  31. trigger_error('Short sequence - at least 3 bytes expected, only 2 seen');
  32. return FALSE;
  33. }
  34. $ord2 = ord($chr{2});
  35. if ( $ord0 >= 224 && $ord0 <= 239 ) {
  36. return ($ord0-224)*4096
  37. + ($ord1-128)*64
  38. + ($ord2-128);
  39. }
  40. if ( !isset($chr{3}) ) {
  41. trigger_error('Short sequence - at least 4 bytes expected, only 3 seen');
  42. return FALSE;
  43. }
  44. $ord3 = ord($chr{3});
  45. if ($ord0>=240 && $ord0<=247) {
  46. return ($ord0-240)*262144
  47. + ($ord1-128)*4096
  48. + ($ord2-128)*64
  49. + ($ord3-128);
  50. }
  51. if ( !isset($chr{4}) ) {
  52. trigger_error('Short sequence - at least 5 bytes expected, only 4 seen');
  53. return FALSE;
  54. }
  55. $ord4 = ord($chr{4});
  56. if ($ord0>=248 && $ord0<=251) {
  57. return ($ord0-248)*16777216
  58. + ($ord1-128)*262144
  59. + ($ord2-128)*4096
  60. + ($ord3-128)*64
  61. + ($ord4-128);
  62. }
  63. if ( !isset($chr{5}) ) {
  64. trigger_error('Short sequence - at least 6 bytes expected, only 5 seen');
  65. return FALSE;
  66. }
  67. if ($ord0>=252 && $ord0<=253) {
  68. return ($ord0-252) * 1073741824
  69. + ($ord1-128)*16777216
  70. + ($ord2-128)*262144
  71. + ($ord3-128)*4096
  72. + ($ord4-128)*64
  73. + (ord($chr{5})-128);
  74. }
  75. if ( $ord0 >= 254 && $ord0 <= 255 ) {
  76. trigger_error('Invalid UTF-8 with surrogate ordinal '.$ord0);
  77. return FALSE;
  78. }
  79. }