| 29 | | if (!is_callable('RandomCompat_strlen')) { |
| 30 | | if ( |
| 31 | | defined('MB_OVERLOAD_STRING') && |
| 32 | | ini_get('mbstring.func_overload') & MB_OVERLOAD_STRING |
| 33 | | ) { |
| 34 | | /** |
| 35 | | * strlen() implementation that isn't brittle to mbstring.func_overload |
| 36 | | * |
| 37 | | * This version uses mb_strlen() in '8bit' mode to treat strings as raw |
| 38 | | * binary rather than UTF-8, ISO-8859-1, etc |
| 39 | | * |
| 40 | | * @param string $binary_string |
| 41 | | * |
| 42 | | * @throws TypeError |
| 43 | | * |
| 44 | | * @return int |
| 45 | | */ |
| 46 | | function RandomCompat_strlen($binary_string) |
| 47 | | { |
| 48 | | if (!is_string($binary_string)) { |
| 49 | | throw new TypeError( |
| 50 | | 'RandomCompat_strlen() expects a string' |
| 51 | | ); |
| 52 | | } |
| | 29 | if ( ! is_callable( 'RandomCompat_strlen' ) ) { |
| | 30 | if ( defined( 'MB_OVERLOAD_STRING' ) && ini_get( 'mbstring.func_overload' ) & MB_OVERLOAD_STRING ) { |
| | 31 | /** |
| | 32 | * strlen() implementation that isn't brittle to mbstring.func_overload |
| | 33 | * |
| | 34 | * This version uses mb_strlen() in '8bit' mode to treat strings as raw |
| | 35 | * binary rather than UTF-8, ISO-8859-1, etc |
| | 36 | * |
| | 37 | * @param string $binary_string |
| | 38 | * |
| | 39 | * @throws TypeError |
| | 40 | * |
| | 41 | * @return int |
| | 42 | */ |
| | 43 | function RandomCompat_strlen( $binary_string ) { |
| | 44 | if ( ! is_string( $binary_string ) ) { |
| | 45 | throw new TypeError( |
| | 46 | 'RandomCompat_strlen() expects a string' |
| | 47 | ); |
| | 48 | } |
| 54 | | return (int) mb_strlen($binary_string, '8bit'); |
| 55 | | } |
| | 50 | return (int) mb_strlen( $binary_string, '8bit' ); |
| | 51 | } |
| | 52 | } else { |
| | 53 | /** |
| | 54 | * strlen() implementation that isn't brittle to mbstring.func_overload |
| | 55 | * |
| | 56 | * This version just used the default strlen() |
| | 57 | * |
| | 58 | * @param string $binary_string |
| | 59 | * |
| | 60 | * @throws TypeError |
| | 61 | * |
| | 62 | * @return int |
| | 63 | */ |
| | 64 | function RandomCompat_strlen( $binary_string ) { |
| | 65 | if ( ! is_string( $binary_string ) ) { |
| | 66 | throw new TypeError( |
| | 67 | 'RandomCompat_strlen() expects a string' |
| | 68 | ); |
| | 69 | } |
| 81 | | if (!is_callable('RandomCompat_substr')) { |
| | 76 | if ( ! is_callable( 'RandomCompat_substr' ) ) { |
| | 77 | if ( |
| | 78 | defined( 'MB_OVERLOAD_STRING' ) && ini_get( 'mbstring.func_overload' ) & MB_OVERLOAD_STRING ) { |
| | 79 | /** |
| | 80 | * substr() implementation that isn't brittle to mbstring.func_overload |
| | 81 | * |
| | 82 | * This version uses mb_substr() in '8bit' mode to treat strings as raw |
| | 83 | * binary rather than UTF-8, ISO-8859-1, etc |
| | 84 | * |
| | 85 | * @param string $binary_string |
| | 86 | * @param int $start |
| | 87 | * @param int $length (optional) |
| | 88 | * |
| | 89 | * @throws TypeError |
| | 90 | * |
| | 91 | * @return string |
| | 92 | */ |
| | 93 | function RandomCompat_substr( $binary_string, $start, $length = null ) { |
| | 94 | if ( ! is_string( $binary_string ) ) { |
| | 95 | throw new TypeError( |
| | 96 | 'RandomCompat_substr(): First argument should be a string' |
| | 97 | ); |
| | 98 | } |
| 83 | | if ( |
| 84 | | defined('MB_OVERLOAD_STRING') |
| 85 | | && |
| 86 | | ini_get('mbstring.func_overload') & MB_OVERLOAD_STRING |
| 87 | | ) { |
| 88 | | /** |
| 89 | | * substr() implementation that isn't brittle to mbstring.func_overload |
| 90 | | * |
| 91 | | * This version uses mb_substr() in '8bit' mode to treat strings as raw |
| 92 | | * binary rather than UTF-8, ISO-8859-1, etc |
| 93 | | * |
| 94 | | * @param string $binary_string |
| 95 | | * @param int $start |
| 96 | | * @param int $length (optional) |
| 97 | | * |
| 98 | | * @throws TypeError |
| 99 | | * |
| 100 | | * @return string |
| 101 | | */ |
| 102 | | function RandomCompat_substr($binary_string, $start, $length = null) |
| 103 | | { |
| 104 | | if (!is_string($binary_string)) { |
| 105 | | throw new TypeError( |
| 106 | | 'RandomCompat_substr(): First argument should be a string' |
| 107 | | ); |
| 108 | | } |
| | 100 | if ( ! is_int( $start ) ) { |
| | 101 | throw new TypeError( |
| | 102 | 'RandomCompat_substr(): Second argument should be an integer' |
| | 103 | ); |
| | 104 | } |
| 110 | | if (!is_int($start)) { |
| 111 | | throw new TypeError( |
| 112 | | 'RandomCompat_substr(): Second argument should be an integer' |
| 113 | | ); |
| 114 | | } |
| | 106 | if ( null === $length ) { |
| | 107 | /** |
| | 108 | * mb_substr($str, 0, NULL, '8bit') returns an empty string on |
| | 109 | * PHP 5.3, so we have to find the length ourselves. |
| | 110 | */ |
| | 111 | $length = RandomCompat_strlen( $binary_string ) - $start; |
| | 112 | } elseif ( ! is_int( $length ) ) { |
| | 113 | throw new TypeError( |
| | 114 | 'RandomCompat_substr(): Third argument should be an integer, or omitted' |
| | 115 | ); |
| | 116 | } |
| 116 | | if ($length === null) { |
| 117 | | /** |
| 118 | | * mb_substr($str, 0, NULL, '8bit') returns an empty string on |
| 119 | | * PHP 5.3, so we have to find the length ourselves. |
| 120 | | */ |
| 121 | | $length = RandomCompat_strlen($binary_string) - $start; |
| 122 | | } elseif (!is_int($length)) { |
| 123 | | throw new TypeError( |
| 124 | | 'RandomCompat_substr(): Third argument should be an integer, or omitted' |
| 125 | | ); |
| 126 | | } |
| | 118 | // Consistency with PHP's behavior |
| | 119 | if ( $start === RandomCompat_strlen( $binary_string ) && $length === 0 ) { |
| | 120 | return ''; |
| | 121 | } |
| | 122 | if ( $start > RandomCompat_strlen( $binary_string ) ) { |
| | 123 | return ''; |
| | 124 | } |
| 128 | | // Consistency with PHP's behavior |
| 129 | | if ($start === RandomCompat_strlen($binary_string) && $length === 0) { |
| 130 | | return ''; |
| 131 | | } |
| 132 | | if ($start > RandomCompat_strlen($binary_string)) { |
| 133 | | return ''; |
| 134 | | } |
| | 126 | return (string) mb_substr( $binary_string, $start, $length, '8bit' ); |
| | 127 | } |
| | 128 | } else { |
| | 129 | /** |
| | 130 | * substr() implementation that isn't brittle to mbstring.func_overload |
| | 131 | * |
| | 132 | * This version just uses the default substr() |
| | 133 | * |
| | 134 | * @param string $binary_string |
| | 135 | * @param int $start |
| | 136 | * @param int $length (optional) |
| | 137 | * |
| | 138 | * @throws TypeError |
| | 139 | * |
| | 140 | * @return string |
| | 141 | */ |
| | 142 | function RandomCompat_substr( $binary_string, $start, $length = null ) { |
| | 143 | if ( ! is_string( $binary_string ) ) { |
| | 144 | throw new TypeError( |
| | 145 | 'RandomCompat_substr(): First argument should be a string' |
| | 146 | ); |
| | 147 | } |
| 141 | | /** |
| 142 | | * substr() implementation that isn't brittle to mbstring.func_overload |
| 143 | | * |
| 144 | | * This version just uses the default substr() |
| 145 | | * |
| 146 | | * @param string $binary_string |
| 147 | | * @param int $start |
| 148 | | * @param int $length (optional) |
| 149 | | * |
| 150 | | * @throws TypeError |
| 151 | | * |
| 152 | | * @return string |
| 153 | | */ |
| 154 | | function RandomCompat_substr($binary_string, $start, $length = null) |
| 155 | | { |
| 156 | | if (!is_string($binary_string)) { |
| 157 | | throw new TypeError( |
| 158 | | 'RandomCompat_substr(): First argument should be a string' |
| 159 | | ); |
| 160 | | } |
| | 162 | return (string) substr( $binary_string, $start, $length ); |
| | 163 | } |
| 162 | | if (!is_int($start)) { |
| 163 | | throw new TypeError( |
| 164 | | 'RandomCompat_substr(): Second argument should be an integer' |
| 165 | | ); |
| 166 | | } |
| 167 | | |
| 168 | | if ($length !== null) { |
| 169 | | if (!is_int($length)) { |
| 170 | | throw new TypeError( |
| 171 | | 'RandomCompat_substr(): Third argument should be an integer, or omitted' |
| 172 | | ); |
| 173 | | } |
| 174 | | |
| 175 | | return (string) substr($binary_string, $start, $length); |
| 176 | | } |
| 177 | | |
| 178 | | return (string) substr($binary_string, $start); |
| 179 | | } |
| 180 | | } |
| 181 | | } |
| | 165 | return (string) substr( $binary_string, $start ); |
| | 166 | } |
| | 167 | } |
| | 168 | } |
| | 169 | No newline at end of file |