From 1dfc62ee356c3e130786dce72695b04dc7db034b Mon Sep 17 00:00:00 2001 From: fab2s Date: Thu, 15 Aug 2019 15:11:32 +0200 Subject: [PATCH] Fix UTF32 LE detection --- .gitignore | 2 +- .travis.yml | 2 - README.md | 10 ++-- docs/index.md | 12 ++--- src/Bom.php | 12 ++++- tests/BomTest.php | 134 ++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 156 insertions(+), 16 deletions(-) create mode 100644 tests/BomTest.php diff --git a/.gitignore b/.gitignore index 29c269d..fbebd00 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ /vendor -.php_cs.cache +.*.cache composer.lock diff --git a/.travis.yml b/.travis.yml index 6a00632..b19fd96 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,5 @@ language: php -dist: trusty - php: - 5.6 - 7.0 diff --git a/README.md b/README.md index 236bca3..a414abb 100644 --- a/README.md +++ b/README.md @@ -2,13 +2,13 @@ [![Documentation Status](https://readthedocs.org/projects/opinhelpers/badge/?version=latest)](http://opinhelpers.readthedocs.io/en/latest/?badge=latest) [![Build Status](https://travis-ci.org/fab2s/OpinHelpers.svg?branch=master)](https://travis-ci.org/fab2s/OpinHelpers) [![Total Downloads](https://poser.pugx.org/fab2s/opinhelpers/downloads)](https://packagist.org/packages/fab2s/opinhelpers) [![Monthly Downloads](https://poser.pugx.org/fab2s/opinhelpers/d/monthly)](https://packagist.org/packages/fab2s/opinhelpers) [![Latest Stable Version](https://poser.pugx.org/fab2s/opinhelpers/v/stable)](https://packagist.org/packages/fab2s/opinhelpers) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/fab2s/OpinHelpers/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/fab2s/OpinHelpers/?branch=master) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat)](http://makeapullrequest.com) [![License](https://poser.pugx.org/fab2s/opinhelpers/license)](https://packagist.org/packages/fab2s/opinhelpers) -OpinHelpers is a bellow "Swiss Army Knife" level set of opinionated Helper libs (hence the [opin[h]el](https://en.wikipedia.org/wiki/Opinel_knife)^^) covering some of the most annoying aspects of php programing, such as UTF8 string manipulation, arbitrary precision Mathematics or properly locking a file. +`OpinHelpers` is a bellow "Swiss Army Knife" level set of opinionated Helper libs (hence the [opin[h]el](https://en.wikipedia.org/wiki/Opinel_knife)^^) covering some of the most annoying aspects of php programing, such as UTF8 string manipulation, arbitrary precision Mathematics or properly locking a file. -OpinHelpers libs are opinionated in several ways and do not aim at being universal, but they should hopefully be pretty useful in many IRL cases. +`OpinHelpers` libs are opinionated in several ways and do not aim at being universal, but they should hopefully be pretty useful in many IRL cases. ## Installation -OpinHelpers can be installed using composer : +`OpinHelpers` can be installed using composer : ``` composer require "fab2s/opinhelpers" @@ -26,7 +26,7 @@ composer require "fab2s/opinhelpers" ## Requirements -OpinHelpers is tested against php 5.6, 7.0, 7.1, 7.2 and 7.3. +`OpinHelpers` is tested against php 5.6, 7.0, 7.1, 7.2 and 7.3. ## Contributing @@ -34,4 +34,4 @@ Contributions are welcome, do not hesitate to open issues and submit pull reques ## License -SoUuid is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT). \ No newline at end of file +`OpinHelpers` is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT). \ No newline at end of file diff --git a/docs/index.md b/docs/index.md index 641c613..0cdf41f 100644 --- a/docs/index.md +++ b/docs/index.md @@ -1,14 +1,14 @@ # OpinHelpers -[![Build Status](https://travis-ci.org/fab2s/OpinHelpers.svg?branch=master)](https://travis-ci.org/fab2s/OpinHelpers) [![Latest Stable Version](https://poser.pugx.org/fab2s/opinhelpers/v/stable)](https://packagist.org/packages/fab2s/opinhelpers) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/fab2s/OpinHelpers/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/fab2s/OpinHelpers/?branch=master) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat)](http://makeapullrequest.com) [![License](https://poser.pugx.org/fab2s/opinhelpers/license)](https://packagist.org/packages/fab2s/opinhelpers) +[![Documentation Status](https://readthedocs.org/projects/opinhelpers/badge/?version=latest)](http://opinhelpers.readthedocs.io/en/latest/?badge=latest) [![Build Status](https://travis-ci.org/fab2s/OpinHelpers.svg?branch=master)](https://travis-ci.org/fab2s/OpinHelpers) [![Total Downloads](https://poser.pugx.org/fab2s/opinhelpers/downloads)](https://packagist.org/packages/fab2s/opinhelpers) [![Monthly Downloads](https://poser.pugx.org/fab2s/opinhelpers/d/monthly)](https://packagist.org/packages/fab2s/opinhelpers) [![Latest Stable Version](https://poser.pugx.org/fab2s/opinhelpers/v/stable)](https://packagist.org/packages/fab2s/opinhelpers) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/fab2s/OpinHelpers/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/fab2s/OpinHelpers/?branch=master) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat)](http://makeapullrequest.com) [![License](https://poser.pugx.org/fab2s/opinhelpers/license)](https://packagist.org/packages/fab2s/opinhelpers) -OpinHelpers is a bellow "Swiss Army Knife" level set of opinionated Helper libs (hence the [opin[h]el](https://en.wikipedia.org/wiki/Opinel_knife)^^) covering some of the most annoying aspects of php programing, such as UTF8 string manipulation, arbitrary precision Mathematics or properly locking a file. +`OpinHelpers` is a bellow "Swiss Army Knife" level set of opinionated Helper libs (hence the [opin[h]el](https://en.wikipedia.org/wiki/Opinel_knife)^^) covering some of the most annoying aspects of php programing, such as UTF8 string manipulation, arbitrary precision Mathematics or properly locking a file. -OpinHelpers libs are opinionated in several ways and do not aim at being universal, but they should hopefully be pretty useful in many IRL cases. +`OpinHelpers` libs are opinionated in several ways and do not aim at being universal, but they should hopefully be pretty useful in many IRL cases. ## Installation -OpinHelpers can be installed using composer : +`OpinHelpers` can be installed using composer : ``` composer require "fab2s/opinhelpers" @@ -16,7 +16,7 @@ composer require "fab2s/opinhelpers" ## Requirements -OpinHelpers is tested against php 5.6, 7.0, 7.1, 7.2 and hhvm. +`OpinHelpers` is tested against php 5.6, 7.0, 7.1, 7.2 and 7.3. ## Contributing @@ -24,4 +24,4 @@ Contributions are welcome, do not hesitate to open issues and submit pull reques ## License -SoUuid is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT). +`OpinHelpers` is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT). \ No newline at end of file diff --git a/src/Bom.php b/src/Bom.php index 196086e..3cc9802 100644 --- a/src/Bom.php +++ b/src/Bom.php @@ -45,9 +45,9 @@ class Bom const ENC_UTF32_LE = 'UTF-32LE'; /** - * UTF8 | UTF16_BE | UTF16_LE | UTF32_BE | UTF32_LE + * UTF8 | UTF16_BE | UTF32_LE | UTF16_LE | UTF32_BE */ - const BOM_REGEX = '\xEF\xBB\xBF|\xFE\xFF|\xFF\xFE|\x00\x00\xFE\xFF|\xFF\xFE\x00\x00'; + const BOM_REGEX = '\xEF\xBB\xBF|\xFE\xFF|\xFF\xFE\x00\x00|\xFF\xFE|\x00\x00\xFE\xFF'; /** * @var string[] @@ -110,4 +110,12 @@ public static function getEncodingBom($encoding) { return isset(static::$boms[$encoding]) ? static::$boms[$encoding] : null; } + + /** + * @return array + */ + public static function getBoms() + { + return static::$boms; + } } diff --git a/tests/BomTest.php b/tests/BomTest.php new file mode 100644 index 0000000..d27d0db --- /dev/null +++ b/tests/BomTest.php @@ -0,0 +1,134 @@ +assertSame(Bom::extract($input), $bom, $encoding); + } + + /** + * @dataProvider dropData + * + * @param string $input + * @param string|null $bom + * @param string $encoding + */ + public function testDrop($input, $bom, $encoding) + { + $this->assertSame(Bom::drop($input), $bom, $encoding); + } + + /** + * @return array + */ + public function extractData() + { + $string = "I am an irrelevant string\n which content does not matter"; + $result = []; + + foreach (Bom::getBoms() as $encoding => $bom) { + $result = [ + // no BOM case + [ + $string, + null, + $encoding, + ], + ]; + + $result[] = [ + "$bom$string", + $bom, + $encoding, + ]; + + $result[] = [ + "$bom $string", + $bom, + $encoding, + ]; + + $result[] = [ + "$bom\n$string", + $bom, + $encoding, + ]; + + $result[] = [ + " $bom$string", + null, + $encoding, + ]; + } + + return $result; + } + + /** + * @return array + */ + public function dropData() + { + $string = "I am yet another irrelevant string\n which content does not matter"; + $result = []; + + foreach (Bom::getBoms() as $encoding => $bom) { + $result = [ + // no BOM case + [ + $string, + $string, + $encoding, + ], + ]; + + $result[] = [ + "$bom$string", + $string, + $encoding, + ]; + + $result[] = [ + "$bom $string", + " $string", + $encoding, + ]; + + $result[] = [ + "$bom\n$string", + "\n$string", + $encoding, + ]; + + $result[] = [ + " $bom$string", + " $bom$string", + $encoding, + ]; + } + + return $result; + } +}