From eae901a9e27b30195ef51acc606bd97463555665 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Fri, 13 Dec 2019 11:00:25 -0500 Subject: [PATCH] Add new methods --- CHANGELOG | 10 ++++++++++ lib/Encoding/Encoding.php | 8 +++++++- lib/Encoding/GenericEncoding.php | 12 +++++++++++- lib/Encoding/SingleByteEncoding.php | 7 ++++++- lib/Encoding/XUserDefined.php | 7 ++++++- tests/bootstrap.php | 4 ++++ tests/cases/Encoding/TestBig5.php | 4 +++- tests/cases/Encoding/TestEUCKR.php | 4 +++- tests/cases/Encoding/TestGB18030.php | 4 +++- tests/cases/Encoding/TestSingleByte.php | 4 +++- tests/cases/Encoding/TestUTF16LE.php | 4 +++- tests/cases/Encoding/TestUTF8.php | 4 +++- tests/cases/Encoding/TestXUserDefined.php | 4 +++- tests/lib/DecoderTest.php | 11 ++++++++++- 14 files changed, 75 insertions(+), 12 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 8c81f04..cac36e0 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,13 @@ +Version 0.5.0 (2019-12-13) +========================== + +Breaking changes: +- Rename Encoding::len() to Encoding::lenChar() + +New features: +- Add Encoding::lenByte() method +- Add Encoding::eof() method + Version 0.4.0 (2018-09-15) ========================== diff --git a/lib/Encoding/Encoding.php b/lib/Encoding/Encoding.php index 3fe7019..5b7a731 100644 --- a/lib/Encoding/Encoding.php +++ b/lib/Encoding/Encoding.php @@ -64,11 +64,17 @@ interface Encoding { /** Retrieves the next $num code points from the string, without advancing the character pointer */ public function peekCode(int $num = 1): array; + /** Calculates the length of the string in bytes */ + public function lenByte(): int; + /** Calculates the length of the string in code points * * Note that this may involve processing to the end of the string */ - public function len(): int; + public function lenChar(): int; + + /** Returns whether the character pointer is at the end of the string */ + public function eof(): bool; /** Generates an iterator which steps through each character in the string */ public function chars(): \Generator; diff --git a/lib/Encoding/GenericEncoding.php b/lib/Encoding/GenericEncoding.php index 1a31de7..8b26626 100644 --- a/lib/Encoding/GenericEncoding.php +++ b/lib/Encoding/GenericEncoding.php @@ -122,11 +122,16 @@ trait GenericEncoding { return $out; } + /** Calculates the length of the string in bytes */ + public function lenByte(): int { + return $this->lenByte; + } + /** Calculates the length of the string in code points * * Note that this may involve processing to the end of the string */ - public function len(): int { + public function lenChar(): int { return $this->lenChar ?? (function() { $state = $this->stateSave(); while ($this->nextCode() !== false); @@ -136,6 +141,11 @@ trait GenericEncoding { })(); } + /** Returns whether the character pointer is at the end of the string */ + public function eof(): bool { + return $this->posByte >= $this->lenByte; + } + /** Generates an iterator which steps through each character in the string */ public function chars(): \Generator { while (($c = $this->nextChar()) !== "") { diff --git a/lib/Encoding/SingleByteEncoding.php b/lib/Encoding/SingleByteEncoding.php index b8bb3d1..9bdf996 100644 --- a/lib/Encoding/SingleByteEncoding.php +++ b/lib/Encoding/SingleByteEncoding.php @@ -101,7 +101,12 @@ abstract class SingleByteEncoding implements StatelessEncoding { * * Note that this may involve processing to the end of the string */ - public function len(): int { + public function lenChar(): int { return $this->lenByte; } + + /** Returns whether the character pointer is at the end of the string */ + public function eof(): bool { + return $this->posChar >= $this->lenByte; + } } diff --git a/lib/Encoding/XUserDefined.php b/lib/Encoding/XUserDefined.php index b4fd015..a3ffc54 100644 --- a/lib/Encoding/XUserDefined.php +++ b/lib/Encoding/XUserDefined.php @@ -88,7 +88,12 @@ class XUserDefined implements Encoding { * * Note that this may involve processing to the end of the string */ - public function len(): int { + public function lenChar(): int { return $this->lenByte; } + + /** Returns whether the character pointer is at the end of the string */ + public function eof(): bool { + return $this->posChar >= $this->lenByte; + } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index b2e4cfb..b2357fa 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -11,3 +11,7 @@ define(NS_BASE."BASE", dirname(__DIR__).DIRECTORY_SEPARATOR); ini_set("memory_limit", "-1"); error_reporting(\E_ALL); require_once BASE."vendor".DIRECTORY_SEPARATOR."autoload.php"; + +if (function_exists("xdebug_set_filter")) { + xdebug_set_filter(\XDEBUG_FILTER_CODE_COVERAGE, \XDEBUG_PATH_WHITELIST, [BASE."lib/"]); +} diff --git a/tests/cases/Encoding/TestBig5.php b/tests/cases/Encoding/TestBig5.php index 55f2933..85711de 100644 --- a/tests/cases/Encoding/TestBig5.php +++ b/tests/cases/Encoding/TestBig5.php @@ -76,6 +76,7 @@ class TestBig5 extends \MensBeam\Intl\Test\CoderDecoderTest { /** * @covers MensBeam\Intl\Encoding\Big5::posChar * @covers MensBeam\Intl\Encoding\Big5::posByte + * @covers MensBeam\Intl\Encoding\Big5::eof */ public function testTraversePastTheEndOfAString() { return parent::testTraversePastTheEndOfAString(); @@ -101,7 +102,8 @@ class TestBig5 extends \MensBeam\Intl\Test\CoderDecoderTest { /** * @dataProvider provideStrings - * @covers MensBeam\Intl\Encoding\Big5::len + * @covers MensBeam\Intl\Encoding\Big5::lenChar + * @covers MensBeam\Intl\Encoding\Big5::lenByte * @covers MensBeam\Intl\Encoding\Big5::stateSave * @covers MensBeam\Intl\Encoding\Big5::stateApply */ diff --git a/tests/cases/Encoding/TestEUCKR.php b/tests/cases/Encoding/TestEUCKR.php index 3d67109..6fc35a7 100644 --- a/tests/cases/Encoding/TestEUCKR.php +++ b/tests/cases/Encoding/TestEUCKR.php @@ -76,6 +76,7 @@ class TestEUCKR extends \MensBeam\Intl\Test\CoderDecoderTest { /** * @covers MensBeam\Intl\Encoding\EUCKR::posChar * @covers MensBeam\Intl\Encoding\EUCKR::posByte + * @covers MensBeam\Intl\Encoding\EUCKR::eof */ public function testTraversePastTheEndOfAString() { return parent::testTraversePastTheEndOfAString(); @@ -101,7 +102,8 @@ class TestEUCKR extends \MensBeam\Intl\Test\CoderDecoderTest { /** * @dataProvider provideStrings - * @covers MensBeam\Intl\Encoding\EUCKR::len + * @covers MensBeam\Intl\Encoding\EUCKR::lenChar + * @covers MensBeam\Intl\Encoding\EUCKR::lenByte * @covers MensBeam\Intl\Encoding\EUCKR::stateSave * @covers MensBeam\Intl\Encoding\EUCKR::stateApply */ diff --git a/tests/cases/Encoding/TestGB18030.php b/tests/cases/Encoding/TestGB18030.php index 24bf602..3e3a722 100644 --- a/tests/cases/Encoding/TestGB18030.php +++ b/tests/cases/Encoding/TestGB18030.php @@ -84,6 +84,7 @@ class TestGB18030 extends \MensBeam\Intl\Test\CoderDecoderTest { /** * @covers MensBeam\Intl\Encoding\GB18030::posChar * @covers MensBeam\Intl\Encoding\GB18030::posByte + * @covers MensBeam\Intl\Encoding\GB18030::eof */ public function testTraversePastTheEndOfAString() { return parent::testTraversePastTheEndOfAString(); @@ -109,7 +110,8 @@ class TestGB18030 extends \MensBeam\Intl\Test\CoderDecoderTest { /** * @dataProvider provideStrings - * @covers MensBeam\Intl\Encoding\GB18030::len + * @covers MensBeam\Intl\Encoding\GB18030::lenChar + * @covers MensBeam\Intl\Encoding\GB18030::lenByte * @covers MensBeam\Intl\Encoding\GB18030::stateSave * @covers MensBeam\Intl\Encoding\GB18030::stateApply */ diff --git a/tests/cases/Encoding/TestSingleByte.php b/tests/cases/Encoding/TestSingleByte.php index 4bbf5d8..7fa81b3 100644 --- a/tests/cases/Encoding/TestSingleByte.php +++ b/tests/cases/Encoding/TestSingleByte.php @@ -139,6 +139,7 @@ class TestSingleByte extends \MensBeam\Intl\Test\CoderDecoderTest { * @dataProvider provideClasses * @covers MensBeam\Intl\Encoding\SingleByteEncoding::posChar * @covers MensBeam\Intl\Encoding\SingleByteEncoding::posByte + * @covers MensBeam\Intl\Encoding\SingleByteEncoding::eof */ public function testTraversePastTheEndOfAString(string $class = SingleByteEncoding::class) { $this->testedClass = $class; @@ -173,7 +174,8 @@ class TestSingleByte extends \MensBeam\Intl\Test\CoderDecoderTest { /** * @dataProvider provideStrings - * @covers MensBeam\Intl\Encoding\SingleByteEncoding::len + * @covers MensBeam\Intl\Encoding\SingleByteEncoding::lenChar + * @covers MensBeam\Intl\Encoding\SingleByteEncoding::lenByte * @covers MensBeam\Intl\Encoding\SingleByteEncoding::stateSave * @covers MensBeam\Intl\Encoding\SingleByteEncoding::stateApply */ diff --git a/tests/cases/Encoding/TestUTF16LE.php b/tests/cases/Encoding/TestUTF16LE.php index a9bd16d..1424b44 100644 --- a/tests/cases/Encoding/TestUTF16LE.php +++ b/tests/cases/Encoding/TestUTF16LE.php @@ -67,6 +67,7 @@ class TestUTF16LE extends \MensBeam\Intl\Test\DecoderTest { /** * @covers MensBeam\Intl\Encoding\UTF16::posChar * @covers MensBeam\Intl\Encoding\UTF16::posByte + * @covers MensBeam\Intl\Encoding\UTF16::eof */ public function testTraversePastTheEndOfAString() { return parent::testTraversePastTheEndOfAString(); @@ -92,7 +93,8 @@ class TestUTF16LE extends \MensBeam\Intl\Test\DecoderTest { /** * @dataProvider provideStrings - * @covers MensBeam\Intl\Encoding\UTF16::len + * @covers MensBeam\Intl\Encoding\UTF16::lenChar + * @covers MensBeam\Intl\Encoding\UTF16::lenByte * @covers MensBeam\Intl\Encoding\UTF16::stateSave * @covers MensBeam\Intl\Encoding\UTF16::stateApply */ diff --git a/tests/cases/Encoding/TestUTF8.php b/tests/cases/Encoding/TestUTF8.php index 72638e0..6be0395 100644 --- a/tests/cases/Encoding/TestUTF8.php +++ b/tests/cases/Encoding/TestUTF8.php @@ -76,6 +76,7 @@ class TestUTF8 extends \MensBeam\Intl\Test\CoderDecoderTest { /** * @covers MensBeam\Intl\Encoding\UTF8::posChar * @covers MensBeam\Intl\Encoding\UTF8::posByte + * @covers MensBeam\Intl\Encoding\UTF8::eof */ public function testTraversePastTheEndOfAString() { return parent::testTraversePastTheEndOfAString(); @@ -101,7 +102,8 @@ class TestUTF8 extends \MensBeam\Intl\Test\CoderDecoderTest { /** * @dataProvider provideStrings - * @covers MensBeam\Intl\Encoding\UTF8::len + * @covers MensBeam\Intl\Encoding\UTF8::lenChar + * @covers MensBeam\Intl\Encoding\UTF8::lenByte * @covers MensBeam\Intl\Encoding\UTF8::stateSave * @covers MensBeam\Intl\Encoding\UTF8::stateApply */ diff --git a/tests/cases/Encoding/TestXUserDefined.php b/tests/cases/Encoding/TestXUserDefined.php index 85c915d..5d47dda 100644 --- a/tests/cases/Encoding/TestXUserDefined.php +++ b/tests/cases/Encoding/TestXUserDefined.php @@ -57,6 +57,7 @@ class TestXUserDefined extends \MensBeam\Intl\Test\DecoderTest { /** * @covers MensBeam\Intl\Encoding\XUserDefined::posChar * @covers MensBeam\Intl\Encoding\XUserDefined::posByte + * @covers MensBeam\Intl\Encoding\XUserDefined::eof */ public function testTraversePastTheEndOfAString() { return parent::testTraversePastTheEndOfAString(); @@ -82,7 +83,8 @@ class TestXUserDefined extends \MensBeam\Intl\Test\DecoderTest { /** * @dataProvider provideStrings - * @covers MensBeam\Intl\Encoding\XUserDefined::len + * @covers MensBeam\Intl\Encoding\XUserDefined::lenChar + * @covers MensBeam\Intl\Encoding\XUserDefined::lenByte * @covers MensBeam\Intl\Encoding\XUserDefined::stateSave * @covers MensBeam\Intl\Encoding\XUserDefined::stateApply */ diff --git a/tests/lib/DecoderTest.php b/tests/lib/DecoderTest.php index ae89559..56dcfa1 100644 --- a/tests/lib/DecoderTest.php +++ b/tests/lib/DecoderTest.php @@ -120,26 +120,32 @@ abstract class DecoderTest extends \PHPUnit\Framework\TestCase { $l = strlen($this->lowerA); $this->assertSame(0, $s->posChar()); $this->assertSame(0, $s->posByte()); + $this->assertFalse($s->eof()); $this->assertSame("a", $s->nextChar()); $this->assertSame(1, $s->posChar()); $this->assertSame($l, $s->posByte()); + $this->assertTrue($s->eof()); $this->assertSame("", $s->nextChar()); $this->assertSame(1, $s->posChar()); $this->assertSame($l, $s->posByte()); + $this->assertTrue($s->eof()); $s = new $class($this->lowerA); $this->assertSame(0, $s->posChar()); $this->assertSame(0, $s->posByte()); + $this->assertFalse($s->eof()); $this->assertSame(ord("a"), $s->nextCode()); $this->assertSame(1, $s->posChar()); $this->assertSame($l, $s->posByte()); + $this->assertTrue($s->eof()); $this->assertSame(false, $s->nextCode()); $this->assertSame(1, $s->posChar()); $this->assertSame($l, $s->posByte()); + $this->assertTrue($s->eof()); } public function testPeekAtCharacters() { @@ -220,7 +226,10 @@ abstract class DecoderTest extends \PHPUnit\Framework\TestCase { $posChar = $s->posChar(); $posByte = $s->posByte(); - $this->assertSame(sizeof($points), $s->len()); + $this->assertSame(sizeof($points), $s->lenChar()); + $this->assertSame($posChar, $s->posChar()); + $this->assertSame($posByte, $s->posByte()); + $this->assertSame(strlen($input), $s->lenByte()); $this->assertSame($posChar, $s->posChar()); $this->assertSame($posByte, $s->posByte()); }