Fix bug in style changes for the Use cases on the live site.
[yaffs-website] / vendor / phpunit / phpunit / src / Util / String.php
1 <?php
2 /*
3  * This file is part of PHPUnit.
4  *
5  * (c) Sebastian Bergmann <sebastian@phpunit.de>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * String helpers.
13  *
14  * @since Class available since Release 3.6.0
15  */
16 class PHPUnit_Util_String
17 {
18     /**
19      * Converts a string to UTF-8 encoding.
20      *
21      * @param string $string
22      *
23      * @return string
24      */
25     public static function convertToUtf8($string)
26     {
27         if (!self::isUtf8($string)) {
28             if (function_exists('mb_convert_encoding')) {
29                 $string = mb_convert_encoding($string, 'UTF-8');
30             } else {
31                 $string = utf8_encode($string);
32             }
33         }
34
35         return $string;
36     }
37
38     /**
39      * Checks a string for UTF-8 encoding.
40      *
41      * @param string $string
42      *
43      * @return bool
44      */
45     protected static function isUtf8($string)
46     {
47         $length = strlen($string);
48
49         for ($i = 0; $i < $length; $i++) {
50             if (ord($string[$i]) < 0x80) {
51                 $n = 0;
52             } elseif ((ord($string[$i]) & 0xE0) == 0xC0) {
53                 $n = 1;
54             } elseif ((ord($string[$i]) & 0xF0) == 0xE0) {
55                 $n = 2;
56             } elseif ((ord($string[$i]) & 0xF0) == 0xF0) {
57                 $n = 3;
58             } else {
59                 return false;
60             }
61
62             for ($j = 0; $j < $n; $j++) {
63                 if ((++$i == $length) || ((ord($string[$i]) & 0xC0) != 0x80)) {
64                     return false;
65                 }
66             }
67         }
68
69         return true;
70     }
71 }