From 26e781ef5a66a776310a4a47ef02f122e1dc0170 Mon Sep 17 00:00:00 2001 From: Aaron Gyes Date: Thu, 29 Dec 2016 03:42:08 -0800 Subject: [PATCH] Use BC_LINE_LENGTH=2 for bc. It's not the case that macOS and old BC doesn't respect this environment variable, just that they don't have special behavior when it's set to 0. However, there is rather universal favorable behavior with a value of 2. Output is of the form: \ 999999999999999999999999999999999... with the second line being arbitrarily long. So just grab that line instead of stitching with `string`. This can yield a 25-30% speedup. --- share/functions/math.fish | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/share/functions/math.fish b/share/functions/math.fish index ed638c695..806d3f570 100644 --- a/share/functions/math.fish +++ b/share/functions/math.fish @@ -21,9 +21,12 @@ function math --description "Perform math calculations in bc" return 2 # no arguments is an error end - # Stitch lines together manually. We can't rely on BC_LINE_LENGTH because some systems don't - # have a new enough version of bc. - set -l out (echo "scale=$scale; $argv" | bc | string replace -r '\\\\$' '' | string join '') + # With BC_LINE_LENGTH set to 2, the first line is a slash and the following line can be arbitrarily long. + # We can't set this to 0 because some systems don't have a new enough version of bc. + set -lx BC_LINE_LENGTH 2 + set -l out (echo "scale=$scale; $argv" | bc) + set -q out[2] + and set out $out[2] switch "$out" case '' # No output indicates an error occurred.