From cd7fbc3919881978fddba84ce097542ab7c5bf1a Mon Sep 17 00:00:00 2001 From: Lars Moelleken Date: Sat, 24 Dec 2016 14:41:52 +0100 Subject: [PATCH] [~]: optimize "UTF8::is_binary()" + more tests --- src/voku/helper/UTF8.php | 16 ++++++---------- tests/Utf8GlobalTest.php | 12 ++++++++++++ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/voku/helper/UTF8.php b/src/voku/helper/UTF8.php index e9846274..c08a96c3 100644 --- a/src/voku/helper/UTF8.php +++ b/src/voku/helper/UTF8.php @@ -2737,20 +2737,16 @@ public static function is_base64($str) */ public static function is_binary($input) { + if (preg_match('~^[01]+$~', $input)) { + return true; + } $testLength = strlen($input); - - if ( - preg_match('~^[01]+$~', $input) - || - substr_count($input, "\x00") > 0 - || - ($testLength ? substr_count($input, '^ -~') / $testLength > 0.3 : 1 === 0) - ) { + if (substr_count($input, "\x0") / $testLength > 0.3) { return true; - } else { - return false; } + + return false; } /** diff --git a/tests/Utf8GlobalTest.php b/tests/Utf8GlobalTest.php index 3238c861..32fb9c95 100644 --- a/tests/Utf8GlobalTest.php +++ b/tests/Utf8GlobalTest.php @@ -1365,8 +1365,20 @@ public function testIsBinary() 'öäü' => false, '' => false, '1' => false, + '01010101' => true, decbin(324546) => true, 01 => true, + 1020304 => false, + 01020304 => false, + 11020304 => false, + '1010101' => true, + 11111111 => true, + 00000000 => true, + "\x00\x01" => true, + "\x01\x00" => true, + "\x01\x02" => false, + "\x01\x00ab" => false, // <= 30% binary + "\x01\x00a" => true, // >= 30% binary ); foreach ($tests as $before => $after) {