Debian Codegraph @

vendor/wikimedia/utfnormal/src/Utils.php

raw
1 <?php
2 namespace UtfNormal;
3
4 use InvalidArgumentException;
5
6 /**
7 * Some of these functions are adapted from places in MediaWiki.
8 * Should probably merge them for consistency.
9 *
10 * Copyright © 2004 Brion Vibber <[email protected]>
11 * https://www.mediawiki.org/
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 * http://www.gnu.org/copyleft/gpl.html
27 *
28 * @file
29 * @ingroup UtfNormal
30 */
31
32 class Utils {
33 /**
34 * Return UTF-8 sequence for a given Unicode code point.
35 *
36 * @param int $codepoint
37 * @return string
38 * @throws InvalidArgumentException if fed out of range data.
39 */
40 public static function codepointToUtf8( $codepoint ) {
41 // In PHP 7.2, mb_chr is buggy when $codepoint is 0 (null byte)
42 if ( $codepoint === 0 ) {
43 return "\u{0000}";
44 }
45 $char = mb_chr( $codepoint );
46 if ( $char === false ) {
47 throw new InvalidArgumentException( "Asked for code outside of range ($codepoint)" );
48 }
49
50 return $char;
51 }
52
53 /**
54 * Take a series of space-separated hexadecimal numbers representing
55 * Unicode code points and return a UTF-8 string composed of those
56 * characters. Used by UTF-8 data generation and testing routines.
57 *
58 * @param string $sequence
59 * @return string
60 * @throws InvalidArgumentException if fed out of range data.
61 * @private Used in tests and data table generation
62 */
63 public static function hexSequenceToUtf8( $sequence ) {
64 $utf = '';
65 foreach ( explode( ' ', $sequence ) as $hex ) {
66 $n = hexdec( $hex );
67 $utf .= self::codepointToUtf8( $n );
68 }
69
70 return $utf;
71 }
72
73 /**
74 * Take a UTF-8 string and return a space-separated series of hex
75 * numbers representing Unicode code points. For debugging.
76 *
77 * @param string $str UTF-8 string.
78 * @return string
79 * @private
80 */
81 private static function utf8ToHexSequence( $str ) {
82 $buf = '';
83 foreach ( preg_split( '//u', $str, -1, PREG_SPLIT_NO_EMPTY ) as $cp ) {
84 $buf .= sprintf( '%04x ', mb_ord( $cp ) );
85 }
86
87 return rtrim( $buf );
88 }
89
90 /**
91 * Determine the Unicode codepoint of a single-character UTF-8 sequence.
92 * Does not check for invalid input data.
93 *
94 * @deprecated since 2.1, use mb_ord()
95 *
96 * @param string $char
97 * @return int|false
98 */
99 public static function utf8ToCodepoint( $char ) {
100 return mb_strlen( $char ) > 1 ? false : mb_ord( $char );
101 }
102
103 /**
104 * Escape a string for inclusion in a PHP single-quoted string literal.
105 *
106 * @param string $string String to be escaped.
107 * @return string Escaped string.
108 */
109 public static function escapeSingleString( $string ) {
110 return strtr(
111 $string,
112 [
113 '\\' => '\\\\',
114 '\'' => '\\\''
115 ]
116 );
117 }
118 }