Skip to content

Commit

Permalink
Fix UTF32 LE detection
Browse files Browse the repository at this point in the history
  • Loading branch information
fab2s committed Aug 15, 2019
1 parent 26927a4 commit 1dfc62e
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/vendor
.php_cs.cache
.*.cache
composer.lock
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
language: php

dist: trusty

php:
- 5.6
- 7.0
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -26,12 +26,12 @@ 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

Contributions are welcome, do not hesitate to open issues and submit pull requests.

## 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).
12 changes: 6 additions & 6 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
# 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"
```

## 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

Contributions are welcome, do not hesitate to open issues and submit pull requests.

## 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).
12 changes: 10 additions & 2 deletions src/Bom.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down Expand Up @@ -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;
}
}
134 changes: 134 additions & 0 deletions tests/BomTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

/*
* This file is part of OpinHelpers.
* (c) Fabrice de Stefanis / https://github.com/fab2s/OpinHelpers
* This source file is licensed under the MIT license which you will
* find in the LICENSE file or at https://opensource.org/licenses/MIT
*/

namespace fab2s\Tests;

use fab2s\OpinHelpers\Bom;

/**
* Class BomTest
*/
class BomTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider extractData
*
* @param string $input
* @param string|null $bom
* @param string $encoding
*/
public function testExtract($input, $bom, $encoding)
{
$this->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;
}
}

0 comments on commit 1dfc62e

Please sign in to comment.