diff --git a/lib/Encoding/UTF8.php b/lib/Encoding/UTF8.php index 19e5b16..946fc21 100644 --- a/lib/Encoding/UTF8.php +++ b/lib/Encoding/UTF8.php @@ -36,7 +36,7 @@ class UTF8 implements \Iterator { } public function current() { - return $this->current ?? ($this->current = $this->nextChar()); + return $this->current ?? ($this->current = $this->nextCode()); } public function key() { diff --git a/perf/perf.php b/perf/perf.php index 2a3b4ea..49e3c1e 100644 --- a/perf/perf.php +++ b/perf/perf.php @@ -35,13 +35,6 @@ $tests = [ $c = $i->nextChar(); } }], - 'Native iterator' => ["", function(string $text) { - $c = null; - $i = new UTF8($text); - while ($c !== "") { - $c = $i->nextChar(); - } - }], 'Intl code points' => ["intl", function(string $text) { $i = (function($text) { $i = \IntlBreakIterator::createCodePointInstance(); @@ -61,6 +54,11 @@ $tests = [ $p = $i->nextCode(); } }], + 'Code point iterator' => ["", function(string $text) { + $c = null; + $i = new UTF8($text); + foreach ($i as $c); + }], ]; if (!file_exists(__DIR__."/docs/")) { diff --git a/tests/cases/Encoding/TestUTF8.php b/tests/cases/Encoding/TestUTF8.php index 3d18c54..c8cfa3a 100644 --- a/tests/cases/Encoding/TestUTF8.php +++ b/tests/cases/Encoding/TestUTF8.php @@ -66,15 +66,12 @@ class TestUTF8 extends \PHPUnit\Framework\TestCase { */ public function testIterateThroughAString(string $input, array $exp) { $out = []; - $exp = array_map(function ($v) { - return \IntlChar::chr($v); - }, $exp); $s = new UTF8($input); $a = 0; $this->assertTrue(true); // prevent risky test of empty string - foreach ($s as $index => $c) { + foreach ($s as $index => $p) { $this->assertSame($a, $index, "Character key at index $a reported incorrectly"); - $this->assertSame(bin2hex($exp[$a]), bin2hex($c), "Character at index $a decoded incorrectly"); + $this->assertSame($exp[$a], $p, "Character at index $a decoded incorrectly"); $a++; } }