Browse Source

Add new methods

span
J. King 4 years ago
parent
commit
eae901a9e2
  1. 10
      CHANGELOG
  2. 8
      lib/Encoding/Encoding.php
  3. 12
      lib/Encoding/GenericEncoding.php
  4. 7
      lib/Encoding/SingleByteEncoding.php
  5. 7
      lib/Encoding/XUserDefined.php
  6. 4
      tests/bootstrap.php
  7. 4
      tests/cases/Encoding/TestBig5.php
  8. 4
      tests/cases/Encoding/TestEUCKR.php
  9. 4
      tests/cases/Encoding/TestGB18030.php
  10. 4
      tests/cases/Encoding/TestSingleByte.php
  11. 4
      tests/cases/Encoding/TestUTF16LE.php
  12. 4
      tests/cases/Encoding/TestUTF8.php
  13. 4
      tests/cases/Encoding/TestXUserDefined.php
  14. 11
      tests/lib/DecoderTest.php

10
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)
==========================

8
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;

12
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()) !== "") {

7
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;
}
}

7
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;
}
}

4
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/"]);
}

4
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
*/

4
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
*/

4
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
*/

4
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
*/

4
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
*/

4
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
*/

4
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
*/

11
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());
}

Loading…
Cancel
Save