From 4df0644bc21ac634749cf894294bec1f7684b622 Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Fri, 25 Oct 2019 15:35:20 -0400 Subject: [PATCH] Refactor array_replace_range and syntax_to_color --- src/functions.php | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/src/functions.php b/src/functions.php index 2a19ff3..643075a 100644 --- a/src/functions.php +++ b/src/functions.php @@ -269,36 +269,28 @@ function read_stdout(int $len = 128): string */ function array_replace_range(array &$array, int $offset, int $length, $value):void { - $end = $offset + $length; + $replacement = array_fill(0, $length, $value); + array_splice($array, $offset, $length, $replacement); + + /* $end = $offset + $length; for ($i = $offset; $i < $end; $i++) { $array[$i] = $value; - } + } */ } function syntax_to_color(int $hl): int { - switch ($hl) - { - case Highlight::COMMENT: - return 36; // Foreground Cyan + $map = [ + Highlight::COMMENT => 36, // Foreground Cyan, + Highlight::KEYWORD1 => 33, // Foreground Yellow + Highlight::KEYWORD2 => 32, // Foreground Green + Highlight::STRING => 35, // Foreground Magenta + Highlight::NUMBER => 31, // Foreground Red + Highlight::MATCH => 34, // Foreground Blue + ]; - case Highlight::KEYWORD1: - return 33; // Foreground Yellow - - case Highlight::KEYWORD2: - return 32; // Foreground Green - - case Highlight::STRING: - return 35; // Foreground Magenta - - case Highlight::NUMBER: - return 31; // Foreground Red - - case Highlight::MATCH: - return 34; // Foreground Blue - - default: - return 37; // Foreground White - } + return (array_key_exists($hl, $map)) + ? $map[$hl] + : 37; // Foreground White }