cp932_encoding.phpt 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. --TEST--
  2. Exhaustive test of CP932 encoding verification and conversion (including 'SJIS-win' variant)
  3. --EXTENSIONS--
  4. mbstring
  5. --SKIPIF--
  6. <?php
  7. if (getenv("SKIP_SLOW_TESTS")) die("skip slow test");
  8. ?>
  9. --FILE--
  10. <?php
  11. srand(4321); /* Make results consistent */
  12. include('encoding_tests.inc');
  13. mb_substitute_character(0x25); // '%'
  14. /* Read in the table of all characters in CP932 */
  15. readConversionTable(__DIR__ . '/data/CP932.txt', $validChars, $fromUnicode);
  16. /* Aside from the characters in that table, we also support a 'user' area
  17. * from 0xF040-0xF9FC, which map to Unicode 'private' codepoints 0xE000-E757 */
  18. $codepoint = 0xE000;
  19. for ($i = 0xF0; $i <= 0xF9; $i++) {
  20. for ($j = 0x40; $j <= 0xFC; $j++) {
  21. if ($j == 0x7F)
  22. continue;
  23. $utf16 = pack('n', $codepoint);
  24. $cp932 = chr($i) . chr($j);
  25. $validChars[$cp932] = $utf16;
  26. $fromUnicode[$utf16] = $cp932;
  27. $codepoint++;
  28. }
  29. }
  30. /* U+00A2 is CENT SIGN; convert to FULLWIDTH CENT SIGN */
  31. $fromUnicode["\x00\xA2"] = "\x81\x91";
  32. /* U+00A3 is POUND SIGN; convert to FULLWIDTH POUND SIGN */
  33. $fromUnicode["\x00\xA3"] = "\x81\x92";
  34. /* U+00A5 is YEN SIGN; convert to 0x5C, which has conflicting uses
  35. * (either as backslash or as Yen sign) */
  36. $fromUnicode["\x00\xA5"] = "\x5C";
  37. /* We map the JIS X 0208 FULLWIDTH TILDE to U+FF5E (FULLWIDTH TILDE)
  38. * But when converting Unicode to CP932, we also accept U+301C (WAVE DASH) */
  39. $fromUnicode["\x30\x1C"] = "\x81\x60";
  40. /* We map the JIS X 0208 MINUS SIGN to U+FF0D (FULLWIDTH HYPHEN-MINUS SIGN),
  41. * but when converting Unicode to CP932, we also accept U+2212 (MINUS SIGN) */
  42. $fromUnicode["\x22\x12"] = "\x81\x7C";
  43. /* We map the JIS X 0208 PARALLEL TO symbol to U+2225 (PARALLEL TO),
  44. * but when converting Unicode to CP932, we also accept U+2016
  45. * (DOUBLE VERTICAL LINE) */
  46. $fromUnicode["\x20\x16"] = "\x81\x61";
  47. /* We map the JIS X 0208 NOT SIGN to U+FFE2 (FULLWIDTH NOT SIGN),
  48. * but when converting Unicode to CP932, we also accept U+00AC (NOT SIGN) */
  49. $fromUnicode["\x00\xAC"] = "\x81\xCA";
  50. /* U+00AF is MACRON; convert to FULLWIDTH MACRON */
  51. $fromUnicode["\x00\xAF"] = "\x81\x50";
  52. /* U+203E is OVERLINE; convert to 0x7E, which has conflicting uses
  53. * (either as tilde or as overline) */
  54. $fromUnicode["\x20\x3E"] = "\x7E";
  55. findInvalidChars($validChars, $invalidChars, $truncated, array_fill_keys(range(0x81, 0x9F), 2) + array_fill_keys(range(0xE0, 0xFC), 2));
  56. findInvalidChars($fromUnicode, $invalidCodepoints, $unused, array_fill_keys(range(0, 0xFF), 2));
  57. /* There are 396 Unicode codepoints which are non-invertible in CP932
  58. * (multiple CP932 byte sequences map to the same codepoint)
  59. * Some of these are 3-way pile-ups. I wonder what the fine folks at MS
  60. * were thinking when they designed this text encoding. */
  61. /* Everything from 0xED00-0xEEFF falls in this unfortunate category
  62. * (Other sequences in 0xFA00-0xFBFF map to the same codepoints, and when
  63. * converting from Unicode back to CP932, we favor the F's rather than the E's) */
  64. $nonInvertible = array();
  65. for ($i = 0xED00; $i <= 0xEEFF; $i++) {
  66. $bytes = pack('n', $i);
  67. if (isset($validChars[$bytes])) {
  68. unset($fromUnicode[$validChars[$bytes]]);
  69. $nonInvertible[$bytes] = $validChars[$bytes];
  70. unset($validChars[$bytes]); // will test these separately
  71. }
  72. }
  73. /* There are 23 other collisions between 2-byte sequences which variously
  74. * start with 0x81, 0x87, or 0xFA
  75. * We _love_ 0x81 and use it when possible. 0x87 is a second favorite */
  76. for ($i = 0xFA4A; $i <= 0xFA53; $i++) {
  77. $bytes = pack('n', $i);
  78. unset($fromUnicode[$validChars[$bytes]]);
  79. $nonInvertible[$bytes] = $validChars[$bytes];
  80. unset($validChars[$bytes]); // will test these separately
  81. }
  82. foreach ([0x8790, 0x8791, 0x8792, 0x8795, 0x8796, 0x8797, 0x879A, 0x879B, 0x879C, 0xFA54, 0xFA58, 0xFA59, 0xFA5A, 0xFA5B] as $i) {
  83. $bytes = pack('n', $i);
  84. unset($fromUnicode[$validChars[$bytes]]);
  85. $nonInvertible[$bytes] = $validChars[$bytes];
  86. unset($validChars[$bytes]); // will test these separately
  87. }
  88. testAllValidChars($validChars, 'CP932', 'UTF-16BE');
  89. foreach ($nonInvertible as $cp932 => $unicode)
  90. testValidString($cp932, $unicode, 'CP932', 'UTF-16BE', false);
  91. echo "CP932 verification and conversion works on all valid characters\n";
  92. testAllInvalidChars($invalidChars, $validChars, 'CP932', 'UTF-16BE', "\x00%");
  93. echo "CP932 verification and conversion works on all invalid characters\n";
  94. convertAllInvalidChars($invalidCodepoints, $fromUnicode, 'UTF-16BE', 'CP932', '%');
  95. echo "Unicode -> CP932 conversion works on all invalid codepoints\n";
  96. /* Now test 'SJIS-win' variant of CP932, which is really CP932 but with
  97. * two different mappings
  98. * Instead of mapping U+00A5 and U+203E to the single bytes 0x5C and 07E
  99. * (which have conflicting uses), 'SJIS-win' maps them to appropriate
  100. * JIS X 0208 characters */
  101. /* U+00A5 is YEN SIGN; convert to FULLWIDTH YEN SIGN */
  102. $fromUnicode["\x00\xA5"] = "\x81\x8F";
  103. /* U+203E is OVERLINE; convert to JIS X 0208 FULLWIDTH MACRON */
  104. $fromUnicode["\x20\x3E"] = "\x81\x50";
  105. testAllValidChars($validChars, 'SJIS-win', 'UTF-16BE');
  106. foreach ($nonInvertible as $cp932 => $unicode)
  107. testValidString($cp932, $unicode, 'SJIS-win', 'UTF-16BE', false);
  108. echo "SJIS-win verification and conversion works on all valid characters\n";
  109. testAllInvalidChars($invalidChars, $validChars, 'SJIS-win', 'UTF-16BE', "\x00%");
  110. echo "SJIS-win verification and conversion works on all invalid characters\n";
  111. convertAllInvalidChars($invalidCodepoints, $fromUnicode, 'UTF-16BE', 'SJIS-win', '%');
  112. echo "Unicode -> SJIS-win conversion works on all invalid codepoints\n";
  113. // Test "long" illegal character markers
  114. mb_substitute_character("long");
  115. convertInvalidString("\x80", "%", "CP932", "UTF-8");
  116. convertInvalidString("\xEA", "%", "CP932", "UTF-8");
  117. convertInvalidString("\x81\x20", "%", "CP932", "UTF-8");
  118. convertInvalidString("\xEA\xA9", "%", "CP932", "UTF-8");
  119. convertInvalidString("\x80", "%", "SJIS-win", "UTF-8");
  120. convertInvalidString("\xEA", "%", "SJIS-win", "UTF-8");
  121. convertInvalidString("\x81\x20", "%", "SJIS-win", "UTF-8");
  122. convertInvalidString("\xEA\xA9", "%", "SJIS-win", "UTF-8");
  123. echo "Done!\n";
  124. ?>
  125. --EXPECT--
  126. CP932 verification and conversion works on all valid characters
  127. CP932 verification and conversion works on all invalid characters
  128. Unicode -> CP932 conversion works on all invalid codepoints
  129. SJIS-win verification and conversion works on all valid characters
  130. SJIS-win verification and conversion works on all invalid characters
  131. Unicode -> SJIS-win conversion works on all invalid codepoints
  132. Done!