UtfNormalUtil.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
  3. # http://www.mediawiki.org/
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License along
  16. # with this program; if not, write to the Free Software Foundation, Inc.,
  17. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. # http://www.gnu.org/copyleft/gpl.html
  19. /**
  20. * Some of these functions are adapted from places in MediaWiki.
  21. * Should probably merge them for consistency.
  22. *
  23. * @ingroup UtfNormal
  24. * @public
  25. */
  26. /** */
  27. require_once dirname(__FILE__).'/UtfNormalDefines.php';
  28. /**
  29. * Return UTF-8 sequence for a given Unicode code point.
  30. * May die if fed out of range data.
  31. *
  32. * @param $codepoint Integer:
  33. * @return String
  34. * @public
  35. */
  36. function codepointToUtf8( $codepoint ) {
  37. if($codepoint < 0x80) return chr($codepoint);
  38. if($codepoint < 0x800) return chr($codepoint >> 6 & 0x3f | 0xc0) .
  39. chr($codepoint & 0x3f | 0x80);
  40. if($codepoint < 0x10000) return chr($codepoint >> 12 & 0x0f | 0xe0) .
  41. chr($codepoint >> 6 & 0x3f | 0x80) .
  42. chr($codepoint & 0x3f | 0x80);
  43. if($codepoint < 0x110000) return chr($codepoint >> 18 & 0x07 | 0xf0) .
  44. chr($codepoint >> 12 & 0x3f | 0x80) .
  45. chr($codepoint >> 6 & 0x3f | 0x80) .
  46. chr($codepoint & 0x3f | 0x80);
  47. echo "Asked for code outside of range ($codepoint)\n";
  48. die( -1 );
  49. }
  50. /**
  51. * Take a series of space-separated hexadecimal numbers representing
  52. * Unicode code points and return a UTF-8 string composed of those
  53. * characters. Used by UTF-8 data generation and testing routines.
  54. *
  55. * @param $sequence String
  56. * @return String
  57. * @private
  58. */
  59. function hexSequenceToUtf8( $sequence ) {
  60. $utf = '';
  61. foreach( explode( ' ', $sequence ) as $hex ) {
  62. $n = hexdec( $hex );
  63. $utf .= codepointToUtf8( $n );
  64. }
  65. return $utf;
  66. }
  67. /**
  68. * Take a UTF-8 string and return a space-separated series of hex
  69. * numbers representing Unicode code points. For debugging.
  70. *
  71. * @param $str String: UTF-8 string.
  72. * @return string
  73. * @private
  74. */
  75. function utf8ToHexSequence( $str ) {
  76. return rtrim( preg_replace( '/(.)/uSe',
  77. 'sprintf("%04x ", utf8ToCodepoint("$1"))',
  78. $str ) );
  79. }
  80. /**
  81. * Determine the Unicode codepoint of a single-character UTF-8 sequence.
  82. * Does not check for invalid input data.
  83. *
  84. * @param $char String
  85. * @return Integer
  86. * @public
  87. */
  88. function utf8ToCodepoint( $char ) {
  89. # Find the length
  90. $z = ord( $char{0} );
  91. if ( $z & 0x80 ) {
  92. $length = 0;
  93. while ( $z & 0x80 ) {
  94. $length++;
  95. $z <<= 1;
  96. }
  97. } else {
  98. $length = 1;
  99. }
  100. if ( $length != strlen( $char ) ) {
  101. return false;
  102. }
  103. if ( $length == 1 ) {
  104. return ord( $char );
  105. }
  106. # Mask off the length-determining bits and shift back to the original location
  107. $z &= 0xff;
  108. $z >>= $length;
  109. # Add in the free bits from subsequent bytes
  110. for ( $i=1; $i<$length; $i++ ) {
  111. $z <<= 6;
  112. $z |= ord( $char{$i} ) & 0x3f;
  113. }
  114. return $z;
  115. }
  116. /**
  117. * Escape a string for inclusion in a PHP single-quoted string literal.
  118. *
  119. * @param $string String: string to be escaped.
  120. * @return String: escaped string.
  121. * @public
  122. */
  123. function escapeSingleString( $string ) {
  124. return strtr( $string,
  125. array(
  126. '\\' => '\\\\',
  127. '\'' => '\\\''
  128. ));
  129. }