Skip to content

Commit

Permalink
bug #18540 Replace iconv_*() uses by mb_*(), add mbstring polyfill wh…
Browse files Browse the repository at this point in the history
…en required (nicolas-grekas)

This PR was merged into the 2.8 branch.

Discussion
----------

Replace iconv_*() uses by mb_*(), add mbstring polyfill when required

| Q             | A
| ------------- | ---
| Branch?       | 2.8
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #18539
| License       | MIT
| Doc PR        | -

Looks like `iconv_*()` functions can be really slow (see #18539).

Commits
-------

27f5f81 Replace iconv_*() uses by mb_*(), add mbstring polyfill when required
  • Loading branch information
fabpot committed Apr 15, 2016
2 parents db31b56 + 27f5f81 commit 5356dbb
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/Symfony/Bridge/Doctrine/Logger/DbalLogger.php
Expand Up @@ -95,8 +95,8 @@ private function normalizeParams(array $params)
}

// detect if the too long string must be shorten
if (self::MAX_STRING_LENGTH < iconv_strlen($params[$index], 'UTF-8')) {
$params[$index] = iconv_substr($params[$index], 0, self::MAX_STRING_LENGTH - 6, 'UTF-8').' [...]';
if (self::MAX_STRING_LENGTH < mb_strlen($params[$index], 'UTF-8')) {
$params[$index] = mb_substr($params[$index], 0, self::MAX_STRING_LENGTH - 6, 'UTF-8').' [...]';
continue;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Bridge/Doctrine/composer.json
Expand Up @@ -17,7 +17,8 @@
],
"require": {
"php": ">=5.3.9",
"doctrine/common": "~2.4"
"doctrine/common": "~2.4",
"symfony/polyfill-mbstring": "~1.0"
},
"require-dev": {
"symfony/stopwatch": "~2.2|~3.0.0",
Expand Down
Expand Up @@ -40,13 +40,10 @@ public function validate($value, Constraint $constraint)

$stringValue = (string) $value;

if ('UTF8' === $charset = strtoupper($constraint->charset)) {
$charset = 'UTF-8'; // iconv on Windows requires "UTF-8" instead of "UTF8"
if (!$invalidCharset = !@mb_check_encoding($stringValue, $constraint->charset)) {
$length = mb_strlen($stringValue, $constraint->charset);
}

$length = @iconv_strlen($stringValue, $charset);
$invalidCharset = false === $length;

if ($invalidCharset) {
if ($this->context instanceof ExecutionContextInterface) {
$this->context->buildViolation($constraint->charsetMessage)
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/composer.json
Expand Up @@ -17,6 +17,7 @@
],
"require": {
"php": ">=5.3.9",
"symfony/polyfill-mbstring": "~1.0",
"symfony/translation": "~2.4|~3.0.0"
},
"require-dev": {
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/VarDumper/Cloner/VarCloner.php
Expand Up @@ -102,12 +102,12 @@ protected function doClone($var)
} else {
$stub->value = $v;
}
} elseif (0 <= $maxString && isset($v[1 + ($maxString >> 2)]) && 0 < $cut = iconv_strlen($v, 'UTF-8') - $maxString) {
} elseif (0 <= $maxString && isset($v[1 + ($maxString >> 2)]) && 0 < $cut = mb_strlen($v, 'UTF-8') - $maxString) {
$stub = new Stub();
$stub->type = Stub::TYPE_STRING;
$stub->class = Stub::STRING_UTF8;
$stub->cut = $cut;
$stub->value = iconv_substr($v, 0, $maxString, 'UTF-8');
$stub->value = mb_substr($v, 0, $maxString, 'UTF-8');
}
break;

Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/VarDumper/Dumper/CliDumper.php
Expand Up @@ -169,7 +169,7 @@ public function dumpString(Cursor $cursor, $str, $bin, $cut)
$this->dumpLine($cursor->depth, true);
} else {
$attr = array(
'length' => 0 <= $cut ? iconv_strlen($str, 'UTF-8') + $cut : 0,
'length' => 0 <= $cut ? mb_strlen($str, 'UTF-8') + $cut : 0,
'binary' => $bin,
);
$str = explode("\n", $str);
Expand All @@ -195,8 +195,8 @@ public function dumpString(Cursor $cursor, $str, $bin, $cut)
if ($i < $m) {
$str .= "\n";
}
if (0 < $this->maxStringWidth && $this->maxStringWidth < $len = iconv_strlen($str, 'UTF-8')) {
$str = iconv_substr($str, 0, $this->maxStringWidth, 'UTF-8');
if (0 < $this->maxStringWidth && $this->maxStringWidth < $len = mb_strlen($str, 'UTF-8')) {
$str = mb_substr($str, 0, $this->maxStringWidth, 'UTF-8');
$lineCut = $len - $this->maxStringWidth;
}
if ($m && 0 < $cursor->depth) {
Expand Down

0 comments on commit 5356dbb

Please sign in to comment.