Skip to content

Commit f12d58b

Browse files
bug #61144 [VarDumper] Fix dumping on systems that don't have a working iconv (nicolas-grekas)
This PR was merged into the 6.4 branch. Discussion ---------- [VarDumper] Fix dumping on systems that don't have a working iconv | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #61141 | License | MIT Commits ------- c1729a1 [VarDumper] Fix dumping on systems that don't have a working iconv
2 parents 6317c59 + c1729a1 commit f12d58b

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
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
}

src/Symfony/Component/VarDumper/composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
"symfony/polyfill-mbstring": "~1.0"
2222
},
2323
"require-dev": {
24-
"ext-iconv": "*",
2524
"symfony/console": "^5.4|^6.0|^7.0",
2625
"symfony/error-handler": "^6.3|^7.0",
2726
"symfony/http-kernel": "^5.4|^6.0|^7.0",

0 commit comments

Comments
 (0)