public static function testEncodingSupportsASCII($encoding, $bypass = false)
{
// All calls to iconv here are unsafe, proof by case analysis:
// If ICONV_OK, no difference.
// If ICONV_TRUNCATE, all calls involve one character inputs,
// so bug is not triggered.
// If ICONV_UNUSABLE, this call is irrelevant
static $encodings = array();
if (!$bypass) {
if (isset($encodings[$encoding])) {
return $encodings[$encoding];
}
$lenc = strtolower($encoding);
switch ($lenc) {
case 'shift_jis':
return array("¥" => '\\', "‾" => '~');
case 'johab':
return array("₩" => '\\');
}
if (strpos($lenc, 'iso-8859-') === 0) {
return array();
}
}
$ret = array();
if (self::unsafeIconv('UTF-8', $encoding, 'a') === false) {
return false;
}
for ($i = 0x20; $i <= 0x7e; $i++) {
// all printable ASCII chars
$c = chr($i);
// UTF-8 char
$r = self::unsafeIconv('UTF-8', "{$encoding}//IGNORE", $c);
// initial conversion
if ($r === '' || $r === $c && self::unsafeIconv($encoding, 'UTF-8//IGNORE', $r) !== $c) {
// Reverse engineer: what's the UTF-8 equiv of this byte
// sequence? This assumes that there's no variable width
// encoding that doesn't support ASCII.
$ret[self::unsafeIconv($encoding, 'UTF-8//IGNORE', $c)] = $c;
}
}
$encodings[$encoding] = $ret;
return $ret;
}