PageRenderTime 69ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/system/utf8/to_unicode.php

https://bitbucket.org/alvinpd/monsterninja
PHP | 141 lines | 97 code | 9 blank | 35 comment | 21 complexity | 43216ea4f400f98e560020206e9aecc5 MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * UTF8::to_unicode
  4. *
  5. * @package Kohana
  6. * @author Kohana Team
  7. * @copyright (c) 2007-2008 Kohana Team
  8. * @copyright (c) 2005 Harry Fuecks
  9. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
  10. */
  11. function _to_unicode($str)
  12. {
  13. $mState = 0; // cached expected number of octets after the current octet until the beginning of the next UTF8 character sequence
  14. $mUcs4 = 0; // cached Unicode character
  15. $mBytes = 1; // cached expected number of octets in the current sequence
  16. $out = array();
  17. $len = strlen($str);
  18. for ($i = 0; $i < $len; $i++)
  19. {
  20. $in = ord($str[$i]);
  21. if ($mState == 0)
  22. {
  23. // When mState is zero we expect either a US-ASCII character or a
  24. // multi-octet sequence.
  25. if (0 == (0x80 & $in))
  26. {
  27. // US-ASCII, pass straight through.
  28. $out[] = $in;
  29. $mBytes = 1;
  30. }
  31. elseif (0xC0 == (0xE0 & $in))
  32. {
  33. // First octet of 2 octet sequence
  34. $mUcs4 = $in;
  35. $mUcs4 = ($mUcs4 & 0x1F) << 6;
  36. $mState = 1;
  37. $mBytes = 2;
  38. }
  39. elseif (0xE0 == (0xF0 & $in))
  40. {
  41. // First octet of 3 octet sequence
  42. $mUcs4 = $in;
  43. $mUcs4 = ($mUcs4 & 0x0F) << 12;
  44. $mState = 2;
  45. $mBytes = 3;
  46. }
  47. elseif (0xF0 == (0xF8 & $in))
  48. {
  49. // First octet of 4 octet sequence
  50. $mUcs4 = $in;
  51. $mUcs4 = ($mUcs4 & 0x07) << 18;
  52. $mState = 3;
  53. $mBytes = 4;
  54. }
  55. elseif (0xF8 == (0xFC & $in))
  56. {
  57. // First octet of 5 octet sequence.
  58. //
  59. // This is illegal because the encoded codepoint must be either
  60. // (a) not the shortest form or
  61. // (b) outside the Unicode range of 0-0x10FFFF.
  62. // Rather than trying to resynchronize, we will carry on until the end
  63. // of the sequence and let the later error handling code catch it.
  64. $mUcs4 = $in;
  65. $mUcs4 = ($mUcs4 & 0x03) << 24;
  66. $mState = 4;
  67. $mBytes = 5;
  68. }
  69. elseif (0xFC == (0xFE & $in))
  70. {
  71. // First octet of 6 octet sequence, see comments for 5 octet sequence.
  72. $mUcs4 = $in;
  73. $mUcs4 = ($mUcs4 & 1) << 30;
  74. $mState = 5;
  75. $mBytes = 6;
  76. }
  77. else
  78. {
  79. // Current octet is neither in the US-ASCII range nor a legal first octet of a multi-octet sequence.
  80. trigger_error('UTF8::to_unicode: Illegal sequence identifier in UTF-8 at byte '.$i, E_USER_WARNING);
  81. return FALSE;
  82. }
  83. }
  84. else
  85. {
  86. // When mState is non-zero, we expect a continuation of the multi-octet sequence
  87. if (0x80 == (0xC0 & $in))
  88. {
  89. // Legal continuation
  90. $shift = ($mState - 1) * 6;
  91. $tmp = $in;
  92. $tmp = ($tmp & 0x0000003F) << $shift;
  93. $mUcs4 |= $tmp;
  94. // End of the multi-octet sequence. mUcs4 now contains the final Unicode codepoint to be output
  95. if (0 == --$mState)
  96. {
  97. // Check for illegal sequences and codepoints
  98. // From Unicode 3.1, non-shortest form is illegal
  99. if (((2 == $mBytes) AND ($mUcs4 < 0x0080)) OR
  100. ((3 == $mBytes) AND ($mUcs4 < 0x0800)) OR
  101. ((4 == $mBytes) AND ($mUcs4 < 0x10000)) OR
  102. (4 < $mBytes) OR
  103. // From Unicode 3.2, surrogate characters are illegal
  104. (($mUcs4 & 0xFFFFF800) == 0xD800) OR
  105. // Codepoints outside the Unicode range are illegal
  106. ($mUcs4 > 0x10FFFF))
  107. {
  108. trigger_error('UTF8::to_unicode: Illegal sequence or codepoint in UTF-8 at byte '.$i, E_USER_WARNING);
  109. return FALSE;
  110. }
  111. if (0xFEFF != $mUcs4)
  112. {
  113. // BOM is legal but we don't want to output it
  114. $out[] = $mUcs4;
  115. }
  116. // Initialize UTF-8 cache
  117. $mState = 0;
  118. $mUcs4 = 0;
  119. $mBytes = 1;
  120. }
  121. }
  122. else
  123. {
  124. // ((0xC0 & (*in) != 0x80) AND (mState != 0))
  125. // Incomplete multi-octet sequence
  126. trigger_error('UTF8::to_unicode: Incomplete multi-octet sequence in UTF-8 at byte '.$i, E_USER_WARNING);
  127. return FALSE;
  128. }
  129. }
  130. }
  131. return $out;
  132. }