Browse Source

API re-organization

multi-byte
J. King 4 years ago
parent
commit
be2134cc71
  1. 3
      lib/Encoding.php
  2. 4
      lib/Encoding/AbstractEncoding.php
  3. 2
      lib/Encoding/Big5.php
  4. 5
      lib/Encoding/Coder.php
  5. 5
      lib/Encoding/Decoder.php
  6. 2
      lib/Encoding/EUCJP.php
  7. 2
      lib/Encoding/EUCKR.php
  8. 8
      lib/Encoding/Encoder.php
  9. 2
      lib/Encoding/GBCommon.php
  10. 2
      lib/Encoding/ISO2022JP.php
  11. 2
      lib/Encoding/Replacement.php
  12. 2
      lib/Encoding/ShiftJIS.php
  13. 2
      lib/Encoding/SingleByteEncoding.php
  14. 18
      lib/Encoding/StatefulEncoding.php
  15. 2
      lib/Encoding/UTF8.php
  16. 2
      lib/Encoding/XUserDefined.php
  17. 12
      tests/cases/Encoding/TestBig5.php
  18. 12
      tests/cases/Encoding/TestEUCJP.php
  19. 12
      tests/cases/Encoding/TestEUCKR.php
  20. 28
      tests/cases/Encoding/TestGB18030.php
  21. 20
      tests/cases/Encoding/TestISO2022JP.php
  22. 14
      tests/cases/Encoding/TestShiftJIS.php
  23. 10
      tests/cases/Encoding/TestUTF8.php
  24. 12
      tests/cases/Encoding/TestXUserDefined.php
  25. 2
      tests/cases/TestEncoding.php
  26. 4
      tools/mklabels.php
  27. 8
      tools/test.js

3
lib/Encoding.php

File diff suppressed because one or more lines are too long

4
lib/Encoding/AbstractEncoding.php

@ -6,7 +6,7 @@
declare(strict_types=1);
namespace MensBeam\Intl\Encoding;
abstract class AbstractEncoding implements Encoding {
abstract class AbstractEncoding implements Decoder {
/** @var string $string The string being decoded */
protected $string;
/** @var int $posByte The current byte position in the string */
@ -199,7 +199,7 @@ abstract class AbstractEncoding implements Encoding {
return "&#".(string) $data.";";
} else {
// fatal replacement mode for encoders; not applicable to Unicode transformation formats
throw new EncoderException("Code point $data not available in target encoding", self::E_UNAVAILABLE_CODE_POINT);
throw new EncoderException("Code point $data not available in target encoding", Coder::E_UNAVAILABLE_CODE_POINT);
}
}
}

2
lib/Encoding/Big5.php

@ -6,7 +6,7 @@
declare(strict_types=1);
namespace MensBeam\Intl\Encoding;
class Big5 extends AbstractEncoding implements StatelessEncoding {
class Big5 extends AbstractEncoding implements Coder, Decoder {
const NAME = "Big5";
const LABELS = [
"big5",

5
lib/Encoding/StatelessEncoding.php → lib/Encoding/Coder.php

@ -6,7 +6,10 @@
declare(strict_types=1);
namespace MensBeam\Intl\Encoding;
interface StatelessEncoding extends Encoding {
interface Coder {
const E_INVALID_CODE_POINT = 1;
const E_UNAVAILABLE_CODE_POINT = 3;
const E_UNAVAILABLE_ENCODER = 4;
/** Returns the encoding of $codePoint as a byte string
*

5
lib/Encoding/Encoding.php → lib/Encoding/Decoder.php

@ -6,15 +6,12 @@
declare(strict_types=1);
namespace MensBeam\Intl\Encoding;
interface Encoding {
interface Decoder {
const MODE_NULL = 0;
const MODE_REPLACE = 1;
const MODE_FATAL = 2;
const E_INVALID_CODE_POINT = 1;
const E_INVALID_BYTE = 2;
const E_UNAVAILABLE_CODE_POINT = 3;
const E_UNAVAILABLE_ENCODER = 4;
/** Constructs a new decoder
* @param bool $fatal If true, throw enceptions when encountering invalid input. If false, substitute U+FFFD REPLACEMENT CHARACTER instead

2
lib/Encoding/EUCJP.php

@ -6,7 +6,7 @@
declare(strict_types=1);
namespace MensBeam\Intl\Encoding;
class EUCJP extends AbstractEncoding implements StatelessEncoding {
class EUCJP extends AbstractEncoding implements Coder, Decoder {
const NAME = "EUC-JP";
const LABELS = [
"cseucpkdfmtjapanese",

2
lib/Encoding/EUCKR.php

@ -6,7 +6,7 @@
declare(strict_types=1);
namespace MensBeam\Intl\Encoding;
class EUCKR extends AbstractEncoding implements StatelessEncoding {
class EUCKR extends AbstractEncoding implements Coder, Decoder {
const NAME = "EUC-KR";
const LABELS = [
"cseuckr",

8
lib/Encoding/Encoder.php

@ -20,7 +20,7 @@ class Encoder {
public function __construct(string $label, bool $fatal = true) {
$l = Matcher::matchLabel($label);
if (!$l || !$l['encoder']) {
throw new EncoderException("Label '$label' does not have an encoder", Encoding::E_UNAVAILABLE_ENCODER);
throw new EncoderException("Label '$label' does not have an encoder", Coder::E_UNAVAILABLE_ENCODER);
} else {
$this->name = $l['name'];
$this->fatal = $fatal;
@ -44,7 +44,7 @@ class Encoder {
public function encodeChar(int $codePoint): string {
if ($codePoint < 0 || $codePoint > 0x10FFFF) {
throw new EncoderException("Encountered code point outside Unicode range ($codePoint)", Encoding::E_INVALID_CODE_POINT);
throw new EncoderException("Encountered code point outside Unicode range ($codePoint)", Coder::E_INVALID_CODE_POINT);
}
switch ($this->name) {
case "UTF-8":
@ -172,7 +172,7 @@ class Encoder {
}
return $out;
} else {
throw new EncoderException("Code point $actual not available in target encoding", Encoding::E_UNAVAILABLE_CODE_POINT);
throw new EncoderException("Code point $actual not available in target encoding", Coder::E_UNAVAILABLE_CODE_POINT);
}
}
@ -182,4 +182,4 @@ class Encoder {
}
return "";
}
}
}

2
lib/Encoding/GBCommon.php

File diff suppressed because one or more lines are too long

2
lib/Encoding/ISO2022JP.php

@ -6,7 +6,7 @@
declare(strict_types=1);
namespace MensBeam\Intl\Encoding;
class ISO2022JP extends AbstractEncoding implements Encoding {
class ISO2022JP extends AbstractEncoding implements Decoder {
const NAME = "ISO-2022-JP";
const LABELS = [
"csiso2022jp",

2
lib/Encoding/Replacement.php

@ -6,7 +6,7 @@
declare(strict_types=1);
namespace MensBeam\Intl\Encoding;
class Replacement implements Encoding {
class Replacement implements Decoder {
const NAME = "replacement";
const LABELS = [
"csiso2022kr",

2
lib/Encoding/ShiftJIS.php

@ -6,7 +6,7 @@
declare(strict_types=1);
namespace MensBeam\Intl\Encoding;
class ShiftJIS extends AbstractEncoding implements StatelessEncoding {
class ShiftJIS extends AbstractEncoding implements Coder, Decoder {
const NAME = "Shift_JIS";
const LABELS = [
"csshiftjis",

2
lib/Encoding/SingleByteEncoding.php

@ -6,7 +6,7 @@
declare(strict_types=1);
namespace MensBeam\Intl\Encoding;
abstract class SingleByteEncoding extends AbstractEncoding implements StatelessEncoding {
abstract class SingleByteEncoding extends AbstractEncoding implements Coder, Decoder {
protected $selfSynchronizing = true;
public function nextChar(): string {

18
lib/Encoding/StatefulEncoding.php

@ -1,18 +0,0 @@
<?php
/** @license MIT
* Copyright 2018 J. King et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
namespace MensBeam\Intl\Encoding;
interface StatefulEncoding extends Encoding {
/** Returns the encoding of $codePoints as a byte string
*
* If any element of $codePoints is less than 0 or greater than 1114111, an exception is thrown
*
* If $fatal is true, an exception will be thrown if any code point cannot be encoded into a character; otherwise HTML character references will be substituted
*/
public static function encode(array $codePoints, bool $fatal = true): string;
}

2
lib/Encoding/UTF8.php

@ -6,7 +6,7 @@
declare(strict_types=1);
namespace MensBeam\Intl\Encoding;
class UTF8 extends AbstractEncoding implements StatelessEncoding {
class UTF8 extends AbstractEncoding implements Coder, Decoder {
const NAME = "UTF-8";
const LABELS = [
"unicode-1-1-utf-8",

2
lib/Encoding/XUserDefined.php

@ -6,7 +6,7 @@
declare(strict_types=1);
namespace MensBeam\Intl\Encoding;
class XUserDefined extends AbstractEncoding implements StatelessEncoding {
class XUserDefined extends AbstractEncoding implements Coder, Decoder {
const NAME = "x-user-defined";
const LABELS = ["x-user-defined"];

12
tests/cases/Encoding/TestBig5.php

@ -7,7 +7,7 @@ declare(strict_types=1);
namespace MensBeam\Intl\TestCase\Encoding;
use MensBeam\Intl\Encoding\Big5;
use MensBeam\Intl\Encoding\Encoding;
use MensBeam\Intl\Encoding\Coder;
use MensBeam\Intl\Encoding\EncoderException;
class TestBig5 extends \MensBeam\Intl\Test\CoderDecoderTest {
@ -33,7 +33,7 @@ class TestBig5 extends \MensBeam\Intl\Test\CoderDecoderTest {
'U+0064 (HTML)' => [false, 0x64, "64"],
'U+0064 (fatal)' => [true, 0x64, "64"],
'U+00CA (HTML)' => [false, 0xCA, bin2hex("&#202;")],
'U+00CA (fatal)' => [true, 0xCA, new EncoderException("", Encoding::E_UNAVAILABLE_CODE_POINT)],
'U+00CA (fatal)' => [true, 0xCA, new EncoderException("", Coder::E_UNAVAILABLE_CODE_POINT)],
'U+3007 (HTML)' => [false, 0x3007, "C6 E2"],
'U+3007 (fatal)' => [true, 0x3007, "C6 E2"],
'U+5341 (HTML)' => [false, 0x5341, "A4 51"],
@ -42,10 +42,10 @@ class TestBig5 extends \MensBeam\Intl\Test\CoderDecoderTest {
'U+2561 (fatal)' => [true, 0x2561, "F9 EB"],
'U+256D (HTML)' => [false, 0x256D, "A2 7E"],
'U+256D (fatal)' => [true, 0x256D, "A2 7E"],
'-1 (HTML)' => [false, -1, new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'-1 (fatal)' => [true, -1, new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'0x110000 (HTML)' => [false, 0x110000, new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'0x110000 (fatal)' => [true, 0x110000, new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'-1 (HTML)' => [false, -1, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
'-1 (fatal)' => [true, -1, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
'0x110000 (HTML)' => [false, 0x110000, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
'0x110000 (fatal)' => [true, 0x110000, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
];
}

12
tests/cases/Encoding/TestEUCJP.php

@ -7,7 +7,7 @@ declare(strict_types=1);
namespace MensBeam\Intl\TestCase\Encoding;
use MensBeam\Intl\Encoding\EUCJP;
use MensBeam\Intl\Encoding\Encoding;
use MensBeam\Intl\Encoding\Coder;
use MensBeam\Intl\Encoding\EncoderException;
class TestEUCJP extends \MensBeam\Intl\Test\CoderDecoderTest {
@ -43,15 +43,15 @@ class TestEUCJP extends \MensBeam\Intl\Test\CoderDecoderTest {
'U+2212 (HTML)' => [false, 0x2212, "A1 DD"],
'U+2212 (fatal)' => [true, 0x2212, "A1 DD"],
'U+00E6 (HTML)' => [false, 0xE6, bin2hex("&#230;")],
'U+00E6 (fatal)' => [true, 0xE6, new EncoderException("", Encoding::E_UNAVAILABLE_CODE_POINT)],
'U+00E6 (fatal)' => [true, 0xE6, new EncoderException("", Coder::E_UNAVAILABLE_CODE_POINT)],
'U+FFE2 (HTML)' => [false, 0xFFE2, "A2 CC"],
'U+FFE2 (fatal)' => [true, 0xFFE2, "A2 CC"],
'U+2116 (HTML)' => [false, 0x2116, "AD E2"],
'U+2116 (fatal)' => [true, 0x2116, "AD E2"],
'-1 (HTML)' => [false, -1, new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'-1 (fatal)' => [true, -1, new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'0x110000 (HTML)' => [false, 0x110000, new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'0x110000 (fatal)' => [true, 0x110000, new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'-1 (HTML)' => [false, -1, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
'-1 (fatal)' => [true, -1, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
'0x110000 (HTML)' => [false, 0x110000, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
'0x110000 (fatal)' => [true, 0x110000, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
];
}

12
tests/cases/Encoding/TestEUCKR.php

@ -7,7 +7,7 @@ declare(strict_types=1);
namespace MensBeam\Intl\TestCase\Encoding;
use MensBeam\Intl\Encoding\EUCKR;
use MensBeam\Intl\Encoding\Encoding;
use MensBeam\Intl\Encoding\Coder;
use MensBeam\Intl\Encoding\EncoderException;
class TestEUCKR extends \MensBeam\Intl\Test\CoderDecoderTest {
@ -33,13 +33,13 @@ class TestEUCKR extends \MensBeam\Intl\Test\CoderDecoderTest {
'U+0064 (HTML)' => [false, 0x64, "64"],
'U+0064 (fatal)' => [true, 0x64, "64"],
'U+00CA (HTML)' => [false, 0xCA, bin2hex("&#202;")],
'U+00CA (fatal)' => [true, 0xCA, new EncoderException("", Encoding::E_UNAVAILABLE_CODE_POINT)],
'U+00CA (fatal)' => [true, 0xCA, new EncoderException("", Coder::E_UNAVAILABLE_CODE_POINT)],
'U+ACF2 (HTML)' => [false, 0xACF2, "81 E9"],
'U+ACF2 (fatal)' => [true, 0xACF2, "81 E9"],
'-1 (HTML)' => [false, -1, new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'-1 (fatal)' => [true, -1, new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'0x110000 (HTML)' => [false, 0x110000, new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'0x110000 (fatal)' => [true, 0x110000, new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'-1 (HTML)' => [false, -1, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
'-1 (fatal)' => [true, -1, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
'0x110000 (HTML)' => [false, 0x110000, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
'0x110000 (fatal)' => [true, 0x110000, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
];
}

28
tests/cases/Encoding/TestGB18030.php

@ -8,7 +8,7 @@ namespace MensBeam\Intl\TestCase\Encoding;
use MensBeam\Intl\Encoding\GBK;
use MensBeam\Intl\Encoding\GB18030;
use MensBeam\Intl\Encoding\Encoding;
use MensBeam\Intl\Encoding\Coder;
use MensBeam\Intl\Encoding\EncoderException;
class TestGB18030 extends \MensBeam\Intl\Test\CoderDecoderTest {
@ -49,13 +49,13 @@ class TestGB18030 extends \MensBeam\Intl\Test\CoderDecoderTest {
'U+1D11E (HTML)' => [false, 0x1D11E, "94 32 BE 34"],
'U+1D11E (fatal)' => [true, 0x1D11E, "94 32 BE 34"],
'U+E5E5 (HTML)' => [false, 0xE5E5, bin2hex("&#58853;")],
'U+E5E5 (fatal)' => [true, 0xE5E5, new EncoderException("", Encoding::E_UNAVAILABLE_CODE_POINT)],
'U+E5E5 (fatal)' => [true, 0xE5E5, new EncoderException("", Coder::E_UNAVAILABLE_CODE_POINT)],
'U+3000 (HTML)' => [false, 0x3000, "A1 A1"],
'U+3000 (fatal)' => [true, 0x3000, "A1 A1"],
'-1 (HTML)' => [false, -1, new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'-1 (fatal)' => [true, -1, new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'0x110000 (HTML)' => [false, 0x110000, new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'0x110000 (fatal)' => [true, 0x110000, new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'-1 (HTML)' => [false, -1, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
'-1 (fatal)' => [true, -1, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
'0x110000 (HTML)' => [false, 0x110000, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
'0x110000 (fatal)' => [true, 0x110000, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
];
$series_gbk = [
'U+0064 (HTML)' => [false, 0x64, "64"],
@ -65,19 +65,19 @@ class TestGB18030 extends \MensBeam\Intl\Test\CoderDecoderTest {
'U+2164 (HTML)' => [false, 0x2164, "A2 F5"],
'U+2164 (fatal)' => [true, 0x2164, "A2 F5"],
'U+3A74 (HTML)' => [false, 0x3A74, bin2hex("&#14964;")],
'U+3A74 (fatal)' => [true, 0x3A74, new EncoderException("", Encoding::E_UNAVAILABLE_CODE_POINT)],
'U+3A74 (fatal)' => [true, 0x3A74, new EncoderException("", Coder::E_UNAVAILABLE_CODE_POINT)],
'U+E7C7 (HTML)' => [false, 0xE7C7, bin2hex("&#59335;")],
'U+E7C7 (fatal)' => [true, 0xE7C7, new EncoderException("", Encoding::E_UNAVAILABLE_CODE_POINT)],
'U+E7C7 (fatal)' => [true, 0xE7C7, new EncoderException("", Coder::E_UNAVAILABLE_CODE_POINT)],
'U+1D11E (HTML)' => [false, 0x1D11E, bin2hex("&#119070;")],
'U+1D11E (fatal)' => [true, 0x1D11E, new EncoderException("", Encoding::E_UNAVAILABLE_CODE_POINT)],
'U+1D11E (fatal)' => [true, 0x1D11E, new EncoderException("", Coder::E_UNAVAILABLE_CODE_POINT)],
'U+E5E5 (HTML)' => [false, 0xE5E5, bin2hex("&#58853;")],
'U+E5E5 (fatal)' => [true, 0xE5E5, new EncoderException("", Encoding::E_UNAVAILABLE_CODE_POINT)],
'U+E5E5 (fatal)' => [true, 0xE5E5, new EncoderException("", Coder::E_UNAVAILABLE_CODE_POINT)],
'U+3000 (HTML)' => [false, 0x3000, "A1 A1"],
'U+3000 (fatal)' => [true, 0x3000, "A1 A1"],
'-1 (HTML)' => [false, -1, new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'-1 (fatal)' => [true, -1, new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'0x110000 (HTML)' => [false, 0x110000, new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'0x110000 (fatal)' => [true, 0x110000, new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'-1 (HTML)' => [false, -1, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
'-1 (fatal)' => [true, -1, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
'0x110000 (HTML)' => [false, 0x110000, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
'0x110000 (fatal)' => [true, 0x110000, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
];
foreach ($series_gb18030 as $name => $test) {
array_push($test, GB18030::class);

20
tests/cases/Encoding/TestISO2022JP.php

@ -7,7 +7,7 @@ declare(strict_types=1);
namespace MensBeam\Intl\TestCase\Encoding;
use MensBeam\Intl\Encoding\ISO2022JP;
use MensBeam\Intl\Encoding\Encoding;
use MensBeam\Intl\Encoding\Coder;
use MensBeam\Intl\Encoding\EncoderException;
class TestISO2022JP extends \MensBeam\Intl\Test\CoderDecoderTest {
@ -55,15 +55,15 @@ class TestISO2022JP extends \MensBeam\Intl\Test\CoderDecoderTest {
'U+FFE2 (HTML)' => [false, [0xFFE2], "1B 24 42 22 4C 1B 28 42"],
'U+FFE2 (fatal)' => [true, [0xFFE2], "1B 24 42 22 4C 1B 28 42"],
'U+00C6 (HTML)' => [false, [0xC6], "26 23 31 39 38 3B"],
'U+00C6 (fatal)' => [true, [0xC6], new EncoderException("", Encoding::E_UNAVAILABLE_CODE_POINT)],
'U+00C6 (fatal)' => [true, [0xC6], new EncoderException("", Coder::E_UNAVAILABLE_CODE_POINT)],
'U+FFFD (HTML)' => [false, [0xFFFD], "26 23 36 35 35 33 33 3B"],
'U+FFFD (fatal)' => [true, [0xFFFD], new EncoderException("", Encoding::E_UNAVAILABLE_CODE_POINT)],
'U+FFFD (fatal)' => [true, [0xFFFD], new EncoderException("", Coder::E_UNAVAILABLE_CODE_POINT)],
'Roman (HTML)' => [false, [0xA5, 0x20, 0x203E], "1B 28 4A 5C 20 7E 1B 28 42"],
'Roman (fatal)' => [true, [0xA5, 0x20, 0x203E], "1B 28 4A 5C 20 7E 1B 28 42"],
'Roman to ASCII (HTML)' => [false, [0xA5, 0x5C], "1B 28 4A 5C 1B 28 42 5C"],
'Roman to ASCII (fatal)' => [true, [0xA5, 0x5C], "1B 28 4A 5C 1B 28 42 5C"],
'Roman to error (HTML)' => [false, [0xA5, 0x80], "1B 28 4A 5C 26 23 31 32 38 3B 1B 28 42"],
'Roman to error (fatal)' => [true, [0xA5, 0x80], new EncoderException("", Encoding::E_UNAVAILABLE_CODE_POINT)],
'Roman to error (fatal)' => [true, [0xA5, 0x80], new EncoderException("", Coder::E_UNAVAILABLE_CODE_POINT)],
'JIS (HTML)' => [false, [0x2116, 0xFFE2, 0x2212], "1B 24 42 2D 62 22 4C 21 5D 1B 28 42"],
'JIS (fatal)' => [true, [0x2116, 0xFFE2, 0x2212], "1B 24 42 2D 62 22 4C 21 5D 1B 28 42"],
'JIS to Roman (HTML)' => [false, [0x2116, 0xA5], "1B 24 42 2D 62 1B 28 4A 5C 1B 28 42"],
@ -73,13 +73,13 @@ class TestISO2022JP extends \MensBeam\Intl\Test\CoderDecoderTest {
'JIS to ASCII 2 (HTML)' => [false, [0x2116, 0x5C], "1B 24 42 2D 62 1B 28 42 5C"],
'JIS to ASCII 2 (fatal)' => [true, [0x2116, 0x5C], "1B 24 42 2D 62 1B 28 42 5C"],
'JIS to error (HTML)' => [false, [0x2116, 0x80], "1B 24 42 2D 62 1B 28 42 26 23 31 32 38 3B"],
'JIS to error (fatal)' => [true, [0x2116, 0x80], new EncoderException("", Encoding::E_UNAVAILABLE_CODE_POINT)],
'JIS to error (fatal)' => [true, [0x2116, 0x80], new EncoderException("", Coder::E_UNAVAILABLE_CODE_POINT)],
'Escape characters (HTML)' => [false, [0x1B, 0xE, 0xF], "26 23 36 35 35 33 33 3B 26 23 36 35 35 33 33 3B 26 23 36 35 35 33 33 3B"],
'Escape characters (fatal)' => [true, [0x1B, 0xE, 0xF], new EncoderException("", Encoding::E_UNAVAILABLE_CODE_POINT)],
'-1 (HTML)' => [false, [-1], new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'-1 (fatal)' => [true, [-1], new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'0x110000 (HTML)' => [false, [0x110000], new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'0x110000 (fatal)' => [true, [0x110000], new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'Escape characters (fatal)' => [true, [0x1B, 0xE, 0xF], new EncoderException("", Coder::E_UNAVAILABLE_CODE_POINT)],
'-1 (HTML)' => [false, [-1], new EncoderException("", Coder::E_INVALID_CODE_POINT)],
'-1 (fatal)' => [true, [-1], new EncoderException("", Coder::E_INVALID_CODE_POINT)],
'0x110000 (HTML)' => [false, [0x110000], new EncoderException("", Coder::E_INVALID_CODE_POINT)],
'0x110000 (fatal)' => [true, [0x110000], new EncoderException("", Coder::E_INVALID_CODE_POINT)],
];
}

14
tests/cases/Encoding/TestShiftJIS.php

@ -7,7 +7,7 @@ declare(strict_types=1);
namespace MensBeam\Intl\TestCase\Encoding;
use MensBeam\Intl\Encoding\ShiftJIS;
use MensBeam\Intl\Encoding\Encoding;
use MensBeam\Intl\Encoding\Coder;
use MensBeam\Intl\Encoding\EncoderException;
class TestShiftJIS extends \MensBeam\Intl\Test\CoderDecoderTest {
@ -43,17 +43,17 @@ class TestShiftJIS extends \MensBeam\Intl\Test\CoderDecoderTest {
'U+2212 (HTML)' => [false, 0x2212, "81 7C"],
'U+2212 (fatal)' => [true, 0x2212, "81 7C"],
'U+00E6 (HTML)' => [false, 0xE6, bin2hex("&#230;")],
'U+00E6 (fatal)' => [true, 0xE6, new EncoderException("", Encoding::E_UNAVAILABLE_CODE_POINT)],
'U+00E6 (fatal)' => [true, 0xE6, new EncoderException("", Coder::E_UNAVAILABLE_CODE_POINT)],
'U+FFE2 (HTML)' => [false, 0xFFE2, "81 CA"],
'U+FFE2 (fatal)' => [true, 0xFFE2, "81 CA"],
'U+2116 (HTML)' => [false, 0x2116, "87 82"],
'U+2116 (fatal)' => [true, 0x2116, "87 82"],
'U+E000 (HTML)' => [false, 0xE000, bin2hex("&#57344;")],
'U+E000 (fatal)' => [true, 0xE000, new EncoderException("", Encoding::E_UNAVAILABLE_CODE_POINT)],
'-1 (HTML)' => [false, -1, new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'-1 (fatal)' => [true, -1, new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'0x110000 (HTML)' => [false, 0x110000, new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'0x110000 (fatal)' => [true, 0x110000, new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'U+E000 (fatal)' => [true, 0xE000, new EncoderException("", Coder::E_UNAVAILABLE_CODE_POINT)],
'-1 (HTML)' => [false, -1, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
'-1 (fatal)' => [true, -1, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
'0x110000 (HTML)' => [false, 0x110000, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
'0x110000 (fatal)' => [true, 0x110000, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
];
}

10
tests/cases/Encoding/TestUTF8.php

@ -7,7 +7,7 @@ declare(strict_types=1);
namespace MensBeam\Intl\TestCase\Encoding;
use MensBeam\Intl\Encoding\UTF8;
use MensBeam\Intl\Encoding\Encoding;
use MensBeam\Intl\Encoding\Coder;
use MensBeam\Intl\Encoding\EncoderException;
class TestUTF8 extends \MensBeam\Intl\Test\CoderDecoderTest {
@ -44,10 +44,10 @@ class TestUTF8 extends \MensBeam\Intl\Test\CoderDecoderTest {
'U+10FFFD (fatal)' => [true, 0x10FFFD, "F4 8F BF BD"],
'U+FFFE (HTML)' => [false, 0xFFFE, "EF BF BE"],
'U+FFFE (fatal)' => [true, 0xFFFE, "EF BF BE"],
'-1 (HTML)' => [false, -1, new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'-1 (fatal)' => [true, -1, new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'0x110000 (HTML)' => [false, 0x110000, new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'0x110000 (fatal)' => [true, 0x110000, new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'-1 (HTML)' => [false, -1, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
'-1 (fatal)' => [true, -1, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
'0x110000 (HTML)' => [false, 0x110000, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
'0x110000 (fatal)' => [true, 0x110000, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
];
}

12
tests/cases/Encoding/TestXUserDefined.php

@ -7,7 +7,7 @@ declare(strict_types=1);
namespace MensBeam\Intl\TestCase\Encoding;
use MensBeam\Intl\Encoding\XUserDefined;
use MensBeam\Intl\Encoding\Encoding;
use MensBeam\Intl\Encoding\Coder;
use MensBeam\Intl\Encoding\EncoderException;
class TestXUserDefined extends \MensBeam\Intl\Test\CoderDecoderTest {
@ -28,11 +28,11 @@ class TestXUserDefined extends \MensBeam\Intl\Test\CoderDecoderTest {
'U+F7FF (HTML)' => [false, 0xF7FF, "FF"],
'U+F7FF (fatal)' => [true, 0xF7FF, "FF"],
'U+00CA (HTML)' => [false, 0xCA, bin2hex("&#202;")],
'U+00CA (fatal)' => [true, 0xCA, new EncoderException("", Encoding::E_UNAVAILABLE_CODE_POINT)],
'-1 (HTML)' => [false, -1, new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'-1 (fatal)' => [true, -1, new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'0x110000 (HTML)' => [false, 0x110000, new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'0x110000 (fatal)' => [true, 0x110000, new EncoderException("", Encoding::E_INVALID_CODE_POINT)],
'U+00CA (fatal)' => [true, 0xCA, new EncoderException("", Coder::E_UNAVAILABLE_CODE_POINT)],
'-1 (HTML)' => [false, -1, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
'-1 (fatal)' => [true, -1, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
'0x110000 (HTML)' => [false, 0x110000, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
'0x110000 (fatal)' => [true, 0x110000, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
];
}

2
tests/cases/TestEncoding.php

@ -57,7 +57,7 @@ class TestEncoding extends \PHPUnit\Framework\TestCase {
$file = basename($file, ".php");
$className = $ns.$file;
$class = new \ReflectionClass($className);
if ($class->implementsInterface(\MensBeam\Intl\Encoding\Encoding::class) && $class->isInstantiable()) {
if ($class->implementsInterface(\MensBeam\Intl\Encoding\Decoder::class) && $class->isInstantiable()) {
$name = $class->getConstant("NAME");
$names[$name] = $className;
foreach ($class->getConstant("LABELS") as $label) {

4
tools/mklabels.php

@ -3,7 +3,7 @@
// class in the Encoding set and generates tables mapping labels
// to names and names to classes
use MensBeam\Intl\Encoding\Encoding;
use MensBeam\Intl\Encoding\Decoder;
define("BASE", dirname(__DIR__).DIRECTORY_SEPARATOR);
require_once BASE."vendor".DIRECTORY_SEPARATOR."autoload.php";
@ -15,7 +15,7 @@ foreach (new \GlobIterator(BASE."/lib/Encoding/*.php", \FilesystemIterator::CURR
$file = basename($file, ".php");
$className = $ns.$file;
$class = new \ReflectionClass($className);
if ($class->implementsInterface(Encoding::class) && $class->isInstantiable()) {
if ($class->implementsInterface(Decoder::class) && $class->isInstantiable()) {
$name = $class->getConstant("NAME");
$names[$name] = $className;
foreach ($class->getConstant("LABELS") as $label) {

8
tools/test.js

@ -7,7 +7,7 @@ var encoding = document.getElementsByTagName("meta")[0].getAttribute("charset");
function encodeCodePoint(code, fatal) {
if (code < 0 || code > 0x10FFFF) {
return 'new EncoderException("", Encoding::E_INVALID_CODE_POINT)';
return 'new EncoderException("", Coder::E_INVALID_CODE_POINT)';
} else {
var l = document.createElement("a");
l.href = "http://example.com/?" + String.fromCodePoint(code) + "#";
@ -17,7 +17,7 @@ function encodeCodePoint(code, fatal) {
if ((url.charAt(a) == "%" && url.substr(a, 6) == "%26%23") || url.charAt(a) == "&") {
// character cannot be encoded
if (fatal) {
return 'new EncoderException("", Encoding::E_UNAVAILABLE_CODE_POINT)';
return 'new EncoderException("", Coder::E_UNAVAILABLE_CODE_POINT)';
} else {
return decodeURIComponent(url);
}
@ -35,7 +35,7 @@ function encodeCodePoint(code, fatal) {
function encodeCodePoints(codes, fatal) {
for (let a = 0; a < codes.length; a++) {
if (codes[a] < 0 || codes[a] > 0x10FFFF) {
return 'new EncoderException("", Encoding::E_INVALID_CODE_POINT)';
return 'new EncoderException("", Coder::E_INVALID_CODE_POINT)';
}
}
var l = document.createElement("a");
@ -43,7 +43,7 @@ function encodeCodePoints(codes, fatal) {
var bytes = [];
let url = decodeURIComponent(l.search.substr(1));
if (fatal && url.indexOf("&#") > -1) {
return 'new EncoderException("", Encoding::E_UNAVAILABLE_CODE_POINT)';
return 'new EncoderException("", Coder::E_UNAVAILABLE_CODE_POINT)';
}
for (let a = 0; a < url.length; a++) {
bytes.push(url.charCodeAt(a).toString(16).padStart(2, "0").toUpperCase());

Loading…
Cancel
Save