From 21985f3b4ef433faa137cd338336d482d45ddeb7 Mon Sep 17 00:00:00 2001 From: epriestley Date: Mon, 24 Oct 2011 10:33:34 -0700 Subject: [PATCH] Add isHeuristicBinaryFile(), a git-like check for binariness of fiels Summary: This is separated from D812. See D812, D1008, D1009. Separation allows us to land these patches safely. See T452 for a broader discussion of the issues involved. Test Plan: See D812. Reviewers: davidreuss, jungejason, nh, tuomaspelkonen, aran Reviewed By: davidreuss CC: aran, davidreuss Differential Revision: 1040 --- src/difference/ArcanistDiffUtils.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/difference/ArcanistDiffUtils.php b/src/difference/ArcanistDiffUtils.php index 9949d464..f41e01dc 100644 --- a/src/difference/ArcanistDiffUtils.php +++ b/src/difference/ArcanistDiffUtils.php @@ -23,6 +23,20 @@ */ final class ArcanistDiffUtils { + /** + * Make a best-effort attempt to determine if a file is definitely binary. + * + * @return bool If true, the file is almost certainly binary. If false, the + * file might still be binary but is subtle about it. + */ + public static function isHeuristicBinaryFile($data) { + // Detect if a file is binary according to the Git heuristic, which is the + // presence of NULL ("\0") bytes. Git only examines the first "few" bytes of + // each file (8KB or so) as an optimization, but we don't have a reasonable + // equivalent in PHP, so just look at all of it. + return (strpos($data, "\0") !== false); + } + public static function renderDifferences( $old, $new,