Skip to content

Commit 1dfc62e

Browse files
committed
Fix UTF32 LE detection
1 parent 26927a4 commit 1dfc62e

6 files changed

Lines changed: 156 additions & 16 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
/vendor
2-
.php_cs.cache
2+
.*.cache
33
composer.lock

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
language: php
22

3-
dist: trusty
4-
53
php:
64
- 5.6
75
- 7.0

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
[![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)
44

5-
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.
5+
`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.
66

7-
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.
7+
`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.
88

99
## Installation
1010

11-
OpinHelpers can be installed using composer :
11+
`OpinHelpers` can be installed using composer :
1212

1313
```
1414
composer require "fab2s/opinhelpers"
@@ -26,12 +26,12 @@ composer require "fab2s/opinhelpers"
2626

2727
## Requirements
2828

29-
OpinHelpers is tested against php 5.6, 7.0, 7.1, 7.2 and 7.3.
29+
`OpinHelpers` is tested against php 5.6, 7.0, 7.1, 7.2 and 7.3.
3030

3131
## Contributing
3232

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

3535
## License
3636

37-
SoUuid is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).
37+
`OpinHelpers` is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).

docs/index.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
# OpinHelpers
22

3-
[![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)
3+
[![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)
44

5-
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.
5+
`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.
66

7-
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.
7+
`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.
88

99
## Installation
1010

11-
OpinHelpers can be installed using composer :
11+
`OpinHelpers` can be installed using composer :
1212

1313
```
1414
composer require "fab2s/opinhelpers"
1515
```
1616

1717
## Requirements
1818

19-
OpinHelpers is tested against php 5.6, 7.0, 7.1, 7.2 and hhvm.
19+
`OpinHelpers` is tested against php 5.6, 7.0, 7.1, 7.2 and 7.3.
2020

2121
## Contributing
2222

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

2525
## License
2626

27-
SoUuid is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).
27+
`OpinHelpers` is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).

src/Bom.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ class Bom
4545
const ENC_UTF32_LE = 'UTF-32LE';
4646

4747
/**
48-
* UTF8 | UTF16_BE | UTF16_LE | UTF32_BE | UTF32_LE
48+
* UTF8 | UTF16_BE | UTF32_LE | UTF16_LE | UTF32_BE
4949
*/
50-
const BOM_REGEX = '\xEF\xBB\xBF|\xFE\xFF|\xFF\xFE|\x00\x00\xFE\xFF|\xFF\xFE\x00\x00';
50+
const BOM_REGEX = '\xEF\xBB\xBF|\xFE\xFF|\xFF\xFE\x00\x00|\xFF\xFE|\x00\x00\xFE\xFF';
5151

5252
/**
5353
* @var string[]
@@ -110,4 +110,12 @@ public static function getEncodingBom($encoding)
110110
{
111111
return isset(static::$boms[$encoding]) ? static::$boms[$encoding] : null;
112112
}
113+
114+
/**
115+
* @return array
116+
*/
117+
public static function getBoms()
118+
{
119+
return static::$boms;
120+
}
113121
}

tests/BomTest.php

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
3+
/*
4+
* This file is part of OpinHelpers.
5+
* (c) Fabrice de Stefanis / https://github.com/fab2s/OpinHelpers
6+
* This source file is licensed under the MIT license which you will
7+
* find in the LICENSE file or at https://opensource.org/licenses/MIT
8+
*/
9+
10+
namespace fab2s\Tests;
11+
12+
use fab2s\OpinHelpers\Bom;
13+
14+
/**
15+
* Class BomTest
16+
*/
17+
class BomTest extends \PHPUnit_Framework_TestCase
18+
{
19+
/**
20+
* @dataProvider extractData
21+
*
22+
* @param string $input
23+
* @param string|null $bom
24+
* @param string $encoding
25+
*/
26+
public function testExtract($input, $bom, $encoding)
27+
{
28+
$this->assertSame(Bom::extract($input), $bom, $encoding);
29+
}
30+
31+
/**
32+
* @dataProvider dropData
33+
*
34+
* @param string $input
35+
* @param string|null $bom
36+
* @param string $encoding
37+
*/
38+
public function testDrop($input, $bom, $encoding)
39+
{
40+
$this->assertSame(Bom::drop($input), $bom, $encoding);
41+
}
42+
43+
/**
44+
* @return array
45+
*/
46+
public function extractData()
47+
{
48+
$string = "I am an irrelevant string\n which content does not matter";
49+
$result = [];
50+
51+
foreach (Bom::getBoms() as $encoding => $bom) {
52+
$result = [
53+
// no BOM case
54+
[
55+
$string,
56+
null,
57+
$encoding,
58+
],
59+
];
60+
61+
$result[] = [
62+
"$bom$string",
63+
$bom,
64+
$encoding,
65+
];
66+
67+
$result[] = [
68+
"$bom $string",
69+
$bom,
70+
$encoding,
71+
];
72+
73+
$result[] = [
74+
"$bom\n$string",
75+
$bom,
76+
$encoding,
77+
];
78+
79+
$result[] = [
80+
" $bom$string",
81+
null,
82+
$encoding,
83+
];
84+
}
85+
86+
return $result;
87+
}
88+
89+
/**
90+
* @return array
91+
*/
92+
public function dropData()
93+
{
94+
$string = "I am yet another irrelevant string\n which content does not matter";
95+
$result = [];
96+
97+
foreach (Bom::getBoms() as $encoding => $bom) {
98+
$result = [
99+
// no BOM case
100+
[
101+
$string,
102+
$string,
103+
$encoding,
104+
],
105+
];
106+
107+
$result[] = [
108+
"$bom$string",
109+
$string,
110+
$encoding,
111+
];
112+
113+
$result[] = [
114+
"$bom $string",
115+
" $string",
116+
$encoding,
117+
];
118+
119+
$result[] = [
120+
"$bom\n$string",
121+
"\n$string",
122+
$encoding,
123+
];
124+
125+
$result[] = [
126+
" $bom$string",
127+
" $bom$string",
128+
$encoding,
129+
];
130+
}
131+
132+
return $result;
133+
}
134+
}

0 commit comments

Comments
 (0)