Skip to content

Commit bcf2afc

Browse files
[VarDumper] Fix dumping on systems that don't have a working iconv
1 parent 3c9cdc6 commit bcf2afc

File tree

1 file changed

+38
-7
lines changed

1 file changed

+38
-7
lines changed

src/Symfony/Component/VarDumper/Dumper/AbstractDumper.php

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,17 +188,48 @@ protected function utf8Encode(?string $s): ?string
188188
return $s;
189189
}
190190

191-
if (!\function_exists('iconv')) {
192-
throw new \RuntimeException('Unable to convert a non-UTF-8 string to UTF-8: required function iconv() does not exist. You should install ext-iconv or symfony/polyfill-iconv.');
191+
if (\function_exists('iconv')) {
192+
if (false !== $c = @iconv($this->charset, 'UTF-8', $s)) {
193+
return $c;
194+
}
195+
if ('CP1252' !== $this->charset && false !== $c = @iconv('CP1252', 'UTF-8', $s)) {
196+
return $c;
197+
}
193198
}
194199

195-
if (false !== $c = @iconv($this->charset, 'UTF-8', $s)) {
196-
return $c;
200+
$s .= $s;
201+
$len = \strlen($s);
202+
$mapCp1252 = false;
203+
204+
for ($i = $len >> 1, $j = 0; $i < $len; ++$i, ++$j) {
205+
if ($s[$i] < "\x80") {
206+
$s[$j] = $s[$i];
207+
} elseif ($s[$i] < "\xC0") {
208+
$s[$j] = "\xC2";
209+
$s[++$j] = $s[$i];
210+
if ($s[$i] < "\xA0") {
211+
$mapCp1252 = true;
212+
}
213+
} else {
214+
$s[$j] = "\xC3";
215+
$s[++$j] = \chr(\ord($s[$i]) - 64);
216+
}
197217
}
198-
if ('CP1252' !== $this->charset && false !== $c = @iconv('CP1252', 'UTF-8', $s)) {
199-
return $c;
218+
219+
$s = substr($s, 0, $j);
220+
221+
if (!$mapCp1252) {
222+
return $s;
200223
}
201224

202-
return iconv('CP850', 'UTF-8', $s);
225+
return strtr($s, [
226+
"\xC2\x80" => '', "\xC2\x82" => '', "\xC2\x83" => 'ƒ', "\xC2\x84" => '',
227+
"\xC2\x85" => '', "\xC2\x86" => '', "\xC2\x87" => '', "\xC2\x88" => 'ˆ',
228+
"\xC2\x89" => '', "\xC2\x8A" => 'Š', "\xC2\x8B" => '', "\xC2\x8C" => 'Œ',
229+
"\xC2\x8D" => 'Ž', "\xC2\x91" => '', "\xC2\x92" => '', "\xC2\x93" => '',
230+
"\xC2\x94" => '', "\xC2\x95" => '', "\xC2\x96" => '', "\xC2\x97" => '',
231+
"\xC2\x98" => '˜', "\xC2\x99" => '', "\xC2\x9A" => 'š', "\xC2\x9B" => '',
232+
"\xC2\x9C" => 'œ', "\xC2\x9E" => 'ž',
233+
]);
203234
}
204235
}

0 commit comments

Comments
 (0)