From be3ce781bb3d44e0e9b4b73e0c6be89e2e120052 Mon Sep 17 00:00:00 2001 From: epriestley Date: Sun, 12 Aug 2012 08:50:01 -0700 Subject: [PATCH] Switch arcanist to phutil_utf8_convert() Summary: See D3252. Reduces code duplication a little bit. Also remove some dire warnings about impending doom -- this has been in use in the wild for a long time. Test Plan: Added a file in ISO-8859-1, ran `arc diff --encoding ISO-8859-1` to generate this revision, got an encoding note in output. Reviewers: davidreuss, vrana, btrahan Reviewed By: davidreuss CC: aran Maniphest Tasks: T452 Differential Revision: https://secure.phabricator.com/D3253 --- src/parser/ArcanistBundle.php | 12 ++---------- src/parser/ArcanistDiffParser.php | 17 ++++++++--------- src/workflow/ArcanistDiffWorkflow.php | 10 ++-------- 3 files changed, 12 insertions(+), 27 deletions(-) diff --git a/src/parser/ArcanistBundle.php b/src/parser/ArcanistBundle.php index 2eb3d9d9..ebf2e095 100644 --- a/src/parser/ArcanistBundle.php +++ b/src/parser/ArcanistBundle.php @@ -399,16 +399,8 @@ final class ArcanistBundle { } private function convertNonUTF8Diff($diff) { - $try_encoding_is_non_utf8 = - ($this->encoding && strtoupper($this->encoding) != 'UTF-8'); - if ($try_encoding_is_non_utf8) { - $diff = mb_convert_encoding($diff, $this->encoding, 'UTF-8'); - if (!$diff) { - throw new Exception( - "Attempted conversion of diff to encoding ". - "'{$this->encoding}' failed. Have you specified ". - "the proper encoding correctly?"); - } + if ($this->encoding) { + $diff = phutil_utf8_convert($diff, $this->encoding, 'UTF-8'); } return $diff; } diff --git a/src/parser/ArcanistDiffParser.php b/src/parser/ArcanistDiffParser.php index 31b74041..fcada2a0 100644 --- a/src/parser/ArcanistDiffParser.php +++ b/src/parser/ArcanistDiffParser.php @@ -913,18 +913,17 @@ final class ArcanistDiffParser { $is_binary = false; if ($this->detectBinaryFiles) { $is_binary = !phutil_is_utf8($corpus); + $try_encoding = $this->tryEncoding; - if ($is_binary && $this->tryEncoding) { + if ($is_binary && $try_encoding) { $is_binary = ArcanistDiffUtils::isHeuristicBinaryFile($corpus); if (!$is_binary) { - // NOTE: This feature is HIGHLY EXPERIMENTAL and will cause a lot - // of issues. Use it at your own risk. - $corpus = mb_convert_encoding( - $corpus, 'UTF-8', $this->tryEncoding); - if (!phutil_is_utf8($corpus)) { - throw new Exception( - 'Failed converting hunk to '.$this->tryEncoding); - } + $corpus = phutil_utf8_convert($corpus, 'UTF-8', $try_encoding); + if (!phutil_is_utf8($corpus)) { + throw new Exception( + "Failed to convert a hunk from '{$try_encoding}' to UTF-8. ". + "Check that the specified encoding is correct."); + } } } diff --git a/src/workflow/ArcanistDiffWorkflow.php b/src/workflow/ArcanistDiffWorkflow.php index 256b7676..fa850845 100644 --- a/src/workflow/ArcanistDiffWorkflow.php +++ b/src/workflow/ArcanistDiffWorkflow.php @@ -916,14 +916,8 @@ EOTEXT } } - if ($try_encoding && $try_encoding != 'UTF-8') { - if (!function_exists('mb_convert_encoding')) { - throw new ArcanistUsageException( - "This diff includes a file encoded in '{$try_encoding}', ". - "but you don't have the PHP mbstring extension installed ". - "so it can't be converted to UTF-8. Install mbstring."); - } - $corpus = mb_convert_encoding($corpus, 'UTF-8', $try_encoding); + if ($try_encoding) { + $corpus = phutil_utf8_convert($corpus, 'UTF-8', $try_encoding); $name = $change->getCurrentPath(); if (phutil_is_utf8($corpus)) { $this->writeStatusMessage( -- 2.43.0