Browse Source

:octocat: use Generator to list Kanji/Hanzi characters

smiley 2 years ago
parent
commit
021db2f0c9
2 changed files with 21 additions and 24 deletions
  1. 10 12
      tests/Data/HanziTest.php
  2. 11 12
      tests/Data/KanjiTest.php

+ 10 - 12
tests/Data/HanziTest.php

@@ -11,10 +11,8 @@
 namespace chillerlan\QRCodeTest\Data;
 namespace chillerlan\QRCodeTest\Data;
 
 
 use chillerlan\QRCode\Data\Hanzi;
 use chillerlan\QRCode\Data\Hanzi;
-use Throwable;
-use function bin2hex;
-use function defined;
-use function sprintf;
+use Generator, Throwable;
+use function bin2hex, chr, defined, sprintf;
 
 
 /**
 /**
  * Tests the Hanzi/GB2312 class
  * Tests the Hanzi/GB2312 class
@@ -25,7 +23,7 @@ class HanziTest extends DataInterfaceTestAbstract{
 	protected string $testdata = '无可奈何燃花作香';
 	protected string $testdata = '无可奈何燃花作香';
 
 
 	/**
 	/**
-	 * isGB2312() should pass on Hanzi/Hanzi characters and fail on everything else
+	 * isGB2312() should pass on Hanzi/GB2312 characters and fail on everything else
 	 */
 	 */
 	public function stringValidateProvider():array{
 	public function stringValidateProvider():array{
 		return [
 		return [
@@ -41,22 +39,22 @@ class HanziTest extends DataInterfaceTestAbstract{
 	/**
 	/**
 	 * lists all characters in the valid GB2312 range
 	 * lists all characters in the valid GB2312 range
 	 */
 	 */
-	public function hanziProvider():array{
-		$list = [];
+	public function hanziProvider():Generator{
 
 
-		for($byte1 = 0xa1; $byte1 < 0xf8; $byte1 += 0x1){
+		for($byte1 = 0xa1; $byte1 < 0xf8; $byte1++){
 
 
 			if($byte1 > 0xa9 && $byte1 < 0xb0){
 			if($byte1 > 0xa9 && $byte1 < 0xb0){
 				continue;
 				continue;
 			}
 			}
 
 
 			for($byte2 = 0xa1; $byte2 < 0xff; $byte2++){
 			for($byte2 = 0xa1; $byte2 < 0xff; $byte2++){
-				$list[] = [chr($byte1).chr($byte2)];
+				yield sprintf('0x%X', ($byte1 << 8 | $byte2)) => [
+					mb_convert_encoding(chr($byte1).chr($byte2), 'UTF-8', Hanzi::ENCODING),
+				];
 			}
 			}
 
 
 		}
 		}
 
 
-		return array_map(fn($chr) => mb_convert_encoding($chr, 'UTF-8', Hanzi::ENCODING), $list);
 	}
 	}
 
 
 	/**
 	/**
@@ -75,8 +73,8 @@ class HanziTest extends DataInterfaceTestAbstract{
 
 
 			$this::markTestSkipped(sprintf(
 			$this::markTestSkipped(sprintf(
 				'invalid glyph: %s => %s',
 				'invalid glyph: %s => %s',
-				bin2hex($chr),
-				mb_convert_encoding($chr, Hanzi::ENCODING, mb_internal_encoding())
+				bin2hex(mb_convert_encoding($chr, Hanzi::ENCODING, 'UTF-8')),
+				$chr
 			));
 			));
 		}
 		}
 	}
 	}

+ 11 - 12
tests/Data/KanjiTest.php

@@ -11,8 +11,8 @@
 namespace chillerlan\QRCodeTest\Data;
 namespace chillerlan\QRCodeTest\Data;
 
 
 use chillerlan\QRCode\Data\Kanji;
 use chillerlan\QRCode\Data\Kanji;
-use Throwable;
-use function array_map, bin2hex, chr, defined, mb_internal_encoding, sprintf;
+use Generator, Throwable;
+use function bin2hex, chr, defined, sprintf;
 
 
 /**
 /**
  * Tests the Kanji class
  * Tests the Kanji class
@@ -37,12 +37,13 @@ final class KanjiTest extends DataInterfaceTestAbstract{
 	}
 	}
 
 
 	/**
 	/**
-	 * lists the valid SJIS kanj
+	 * lists the valid SJIS kanji
 	 */
 	 */
-	public function kanjiProvider():array{
-		$list = [];
+	public function kanjiProvider():Generator{
+		$key = fn(int $byte1, int $byte2):string => sprintf('0x%X', ($byte1 << 8 | $byte2));
+		$val = fn(int $byte1, int $byte2):string => mb_convert_encoding(chr($byte1).chr($byte2), 'UTF-8', Kanji::ENCODING);
 
 
-		for($byte1 = 0x81; $byte1 < 0xeb; $byte1 += 0x1){
+		for($byte1 = 0x81; $byte1 < 0xeb; $byte1++){
 
 
 			// skip invalid/vendor ranges
 			// skip invalid/vendor ranges
 			if(($byte1 > 0x84 && $byte1 < 0x88) || ($byte1 > 0x9f && $byte1 < 0xe0)){
 			if(($byte1 > 0x84 && $byte1 < 0x88) || ($byte1 > 0x9f && $byte1 < 0xe0)){
@@ -58,7 +59,7 @@ final class KanjiTest extends DataInterfaceTestAbstract{
 						continue;
 						continue;
 					}
 					}
 
 
-					$list[] = [chr($byte1).chr($byte2)];
+					yield $key($byte1, $byte2) => [$val($byte1, $byte2)];
 				}
 				}
 
 
 			}
 			}
@@ -66,15 +67,13 @@ final class KanjiTest extends DataInterfaceTestAbstract{
 			else{
 			else{
 
 
 				for($byte2 = 0x9f; $byte2 < 0xfd; $byte2++){
 				for($byte2 = 0x9f; $byte2 < 0xfd; $byte2++){
-					$list[] = [chr($byte1).chr($byte2)];
+					yield $key($byte1, $byte2) => [$val($byte1, $byte2)];
 				}
 				}
 
 
 			}
 			}
 
 
 		}
 		}
 
 
-		// we need to put the joined byte sequence in a proper encoding
-		return array_map(fn($chr) => mb_convert_encoding($chr, Kanji::ENCODING, Kanji::ENCODING), $list);
 	}
 	}
 
 
 	/**
 	/**
@@ -93,8 +92,8 @@ final class KanjiTest extends DataInterfaceTestAbstract{
 
 
 			$this::markTestSkipped(sprintf(
 			$this::markTestSkipped(sprintf(
 				'invalid glyph: %s => %s',
 				'invalid glyph: %s => %s',
-				bin2hex($chr),
-				mb_convert_encoding($chr, Kanji::ENCODING, mb_internal_encoding())
+				bin2hex(mb_convert_encoding($chr, Kanji::ENCODING, 'UTF-8')),
+				$chr
 			));
 			));
 		}
 		}
 	}
 	}