Browse Source

Add label matcher

span
J. King 4 years ago
parent
commit
f9e3d795a7
  1. 6
      CHANGELOG
  2. 36
      lib/Encoding.php
  3. 4
      tools/mkindex.php
  4. 40
      tools/mklabels.php

6
CHANGELOG

@ -1,9 +1,9 @@
Version 0.6.0 (2019-12-18)
Version 0.7.0 (2019-12-20)
==========================
New features:
- Added $allowSurrogates parameter to Encoding constructor
- Added posErr public instance property to Encoding
- Added \MensBeam\Intl\Encoding abstract class with createDecoder() and
matchLabel() static methods
Version 0.5.0 (2019-12-13)
==========================

36
lib/Encoding.php

File diff suppressed because one or more lines are too long

4
tools/mkindex.php

@ -1,4 +1,7 @@
<?php
// This script produces the index lookup tables
// for a given encoding from the source data at WHATWG
$labels = [
'big5' => "big5",
//'euc-jp' => "eucjp",
@ -160,6 +163,7 @@ function make_decoder_point_array(array $entries): string {
function make_decoder_char_array(array $entries): string {
$out = [];
$i = 0;
foreach ($entries as $match) {
$index = (int) $match[1];
$code = $match[2];

40
tools/mklabels.php

@ -0,0 +1,40 @@
<?php
// this script read and names and labels from each concrete
// class in the Encoding set and generates tables mapping labels
// to names and names to classes
use MensBeam\Intl\Encoding\Encoding;
define("BASE", dirname(__DIR__).DIRECTORY_SEPARATOR);
require_once BASE."vendor".DIRECTORY_SEPARATOR."autoload.php";
$ns = "\\MensBeam\\Intl\\Encoding\\";
$labels = [];
$names = [];
foreach (new \GlobIterator(BASE."/lib/Encoding/*.php", \FilesystemIterator::CURRENT_AS_PATHNAME) as $file) {
$file = basename($file, ".php");
$className = $ns.$file;
$class = new \ReflectionClass($className);
if ($class->implementsInterface(Encoding::class) && $class->isInstantiable()) {
$name = $class->getConstant("NAME");
$names[$name] = $className;
foreach ($class->getConstant("LABELS") as $label) {
$labels[$label] = $name;
}
}
}
$labelList = [];
foreach ($labels as $k => $v) {
$labelList[] = "'$k'=>\"$v\"";
}
$labelList = "const LABEL_MAP = [".implode(",", $labelList)."];";
$nameList = [];
foreach ($names as $k => $v) {
$nameList[] = "'$k'=>$v::class";
}
$nameList = "const NAME_MAP = [".implode(",", $nameList)."];";
echo "$labelList\n";
echo "$nameList\n";
Loading…
Cancel
Save