Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorder ByteSequence constants #364

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/ByteSequence.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,27 @@
interface ByteSequence
{
/**
* UTF-8 BOM sequence.
* UTF-32 BE BOM sequence.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the re-ordering is not the issue, the issue is in the implementation of bom_match the function should be updated by sorting the constant using their value length.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I too thought relying on a definition order in the interface was flaky but it was the path of least resistance. And, the test would guard it from becoming out of order in the future.

*/
const BOM_UTF8 = "\xEF\xBB\xBF";
const BOM_UTF32_BE = "\x00\x00\xFE\xFF";

/**
* UTF-16 BE BOM sequence.
* UTF-32 LE BOM sequence.
*/
const BOM_UTF16_BE = "\xFE\xFF";
const BOM_UTF32_LE = "\xFF\xFE\x00\x00";

/**
* UTF-16 LE BOM sequence.
* UTF-8 BOM sequence.
*/
const BOM_UTF16_LE = "\xFF\xFE";
const BOM_UTF8 = "\xEF\xBB\xBF";

/**
* UTF-32 BE BOM sequence.
* UTF-16 BE BOM sequence.
*/
const BOM_UTF32_BE = "\x00\x00\xFE\xFF";
const BOM_UTF16_BE = "\xFE\xFF";

/**
* UTF-32 LE BOM sequence.
* UTF-16 LE BOM sequence.
*/
const BOM_UTF32_LE = "\xFF\xFE\x00\x00";
const BOM_UTF16_LE = "\xFF\xFE";
}
10 changes: 7 additions & 3 deletions tests/ByteSequenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,22 @@ public function ByteSequenceMatchProvider(): array
'sequence' => 'foo bar',
'expected' => '',
],
'UTF8 BOM sequence' => [
'UTF-8 BOM sequence' => [
'sequence' => chr(239).chr(187).chr(191),
'expected' => ByteSequence::BOM_UTF8,
],
'UTF8 BOM sequence at the start of a text' => [
'UTF-8 BOM sequence at the start of a text' => [
'sequence' => ByteSequence::BOM_UTF8.'The quick brown fox jumps over the lazy dog',
'expected' => chr(239).chr(187).chr(191),
],
'UTF8 BOM sequence inside a text' => [
'UTF-8 BOM sequence inside a text' => [
'sequence' => 'The quick brown fox '.ByteSequence::BOM_UTF8.' jumps over the lazy dog',
'expected' => '',
],
'UTF-32 LE BOM sequence' => [
'sequence' => chr(255).chr(254).chr(0).chr(0),
'expected' => ByteSequence::BOM_UTF32_LE,
],
];
}
}