From e32a2894c41153d44add39b4978951f91c67e683 Mon Sep 17 00:00:00 2001
From: SheetJS <dev@sheetjs.com>
Date: Fri, 13 Jun 2014 11:02:06 -0400
Subject: [PATCH] version bump 0.8.0: performance

- separate i4/r8 code (core functions optimized by v8)
- prefer faster methods when convenient
- node-based performance tests
---
 .npmignore         |   2 +
 Makefile           |   4 +
 misc/perf.js       |  77 ++++
 misc/perf.sh       |  13 +
 package.json       |   6 +-
 ssf.js             | 776 ++++++++++++++++++++++--------------
 ssf.md             | 966 +++++++++++++++++++++++++++++----------------
 test/date.js       |  12 +-
 test/exp.js        |   2 +-
 test/implied.js    |   2 +-
 test/oddities.js   |   2 +-
 test/oddities.json |  33 +-
 12 files changed, 1241 insertions(+), 654 deletions(-)
 mode change 100644 => 100755 Makefile
 create mode 100644 misc/perf.js
 create mode 100755 misc/perf.sh

diff --git a/.npmignore b/.npmignore
index 6b40d6b..19fb245 100644
--- a/.npmignore
+++ b/.npmignore
@@ -3,3 +3,5 @@ node_modules/
 tmp/
 .gitignore
 .vocrc
+v8.log
+perf.log
diff --git a/Makefile b/Makefile
old mode 100644
new mode 100755
index 52dab07..735ff4f
--- a/Makefile
+++ b/Makefile
@@ -13,6 +13,10 @@ lint:
 	jshint ssf.js test/
 	jscs ssf.js
 
+.PHONY: perf
+perf:
+	bash misc/perf.sh
+
 .PHONY: cov
 cov: tmp/coverage.html
 
diff --git a/misc/perf.js b/misc/perf.js
new file mode 100644
index 0000000..d6b2260
--- /dev/null
+++ b/misc/perf.js
@@ -0,0 +1,77 @@
+/* vim: set ts=2: */
+/*jshint loopfunc:true */
+var SSF = require('../');
+var fs = require('fs')//, assert = require('assert');
+var data = JSON.parse(fs.readFileSync('./test/oddities.json','utf8'));
+var dates = fs.readFileSync('./test/dates.tsv','utf8').split("\n");
+var date2 = fs.readFileSync('./test/cal.tsv',  'utf8').split("\n");
+var times = fs.readFileSync('./test/times.tsv','utf8').split("\n");
+function doit(data) {
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  var headers = data[0].split("\t");
+  for(var k = 1; k <= data.length; ++k) {
+    if(data[k] == null) return;
+    var d = data[k].replace(/#{255}/g,"").split("\t");
+    for(var w = 1; w < headers.length; ++w) {
+      var expected = d[w], actual = SSF.format(headers[w], parseFloat(d[0]), {});
+    }
+  }
+}
+
+function testit() {
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  /* Prevent Optimization */
+  doit(times.slice(0,4000));
+  doit(dates.slice(0,4000));
+    for(var i = 0; i != 1000; ++i) {
+    for(var k = 0; k != data.length; ++k) {
+      var d = data[k];
+      for(var j=1;j<d.length;++j) {
+        if(d[j].length == 2) {
+          var expected = d[j][1], actual = SSF.format(d[0], d[j][0], {});
+          //if(actual != expected) console.log(d[j]);
+        }
+      }
+    }
+  }
+}
+
+testit();
diff --git a/misc/perf.sh b/misc/perf.sh
new file mode 100755
index 0000000..8f4875f
--- /dev/null
+++ b/misc/perf.sh
@@ -0,0 +1,13 @@
+#!/bin/bash
+ARGS="--trace_opt --trace_deopt --trace_inlining --code_comments"
+#ARGS="--trace_opt --trace_deopt --code_comments"
+SCPT=misc/perf.js
+
+echo 1
+make && jshint --show-non-errors ssf.js && make lint &&
+MINTEST=1 mocha -b && time node $SCPT && {
+node $ARGS $SCPT > perf.log
+node --prof $SCPT
+echo 1; time node $SCPT >/dev/null
+echo 1; time node $SCPT >/dev/null
+} && grep disabled perf.log
diff --git a/package.json b/package.json
index c07ad86..4cc1431 100644
--- a/package.json
+++ b/package.json
@@ -1,13 +1,13 @@
 {
   "name": "ssf",
-  "version": "0.7.1",
+  "version": "0.8.0",
   "author": "SheetJS",
-  "description": "pure-JS library to format data using ECMA-376 spreadsheet Format Codes",
+  "description": "Format data using ECMA-376 spreadsheet Format Codes",
   "keywords": [ "format", "sprintf", "spreadsheet" ],
   "main": "ssf.js",
   "dependencies": {
     "voc":"",
-    "colors":"",
+    "colors":"0.6.2",
     "frac":"0.3.1"
   },
   "devDependencies": {
diff --git a/ssf.js b/ssf.js
index 4263ca1..39e7751 100644
--- a/ssf.js
+++ b/ssf.js
@@ -1,18 +1,27 @@
 /* ssf.js (C) 2013-2014 SheetJS -- http://sheetjs.com */
+/*jshint -W041 */
 var SSF = {};
-var make_ssf = function(SSF){
-var _strrev = function(x) { return String(x).split("").reverse().join("");};
-function fill(c,l) { return new Array(l+1).join(c); }
-function pad(v,d,c){var t=String(v);return t.length>=d?t:(fill(c||0,d-t.length)+t);}
-function rpad(v,d,c){var t=String(v);return t.length>=d?t:(t+fill(c||0,d-t.length));}
-SSF.version = '0.7.1';
+var make_ssf = function make_ssf(SSF){
+SSF.version = '0.8.0';
+function _strrev(x) { var o = "", i = x.length-1; while(i>=0) o += x.charAt(i--); return o; }
+function fill(c,l) { var o = ""; while(o.length < l) o+=c; return o; }
+function pad0(v,d){var t=""+v; return t.length>=d?t:fill('0',d-t.length)+t;}
+function pad_(v,d){var t=""+v;return t.length>=d?t:fill(' ',d-t.length)+t;}
+function rpad_(v,d){var t=""+v; return t.length>=d?t:t+fill(' ',d-t.length);}
+function pad0r1(v,d){var t=""+Math.round(v); return t.length>=d?t:fill('0',d-t.length)+t;}
+function pad0r2(v,d){var t=""+v; return t.length>=d?t:fill('0',d-t.length)+t;}
+var p2_32 = Math.pow(2,32);
+function pad0r(v,d){if(v>p2_32||v<-p2_32) return pad0r1(v,d); var i = Math.round(v); return pad0r2(i,d); }
+function isgeneral(s, i) { return s.length >= 7 + i && (s.charCodeAt(i)|32) === 103 && (s.charCodeAt(i+1)|32) === 101 && (s.charCodeAt(i+2)|32) === 110 && (s.charCodeAt(i+3)|32) === 101 && (s.charCodeAt(i+4)|32) === 114 && (s.charCodeAt(i+5)|32) === 97 && (s.charCodeAt(i+6)|32) === 108; }
 /* Options */
-var opts_fmt = {
-  date1904:0,
-  output:"",
-  WTF:false
-};
-function fixopts(o){for(var y in opts_fmt) if(o[y]===undefined) o[y]=opts_fmt[y];}
+var opts_fmt = [
+  ["date1904", 0],
+  ["output", ""],
+  ["WTF", false]
+];
+function fixopts(o){
+  for(var y = 0; y != opts_fmt.length; ++y) if(o[opts_fmt[y][0]]===undefined) o[opts_fmt[y][0]]=opts_fmt[y][1];
+}
 SSF.opts = opts_fmt;
 var table_fmt = {
   0:  'General',
@@ -69,7 +78,7 @@ var months = [
   ['N', 'Nov', 'November'],
   ['D', 'Dec', 'December']
 ];
-var frac = function frac(x, D, mixed) {
+function frac(x, D, mixed) {
   var sgn = x < 0 ? -1 : 1;
   var B = x * sgn;
   var P_2 = 0, P_1 = 1, P = 0;
@@ -90,40 +99,58 @@ var frac = function frac(x, D, mixed) {
   if(Q===0) throw "Unexpected state: "+P+" "+P_1+" "+P_2+" "+Q+" "+Q_1+" "+Q_2;
   var q = Math.floor(sgn * P/Q);
   return [q, sgn*P - q*Q, Q];
-};
-var general_fmt = function(v, opts) {
-  if(typeof v === 'boolean') return v ? "TRUE" : "FALSE";
-  if(typeof v === 'number') {
-    var o, V = v < 0 ? -v : v;
-    if(V >= 0.1 && V < 1) o = v.toPrecision(9);
-    else if(V >= 0.01 && V < 0.1) o = v.toPrecision(8);
-    else if(V >= 0.001 && V < 0.01) o = v.toPrecision(7);
-    else if(V >= 0.0001 && V < 0.001) o = v.toPrecision(6);
-    else if(V >= Math.pow(10,10) && V < Math.pow(10,11)) o = v.toFixed(10).substr(0,12);
-    else if(V > Math.pow(10,-9) && V < Math.pow(10,11)) {
-      o = v.toFixed(12).replace(/(\.[0-9]*[1-9])0*$/,"$1").replace(/\.$/,"");
-      if(o.length > (v<0?12:11)) o = v.toPrecision(10);
-      if(o.length > (v<0?12:11)) o = v.toExponential(5);
-    }
-    else {
-      o = v.toFixed(11).replace(/(\.[0-9]*[1-9])0*$/,"$1");
-      if(o.length > (v<0?12:11)) o = v.toPrecision(6);
-    }
-    o = o.replace(/(\.[0-9]*[1-9])0+e/,"$1e").replace(/\.0*e/,"e");
-    return o.replace("e","E").replace(/\.0*$/,"").replace(/\.([0-9]*[^0])0*$/,".$1").replace(/(E[+-])([0-9])$/,"$1"+"0"+"$2");
+}
+function general_fmt_int(v, opts) { return ""+v; }
+SSF._general_int = general_fmt_int;
+var general_fmt_num = (function make_general_fmt_num() {
+var gnr1 = /\.(\d*[1-9])0+$/, gnr2 = /\.0*$/, gnr4 = /\.(\d*[1-9])0+/, gnr5 = /\.0*[Ee]/, gnr6 = /(E[+-])(\d)$/;
+function gfn2(v) {
+  var w = (v<0?12:11);
+  var o = gfn5(v.toFixed(12)); if(o.length <= w) return o;
+  o = v.toPrecision(10); if(o.length <= w) return o;
+  return v.toExponential(5);
+}
+function gfn3(v) {
+  var o = v.toFixed(11).replace(gnr1,".$1");
+  if(o.length > (v<0?12:11)) o = v.toPrecision(6);
+  return o;
+}
+function gfn4(o) {
+  for(var i = 0; i != o.length; ++i) if((o.charCodeAt(i) | 0x20) === 101) return o.replace(gnr4,".$1").replace(gnr5,"E").replace("e","E").replace(gnr6,"$10$2");
+  return o;
+}
+function gfn5(o) {
+  //for(var i = 0; i != o.length; ++i) if(o.charCodeAt(i) === 46) return o.replace(gnr2,"").replace(gnr1,".$1");
+  //return o;
+  return o.indexOf(".") > -1 ? o.replace(gnr2,"").replace(gnr1,".$1") : o;
+}
+return function general_fmt_num(v, opts) {
+  var V = Math.floor(Math.log(Math.abs(v))*Math.LOG10E), o;
+  if(V >= -4 && V <= -1) o = v.toPrecision(10+V);
+  else if(Math.abs(V) <= 9) o = gfn2(v);
+  else if(V === 10) o = v.toFixed(10).substr(0,12);
+  else o = gfn3(v);
+  return gfn5(gfn4(o));
+};})();
+SSF._general_num = general_fmt_num;
+function general_fmt(v, opts) {
+  switch(typeof v) {
+    case 'string': return v;
+    case 'boolean': return v ? "TRUE" : "FALSE";
+    case 'number': return (v|0) === v ? general_fmt_int(v, opts) : general_fmt_num(v, opts);
   }
-  if(typeof v === 'string') return v;
   throw new Error("unsupported value in General format: " + v);
-};
+}
 SSF._general = general_fmt;
 function fix_hijri(date, o) { return 0; }
-var parse_date_code = function parse_date_code(v,opts,b2) {
-  var date = Math.floor(v), time = Math.floor(86400 * (v - date)+1e-6), dow=0;
+function parse_date_code(v,opts,b2) {
+  if(v > 2958465 || v < 0) return null;
+  var date = (v|0), time = Math.floor(86400 * (v - date)), dow=0;
   var dout=[];
   var out={D:date, T:time, u:86400*(v-date)-time,y:0,m:0,d:0,H:0,M:0,S:0,q:0};
-  fixopts(opts = (opts||{}));
+  if(Math.abs(out.u) < 1e-6) out.u = 0;
+  fixopts(opts != null ? opts : (opts=[]));
   if(opts.date1904) date += 1462;
-  if(date > 2958465) return null;
   if(out.u > 0.999) {
     out.u = 0;
     if(++time == 86400) { time = 0; ++date; }
@@ -146,223 +173,374 @@ var parse_date_code = function parse_date_code(v,opts,b2) {
   out.H = time;
   out.q = dow;
   return out;
-};
+}
 SSF.parse_date_code = parse_date_code;
 /*jshint -W086 */
-var write_date = function(type, fmt, val, ss0) {
-  var o, ss, tt, y = val.y, sss0;
+function write_date(type, fmt, val, ss0) {
+  var o="", ss=0, tt=0, y = val.y, out, outl = 0;
   switch(type) {
-    case 'b': y = val.y + 543;
+    case 98: /* 'b' buddhist year */
+      y = val.y + 543;
       /* falls through */
-    case 'y': switch(fmt.length) { /* year */
-      case 1: case 2: return pad(y % 100,2);
-      default: return pad(y % 10000,4);
-    }
-    case 'm': switch(fmt) {
-      case 'm': return val.m;
-      case 'mm': return pad(val.m,2);
-      case 'mmm': return months[val.m-1][1];
-      case 'mmmmm': return months[val.m-1][0];
+    case 121: /* 'y' year */
+    switch(fmt.length) {
+      case 1: case 2: out = y % 100; outl = 2; break;
+      default: out = y % 10000; outl = 4; break;
+    } break;
+    case 109: /* 'm' month */
+    switch(fmt.length) {
+      case 1: case 2: out = val.m; outl = fmt.length; break;
+      case 3: return months[val.m-1][1];
+      case 5: return months[val.m-1][0];
       default: return months[val.m-1][2];
-    }
-    case 'd': switch(fmt) { /* day */
-      case 'd': return val.d;
-      case 'dd': return pad(val.d,2);
-      case 'ddd': return days[val.q][0];
+    } break;
+    case 100: /* 'd' day */
+    switch(fmt.length) {
+      case 1: case 2: out = val.d; outl = fmt.length; break;
+      case 3: return days[val.q][0];
       default: return days[val.q][1];
-    }
-    case 'h': switch(fmt) { /* 12-hour */
-      case 'h': return 1+(val.H+11)%12;
-      case 'hh': return pad(1+(val.H+11)%12, 2);
+    } break;
+    case 104: /* 'h' 12-hour */
+    switch(fmt.length) {
+      case 1: case 2: out = 1+(val.H+11)%12; outl = fmt.length; break;
       default: throw 'bad hour format: ' + fmt;
-    }
-    case 'H': switch(fmt) { /* 24-hour */
-      case 'h': return val.H;
-      case 'hh': return pad(val.H, 2);
+    } break;
+    case 72: /* 'H' 24-hour */
+    switch(fmt.length) {
+      case 1: case 2: out = val.H; outl = fmt.length; break;
       default: throw 'bad hour format: ' + fmt;
-    }
-    case 'M': switch(fmt) { /* minutes */
-      case 'm': return val.M;
-      case 'mm': return pad(val.M, 2);
+    } break;
+    case 77: /* 'M' minutes */
+    switch(fmt.length) {
+      case 1: case 2: out = val.M; outl = fmt.length; break;
       default: throw 'bad minute format: ' + fmt;
+    } break;
+    case 115: /* 's' seconds */
+    if(val.u === 0) switch(fmt) {
+      case 's': case 'ss': return pad0(val.S, fmt.length);
+      case '.0': case '.00': case '.000':
     }
-    case 's': switch(fmt) { /* seconds */
+    switch(fmt) {
       case 's': case 'ss': case '.0': case '.00': case '.000':
-        sss0 = ss0 || 0;
-        tt = Math.pow(10,sss0);
+        if(ss0 >= 2) tt = ss0 === 3 ? 1000 : 100;
+        else tt = ss0 === 1 ? 10 : 1;
         ss = Math.round((tt)*(val.S + val.u));
-        if(fmt === 's') return ss >= 60*tt ? 0 : ss/tt;
-        else if(fmt === 'ss') { if(ss>=60*tt) ss=0; return pad(ss,(2+sss0)).substr(0,2); }
         if(ss >= 60*tt) ss = 0;
-        o = pad(ss,2 + sss0);
+        if(fmt === 's') return ss === 0 ? "0" : ""+ss/tt;
+        o = pad0(ss,2 + ss0);
+        if(fmt === 'ss') return o.substr(0,2);
         return "." + o.substr(2,fmt.length-1);
       default: throw 'bad second format: ' + fmt;
     }
-    case 'Z': switch(fmt) {
-      case '[h]': case '[hh]': o = val.D*24+val.H; break;
-      case '[m]': case '[mm]': o = (val.D*24+val.H)*60+val.M; break;
-      case '[s]': case '[ss]': o = ((val.D*24+val.H)*60+val.M)*60+Math.round(val.S+val.u); break;
+    case 90: /* 'Z' absolute time */
+    switch(fmt) {
+      case '[h]': case '[hh]': out = val.D*24+val.H; break;
+      case '[m]': case '[mm]': out = (val.D*24+val.H)*60+val.M; break;
+      case '[s]': case '[ss]': out = ((val.D*24+val.H)*60+val.M)*60+Math.round(val.S+val.u); break;
       default: throw 'bad abstime format: ' + fmt;
-    } return fmt.length === 3 ? o : pad(o, 2);
-    case 'e': { return val.y; } break;
+    } outl = fmt.length === 3 ? 1 : 2; break;
+    case 101: /* 'e' era */
+      out = y; outl = 1;
   }
-};
+  if(outl > 0) return pad0(out, outl); else return "";
+}
 /*jshint +W086 */
-var commaify = function(s) { return _strrev(_strrev(s).replace(/.../g,"$&,")).replace(/^,/,""); };
-var write_num = function(type, fmt, val) {
-  if(type === '(' && !fmt.match(/\).*[0#]/)) {
-    var ffmt = fmt.replace(/\( */,"").replace(/ \)/,"").replace(/\)/,"");
-    if(val >= 0) return write_num('n', ffmt, val);
-    return '(' + write_num('n', ffmt, -val) + ')';
-  }
-  var mul = 0, o;
-  fmt = fmt.replace(/%/g,function() { mul++; return ""; });
-  if(mul !== 0) return write_num(type, fmt, val * Math.pow(10,2*mul)) + fill("%",mul);
-  fmt = fmt.replace(/(\.0+)(,+)$/g,function($$,$1,$2) { mul=$2.length; return $1; });
-  if(mul !== 0) return write_num(type, fmt, val / Math.pow(10,3*mul));
-  if(fmt.indexOf("E") > -1) {
-    var idx = fmt.indexOf("E") - fmt.indexOf(".") - 1;
-    if(fmt.match(/^#+0.0E\+0$/)) {
-      var period = fmt.indexOf("."); if(period === -1) period=fmt.indexOf('E');
-      var ee = (Number(val.toExponential(0).substr(2+(val<0?1:0))))%period;
-      if(ee < 0) ee += period;
-      o = (val/Math.pow(10,ee)).toPrecision(idx+1+(period+ee)%period);
-      if(!o.match(/[Ee]/)) {
-        var fakee = (Number(val.toExponential(0).substr(2+(val<0?1:0))));
-        if(o.indexOf(".") === -1) o = o[0] + "." + o.substr(1) + "E+" + (fakee - o.length+ee);
-        else o += "E+" + (fakee - ee);
-        while(o.substr(0,2) === "0.") {
-          o = o[0] + o.substr(2,period) + "." + o.substr(2+period);
-          o = o.replace(/^0+([1-9])/,"$1").replace(/^0+\./,"0.");
-        }
-        o = o.replace(/\+-/,"-");
+function commaify(s) {
+  if(s.length <= 3) return s;
+  var j = (s.length % 3), o = s.substr(0,j);
+  for(; j!=s.length; j+=3) o+=(o.length > 0 ? "," : "") + s.substr(j,3);
+  return o;
+}
+var write_num = (function make_write_num(){
+var pct1 = /%/g;
+function write_num_pct(type, fmt, val){
+  var sfmt = fmt.replace(pct1,""), mul = fmt.length - sfmt.length;
+  return write_num(type, sfmt, val * Math.pow(10,2*mul)) + fill("%",mul);
+}
+function write_num_cm(type, fmt, val){
+  var idx = fmt.length - 1;
+  while(fmt.charCodeAt(idx-1) === 44) --idx;
+  return write_num(type, fmt.substr(0,idx), val / Math.pow(10,3*(fmt.length-idx)));
+}
+function write_num_exp(fmt, val){
+  var o;
+  var idx = fmt.indexOf("E") - fmt.indexOf(".") - 1;
+  if(fmt.match(/^#+0.0E\+0$/)) {
+    var period = fmt.indexOf("."); if(period === -1) period=fmt.indexOf('E');
+    var ee = Math.floor(Math.log(Math.abs(val))*Math.LOG10E)%period;
+    if(ee < 0) ee += period;
+    o = (val/Math.pow(10,ee)).toPrecision(idx+1+(period+ee)%period);
+    if(o.indexOf("e") === -1) {
+      var fakee = Math.floor(Math.log(Math.abs(val))*Math.LOG10E);
+      if(o.indexOf(".") === -1) o = o[0] + "." + o.substr(1) + "E+" + (fakee - o.length+ee);
+      else o += "E+" + (fakee - ee);
+      while(o.substr(0,2) === "0.") {
+        o = o[0] + o.substr(2,period) + "." + o.substr(2+period);
+        o = o.replace(/^0+([1-9])/,"$1").replace(/^0+\./,"0.");
       }
-      o = o.replace(/^([+-]?)([0-9]*)\.([0-9]*)[Ee]/,function($$,$1,$2,$3) { return $1 + $2 + $3.substr(0,(period+ee)%period) + "." + $3.substr(ee) + "E"; });
-    } else o = val.toExponential(idx);
-    if(fmt.match(/E\+00$/) && o.match(/e[+-][0-9]$/)) o = o.substr(0,o.length-1) + "0" + o[o.length-1];
-    if(fmt.match(/E\-/) && o.match(/e\+/)) o = o.replace(/e\+/,"e");
-    return o.replace("e","E");
+      o = o.replace(/\+-/,"-");
+    }
+    o = o.replace(/^([+-]?)(\d*)\.(\d*)[Ee]/,function($$,$1,$2,$3) { return $1 + $2 + $3.substr(0,(period+ee)%period) + "." + $3.substr(ee) + "E"; });
+  } else o = val.toExponential(idx);
+  if(fmt.match(/E\+00$/) && o.match(/e[+-]\d$/)) o = o.substr(0,o.length-1) + "0" + o[o.length-1];
+  if(fmt.match(/E\-/) && o.match(/e\+/)) o = o.replace(/e\+/,"e");
+  return o.replace("e","E");
+}
+var frac1 = /# (\?+)( ?)\/( ?)(\d+)/;
+function write_num_f1(r, aval, sign) {
+  var den = parseInt(r[4]), rr = Math.round(aval * den), base = Math.floor(rr/den);
+  var myn = (rr - base*den), myd = den;
+  return sign + (base === 0 ? "" : ""+base) + " " + (myn === 0 ? fill(" ", r[1].length + 1 + r[4].length) : pad_(myn,r[1].length) + r[2] + "/" + r[3] + pad0(myd,r[4].length));
+}
+function write_num_f2(r, aval, sign) {
+  return sign + (aval === 0 ? "" : ""+aval) + fill(" ", r[1].length + 2 + r[4].length);
+}
+var dec1 = /^#*0*\.(0+)/;
+var closeparen = /\).*[0#]/;
+var phone = /\(###\) ###\\?-####/;
+function hashq(str) {
+  var o = "", cc;
+  for(var i = 0; i != str.length; ++i) switch((cc=str.charCodeAt(i))) {
+    case 35: break;
+    case 63: o+= " "; break;
+    case 48: o+= "0"; break;
+    default: o+= String.fromCharCode(cc);
   }
-  if(fmt[0] === "$") return "$"+write_num(type,fmt.substr(fmt[1]==' '?2:1),val);
-  var r, rr, ff, aval = val < 0 ? -val : val, sign = val < 0 ? "-" : "";
-  if((r = fmt.match(/# (\?+)([ ]?)\/([ ]?)(\d+)/))) {
-    var den = Number(r[4]), rnd = Math.round(aval * den), base = Math.floor(rnd/den);
-    var myn = (rnd - base*den), myd = den;
-    return sign + String(base||"") + " " + (myn === 0 ? fill(" ", r[1].length + 1 + r[4].length) : pad(myn,r[1].length," ") + r[2] + "/" + r[3] + pad(myd,r[4].length));
+  return o;
+}
+function rnd(val, d) { var dd = Math.pow(10,d); return ""+(Math.round(val * dd)/dd); }
+function dec(val, d) { return Math.round((val-Math.floor(val))*Math.pow(10,d)); }
+function flr(val) { if(val < 2147483647 && val > -2147483648) return ""+(val >= 0 ? (val|0) : (val-1|0)); return ""+Math.floor(val); }
+function write_num_flt(type, fmt, val) {
+  if(type.charCodeAt(0) === 40 && !fmt.match(closeparen)) {
+    var ffmt = fmt.replace(/\( */,"").replace(/ \)/,"").replace(/\)/,"");
+    if(val >= 0) return write_num_flt('n', ffmt, val);
+    return '(' + write_num_flt('n', ffmt, -val) + ')';
   }
-  if(fmt.match(/^#+0+$/)) fmt = fmt.replace(/#/g,"");
-  if(fmt.match(/^00+$/)) return (val<0?"-":"")+pad(Math.round(aval),fmt.length);
+  if(fmt.charCodeAt(fmt.length - 1) === 44) return write_num_cm(type, fmt, val);
+  if(fmt.indexOf('%') !== -1) return write_num_pct(type, fmt, val);
+  if(fmt.indexOf('E') !== -1) return write_num_exp(fmt, val);
+  if(fmt.charCodeAt(0) === 36) return "$"+write_num_flt(type,fmt.substr(fmt[1]==' '?2:1),val);
+  var o, oo;
+  var r, ri, ff, aval = Math.abs(val), sign = val < 0 ? "-" : "";
+  if(fmt.match(/^00+$/)) return sign + pad0r(aval,fmt.length);
   if(fmt.match(/^[#?]+$/)) {
-    o = String(Math.round(val)).replace(/^0$/,"");
-    return o.length > fmt.length ? o : fmt.substr(0,fmt.length-o.length).replace(/#/g,"").replace(/[?]/g," ") + o;
+    o = pad0r(val,0); if(o === "0") o = "";
+    return o.length > fmt.length ? o : hashq(fmt.substr(0,fmt.length-o.length)) + o;
   }
-  if((r = fmt.match(/^#*0*\.(0+)/))) {
-    o = Math.round(val * Math.pow(10,r[1].length));
-    rr = String(o/Math.pow(10,r[1].length)).replace(/^([^\.]+)$/,"$1."+r[1]).replace(/\.$/,"."+r[1]).replace(/\.([0-9]*)$/,function($$, $1) { return "." + $1 + fill("0", r[1].length-$1.length); });
-    return fmt.match(/0\./) ? rr : rr.replace(/^0\./,".");
+  if((r = fmt.match(frac1)) !== null) return write_num_f1(r, aval, sign);
+  if(fmt.match(/^#+0+$/) !== null) return sign + pad0r(aval,fmt.length - fmt.indexOf("0"));
+  if((r = fmt.match(dec1)) !== null) {
+    o = rnd(val, r[1].length).replace(/^([^\.]+)$/,"$1."+r[1]).replace(/\.$/,"."+r[1]).replace(/\.(\d*)$/,function($$, $1) { return "." + $1 + fill("0", r[1].length-$1.length); });
+    return fmt.indexOf("0.") !== -1 ? o : o.replace(/^0\./,".");
   }
   fmt = fmt.replace(/^#+([0.])/, "$1");
-  if((r = fmt.match(/^(0*)\.(#*)$/))) {
-    o = Math.round(aval*Math.pow(10,r[2].length));
-    return sign + String(o / Math.pow(10,r[2].length)).replace(/\.(\d*[1-9])0*$/,".$1").replace(/^([-]?\d*)$/,"$1.").replace(/^0\./,r[1].length?"0.":".");
+  if((r = fmt.match(/^(0*)\.(#*)$/)) !== null) {
+    return sign + rnd(aval, r[2].length).replace(/\.(\d*[1-9])0*$/,".$1").replace(/^(-?\d*)$/,"$1.").replace(/^0\./,r[1].length?"0.":".");
   }
-  if((r = fmt.match(/^#,##0([.]?)$/))) return sign + commaify(String(Math.round(aval)));
-  if((r = fmt.match(/^#,##0\.([#0]*0)$/))) {
-    rr = Math.round((val-Math.floor(val))*Math.pow(10,r[1].length));
-    return val < 0 ? "-" + write_num(type, fmt, -val) : commaify(String(Math.floor(val))) + "." + pad(rr,r[1].length,0);
+  if((r = fmt.match(/^#,##0(\.?)$/)) !== null) return sign + commaify(pad0r(aval,0));
+  if((r = fmt.match(/^#,##0\.([#0]*0)$/)) !== null) {
+    return val < 0 ? "-" + write_num_flt(type, fmt, -val) : commaify(""+(Math.floor(val))) + "." + pad0(dec(val, r[1].length),r[1].length);
   }
-  if((r = fmt.match(/^#,#*,#0/))) return write_num(type,fmt.replace(/^#,#*,/,""),val);
-  if((r = fmt.match(/^([0#]+)(\\?-([0#]+))+$/))) {
-    ff = _strrev(write_num(type, fmt.replace(/[\\-]/g,""), val));
-    rr = 0;
-    return _strrev(_strrev(fmt.replace(/\\/g,"")).replace(/[0#]/g,function(x){return rr<ff.length?ff[rr++]:x==='0'?'0':'';}));
+  if((r = fmt.match(/^#,#*,#0/)) !== null) return write_num_flt(type,fmt.replace(/^#,#*,/,""),val);
+  if((r = fmt.match(/^([0#]+)(\\?-([0#]+))+$/)) !== null) {
+    o = _strrev(write_num_flt(type, fmt.replace(/[\\-]/g,""), val));
+    ri = 0;
+    return _strrev(_strrev(fmt.replace(/\\/g,"")).replace(/[0#]/g,function(x){return ri<o.length?o[ri++]:x==='0'?'0':"";}));
   }
-  if(fmt.match(/\(###\) ###\\?-####/)) {
-    ff = write_num(type, "##########", val);
-    return "(" + ff.substr(0,3) + ") " + ff.substr(3, 3) + "-" + ff.substr(6);
+  if(fmt.match(phone) !== null) {
+    o = write_num_flt(type, "##########", val);
+    return "(" + o.substr(0,3) + ") " + o.substr(3, 3) + "-" + o.substr(6);
   }
   var oa = "";
-  if((r = fmt.match(/^([#0?]+)([ ]?)\/([ ]?)([#0?]+)/))) {
-    o="";
-    rr = Math.min(r[4].length,7);
-    ff = frac(aval, Math.pow(10,rr)-1, false);
-    o += sign;
+  if((r = fmt.match(/^([#0?]+)( ?)\/( ?)([#0?]+)/)) !== null) {
+    ri = Math.min(r[4].length,7);
+    ff = frac(aval, Math.pow(10,ri)-1, false);
+    o = "" + sign;
     oa = write_num("n", r[1], ff[1]);
     if(oa[oa.length-1] == " ") oa = oa.substr(0,oa.length-1) + "0";
-    o += oa;
-    o += r[2];
-    o += "/";
-    o += r[3];
-    oa = rpad(ff[2],rr," ");
-    if(oa.length < r[4].length) oa = r[4].substr(r[4].length-oa.length).replace(/[?]/g," ").replace(/#/g,"") + oa;
+    o += oa + r[2] + "/" + r[3];
+    oa = rpad_(ff[2],ri);
+    if(oa.length < r[4].length) oa = hashq(r[4].substr(r[4].length-oa.length)) + oa;
     o += oa;
     return o;
   }
-  if((r = fmt.match(/^# ([#0?]+)([ ]?)\/([ ]?)([#0?]+)/))) {
-    rr = Math.min(Math.max(r[1].length, r[4].length),7);
-    ff = frac(aval, Math.pow(10,rr)-1, true);
-    return sign + (ff[0]||(ff[1] ? "" : "0")) + " " + (ff[1] ? pad(ff[1],rr," ") + r[2] + "/" + r[3] + rpad(ff[2],rr," "): fill(" ", 2*rr+1 + r[2].length + r[3].length));
+  if((r = fmt.match(/^# ([#0?]+)( ?)\/( ?)([#0?]+)/)) !== null) {
+    ri = Math.min(Math.max(r[1].length, r[4].length),7);
+    ff = frac(aval, Math.pow(10,ri)-1, true);
+    return sign + (ff[0]||(ff[1] ? "" : "0")) + " " + (ff[1] ? pad_(ff[1],ri) + r[2] + "/" + r[3] + rpad_(ff[2],ri): fill(" ", 2*ri+1 + r[2].length + r[3].length));
   }
-  if((r = fmt.match(/^[#0?]+$/))) {
-    o = "" + Math.round(val);
+  if((r = fmt.match(/^[#0?]+$/)) !== null) {
+    o = pad0r(val, 0);
     if(fmt.length <= o.length) return o;
-    return fmt.substr(0,fmt.length-o.length).replace(/#/g,"").replace(/\?/g," ") + o;
+    return hashq(fmt.substr(0,fmt.length-o.length)) + o;
   }
-  if((r = fmt.match(/^([#0]+)\.([#0]+)$/))) {
+  if((r = fmt.match(/^([#0]+)\.([#0]+)$/)) !== null) {
     o = "" + val.toFixed(Math.min(r[2].length,10)).replace(/([^0])0+$/,"$1");
-    rr = o.indexOf(".");
-    var lres = fmt.indexOf(".") - rr, rres = fmt.length - o.length - lres;
-    return fmt.substr(0,lres).replace(/#/g,"") + o + fmt.substr(fmt.length-rres).replace(/#/g,"");
+    ri = o.indexOf(".");
+    var lres = fmt.indexOf(".") - ri, rres = fmt.length - o.length - lres;
+    return hashq(fmt.substr(0,lres) + o + fmt.substr(fmt.length-rres));
   }
-  if((r = fmt.match(/^00,000\.([#0]*0)$/))) {
-    rr = val == Math.floor(val) ? 0 : Math.round((val-Math.floor(val))*Math.pow(10,r[1].length));
-    return val < 0 ? "-" + write_num(type, fmt, -val) : commaify(String(Math.floor(val))).replace(/^\d,\d{3}$/,"0$&").replace(/^\d*$/,function($$) { return "00," + ($$.length < 3 ? pad(0,3-$$.length) : "") + $$; }) + "." + pad(rr,r[1].length,0);
+  if((r = fmt.match(/^00,000\.([#0]*0)$/)) !== null) {
+    ri = dec(val, r[1].length);
+    return val < 0 ? "-" + write_num_flt(type, fmt, -val) : commaify(flr(val)).replace(/^\d,\d{3}$/,"0$&").replace(/^\d*$/,function($$) { return "00," + ($$.length < 3 ? pad0(0,3-$$.length) : "") + $$; }) + "." + pad0(ri,r[1].length);
   }
   switch(fmt) {
-    case "#,###": var x = commaify(String(Math.round(aval))); return x !== "0" ? sign + x : "";
+    case "#,###": var x = commaify(pad0r(aval,0)); return x !== "0" ? sign + x : "";
     default:
   }
   throw new Error("unsupported format |" + fmt + "|");
-};
+}
+function write_num_cm2(type, fmt, val){
+  var idx = fmt.length - 1;
+  while(fmt.charCodeAt(idx-1) === 44) --idx;
+  return write_num(type, fmt.substr(0,idx), val / Math.pow(10,3*(fmt.length-idx)));
+}
+function write_num_pct2(type, fmt, val){
+  var sfmt = fmt.replace(pct1,""), mul = fmt.length - sfmt.length;
+  return write_num(type, sfmt, val * Math.pow(10,2*mul)) + fill("%",mul);
+}
+function write_num_exp2(fmt, val){
+  var o;
+  var idx = fmt.indexOf("E") - fmt.indexOf(".") - 1;
+  if(fmt.match(/^#+0.0E\+0$/)) {
+    var period = fmt.indexOf("."); if(period === -1) period=fmt.indexOf('E');
+    var ee = Math.floor(Math.log(Math.abs(val))*Math.LOG10E)%period;
+    if(ee < 0) ee += period;
+    o = (val/Math.pow(10,ee)).toPrecision(idx+1+(period+ee)%period);
+    if(!o.match(/[Ee]/)) {
+      var fakee = Math.floor(Math.log(Math.abs(val))*Math.LOG10E);
+      if(o.indexOf(".") === -1) o = o[0] + "." + o.substr(1) + "E+" + (fakee - o.length+ee);
+      else o += "E+" + (fakee - ee);
+      o = o.replace(/\+-/,"-");
+    }
+    o = o.replace(/^([+-]?)(\d*)\.(\d*)[Ee]/,function($$,$1,$2,$3) { return $1 + $2 + $3.substr(0,(period+ee)%period) + "." + $3.substr(ee) + "E"; });
+  } else o = val.toExponential(idx);
+  if(fmt.match(/E\+00$/) && o.match(/e[+-]\d$/)) o = o.substr(0,o.length-1) + "0" + o[o.length-1];
+  if(fmt.match(/E\-/) && o.match(/e\+/)) o = o.replace(/e\+/,"e");
+  return o.replace("e","E");
+}
+function write_num_int(type, fmt, val) {
+  if(type.charCodeAt(0) === 40 && !fmt.match(closeparen)) {
+    var ffmt = fmt.replace(/\( */,"").replace(/ \)/,"").replace(/\)/,"");
+    if(val >= 0) return write_num_int('n', ffmt, val);
+    return '(' + write_num_int('n', ffmt, -val) + ')';
+  }
+  if(fmt.charCodeAt(fmt.length - 1) === 44) return write_num_cm2(type, fmt, val);
+  if(fmt.indexOf('%') !== -1) return write_num_pct2(type, fmt, val);
+  if(fmt.indexOf('E') !== -1) return write_num_exp2(fmt, val);
+  if(fmt.charCodeAt(0) === 36) return "$"+write_num_int(type,fmt.substr(fmt[1]==' '?2:1),val);
+  var o;
+  var r, ri, ff, aval = Math.abs(val), sign = val < 0 ? "-" : "";
+  if(fmt.match(/^00+$/)) return sign + pad0(aval,fmt.length);
+  if(fmt.match(/^[#?]+$/)) {
+    o = (""+val); if(val === 0) o = "";
+    return o.length > fmt.length ? o : hashq(fmt.substr(0,fmt.length-o.length)) + o;
+  }
+  if((r = fmt.match(frac1)) !== null) return write_num_f2(r, aval, sign);
+  if(fmt.match(/^#+0+$/) !== null) return sign + pad0(aval,fmt.length - fmt.indexOf("0"));
+  if((r = fmt.match(dec1)) !== null) {
+    o = (""+val).replace(/^([^\.]+)$/,"$1."+r[1]).replace(/\.$/,"."+r[1]).replace(/\.(\d*)$/,function($$, $1) { return "." + $1 + fill("0", r[1].length-$1.length); });
+    return fmt.indexOf("0.") !== -1 ? o : o.replace(/^0\./,".");
+  }
+  fmt = fmt.replace(/^#+([0.])/, "$1");
+  if((r = fmt.match(/^(0*)\.(#*)$/)) !== null) {
+    return sign + (""+aval).replace(/\.(\d*[1-9])0*$/,".$1").replace(/^(-?\d*)$/,"$1.").replace(/^0\./,r[1].length?"0.":".");
+  }
+  if((r = fmt.match(/^#,##0(\.?)$/)) !== null) return sign + commaify((""+aval));
+  if((r = fmt.match(/^#,##0\.([#0]*0)$/)) !== null) {
+    return val < 0 ? "-" + write_num_int(type, fmt, -val) : commaify((""+val)) + "." + fill('0',r[1].length);
+  }
+  if((r = fmt.match(/^#,#*,#0/)) !== null) return write_num_int(type,fmt.replace(/^#,#*,/,""),val);
+  if((r = fmt.match(/^([0#]+)(\\?-([0#]+))+$/)) !== null) {
+    o = _strrev(write_num_int(type, fmt.replace(/[\\-]/g,""), val));
+    ri = 0;
+    return _strrev(_strrev(fmt.replace(/\\/g,"")).replace(/[0#]/g,function(x){return ri<o.length?o[ri++]:x==='0'?'0':"";}));
+  }
+  if(fmt.match(phone) !== null) {
+    o = write_num_int(type, "##########", val);
+    return "(" + o.substr(0,3) + ") " + o.substr(3, 3) + "-" + o.substr(6);
+  }
+  var oa = "";
+  if((r = fmt.match(/^([#0?]+)( ?)\/( ?)([#0?]+)/)) !== null) {
+    ri = Math.min(r[4].length,7);
+    ff = frac(aval, Math.pow(10,ri)-1, false);
+    o = "" + sign;
+    oa = write_num("n", r[1], ff[1]);
+    if(oa[oa.length-1] == " ") oa = oa.substr(0,oa.length-1) + "0";
+    o += oa + r[2] + "/" + r[3];
+    oa = rpad_(ff[2],ri);
+    if(oa.length < r[4].length) oa = hashq(r[4].substr(r[4].length-oa.length)) + oa;
+    o += oa;
+    return o;
+  }
+  if((r = fmt.match(/^# ([#0?]+)( ?)\/( ?)([#0?]+)/)) !== null) {
+    ri = Math.min(Math.max(r[1].length, r[4].length),7);
+    ff = frac(aval, Math.pow(10,ri)-1, true);
+    return sign + (ff[0]||(ff[1] ? "" : "0")) + " " + (ff[1] ? pad_(ff[1],ri) + r[2] + "/" + r[3] + rpad_(ff[2],ri): fill(" ", 2*ri+1 + r[2].length + r[3].length));
+  }
+  if((r = fmt.match(/^[#0?]+$/)) !== null) {
+    o = "" + val;
+    if(fmt.length <= o.length) return o;
+    return hashq(fmt.substr(0,fmt.length-o.length)) + o;
+  }
+  if((r = fmt.match(/^([#0]+)\.([#0]+)$/)) !== null) {
+    o = "" + val.toFixed(Math.min(r[2].length,10)).replace(/([^0])0+$/,"$1");
+    ri = o.indexOf(".");
+    var lres = fmt.indexOf(".") - ri, rres = fmt.length - o.length - lres;
+    return hashq(fmt.substr(0,lres) + o + fmt.substr(fmt.length-rres));
+  }
+  if((r = fmt.match(/^00,000\.([#0]*0)$/)) !== null) {
+    return val < 0 ? "-" + write_num_int(type, fmt, -val) : commaify(""+val).replace(/^\d,\d{3}$/,"0$&").replace(/^\d*$/,function($$) { return "00," + ($$.length < 3 ? pad0(0,3-$$.length) : "") + $$; }) + "." + pad0(0,r[1].length);
+  }
+  switch(fmt) {
+    case "#,###": var x = commaify(""+aval); return x !== "0" ? sign + x : "";
+    default:
+  }
+  throw new Error("unsupported format |" + fmt + "|");
+}
+return function write_num(type, fmt, val) {
+  return (val|0) === val ? write_num_int(type, fmt, val) : write_num_flt(type, fmt, val);
+};})();
 function split_fmt(fmt) {
   var out = [];
-  var in_str = -1;
-  for(var i = 0, j = 0; i < fmt.length; ++i) {
-    if(in_str != -1) { if(fmt[i] == '"') in_str = -1; continue; }
-    if(fmt[i] == "_" || fmt[i] == "*" || fmt[i] == "\\") { ++i; continue; }
-    if(fmt[i] == '"') { in_str = i; continue; }
-    if(fmt[i] != ";") continue;
-    out.push(fmt.slice(j,i));
-    j = i+1;
+  var in_str = false, cc;
+  for(var i = 0, j = 0; i < fmt.length; ++i) switch((cc=fmt.charCodeAt(i))) {
+    case 34: /* '"' */
+      in_str = !in_str; break;
+    case 95: case 42: case 92: /* '_' '*' '\\' */
+      ++i; break;
+    case 59: /* ';' */
+      out[out.length] = fmt.substr(j,i-j);
+      j = i+1;
   }
-  out.push(fmt.slice(j));
-  if(in_str !=-1) throw new Error("Format |" + fmt + "| unterminated string at " + in_str);
+  out[out.length] = fmt.substr(j);
+  if(in_str === true) throw new Error("Format |" + fmt + "| unterminated string ");
   return out;
 }
 SSF._split = split_fmt;
+var abstime = /\[[HhMmSs]*\]/;
 function eval_fmt(fmt, v, opts, flen) {
-  var out = [], o = "", i = 0, c = "", lst='t', q, dt, j;
-  fixopts(opts = (opts || {}));
+  var out = [], o = "", i = 0, c = "", lst='t', q, dt, j, cc;
   var hr='H';
   /* Tokenize */
   while(i < fmt.length) {
     switch((c = fmt[i])) {
       case 'G': /* General */
-        if(fmt.substr(i, 7).toLowerCase() !== "general")
-          throw new Error('unrecognized character ' + fmt[i] + ' in ' +fmt);
-        out.push({t:'G',v:'General'}); i+=7; break;
+        if(!isgeneral(fmt, i)) throw new Error('unrecognized character ' + c + ' in ' +fmt);
+        out[out.length] = {t:'G', v:'General'}; i+=7; break;
       case '"': /* Literal text */
-        for(o="";fmt[++i] !== '"' && i < fmt.length;) o += fmt[i];
-        out.push({t:'t', v:o}); ++i; break;
-      case '\\': var w = fmt[++i], t = "()".indexOf(w) === -1 ? 't' : w;
-        out.push({t:t, v:w}); ++i; break;
-      case '_': out.push({t:'t', v:" "}); i+=2; break;
+        for(o="";(cc=fmt.charCodeAt(++i)) !== 34 && i < fmt.length;) o += String.fromCharCode(cc);
+        out[out.length] = {t:'t', v:o}; ++i; break;
+      case '\\': var w = fmt[++i], t = (w === "(" || w === ")") ? w : 't';
+        out[out.length] = {t:t, v:w}; ++i; break;
+      case '_': out[out.length] = {t:'t', v:" "}; i+=2; break;
       case '@': /* Text Placeholder */
-        out.push({t:'T', v:v}); ++i; break;
+        out[out.length] = {t:'T', v:v}; ++i; break;
       case 'B': case 'b':
         if(fmt[i+1] === "1" || fmt[i+1] === "2") {
-          if(!dt) dt = parse_date_code(v, opts, fmt[i+1] === "2");
-          q={t:'X', v:fmt.substr(i,2)}; out.push(q); lst = c; i+=2; break;
+          if(dt == null) dt = parse_date_code(v, opts, fmt[i+1] === "2");
+          out[out.length] = {t:'X', v:fmt.substr(i,2)}; lst = c; i+=2; break;
         }
         /* falls through */
       case 'M': case 'D': case 'Y': case 'H': case 'S': case 'E':
@@ -370,54 +548,50 @@ function eval_fmt(fmt, v, opts, flen) {
         /* falls through */
       case 'm': case 'd': case 'y': case 'h': case 's': case 'e': case 'g':
         if(v < 0) return "";
-        if(!dt) dt = parse_date_code(v, opts);
-        if(!dt) return "";
-        o = fmt[i]; while((fmt[++i]||"").toLowerCase() === c) o+=c;
+        if(dt==null) { dt=parse_date_code(v, opts); if(dt==null) return ""; }
+        o = c; while(++i<fmt.length && fmt[i].toLowerCase() === c) o+=c;
         if(c === 'm' && lst.toLowerCase() === 'h') c = 'M'; /* m = minute */
         if(c === 'h') c = hr;
-        o = o.toLowerCase();
-        q={t:c, v:o}; out.push(q); lst = c; break;
+        out[out.length] = {t:c, v:o}; lst = c; break;
       case 'A':
-        if(!dt) dt = parse_date_code(v, opts);
-        if(!dt) return "";
-        q={t:c,v:"A"};
+        q={t:c, v:"A"};
+        if(dt==null) dt=parse_date_code(v, opts);
         if(fmt.substr(i, 3) === "A/P") {q.v = dt.H >= 12 ? "P" : "A"; q.t = 'T'; hr='h';i+=3;}
         else if(fmt.substr(i,5) === "AM/PM") { q.v = dt.H >= 12 ? "PM" : "AM"; q.t = 'T'; i+=5; hr='h'; }
-        else { q.t = "t"; i++; }
-        out.push(q); lst = c; break;
+        else { q.t = "t"; ++i; }
+        if(dt==null && q.t === 'T') return "";
+        out[out.length] = q; lst = c; break;
       case '[':
         o = c;
         while(fmt[i++] !== ']' && i < fmt.length) o += fmt[i];
         if(o.substr(-1) !== ']') throw 'unterminated "[" block: |' + o + '|';
-        if(o.match(/\[[HhMmSs]*\]/)) {
-          if(!dt) dt = parse_date_code(v, opts);
-          if(!dt) return "";
-          out.push({t:'Z', v:o.toLowerCase()});
+        if(o.match(abstime)) {
+          if(dt==null) { dt=parse_date_code(v, opts); if(dt==null) return ""; }
+          out[out.length] = {t:'Z', v:o.toLowerCase()};
         } else { o=""; }
         break;
       /* Numbers */
       case '.':
-        if(dt) {
+        if(dt != null) {
           o = c; while((c=fmt[++i]) === "0") o += c;
-          out.push({t:'s', v:o}); break;
+          out[out.length] = {t:'s', v:o}; break;
         }
         /* falls through */
       case '0': case '#':
         o = c; while("0#?.,E+-%".indexOf(c=fmt[++i]) > -1 || c=='\\' && fmt[i+1] == "-" && "0#".indexOf(fmt[i+2])>-1) o += c;
-        out.push({t:'n', v:o}); break;
+        out[out.length] = {t:'n', v:o}; break;
       case '?':
-        o = fmt[i]; while(fmt[++i] === c) o+=c;
-        q={t:c, v:o}; out.push(q); lst = c; break;
+        o = c; while(fmt[++i] === c) o+=c;
+        q={t:c, v:o}; out[out.length] = q; lst = c; break;
       case '*': ++i; if(fmt[i] == ' ' || fmt[i] == '*') ++i; break; // **
-      case '(': case ')': out.push({t:(flen===1?'t':c),v:c}); ++i; break;
+      case '(': case ')': out[out.length] = {t:(flen===1?'t':c), v:c}; ++i; break;
       case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
-        o = fmt[i]; while("0123456789".indexOf(fmt[++i]) > -1) o+=fmt[i];
-        out.push({t:'D', v:o}); break;
-      case ' ': out.push({t:c,v:c}); ++i; break;
+        o = c; while("0123456789".indexOf(fmt[++i]) > -1) o+=fmt[i];
+        out[out.length] = {t:'D', v:o}; break;
+      case ' ': out[out.length] = {t:c, v:c}; ++i; break;
       default:
-        if(",$-+/():!^&'~{}<>=€acfijklopqrtuvwxz".indexOf(c) === -1)
-          throw 'unrecognized character ' + fmt[i] + ' in ' + fmt;
-        out.push({t:'t', v:c}); ++i; break;
+        if(",$-+/():!^&'~{}<>=€acfijklopqrtuvwxz".indexOf(c) === -1) throw new Error('unrecognized character ' + c + ' in ' + fmt);
+        out[out.length] = {t:'t', v:c}; ++i; break;
     }
   }
   var bt = 0, ss0 = 0, ssm;
@@ -455,130 +629,138 @@ function eval_fmt(fmt, v, opts, flen) {
   for(i=0; i < out.length; ++i) {
     switch(out[i].t) {
       case 't': case 'T': case ' ': case 'D': break;
-      case 'X': delete out[i]; break;
+      case 'X': out[i] = undefined; break;
       case 'd': case 'm': case 'y': case 'h': case 'H': case 'M': case 's': case 'e': case 'b': case 'Z':
-        out[i].v = write_date(out[i].t, out[i].v, dt, ss0);
+        out[i].v = write_date(out[i].t.charCodeAt(0), out[i].v, dt, ss0);
         out[i].t = 't'; break;
       case 'n': case '(': case '?':
         jj = i+1;
-        while(out[jj] && ("?D".indexOf(out[jj].t) > -1 || (" t".indexOf(out[jj].t) > -1 && "?t".indexOf((out[jj+1]||{}).t)>-1 && (out[jj+1].t == '?' || out[jj+1].v == '/')) || out[i].t == '(' && (")n ".indexOf(out[jj].t) > -1) || out[jj].t == 't' && (out[jj].v == '/' || '$€'.indexOf(out[jj].v) > -1 || (out[jj].v == ' ' && (out[jj+1]||{}).t == '?')))) {
+        while(out[jj] != null && (
+          (c=out[jj].t) === "?" || c === "D" ||
+          (c === " " || c === "t") && out[jj+1] != null && (out[jj+1].t === '?' || out[jj+1].t === "t" && out[jj+1].v === '/') ||
+          out[i].t === '(' && (c === ' ' || c === 'n' || c === ')') ||
+          c === 't' && (out[jj].v === '/' || '$€'.indexOf(out[jj].v) > -1 || out[jj].v === ' ' && out[jj+1] != null && out[jj+1].t == '?')
+        )) {
           out[i].v += out[jj].v;
-          delete out[jj]; ++jj;
+          out[jj] = undefined; ++jj;
         }
         nstr += out[i].v;
         i = jj-1; break;
       case 'G': out[i].t = 't'; out[i].v = general_fmt(v,opts); break;
     }
   }
-  if(nstr) {
-    var ostr = write_num(nstr[0]=='(' ? '(' : 'n', nstr, (v<0&&nstr[0] == "-" ? -v : v));
+  var vv = "", myv, ostr;
+  if(nstr.length > 0) {
+    myv = (v<0&&nstr.charCodeAt(0) === 45 ? -v : v); /* '-' */
+    ostr = write_num(nstr.charCodeAt(0) === 40 ? '(' : 'n', nstr, myv); /* '(' */
     jj=ostr.length-1;
     var decpt = out.length;
-    for(i=0; i < out.length; ++i) if(out[i] && out[i].v.indexOf(".") > -1) { decpt = i; break; }
-    var lasti=out.length, vv;
-    if(decpt === out.length && !ostr.match(/E/)) {
+    for(i=0; i < out.length; ++i) if(out[i] != null && out[i].v.indexOf(".") > -1) { decpt = i; break; }
+    var lasti=out.length;
+    if(decpt === out.length && ostr.indexOf("E") === -1) {
       for(i=out.length-1; i>= 0;--i) {
-        if(!out[i] || 'n?('.indexOf(out[i].t) === -1) continue;
-        vv = out[i].v.split("");
-        for(j=vv.length-1; j>=0; --j) {
-          if(jj>=0) vv[j] = ostr[jj--];
-          else vv[j] = "";
-        }
-        out[i].v = vv.join("");
+        if(out[i] == null || 'n?('.indexOf(out[i].t) === -1) continue;
+        if(jj>=out[i].v.length-1) { jj -= out[i].v.length; out[i].v = ostr.substr(jj+1, out[i].v.length); }
+        else if(jj < 0) out[i].v = "";
+        else { out[i].v = ostr.substr(0, jj+1); jj = -1; }
         out[i].t = 't';
         lasti = i;
       }
       if(jj>=0 && lasti<out.length) out[lasti].v = ostr.substr(0,jj+1) + out[lasti].v;
     }
-    else if(decpt !== out.length && !ostr.match(/E/)) {
+    else if(decpt !== out.length && ostr.indexOf("E") === -1) {
       jj = ostr.indexOf(".")-1;
       for(i=decpt; i>= 0; --i) {
-        if(!out[i] || 'n?('.indexOf(out[i].t) === -1) continue;
-        vv = out[i].v.split("");
-        for(j=out[i].v.indexOf(".")>-1&&i==decpt?out[i].v.indexOf(".")-1:vv.length-1; j>=0; --j) {
-          if(jj>=0 && "0#".indexOf(vv[j])>-1) vv[j] = ostr[jj--];
-          else vv[j] = "";
+        if(out[i] == null || 'n?('.indexOf(out[i].t) === -1) continue;
+        j=out[i].v.indexOf(".")>-1&&i===decpt?out[i].v.indexOf(".")-1:out[i].v.length-1;
+        vv = out[i].v.substr(j+1);
+        for(; j>=0; --j) {
+          if(jj>=0 && (out[i].v[j] === "0" || out[i].v[j] === "#")) vv = ostr[jj--] + vv;
         }
-        out[i].v = vv.join("");
+        out[i].v = vv;
         out[i].t = 't';
         lasti = i;
       }
       if(jj>=0 && lasti<out.length) out[lasti].v = ostr.substr(0,jj+1) + out[lasti].v;
       jj = ostr.indexOf(".")+1;
       for(i=decpt; i<out.length; ++i) {
-        if(!out[i] || 'n?('.indexOf(out[i].t) === -1 && i != decpt ) continue;
-        vv = out[i].v.split("");
-        for(j=out[i].v.indexOf(".")>-1&&i==decpt?out[i].v.indexOf(".")+1:0; j<vv.length; ++j) {
-          if(jj<ostr.length) vv[j] = ostr[jj++];
-          else vv[j] = "";
+        if(out[i] == null || 'n?('.indexOf(out[i].t) === -1 && i !== decpt ) continue;
+        j=out[i].v.indexOf(".")>-1&&i===decpt?out[i].v.indexOf(".")+1:0;
+        vv = out[i].v.substr(0,j);
+        for(; j<out[i].v.length; ++j) {
+          if(jj<ostr.length) vv += ostr[jj++];
         }
-        out[i].v = vv.join("");
+        out[i].v = vv;
         out[i].t = 't';
         lasti = i;
       }
     }
   }
-  for(i=0; i<out.length; ++i) if(out[i] && 'n(?'.indexOf(out[i].t)>-1) {
-    out[i].v = write_num(out[i].t, out[i].v, (flen >1 && v < 0 && i>0 && out[i-1].v == "-" ? -v:v));
+  for(i=0; i<out.length; ++i) if(out[i] != null && 'n(?'.indexOf(out[i].t)>-1) {
+    myv = (flen >1 && v < 0 && i>0 && out[i-1].v === "-" ? -v:v);
+    out[i].v = write_num(out[i].t, out[i].v, myv);
     out[i].t = 't';
   }
   var retval = "";
-  for(i=0; i != out.length; ++i) if(out[i]) retval += out[i].v;
+  for(i=0; i !== out.length; ++i) if(out[i] != null) retval += out[i].v;
   return retval;
 }
 SSF._eval = eval_fmt;
-function choose_fmt(fmt, v, o) {
-  if(typeof fmt === 'number') fmt = ((o&&o.table) ? o.table : table_fmt)[fmt];
-  if(typeof fmt === "string") fmt = split_fmt(fmt);
-  var l = fmt.length;
-  if(l<4 && fmt[l-1].indexOf("@")>-1) --l;
-  switch(fmt.length) {
-    case 1: fmt = fmt[0].indexOf("@")>-1 ? ["General", "General", "General", fmt[0]] : [fmt[0], fmt[0], fmt[0], "@"]; break;
-    case 2: fmt = fmt[1].indexOf("@")>-1 ? [fmt[0], fmt[0], fmt[0], fmt[1]] : [fmt[0], fmt[1], fmt[0], "@"]; break;
-    case 3: fmt = fmt[2].indexOf("@")>-1 ? [fmt[0], fmt[1], fmt[0], fmt[2]] : [fmt[0], fmt[1], fmt[2], "@"]; break;
-    case 4: break;
-    default: throw "cannot find right format for |" + fmt + "|";
+var cfregex = /\[[=<>]/;
+var cfregex2 = /\[([=<>]*)(-?\d+\.?\d*)\]/;
+function chkcond(v, rr) {
+  if(rr == null) return false;
+  var thresh = parseFloat(rr[2]);
+  switch(rr[1]) {
+    case "=":  if(v == thresh) return true; break;
+    case ">":  if(v >  thresh) return true; break;
+    case "<":  if(v <  thresh) return true; break;
+    case "<>": if(v != thresh) return true; break;
+    case ">=": if(v >= thresh) return true; break;
+    case "<=": if(v <= thresh) return true; break;
+  }
+  return false;
+}
+function choose_fmt(f, v) {
+  var fmt = split_fmt(f);
+  var l = fmt.length, lat = fmt[l-1].indexOf("@");
+  if(l<4 && lat>-1) --l;
+  if(fmt.length > 4) throw "cannot find right format for |" + fmt + "|";
+  if(typeof v !== "number") return [4, fmt.length === 4 || lat>-1?fmt[fmt.length-1]:"@"];
+  switch(fmt.length) {
+    case 1: fmt = lat>-1 ? ["General", "General", "General", fmt[0]] : [fmt[0], fmt[0], fmt[0], "@"]; break;
+    case 2: fmt = lat>-1 ? [fmt[0], fmt[0], fmt[0], fmt[1]] : [fmt[0], fmt[1], fmt[0], "@"]; break;
+    case 3: fmt = lat>-1 ? [fmt[0], fmt[1], fmt[0], fmt[2]] : [fmt[0], fmt[1], fmt[2], "@"]; break;
+    case 4: break;
   }
-  if(typeof v !== "number") return [fmt.length, fmt[3]];
   var ff = v > 0 ? fmt[0] : v < 0 ? fmt[1] : fmt[2];
-  if(fmt[0].match(/\[[=<>]/) || fmt[1].match(/\[[=<>]/)) {
-    var chk = function(v, rr, out) {
-      if(!rr) return null;
-      var found = false;
-      var thresh = Number(rr[2]);
-      switch(rr[1]) {
-        case "=":  if(v == thresh) found = true; break;
-        case ">":  if(v >  thresh) found = true; break;
-        case "<":  if(v <  thresh) found = true; break;
-        case "<>": if(v != thresh) found = true; break;
-        case ">=": if(v >= thresh) found = true; break;
-        case "<=": if(v <= thresh) found = true; break;
-      }
-      return found ? out : null;
-    };
-    var m1 = fmt[0].match(/\[([=<>]*)([-]?\d+)\]/);
-    var m2 = fmt[1].match(/\[([=<>]*)([-]?\d+)\]/);
-    return chk(v, m1, [l, fmt[0]]) || chk(v, m2, [l, fmt[1]]) || [l, fmt[m1&&m2?2:1]];
+  if(fmt[0].indexOf("[") === -1 && fmt[1].indexOf("[") === -1) return [l, ff];
+  if(fmt[0].match(cfregex) != null || fmt[1].match(cfregex) != null) {
+    var m1 = fmt[0].match(cfregex2);
+    var m2 = fmt[1].match(cfregex2);
+    return chkcond(v, m1) ? [l, fmt[0]] : chkcond(v, m2) ? [l, fmt[1]] : [l, fmt[m1 != null && m2 != null ? 2 : 1]];
   }
   return [l, ff];
 }
-var format = function format(fmt,v,o) {
-  fixopts(o = (o||{}));
-  if(typeof fmt === "string" && fmt.toLowerCase() === "general") return general_fmt(v, o);
-  if(typeof fmt === 'number') fmt = (o.table || table_fmt)[fmt];
-  var f = choose_fmt(fmt, v, o);
-  if(f[1].toLowerCase() === "general") return general_fmt(v,o);
-  if(v === true) v = "TRUE"; if(v === false) v = "FALSE";
-  if(v === "" || typeof v === "undefined") return "";
+function format(fmt,v,o) {
+  fixopts(o != null ? o : (o=[]));
+  var sfmt = "";
+  switch(typeof fmt) {
+    case "string": sfmt = fmt; break;
+    case "number": sfmt = (o.table != null ? o.table : table_fmt)[fmt]; break;
+  }
+  if(isgeneral(sfmt,0)) return general_fmt(v, o);
+  var f = choose_fmt(sfmt, v);
+  if(isgeneral(f[1])) return general_fmt(v, o);
+  if(v === true) v = "TRUE"; else if(v === false) v = "FALSE";
+  else if(v === "" || v == null) return "";
   return eval_fmt(f[1], v, o, f[0]);
-};
-
-SSF._choose = choose_fmt;
+}
 SSF._table = table_fmt;
-SSF.load = function(fmt, idx) { table_fmt[idx] = fmt; };
+SSF.load = function load_entry(fmt, idx) { table_fmt[idx] = fmt; };
 SSF.format = format;
-SSF.get_table = function() { return table_fmt; };
-SSF.load_table = function(tbl) { for(var i=0; i!=0x0188; ++i) if(tbl[i]) SSF.load(tbl[i], i); };
+SSF.get_table = function get_table() { return table_fmt; };
+SSF.load_table = function load_table(tbl) { for(var i=0; i!=0x0188; ++i) if(tbl[i] !== undefined) SSF.load(tbl[i], i); };
 };
 make_ssf(SSF);
 if(typeof module !== 'undefined' && typeof DO_NOT_EXPORT_SSF === 'undefined') module.exports = SSF;
diff --git a/ssf.md b/ssf.md
index ec6420f..39096f3 100644
--- a/ssf.md
+++ b/ssf.md
@@ -10,7 +10,7 @@ default options are described below:
 
 ```js>tmp/10_opts.js
 /* Options */
-var opts_fmt = {
+var opts_fmt = [
 ```
 
 There are two commonly-recognized date code formats:
@@ -23,7 +23,7 @@ the 1904 date mode was only default in a few Mac variants of Excel (2011 uses
 `date1904`:
 
 ```
-  date1904:0,
+  ["date1904", 0],
 ```
 
 The default output is a text representation (no effort to capture colors).  To
@@ -34,15 +34,17 @@ control the output, set the `output` variable:
 - `ansi`: ansi color codes (requires `colors` module)
 
 ```
-  output:"",
+  ["output", ""],
 ```
 
 These options are made available via the `opts` field:
 
 ```
-  WTF:false
-};
-function fixopts(o){for(var y in opts_fmt) if(o[y]===undefined) o[y]=opts_fmt[y];}
+  ["WTF", false]
+];
+function fixopts(o){
+  for(var y = 0; y != opts_fmt.length; ++y) if(o[opts_fmt[y][0]]===undefined) o[opts_fmt[y][0]]=opts_fmt[y][1];
+}
 SSF.opts = opts_fmt;
 ```
 
@@ -57,20 +59,21 @@ numbers, zero values, and text, in that order.
 Semicolons can be escaped with the `\` character, so we need to split on those
 semicolons that aren't prefaced by a slash or within a quoted string:
 
-```js>tmp/90_main.js
+```js>tmp/80_split.js
 function split_fmt(fmt) {
   var out = [];
-  var in_str = -1;
-  for(var i = 0, j = 0; i < fmt.length; ++i) {
-    if(in_str != -1) { if(fmt[i] == '"') in_str = -1; continue; }
-    if(fmt[i] == "_" || fmt[i] == "*" || fmt[i] == "\\") { ++i; continue; }
-    if(fmt[i] == '"') { in_str = i; continue; }
-    if(fmt[i] != ";") continue;
-    out.push(fmt.slice(j,i));
-    j = i+1;
+  var in_str = false, cc;
+  for(var i = 0, j = 0; i < fmt.length; ++i) switch((cc=fmt.charCodeAt(i))) {
+    case 34: /* '"' */
+      in_str = !in_str; break;
+    case 95: case 42: case 92: /* '_' '*' '\\' */
+      ++i; break;
+    case 59: /* ';' */
+      out[out.length] = fmt.substr(j,i-j);
+      j = i+1;
   }
-  out.push(fmt.slice(j));
-  if(in_str !=-1) throw new Error("Format |" + fmt + "| unterminated string at " + in_str);
+  out[out.length] = fmt.substr(j);
+  if(in_str === true) throw new Error("Format |" + fmt + "| unterminated string ");
   return out;
 }
 SSF._split = split_fmt;
@@ -111,6 +114,30 @@ numbers, and Yellow for text.
 Only the text case is allowed to have the `@` text sigil.  Excel interprets it
 as the last format.
 
+## Utility Functions
+
+```js>tmp/02_utilities.js
+function _strrev(x) { var o = "", i = x.length-1; while(i>=0) o += x.charAt(i--); return o; }
+function fill(c,l) { var o = ""; while(o.length < l) o+=c; return o; }
+```
+
+The next few helpers break up the general `pad` function into special cases:
+
+```
+function pad0(v,d){var t=""+v; return t.length>=d?t:fill('0',d-t.length)+t;}
+function pad_(v,d){var t=""+v;return t.length>=d?t:fill(' ',d-t.length)+t;}
+function rpad_(v,d){var t=""+v; return t.length>=d?t:t+fill(' ',d-t.length);}
+function pad0r1(v,d){var t=""+Math.round(v); return t.length>=d?t:fill('0',d-t.length)+t;}
+function pad0r2(v,d){var t=""+v; return t.length>=d?t:fill('0',d-t.length)+t;}
+var p2_32 = Math.pow(2,32);
+function pad0r(v,d){if(v>p2_32||v<-p2_32) return pad0r1(v,d); var i = Math.round(v); return pad0r2(i,d); }
+```
+
+Comparing against the string "general" is faster via char codes:
+
+```
+function isgeneral(s, i) { return s.length >= 7 + i && (s.charCodeAt(i)|32) === 103 && (s.charCodeAt(i+1)|32) === 101 && (s.charCodeAt(i+2)|32) === 110 && (s.charCodeAt(i+3)|32) === 101 && (s.charCodeAt(i+4)|32) === 114 && (s.charCodeAt(i+5)|32) === 97 && (s.charCodeAt(i+6)|32) === 108; }
+```
 
 ## General Number Format
 
@@ -118,52 +145,81 @@ The 'general' format for spreadsheets (identified by format code 0) is highly
 context-sensitive and the implementation tries to follow the format to the best
 of its abilities given the knowledge.
 
+First: 32-bit integers in base 10 are shorter than 11 characters, so they will
+always be written in full:
+
 ```js>tmp/40_general.js
-var general_fmt = function(v, opts) {
+function general_fmt_int(v, opts) { return ""+v; }
+SSF._general_int = general_fmt_int;
 ```
 
-Booleans are serialized in upper case:
+Next: other numbers require some finessing:
 
 ```
-  if(typeof v === 'boolean') return v ? "TRUE" : "FALSE";
+var general_fmt_num = (function make_general_fmt_num() {
+var gnr1 = /\.(\d*[1-9])0+$/, gnr2 = /\.0*$/, gnr4 = /\.(\d*[1-9])0+/, gnr5 = /\.0*[Ee]/, gnr6 = /(E[+-])(\d)$/;
+function gfn2(v) {
+  var w = (v<0?12:11);
+  var o = gfn5(v.toFixed(12)); if(o.length <= w) return o;
+  o = v.toPrecision(10); if(o.length <= w) return o;
+  return v.toExponential(5);
+}
+function gfn3(v) {
+  var o = v.toFixed(11).replace(gnr1,".$1");
+  if(o.length > (v<0?12:11)) o = v.toPrecision(6);
+  return o;
+}
+function gfn4(o) {
+  for(var i = 0; i != o.length; ++i) if((o.charCodeAt(i) | 0x20) === 101) return o.replace(gnr4,".$1").replace(gnr5,"E").replace("e","E").replace(gnr6,"$10$2");
+  return o;
+}
+function gfn5(o) {
+  //for(var i = 0; i != o.length; ++i) if(o.charCodeAt(i) === 46) return o.replace(gnr2,"").replace(gnr1,".$1");
+  //return o;
+  return o.indexOf(".") > -1 ? o.replace(gnr2,"").replace(gnr1,".$1") : o;
+}
+return function general_fmt_num(v, opts) {
+  var V = Math.floor(Math.log(Math.abs(v))*Math.LOG10E), o;
+  if(V >= -4 && V <= -1) o = v.toPrecision(10+V);
+  else if(Math.abs(V) <= 9) o = gfn2(v);
+  else if(V === 10) o = v.toFixed(10).substr(0,12);
+  else o = gfn3(v);
+  return gfn5(gfn4(o));
+};})();
+SSF._general_num = general_fmt_num;
 ```
 
-For numbers, try to display up to 11 digits of the number (the original code
-`return v.toString().substr(0,11);` was not satisfactory in the case of 11 2/3)
+Finally
 
-```
-  if(typeof v === 'number') {
-    var o, V = v < 0 ? -v : v;
-    if(V >= 0.1 && V < 1) o = v.toPrecision(9);
-    else if(V >= 0.01 && V < 0.1) o = v.toPrecision(8);
-    else if(V >= 0.001 && V < 0.01) o = v.toPrecision(7);
-    else if(V >= 0.0001 && V < 0.001) o = v.toPrecision(6);
-    else if(V >= Math.pow(10,10) && V < Math.pow(10,11)) o = v.toFixed(10).substr(0,12);
-    else if(V > Math.pow(10,-9) && V < Math.pow(10,11)) {
-      o = v.toFixed(12).replace(/(\.[0-9]*[1-9])0*$/,"$1").replace(/\.$/,"");
-      if(o.length > (v<0?12:11)) o = v.toPrecision(10);
-      if(o.length > (v<0?12:11)) o = v.toExponential(5);
-    }
-    else {
-      o = v.toFixed(11).replace(/(\.[0-9]*[1-9])0*$/,"$1");
-      if(o.length > (v<0?12:11)) o = v.toPrecision(6);
-    }
-    o = o.replace(/(\.[0-9]*[1-9])0+e/,"$1e").replace(/\.0*e/,"e");
-    return o.replace("e","E").replace(/\.0*$/,"").replace(/\.([0-9]*[^0])0*$/,".$1").replace(/(E[+-])([0-9])$/,"$1"+"0"+"$2");
-  }
+```js>tmp/40_general.js
+function general_fmt(v, opts) {
+  switch(typeof v) {
 ```
 
 For strings, just return the text as-is:
 
 ```
-  if(typeof v === 'string') return v;
+    case 'string': return v;
+```
+
+Booleans are serialized in upper case:
+
+```
+    case 'boolean': return v ? "TRUE" : "FALSE";
+```
+
+For numbers, call the relevant function (int or num) based on the value:
+
+```
+    case 'number': return (v|0) === v ? general_fmt_int(v, opts) : general_fmt_num(v, opts);
+  }
 ```
 
 Anything else is bad:
 
 ```
   throw new Error("unsupported value in General format: " + v);
-};
+}
 SSF._general = general_fmt;
 ```
 
@@ -270,11 +326,23 @@ portion of a 24 hour day).
 Excel supports the alternative Hijri calendar (indicated with `b2`):
 
 ```js>tmp/50_date.js
-var parse_date_code = function parse_date_code(v,opts,b2) {
-  var date = Math.floor(v), time = Math.floor(86400 * (v - date)+1e-6), dow=0;
+function parse_date_code(v,opts,b2) {
+```
+
+Date codes beyond 12/31/9999 are invalid:
+
+```
+  if(v > 2958465 || v < 0) return null;
+```
+
+Now we can parse!
+
+```
+  var date = (v|0), time = Math.floor(86400 * (v - date)), dow=0;
   var dout=[];
   var out={D:date, T:time, u:86400*(v-date)-time,y:0,m:0,d:0,H:0,M:0,S:0,q:0};
-  fixopts(opts = (opts||{}));
+  if(Math.abs(out.u) < 1e-6) out.u = 0;
+  fixopts(opts != null ? opts : (opts=[]));
 ```
 
 Excel help actually recommends treating the 1904 date codes as 1900 date codes
@@ -284,12 +352,6 @@ shifted by 1462 days.
   if(opts.date1904) date += 1462;
 ```
 
-Date codes beyond 12/31/9999 are invalid:
-
-```
-  if(date > 2958465) return null;
-```
-
 Due to floating point issues, correct for subseconds:
 
 ```
@@ -349,7 +411,7 @@ Because JS dates cannot represent the bad leap day, this returns an object:
   out.H = time;
   out.q = dow;
   return out;
-};
+}
 SSF.parse_date_code = parse_date_code;
 ```
 
@@ -361,105 +423,184 @@ function fix_hijri(date, o) { return 0; }
 
 ## Evaluating Number Formats
 
-```js>tmp/60_number.js
-var commaify = function(s) { return _strrev(_strrev(s).replace(/.../g,"$&,")).replace(/^,/,""); };
-var write_num = function(type, fmt, val) {
+The utility `commaify` adds commas to integers:
+
+```js>tmp/56_commaify.js
+function commaify(s) {
+  if(s.length <= 3) return s;
+  var j = (s.length % 3), o = s.substr(0,j);
+  for(; j!=s.length; j+=3) o+=(o.length > 0 ? "," : "") + s.substr(j,3);
+  return o;
+}
 ```
 
-For parentheses, explicitly resolve the sign issue:
+`write_num` is broken into sub-functions to help with optimization:
 
-```js>tmp/60_number.js
-  if(type === '(' && !fmt.match(/\).*[0#]/)) {
-    var ffmt = fmt.replace(/\( */,"").replace(/ \)/,"").replace(/\)/,"");
-    if(val >= 0) return write_num('n', ffmt, val);
-    return '(' + write_num('n', ffmt, -val) + ')';
-  }
+```js>tmp/57_numhead.js
+var write_num = (function make_write_num(){
 ```
 
-Percentage values should be physically shifted:
+### Percentages
 
-```js>tmp/60_number.js
-  var mul = 0, o;
-  fmt = fmt.replace(/%/g,function() { mul++; return ""; });
-  if(mul !== 0) return write_num(type, fmt, val * Math.pow(10,2*mul)) + fill("%",mul);
+The underlying number for the percentages should be physically shifted:
+
+```js>tmp/59_numhelp.js
+var pct1 = /%/g;
+function write_num_pct(type, fmt, val){
+  var sfmt = fmt.replace(pct1,""), mul = fmt.length - sfmt.length;
+  return write_num(type, sfmt, val * Math.pow(10,2*mul)) + fill("%",mul);
+}
 ```
 
+### Trailing Commas
+
 Formats with multiple commas after the decimal point should be shifted by the
 appropiate multiple of 1000 (more magic):
 
 ```js>tmp/60_number.js
-  fmt = fmt.replace(/(\.0+)(,+)$/g,function($$,$1,$2) { mul=$2.length; return $1; });
-  if(mul !== 0) return write_num(type, fmt, val / Math.pow(10,3*mul));
+function write_num_cm(type, fmt, val){
+  var idx = fmt.length - 1;
+  while(fmt.charCodeAt(idx-1) === 44) --idx;
+  return write_num(type, fmt.substr(0,idx), val / Math.pow(10,3*(fmt.length-idx)));
+}
 ```
 
+### Exponential
+
 For exponents, get the exponent and mantissa and format them separately:
 
 ```
-  if(fmt.indexOf("E") > -1) {
-    var idx = fmt.indexOf("E") - fmt.indexOf(".") - 1;
+function write_num_exp(fmt, val){
+  var o;
+  var idx = fmt.indexOf("E") - fmt.indexOf(".") - 1;
 ```
 
 For the special case of engineering notation, "shift" the decimal:
 
 ```
-    if(fmt.match(/^#+0.0E\+0$/)) {
-      var period = fmt.indexOf("."); if(period === -1) period=fmt.indexOf('E');
-      var ee = (Number(val.toExponential(0).substr(2+(val<0?1:0))))%period;
-      if(ee < 0) ee += period;
-      o = (val/Math.pow(10,ee)).toPrecision(idx+1+(period+ee)%period);
-      if(!o.match(/[Ee]/)) {
+  if(fmt.match(/^#+0.0E\+0$/)) {
+    var period = fmt.indexOf("."); if(period === -1) period=fmt.indexOf('E');
+    var ee = Math.floor(Math.log(Math.abs(val))*Math.LOG10E)%period;
+    if(ee < 0) ee += period;
+    o = (val/Math.pow(10,ee)).toPrecision(idx+1+(period+ee)%period);
+    if(o.indexOf("e") === -1) {
 ```
 
-TODO: something reasonable
+TODO: something cleaner
 
 ```
-        var fakee = (Number(val.toExponential(0).substr(2+(val<0?1:0))));
-        if(o.indexOf(".") === -1) o = o[0] + "." + o.substr(1) + "E+" + (fakee - o.length+ee);
-        else o += "E+" + (fakee - ee);
-        while(o.substr(0,2) === "0.") {
-          o = o[0] + o.substr(2,period) + "." + o.substr(2+period);
-          o = o.replace(/^0+([1-9])/,"$1").replace(/^0+\./,"0.");
-        }
-        o = o.replace(/\+-/,"-");
+      var fakee = Math.floor(Math.log(Math.abs(val))*Math.LOG10E);
+      if(o.indexOf(".") === -1) o = o[0] + "." + o.substr(1) + "E+" + (fakee - o.length+ee);
+      else o += "E+" + (fakee - ee);
+      while(o.substr(0,2) === "0.") {
+        o = o[0] + o.substr(2,period) + "." + o.substr(2+period);
+        o = o.replace(/^0+([1-9])/,"$1").replace(/^0+\./,"0.");
       }
-      o = o.replace(/^([+-]?)([0-9]*)\.([0-9]*)[Ee]/,function($$,$1,$2,$3) { return $1 + $2 + $3.substr(0,(period+ee)%period) + "." + $3.substr(ee) + "E"; });
-    } else o = val.toExponential(idx);
-    if(fmt.match(/E\+00$/) && o.match(/e[+-][0-9]$/)) o = o.substr(0,o.length-1) + "0" + o[o.length-1];
-    if(fmt.match(/E\-/) && o.match(/e\+/)) o = o.replace(/e\+/,"e");
-    return o.replace("e","E");
+      o = o.replace(/\+-/,"-");
+    }
+    o = o.replace(/^([+-]?)(\d*)\.(\d*)[Ee]/,function($$,$1,$2,$3) { return $1 + $2 + $3.substr(0,(period+ee)%period) + "." + $3.substr(ee) + "E"; });
+  } else o = val.toExponential(idx);
+  if(fmt.match(/E\+00$/) && o.match(/e[+-]\d$/)) o = o.substr(0,o.length-1) + "0" + o[o.length-1];
+  if(fmt.match(/E\-/) && o.match(/e\+/)) o = o.replace(/e\+/,"e");
+  return o.replace("e","E");
+}
+```
+
+### Fractions
+
+```
+var frac1 = /# (\?+)( ?)\/( ?)(\d+)/;
+function write_num_f1(r, aval, sign) {
+  var den = parseInt(r[4]), rr = Math.round(aval * den), base = Math.floor(rr/den);
+  var myn = (rr - base*den), myd = den;
+  return sign + (base === 0 ? "" : ""+base) + " " + (myn === 0 ? fill(" ", r[1].length + 1 + r[4].length) : pad_(myn,r[1].length) + r[2] + "/" + r[3] + pad0(myd,r[4].length));
+}
+function write_num_f2(r, aval, sign) {
+  return sign + (aval === 0 ? "" : ""+aval) + fill(" ", r[1].length + 2 + r[4].length);
+}
+var dec1 = /^#*0*\.(0+)/;
+var closeparen = /\).*[0#]/;
+var phone = /\(###\) ###\\?-####/;
+function hashq(str) {
+  var o = "", cc;
+  for(var i = 0; i != str.length; ++i) switch((cc=str.charCodeAt(i))) {
+    case 35: break;
+    case 63: o+= " "; break;
+    case 48: o+= "0"; break;
+    default: o+= String.fromCharCode(cc);
   }
+  return o;
+}
+```
+
+V8 has an annoying habit of deoptimizing round and floor
+
+```
+function rnd(val, d) { var dd = Math.pow(10,d); return ""+(Math.round(val * dd)/dd); }
+function dec(val, d) { return Math.round((val-Math.floor(val))*Math.pow(10,d)); }
+function flr(val) { if(val < 2147483647 && val > -2147483648) return ""+(val >= 0 ? (val|0) : (val-1|0)); return ""+Math.floor(val); }
+```
+
+### Main Number Writing Function
+
+Finally the body:
+
+```
+function write_num_flt(type, fmt, val) {
+```
+
+For parentheses, explicitly resolve the sign issue:
+
+```js>tmp/60_number.js
+  if(type.charCodeAt(0) === 40 && !fmt.match(closeparen)) {
+    var ffmt = fmt.replace(/\( */,"").replace(/ \)/,"").replace(/\)/,"");
+    if(val >= 0) return write_num_flt('n', ffmt, val);
+    return '(' + write_num_flt('n', ffmt, -val) + ')';
+  }
+```
+
+Helpers are used for:
+- Percentage values
+- Trailing commas
+- Exponentials
+
+```js>tmp/60_number.js
+  if(fmt.charCodeAt(fmt.length - 1) === 44) return write_num_cm(type, fmt, val);
+  if(fmt.indexOf('%') !== -1) return write_num_pct(type, fmt, val);
+  if(fmt.indexOf('E') !== -1) return write_num_exp(fmt, val);
 ```
 
 TODO: localize the currency:
 
 ```
-  if(fmt[0] === "$") return "$"+write_num(type,fmt.substr(fmt[1]==' '?2:1),val);
+  if(fmt.charCodeAt(0) === 36) return "$"+write_num_flt(type,fmt.substr(fmt[1]==' '?2:1),val);
+```
+
+Some simple cases should be resolved first:
+
+```
+  var o, oo;
+  var r, ri, ff, aval = Math.abs(val), sign = val < 0 ? "-" : "";
+  if(fmt.match(/^00+$/)) return sign + pad0r(aval,fmt.length);
+  if(fmt.match(/^[#?]+$/)) {
+    o = pad0r(val,0); if(o === "0") o = "";
+    return o.length > fmt.length ? o : hashq(fmt.substr(0,fmt.length-o.length)) + o;
+  }
 ```
 
 Fractions with known denominator are resolved by rounding:
 
-```js>tmp/60_number.js
-  var r, rr, ff, aval = val < 0 ? -val : val, sign = val < 0 ? "-" : "";
-  if((r = fmt.match(/# (\?+)([ ]?)\/([ ]?)(\d+)/))) {
-    var den = Number(r[4]), rnd = Math.round(aval * den), base = Math.floor(rnd/den);
-    var myn = (rnd - base*den), myd = den;
-    return sign + String(base||"") + " " + (myn === 0 ? fill(" ", r[1].length + 1 + r[4].length) : pad(myn,r[1].length," ") + r[2] + "/" + r[3] + pad(myd,r[4].length));
-  }
+```
+  if((r = fmt.match(frac1)) !== null) return write_num_f1(r, aval, sign);
 ```
 
 A few special general cases can be handled in a very dumb manner:
 
 ```
-  if(fmt.match(/^#+0+$/)) fmt = fmt.replace(/#/g,"");
-  if(fmt.match(/^00+$/)) return (val<0?"-":"")+pad(Math.round(aval),fmt.length);
-  if(fmt.match(/^[#?]+$/)) {
-    o = String(Math.round(val)).replace(/^0$/,"");
-    return o.length > fmt.length ? o : fmt.substr(0,fmt.length-o.length).replace(/#/g,"").replace(/[?]/g," ") + o;
-  }
-  if((r = fmt.match(/^#*0*\.(0+)/))) {
-    o = Math.round(val * Math.pow(10,r[1].length));
-    rr = String(o/Math.pow(10,r[1].length)).replace(/^([^\.]+)$/,"$1."+r[1]).replace(/\.$/,"."+r[1]).replace(/\.([0-9]*)$/,function($$, $1) { return "." + $1 + fill("0", r[1].length-$1.length); });
-    return fmt.match(/0\./) ? rr : rr.replace(/^0\./,".");
+  if(fmt.match(/^#+0+$/) !== null) return sign + pad0r(aval,fmt.length - fmt.indexOf("0"));
+  if((r = fmt.match(dec1)) !== null) {
+    o = rnd(val, r[1].length).replace(/^([^\.]+)$/,"$1."+r[1]).replace(/\.$/,"."+r[1]).replace(/\.(\d*)$/,function($$, $1) { return "." + $1 + fill("0", r[1].length-$1.length); });
+    return fmt.indexOf("0.") !== -1 ? o : o.replace(/^0\./,".");
   }
 ```
 
@@ -467,25 +608,23 @@ The next few simplifications ignore leading optional sigils (`#`):
 
 ```
   fmt = fmt.replace(/^#+([0.])/, "$1");
-  if((r = fmt.match(/^(0*)\.(#*)$/))) {
-    o = Math.round(aval*Math.pow(10,r[2].length));
-    return sign + String(o / Math.pow(10,r[2].length)).replace(/\.(\d*[1-9])0*$/,".$1").replace(/^([-]?\d*)$/,"$1.").replace(/^0\./,r[1].length?"0.":".");
+  if((r = fmt.match(/^(0*)\.(#*)$/)) !== null) {
+    return sign + rnd(aval, r[2].length).replace(/\.(\d*[1-9])0*$/,".$1").replace(/^(-?\d*)$/,"$1.").replace(/^0\./,r[1].length?"0.":".");
   }
-  if((r = fmt.match(/^#,##0([.]?)$/))) return sign + commaify(String(Math.round(aval)));
-  if((r = fmt.match(/^#,##0\.([#0]*0)$/))) {
-    rr = Math.round((val-Math.floor(val))*Math.pow(10,r[1].length));
-    return val < 0 ? "-" + write_num(type, fmt, -val) : commaify(String(Math.floor(val))) + "." + pad(rr,r[1].length,0);
+  if((r = fmt.match(/^#,##0(\.?)$/)) !== null) return sign + commaify(pad0r(aval,0));
+  if((r = fmt.match(/^#,##0\.([#0]*0)$/)) !== null) {
+    return val < 0 ? "-" + write_num_flt(type, fmt, -val) : commaify(""+(Math.floor(val))) + "." + pad0(dec(val, r[1].length),r[1].length);
   }
-  if((r = fmt.match(/^#,#*,#0/))) return write_num(type,fmt.replace(/^#,#*,/,""),val);
+  if((r = fmt.match(/^#,#*,#0/)) !== null) return write_num_flt(type,fmt.replace(/^#,#*,/,""),val);
 ```
 
 The `Zip Code + 4` format needs to treat an interstitial hyphen as a character:
 
 ```
-  if((r = fmt.match(/^([0#]+)(\\?-([0#]+))+$/))) {
-    ff = _strrev(write_num(type, fmt.replace(/[\\-]/g,""), val));
-    rr = 0;
-    return _strrev(_strrev(fmt.replace(/\\/g,"")).replace(/[0#]/g,function(x){return rr<ff.length?ff[rr++]:x==='0'?'0':'';}));
+  if((r = fmt.match(/^([0#]+)(\\?-([0#]+))+$/)) !== null) {
+    o = _strrev(write_num_flt(type, fmt.replace(/[\\-]/g,""), val));
+    ri = 0;
+    return _strrev(_strrev(fmt.replace(/\\/g,"")).replace(/[0#]/g,function(x){return ri<o.length?o[ri++]:x==='0'?'0':"";}));
   }
 ```
 
@@ -493,9 +632,9 @@ There's a better way to generalize the phone number and other formats in terms
 of first drawing the digits, but this selection allows for more nuance:
 
 ```
-  if(fmt.match(/\(###\) ###\\?-####/)) {
-    ff = write_num(type, "##########", val);
-    return "(" + ff.substr(0,3) + ") " + ff.substr(3, 3) + "-" + ff.substr(6);
+  if(fmt.match(phone) !== null) {
+    o = write_num_flt(type, "##########", val);
+    return "(" + o.substr(0,3) + ") " + o.substr(3, 3) + "-" + o.substr(6);
   }
 ```
 
@@ -503,59 +642,55 @@ The frac helper function is used for fraction formats (defined below).
 
 ```
   var oa = "";
-  if((r = fmt.match(/^([#0?]+)([ ]?)\/([ ]?)([#0?]+)/))) {
-    o="";
-    rr = Math.min(r[4].length,7);
-    ff = frac(aval, Math.pow(10,rr)-1, false);
-    o += sign;
+  if((r = fmt.match(/^([#0?]+)( ?)\/( ?)([#0?]+)/)) !== null) {
+    ri = Math.min(r[4].length,7);
+    ff = frac(aval, Math.pow(10,ri)-1, false);
+    o = "" + sign;
     oa = write_num("n", r[1], ff[1]);
     if(oa[oa.length-1] == " ") oa = oa.substr(0,oa.length-1) + "0";
-    o += oa;
-    o += r[2];
-    o += "/";
-    o += r[3];
-    oa = rpad(ff[2],rr," ");
-    if(oa.length < r[4].length) oa = r[4].substr(r[4].length-oa.length).replace(/[?]/g," ").replace(/#/g,"") + oa;
+    o += oa + r[2] + "/" + r[3];
+    oa = rpad_(ff[2],ri);
+    if(oa.length < r[4].length) oa = hashq(r[4].substr(r[4].length-oa.length)) + oa;
     o += oa;
     return o;
   }
-  if((r = fmt.match(/^# ([#0?]+)([ ]?)\/([ ]?)([#0?]+)/))) {
-    rr = Math.min(Math.max(r[1].length, r[4].length),7);
-    ff = frac(aval, Math.pow(10,rr)-1, true);
-    return sign + (ff[0]||(ff[1] ? "" : "0")) + " " + (ff[1] ? pad(ff[1],rr," ") + r[2] + "/" + r[3] + rpad(ff[2],rr," "): fill(" ", 2*rr+1 + r[2].length + r[3].length));
+  if((r = fmt.match(/^# ([#0?]+)( ?)\/( ?)([#0?]+)/)) !== null) {
+    ri = Math.min(Math.max(r[1].length, r[4].length),7);
+    ff = frac(aval, Math.pow(10,ri)-1, true);
+    return sign + (ff[0]||(ff[1] ? "" : "0")) + " " + (ff[1] ? pad_(ff[1],ri) + r[2] + "/" + r[3] + rpad_(ff[2],ri): fill(" ", 2*ri+1 + r[2].length + r[3].length));
   }
 ```
 
 The general class `/^[#0?]+$/` treats the '0' as literal, '#' as noop, '?' as space:
 
 ```
-  if((r = fmt.match(/^[#0?]+$/))) {
-    o = "" + Math.round(val);
+  if((r = fmt.match(/^[#0?]+$/)) !== null) {
+    o = pad0r(val, 0);
     if(fmt.length <= o.length) return o;
-    return fmt.substr(0,fmt.length-o.length).replace(/#/g,"").replace(/\?/g," ") + o;
+    return hashq(fmt.substr(0,fmt.length-o.length)) + o;
   }
-  if((r = fmt.match(/^([#0]+)\.([#0]+)$/))) {
+  if((r = fmt.match(/^([#0]+)\.([#0]+)$/)) !== null) {
     o = "" + val.toFixed(Math.min(r[2].length,10)).replace(/([^0])0+$/,"$1");
-    rr = o.indexOf(".");
-    var lres = fmt.indexOf(".") - rr, rres = fmt.length - o.length - lres;
-    return fmt.substr(0,lres).replace(/#/g,"") + o + fmt.substr(fmt.length-rres).replace(/#/g,"");
+    ri = o.indexOf(".");
+    var lres = fmt.indexOf(".") - ri, rres = fmt.length - o.length - lres;
+    return hashq(fmt.substr(0,lres) + o + fmt.substr(fmt.length-rres));
   }
 ```
 
 The default cases are hard-coded.  TODO: actually parse them
 
 ```js>tmp/60_number.js
-  if((r = fmt.match(/^00,000\.([#0]*0)$/))) {
-    rr = val == Math.floor(val) ? 0 : Math.round((val-Math.floor(val))*Math.pow(10,r[1].length));
+  if((r = fmt.match(/^00,000\.([#0]*0)$/)) !== null) {
+    ri = dec(val, r[1].length);
 ```
 
 Note that this is technically incorrect
 
 ```
-    return val < 0 ? "-" + write_num(type, fmt, -val) : commaify(String(Math.floor(val))).replace(/^\d,\d{3}$/,"0$&").replace(/^\d*$/,function($$) { return "00," + ($$.length < 3 ? pad(0,3-$$.length) : "") + $$; }) + "." + pad(rr,r[1].length,0);
+    return val < 0 ? "-" + write_num_flt(type, fmt, -val) : commaify(flr(val)).replace(/^\d,\d{3}$/,"0$&").replace(/^\d*$/,function($$) { return "00," + ($$.length < 3 ? pad0(0,3-$$.length) : "") + $$; }) + "." + pad0(ri,r[1].length);
   }
   switch(fmt) {
-    case "#,###": var x = commaify(String(Math.round(aval))); return x !== "0" ? sign + x : "";
+    case "#,###": var x = commaify(pad0r(aval,0)); return x !== "0" ? sign + x : "";
 ```
 
 For now, the default case is an error:
@@ -564,15 +699,137 @@ For now, the default case is an error:
     default:
   }
   throw new Error("unsupported format |" + fmt + "|");
-};
+}
+
+```
+
+### Integer Optimizations
+
+```
+function write_num_cm2(type, fmt, val){
+  var idx = fmt.length - 1;
+  while(fmt.charCodeAt(idx-1) === 44) --idx;
+  return write_num(type, fmt.substr(0,idx), val / Math.pow(10,3*(fmt.length-idx)));
+}
+function write_num_pct2(type, fmt, val){
+  var sfmt = fmt.replace(pct1,""), mul = fmt.length - sfmt.length;
+  return write_num(type, sfmt, val * Math.pow(10,2*mul)) + fill("%",mul);
+}
+function write_num_exp2(fmt, val){
+  var o;
+  var idx = fmt.indexOf("E") - fmt.indexOf(".") - 1;
+  if(fmt.match(/^#+0.0E\+0$/)) {
+    var period = fmt.indexOf("."); if(period === -1) period=fmt.indexOf('E');
+    var ee = Math.floor(Math.log(Math.abs(val))*Math.LOG10E)%period;
+    if(ee < 0) ee += period;
+    o = (val/Math.pow(10,ee)).toPrecision(idx+1+(period+ee)%period);
+    if(!o.match(/[Ee]/)) {
+      var fakee = Math.floor(Math.log(Math.abs(val))*Math.LOG10E);
+      if(o.indexOf(".") === -1) o = o[0] + "." + o.substr(1) + "E+" + (fakee - o.length+ee);
+      else o += "E+" + (fakee - ee);
+      o = o.replace(/\+-/,"-");
+    }
+    o = o.replace(/^([+-]?)(\d*)\.(\d*)[Ee]/,function($$,$1,$2,$3) { return $1 + $2 + $3.substr(0,(period+ee)%period) + "." + $3.substr(ee) + "E"; });
+  } else o = val.toExponential(idx);
+  if(fmt.match(/E\+00$/) && o.match(/e[+-]\d$/)) o = o.substr(0,o.length-1) + "0" + o[o.length-1];
+  if(fmt.match(/E\-/) && o.match(/e\+/)) o = o.replace(/e\+/,"e");
+  return o.replace("e","E");
+}
+function write_num_int(type, fmt, val) {
+  if(type.charCodeAt(0) === 40 && !fmt.match(closeparen)) {
+    var ffmt = fmt.replace(/\( */,"").replace(/ \)/,"").replace(/\)/,"");
+    if(val >= 0) return write_num_int('n', ffmt, val);
+    return '(' + write_num_int('n', ffmt, -val) + ')';
+  }
+  if(fmt.charCodeAt(fmt.length - 1) === 44) return write_num_cm2(type, fmt, val);
+  if(fmt.indexOf('%') !== -1) return write_num_pct2(type, fmt, val);
+  if(fmt.indexOf('E') !== -1) return write_num_exp2(fmt, val);
+  if(fmt.charCodeAt(0) === 36) return "$"+write_num_int(type,fmt.substr(fmt[1]==' '?2:1),val);
+  var o;
+  var r, ri, ff, aval = Math.abs(val), sign = val < 0 ? "-" : "";
+  if(fmt.match(/^00+$/)) return sign + pad0(aval,fmt.length);
+  if(fmt.match(/^[#?]+$/)) {
+    o = (""+val); if(val === 0) o = "";
+    return o.length > fmt.length ? o : hashq(fmt.substr(0,fmt.length-o.length)) + o;
+  }
+  if((r = fmt.match(frac1)) !== null) return write_num_f2(r, aval, sign);
+  if(fmt.match(/^#+0+$/) !== null) return sign + pad0(aval,fmt.length - fmt.indexOf("0"));
+  if((r = fmt.match(dec1)) !== null) {
+    o = (""+val).replace(/^([^\.]+)$/,"$1."+r[1]).replace(/\.$/,"."+r[1]).replace(/\.(\d*)$/,function($$, $1) { return "." + $1 + fill("0", r[1].length-$1.length); });
+    return fmt.indexOf("0.") !== -1 ? o : o.replace(/^0\./,".");
+  }
+  fmt = fmt.replace(/^#+([0.])/, "$1");
+  if((r = fmt.match(/^(0*)\.(#*)$/)) !== null) {
+    return sign + (""+aval).replace(/\.(\d*[1-9])0*$/,".$1").replace(/^(-?\d*)$/,"$1.").replace(/^0\./,r[1].length?"0.":".");
+  }
+  if((r = fmt.match(/^#,##0(\.?)$/)) !== null) return sign + commaify((""+aval));
+  if((r = fmt.match(/^#,##0\.([#0]*0)$/)) !== null) {
+    return val < 0 ? "-" + write_num_int(type, fmt, -val) : commaify((""+val)) + "." + fill('0',r[1].length);
+  }
+  if((r = fmt.match(/^#,#*,#0/)) !== null) return write_num_int(type,fmt.replace(/^#,#*,/,""),val);
+  if((r = fmt.match(/^([0#]+)(\\?-([0#]+))+$/)) !== null) {
+    o = _strrev(write_num_int(type, fmt.replace(/[\\-]/g,""), val));
+    ri = 0;
+    return _strrev(_strrev(fmt.replace(/\\/g,"")).replace(/[0#]/g,function(x){return ri<o.length?o[ri++]:x==='0'?'0':"";}));
+  }
+  if(fmt.match(phone) !== null) {
+    o = write_num_int(type, "##########", val);
+    return "(" + o.substr(0,3) + ") " + o.substr(3, 3) + "-" + o.substr(6);
+  }
+  var oa = "";
+  if((r = fmt.match(/^([#0?]+)( ?)\/( ?)([#0?]+)/)) !== null) {
+    ri = Math.min(r[4].length,7);
+    ff = frac(aval, Math.pow(10,ri)-1, false);
+    o = "" + sign;
+    oa = write_num("n", r[1], ff[1]);
+    if(oa[oa.length-1] == " ") oa = oa.substr(0,oa.length-1) + "0";
+    o += oa + r[2] + "/" + r[3];
+    oa = rpad_(ff[2],ri);
+    if(oa.length < r[4].length) oa = hashq(r[4].substr(r[4].length-oa.length)) + oa;
+    o += oa;
+    return o;
+  }
+  if((r = fmt.match(/^# ([#0?]+)( ?)\/( ?)([#0?]+)/)) !== null) {
+    ri = Math.min(Math.max(r[1].length, r[4].length),7);
+    ff = frac(aval, Math.pow(10,ri)-1, true);
+    return sign + (ff[0]||(ff[1] ? "" : "0")) + " " + (ff[1] ? pad_(ff[1],ri) + r[2] + "/" + r[3] + rpad_(ff[2],ri): fill(" ", 2*ri+1 + r[2].length + r[3].length));
+  }
+  if((r = fmt.match(/^[#0?]+$/)) !== null) {
+    o = "" + val;
+    if(fmt.length <= o.length) return o;
+    return hashq(fmt.substr(0,fmt.length-o.length)) + o;
+  }
+  if((r = fmt.match(/^([#0]+)\.([#0]+)$/)) !== null) {
+    o = "" + val.toFixed(Math.min(r[2].length,10)).replace(/([^0])0+$/,"$1");
+    ri = o.indexOf(".");
+    var lres = fmt.indexOf(".") - ri, rres = fmt.length - o.length - lres;
+    return hashq(fmt.substr(0,lres) + o + fmt.substr(fmt.length-rres));
+  }
+  if((r = fmt.match(/^00,000\.([#0]*0)$/)) !== null) {
+    return val < 0 ? "-" + write_num_int(type, fmt, -val) : commaify(""+val).replace(/^\d,\d{3}$/,"0$&").replace(/^\d*$/,function($$) { return "00," + ($$.length < 3 ? pad0(0,3-$$.length) : "") + $$; }) + "." + pad0(0,r[1].length);
+  }
+  switch(fmt) {
+    case "#,###": var x = commaify(""+aval); return x !== "0" ? sign + x : "";
+    default:
+  }
+  throw new Error("unsupported format |" + fmt + "|");
+}
+```
+
+The final function simply dispatches:
+
+```
+return function write_num(type, fmt, val) {
+  return (val|0) === val ? write_num_int(type, fmt, val) : write_num_flt(type, fmt, val);
+};})();
 ```
 
 ## Evaluating Format Strings
 
-```js>tmp/90_main.js
+```js>tmp/82_eval.js
+var abstime = /\[[HhMmSs]*\]/;
 function eval_fmt(fmt, v, opts, flen) {
-  var out = [], o = "", i = 0, c = "", lst='t', q, dt, j;
-  fixopts(opts = (opts || {}));
+  var out = [], o = "", i = 0, c = "", lst='t', q, dt, j, cc;
   var hr='H';
   /* Tokenize */
   while(i < fmt.length) {
@@ -583,9 +840,8 @@ LO Formats sometimes leak "GENERAL" or "General" to stand for general format:
 
 ```
       case 'G': /* General */
-        if(fmt.substr(i, 7).toLowerCase() !== "general")
-          throw new Error('unrecognized character ' + fmt[i] + ' in ' +fmt);
-        out.push({t:'G',v:'General'}); i+=7; break;
+        if(!isgeneral(fmt, i)) throw new Error('unrecognized character ' + c + ' in ' +fmt);
+        out[out.length] = {t:'G', v:'General'}; i+=7; break;
 ```
 
 Text between double-quotes are treated literally, and individual characters are
@@ -596,17 +852,17 @@ strings generated by LO:
 
 ```
       case '"': /* Literal text */
-        for(o="";fmt[++i] !== '"' && i < fmt.length;) o += fmt[i];
-        out.push({t:'t', v:o}); ++i; break;
-      case '\\': var w = fmt[++i], t = "()".indexOf(w) === -1 ? 't' : w;
-        out.push({t:t, v:w}); ++i; break;
+        for(o="";(cc=fmt.charCodeAt(++i)) !== 34 && i < fmt.length;) o += String.fromCharCode(cc);
+        out[out.length] = {t:'t', v:o}; ++i; break;
+      case '\\': var w = fmt[++i], t = (w === "(" || w === ")") ? w : 't';
+        out[out.length] = {t:t, v:w}; ++i; break;
 ```
 
 The underscore character represents a literal space.  Apparently, it also marks
 that the next character is junk.  Hence the read pointer is moved by 2:
 
 ```
-      case '_': out.push({t:'t', v:" "}); i+=2; break;
+      case '_': out[out.length] = {t:'t', v:" "}; i+=2; break;
 ```
 
 The '@' symbol refers to the original text.  The ECMA spec is not complete, but
@@ -616,7 +872,7 @@ mode but I'm not convinced that's the right approach)
 
 ```
       case '@': /* Text Placeholder */
-        out.push({t:'T', v:v}); ++i; break;
+        out[out.length] = {t:'T', v:v}; ++i; break;
 ```
 
 `B1` and `B2` specify which calendar to use, while `b` is the buddhist year.  It
@@ -625,8 +881,8 @@ acts just like `y` except the year is shifted:
 ```
       case 'B': case 'b':
         if(fmt[i+1] === "1" || fmt[i+1] === "2") {
-          if(!dt) dt = parse_date_code(v, opts, fmt[i+1] === "2");
-          q={t:'X', v:fmt.substr(i,2)}; out.push(q); lst = c; i+=2; break;
+          if(dt == null) dt = parse_date_code(v, opts, fmt[i+1] === "2");
+          out[out.length] = {t:'X', v:fmt.substr(i,2)}; lst = c; i+=2; break;
         }
         /* falls through */
 ```
@@ -650,9 +906,8 @@ Negative dates are immediately thrown out:
 Merge strings like "mmmmm" or "hh" into one block:
 
 ```
-        if(!dt) dt = parse_date_code(v, opts);
-        if(!dt) return "";
-        o = fmt[i]; while((fmt[++i]||"").toLowerCase() === c) o+=c;
+        if(dt==null) { dt=parse_date_code(v, opts); if(dt==null) return ""; }
+        o = c; while(++i<fmt.length && fmt[i].toLowerCase() === c) o+=c;
 ```
 
 Only the forward corrections are made here.  The reverse corrections are made later:
@@ -660,8 +915,7 @@ Only the forward corrections are made here.  The reverse corrections are made la
 ```
         if(c === 'm' && lst.toLowerCase() === 'h') c = 'M'; /* m = minute */
         if(c === 'h') c = hr;
-        o = o.toLowerCase();
-        q={t:c, v:o}; out.push(q); lst = c; break;
+        out[out.length] = {t:c, v:o}; lst = c; break;
 ```
 
 The (poorly documented) rule regarding `A/P` and `AM/PM` is that if they show up
@@ -674,13 +928,13 @@ the HH/hh jazz.  TODO: investigate this further.
 
 ```
       case 'A':
-        if(!dt) dt = parse_date_code(v, opts);
-        if(!dt) return "";
-        q={t:c,v:"A"};
+        q={t:c, v:"A"};
+        if(dt==null) dt=parse_date_code(v, opts);
         if(fmt.substr(i, 3) === "A/P") {q.v = dt.H >= 12 ? "P" : "A"; q.t = 'T'; hr='h';i+=3;}
         else if(fmt.substr(i,5) === "AM/PM") { q.v = dt.H >= 12 ? "PM" : "AM"; q.t = 'T'; i+=5; hr='h'; }
-        else { q.t = "t"; i++; }
-        out.push(q); lst = c; break;
+        else { q.t = "t"; ++i; }
+        if(dt==null && q.t === 'T') return "";
+        out[out.length] = q; lst = c; break;
 ```
 
 Conditional and color blocks should be handled at one point (TODO).  The
@@ -691,10 +945,9 @@ pseudo-type `Z` is used to capture absolute time blocks:
         o = c;
         while(fmt[i++] !== ']' && i < fmt.length) o += fmt[i];
         if(o.substr(-1) !== ']') throw 'unterminated "[" block: |' + o + '|';
-        if(o.match(/\[[HhMmSs]*\]/)) {
-          if(!dt) dt = parse_date_code(v, opts);
-          if(!dt) return "";
-          out.push({t:'Z', v:o.toLowerCase()});
+        if(o.match(abstime)) {
+          if(dt==null) { dt=parse_date_code(v, opts); if(dt==null) return ""; }
+          out[out.length] = {t:'Z', v:o.toLowerCase()};
         } else { o=""; }
         break;
 ```
@@ -707,14 +960,14 @@ separately:
 ```
       /* Numbers */
       case '.':
-        if(dt) {
+        if(dt != null) {
           o = c; while((c=fmt[++i]) === "0") o += c;
-          out.push({t:'s', v:o}); break;
+          out[out.length] = {t:'s', v:o}; break;
         }
         /* falls through */
       case '0': case '#':
         o = c; while("0#?.,E+-%".indexOf(c=fmt[++i]) > -1 || c=='\\' && fmt[i+1] == "-" && "0#".indexOf(fmt[i+2])>-1) o += c;
-        out.push({t:'n', v:o}); break;
+        out[out.length] = {t:'n', v:o}; break;
 
 ```
 
@@ -723,8 +976,8 @@ number 123.456 under format `|??| /  |???| |???| foo` is `|15432| /  |125| |   |
 
 ```
       case '?':
-        o = fmt[i]; while(fmt[++i] === c) o+=c;
-        q={t:c, v:o}; out.push(q); lst = c; break;
+        o = c; while(fmt[++i] === c) o+=c;
+        q={t:c, v:o}; out[out.length] = q; lst = c; break;
 ```
 
 Due to how the CSV generation works, asterisk characters are discarded.  TODO:
@@ -738,28 +991,26 @@ communicate this somehow, possibly with an option
 The open and close parens `()` also has special meaning (for negative numbers):
 
 ```
-      case '(': case ')': out.push({t:(flen===1?'t':c),v:c}); ++i; break;
+      case '(': case ')': out[out.length] = {t:(flen===1?'t':c), v:c}; ++i; break;
 ```
 
 The nonzero digits show up in fraction denominators:
 
 ```
       case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
-        o = fmt[i]; while("0123456789".indexOf(fmt[++i]) > -1) o+=fmt[i];
-        out.push({t:'D', v:o}); break;
+        o = c; while("0123456789".indexOf(fmt[++i]) > -1) o+=fmt[i];
+        out[out.length] = {t:'D', v:o}; break;
 ```
 
 The default magic characters are listed in subsubsections 18.8.30-31 of ECMA376:
 
 ```
-      case ' ': out.push({t:c,v:c}); ++i; break;
+      case ' ': out[out.length] = {t:c, v:c}; ++i; break;
       default:
-        if(",$-+/():!^&'~{}<>=€acfijklopqrtuvwxz".indexOf(c) === -1)
-          throw 'unrecognized character ' + fmt[i] + ' in ' + fmt;
-        out.push({t:'t', v:c}); ++i; break;
+        if(",$-+/():!^&'~{}<>=€acfijklopqrtuvwxz".indexOf(c) === -1) throw new Error('unrecognized character ' + c + ' in ' + fmt);
+        out[out.length] = {t:'t', v:c}; ++i; break;
     }
   }
-
 ```
 
 In order to identify cases like `MMSS`, where the fact that this is a minute
@@ -814,15 +1065,20 @@ group them together to construct the real number string:
   for(i=0; i < out.length; ++i) {
     switch(out[i].t) {
       case 't': case 'T': case ' ': case 'D': break;
-      case 'X': delete out[i]; break;
+      case 'X': out[i] = undefined; break;
       case 'd': case 'm': case 'y': case 'h': case 'H': case 'M': case 's': case 'e': case 'b': case 'Z':
-        out[i].v = write_date(out[i].t, out[i].v, dt, ss0);
+        out[i].v = write_date(out[i].t.charCodeAt(0), out[i].v, dt, ss0);
         out[i].t = 't'; break;
       case 'n': case '(': case '?':
         jj = i+1;
-        while(out[jj] && ("?D".indexOf(out[jj].t) > -1 || (" t".indexOf(out[jj].t) > -1 && "?t".indexOf((out[jj+1]||{}).t)>-1 && (out[jj+1].t == '?' || out[jj+1].v == '/')) || out[i].t == '(' && (")n ".indexOf(out[jj].t) > -1) || out[jj].t == 't' && (out[jj].v == '/' || '$€'.indexOf(out[jj].v) > -1 || (out[jj].v == ' ' && (out[jj+1]||{}).t == '?')))) {
+        while(out[jj] != null && (
+          (c=out[jj].t) === "?" || c === "D" ||
+          (c === " " || c === "t") && out[jj+1] != null && (out[jj+1].t === '?' || out[jj+1].t === "t" && out[jj+1].v === '/') ||
+          out[i].t === '(' && (c === ' ' || c === 'n' || c === ')') ||
+          c === 't' && (out[jj].v === '/' || '$€'.indexOf(out[jj].v) > -1 || out[jj].v === ' ' && out[jj+1] != null && out[jj+1].t == '?')
+        )) {
           out[i].v += out[jj].v;
-          delete out[jj]; ++jj;
+          out[jj] = undefined; ++jj;
         }
         nstr += out[i].v;
         i = jj-1; break;
@@ -834,8 +1090,10 @@ group them together to construct the real number string:
 Next, process the complete number string:
 
 ```
-  if(nstr) {
-    var ostr = write_num(nstr[0]=='(' ? '(' : 'n', nstr, (v<0&&nstr[0] == "-" ? -v : v));
+  var vv = "", myv, ostr;
+  if(nstr.length > 0) {
+    myv = (v<0&&nstr.charCodeAt(0) === 45 ? -v : v); /* '-' */
+    ostr = write_num(nstr.charCodeAt(0) === 40 ? '(' : 'n', nstr, myv); /* '(' */
     jj=ostr.length-1;
 ```
 
@@ -843,22 +1101,19 @@ Find the first decimal point:
 
 ```
     var decpt = out.length;
-    for(i=0; i < out.length; ++i) if(out[i] && out[i].v.indexOf(".") > -1) { decpt = i; break; }
-    var lasti=out.length, vv;
+    for(i=0; i < out.length; ++i) if(out[i] != null && out[i].v.indexOf(".") > -1) { decpt = i; break; }
+    var lasti=out.length;
 ```
 
 If there is no decimal point or exponential, the algorithm is straightforward:
 
 ```
-    if(decpt === out.length && !ostr.match(/E/)) {
+    if(decpt === out.length && ostr.indexOf("E") === -1) {
       for(i=out.length-1; i>= 0;--i) {
-        if(!out[i] || 'n?('.indexOf(out[i].t) === -1) continue;
-        vv = out[i].v.split("");
-        for(j=vv.length-1; j>=0; --j) {
-          if(jj>=0) vv[j] = ostr[jj--];
-          else vv[j] = "";
-        }
-        out[i].v = vv.join("");
+        if(out[i] == null || 'n?('.indexOf(out[i].t) === -1) continue;
+        if(jj>=out[i].v.length-1) { jj -= out[i].v.length; out[i].v = ostr.substr(jj+1, out[i].v.length); }
+        else if(jj < 0) out[i].v = "";
+        else { out[i].v = ostr.substr(0, jj+1); jj = -1; }
         out[i].t = 't';
         lasti = i;
       }
@@ -868,29 +1123,29 @@ If there is no decimal point or exponential, the algorithm is straightforward:
 Otherwise we have to do something a bit trickier:
 
 ```
-    else if(decpt !== out.length && !ostr.match(/E/)) {
+    else if(decpt !== out.length && ostr.indexOf("E") === -1) {
       jj = ostr.indexOf(".")-1;
       for(i=decpt; i>= 0; --i) {
-        if(!out[i] || 'n?('.indexOf(out[i].t) === -1) continue;
-        vv = out[i].v.split("");
-        for(j=out[i].v.indexOf(".")>-1&&i==decpt?out[i].v.indexOf(".")-1:vv.length-1; j>=0; --j) {
-          if(jj>=0 && "0#".indexOf(vv[j])>-1) vv[j] = ostr[jj--];
-          else vv[j] = "";
+        if(out[i] == null || 'n?('.indexOf(out[i].t) === -1) continue;
+        j=out[i].v.indexOf(".")>-1&&i===decpt?out[i].v.indexOf(".")-1:out[i].v.length-1;
+        vv = out[i].v.substr(j+1);
+        for(; j>=0; --j) {
+          if(jj>=0 && (out[i].v[j] === "0" || out[i].v[j] === "#")) vv = ostr[jj--] + vv;
         }
-        out[i].v = vv.join("");
+        out[i].v = vv;
         out[i].t = 't';
         lasti = i;
       }
       if(jj>=0 && lasti<out.length) out[lasti].v = ostr.substr(0,jj+1) + out[lasti].v;
       jj = ostr.indexOf(".")+1;
       for(i=decpt; i<out.length; ++i) {
-        if(!out[i] || 'n?('.indexOf(out[i].t) === -1 && i != decpt ) continue;
-        vv = out[i].v.split("");
-        for(j=out[i].v.indexOf(".")>-1&&i==decpt?out[i].v.indexOf(".")+1:0; j<vv.length; ++j) {
-          if(jj<ostr.length) vv[j] = ostr[jj++];
-          else vv[j] = "";
+        if(out[i] == null || 'n?('.indexOf(out[i].t) === -1 && i !== decpt ) continue;
+        j=out[i].v.indexOf(".")>-1&&i===decpt?out[i].v.indexOf(".")+1:0;
+        vv = out[i].v.substr(0,j);
+        for(; j<out[i].v.length; ++j) {
+          if(jj<ostr.length) vv += ostr[jj++];
         }
-        out[i].v = vv.join("");
+        out[i].v = vv;
         out[i].t = 't';
         lasti = i;
       }
@@ -902,8 +1157,9 @@ The magic in the next line is to ensure that the negative number is passed as
 positive when there is an explicit hyphen before it (e.g. `#,##0.0;-#,##0.0`):
 
 ```
-  for(i=0; i<out.length; ++i) if(out[i] && 'n(?'.indexOf(out[i].t)>-1) {
-    out[i].v = write_num(out[i].t, out[i].v, (flen >1 && v < 0 && i>0 && out[i-1].v == "-" ? -v:v));
+  for(i=0; i<out.length; ++i) if(out[i] != null && 'n(?'.indexOf(out[i].t)>-1) {
+    myv = (flen >1 && v < 0 && i>0 && out[i-1].v === "-" ? -v:v);
+    out[i].v = write_num(out[i].t, out[i].v, myv);
     out[i].t = 't';
   }
 ```
@@ -912,7 +1168,7 @@ Now we just need to combine the elements
 
 ```
   var retval = "";
-  for(i=0; i != out.length; ++i) if(out[i]) retval += out[i].v;
+  for(i=0; i !== out.length; ++i) if(out[i] != null) retval += out[i].v;
   return retval;
 }
 SSF._eval = eval_fmt;
@@ -927,78 +1183,90 @@ display minutes instead of the month.
 
 ```js>tmp/50_date.js
 /*jshint -W086 */
-var write_date = function(type, fmt, val, ss0) {
-  var o, ss, tt, y = val.y, sss0;
+function write_date(type, fmt, val, ss0) {
+  var o="", ss=0, tt=0, y = val.y, out, outl = 0;
   switch(type) {
 ```
 
 `b` years are shifted by 543 (`y` 1900 == `b` 2443):
 
 ```
-    case 'b': y = val.y + 543;
+    case 98: /* 'b' buddhist year */
+      y = val.y + 543;
       /* falls through */
-    case 'y': switch(fmt.length) { /* year */
-      case 1: case 2: return pad(y % 100,2);
 ```
 
-Apparently, even `yyyyyyyyyyyyyyyyyyyy` is a 4 digit year
+`yyyyyyyyyyyyyyyyyyyy` is a 4 digit year
 
 ```
-      default: return pad(y % 10000,4);
-    }
-    case 'm': switch(fmt) {
-      case 'm': return val.m;
-      case 'mm': return pad(val.m,2);
-      case 'mmm': return months[val.m-1][1];
-      case 'mmmmm': return months[val.m-1][0];
+    case 121: /* 'y' year */
+    switch(fmt.length) {
+      case 1: case 2: out = y % 100; outl = 2; break;
+      default: out = y % 10000; outl = 4; break;
+    } break;
 ```
 
-Strangely enough, `mmmmmmmmmmmmmmmmmmmm` is treated as the full month name:
+`mmmmmmmmmmmmmmmmmmmm` is treated as the full month name:
 
 ```
+    case 109: /* 'm' month */
+    switch(fmt.length) {
+      case 1: case 2: out = val.m; outl = fmt.length; break;
+      case 3: return months[val.m-1][1];
+      case 5: return months[val.m-1][0];
       default: return months[val.m-1][2];
-    }
-    case 'd': switch(fmt) { /* day */
-      case 'd': return val.d;
-      case 'dd': return pad(val.d,2);
-      case 'ddd': return days[val.q][0];
+    } break;
 ```
 
-Strangely enough, `dddddddddddddddddddd` is treated as the full day name:
+`dddddddddddddddddddd` is treated as the full day name:
 
 ```
+    case 100: /* 'd' day */
+    switch(fmt.length) {
+      case 1: case 2: out = val.d; outl = fmt.length; break;
+      case 3: return days[val.q][0];
       default: return days[val.q][1];
-    }
-    case 'h': switch(fmt) { /* 12-hour */
-      case 'h': return 1+(val.H+11)%12;
-      case 'hh': return pad(1+(val.H+11)%12, 2);
+    } break;
+```
+
+Abnormal hours and minutes are rejected:
+
+```
+    case 104: /* 'h' 12-hour */
+    switch(fmt.length) {
+      case 1: case 2: out = 1+(val.H+11)%12; outl = fmt.length; break;
       default: throw 'bad hour format: ' + fmt;
-    }
-    case 'H': switch(fmt) { /* 24-hour */
-      case 'h': return val.H;
-      case 'hh': return pad(val.H, 2);
+    } break;
+    case 72: /* 'H' 24-hour */
+    switch(fmt.length) {
+      case 1: case 2: out = val.H; outl = fmt.length; break;
       default: throw 'bad hour format: ' + fmt;
-    }
-    case 'M': switch(fmt) { /* minutes */
-      case 'm': return val.M;
-      case 'mm': return pad(val.M, 2);
+    } break;
+    case 77: /* 'M' minutes */
+    switch(fmt.length) {
+      case 1: case 2: out = val.M; outl = fmt.length; break;
       default: throw 'bad minute format: ' + fmt;
-    }
-    case 's': switch(fmt) { /* seconds */
+    } break;
 ```
 
 Unfortunately, the actual subsecond string is based on the presence of other
 terms.  That is passed via the `ss0` parameter:
 
 ```
+    case 115: /* 's' seconds */
+    if(val.u === 0) switch(fmt) {
+      case 's': case 'ss': return pad0(val.S, fmt.length);
+      case '.0': case '.00': case '.000':
+    }
+    switch(fmt) {
       case 's': case 'ss': case '.0': case '.00': case '.000':
-        sss0 = ss0 || 0;
-        tt = Math.pow(10,sss0);
+        if(ss0 >= 2) tt = ss0 === 3 ? 1000 : 100;
+        else tt = ss0 === 1 ? 10 : 1;
         ss = Math.round((tt)*(val.S + val.u));
-        if(fmt === 's') return ss >= 60*tt ? 0 : ss/tt;
-        else if(fmt === 'ss') { if(ss>=60*tt) ss=0; return pad(ss,(2+sss0)).substr(0,2); }
         if(ss >= 60*tt) ss = 0;
-        o = pad(ss,2 + sss0);
+        if(fmt === 's') return ss === 0 ? "0" : ""+ss/tt;
+        o = pad0(ss,2 + ss0);
+        if(fmt === 'ss') return o.substr(0,2);
         return "." + o.substr(2,fmt.length-1);
       default: throw 'bad second format: ' + fmt;
     }
@@ -1007,19 +1275,21 @@ terms.  That is passed via the `ss0` parameter:
 The `Z` type refers to absolute time measures:
 
 ```
-    case 'Z': switch(fmt) {
-      case '[h]': case '[hh]': o = val.D*24+val.H; break;
-      case '[m]': case '[mm]': o = (val.D*24+val.H)*60+val.M; break;
-      case '[s]': case '[ss]': o = ((val.D*24+val.H)*60+val.M)*60+Math.round(val.S+val.u); break;
+    case 90: /* 'Z' absolute time */
+    switch(fmt) {
+      case '[h]': case '[hh]': out = val.D*24+val.H; break;
+      case '[m]': case '[mm]': out = (val.D*24+val.H)*60+val.M; break;
+      case '[s]': case '[ss]': out = ((val.D*24+val.H)*60+val.M)*60+Math.round(val.S+val.u); break;
       default: throw 'bad abstime format: ' + fmt;
-    } return fmt.length === 3 ? o : pad(o, 2);
+    } outl = fmt.length === 3 ? 1 : 2; break;
 ```
 
 The `e` format behavior in excel diverges from the spec.  It claims that `ee`
 should be a two-digit year, but `ee` in excel is actually the four-digit year:
 
 ```
-    case 'e': { return val.y; } break;
+    case 101: /* 'e' era */
+      out = y; outl = 1;
 ```
 
 There is no input to the function that ends up triggering the default behavior:
@@ -1027,7 +1297,8 @@ it is not exported and is only called when the type is in `ymdhHMsZe`
 
 ```
   }
-};
+  if(outl > 0) return pad0(out, outl); else return "";
+}
 /*jshint +W086 */
 ```
 
@@ -1035,11 +1306,17 @@ Based on the value, `choose_fmt` picks the right format string.  If formats have
 explicit negative specifications, those values should be passed as positive:
 
 ```js>tmp/90_main.js
-function choose_fmt(fmt, v, o) {
-  if(typeof fmt === 'number') fmt = ((o&&o.table) ? o.table : table_fmt)[fmt];
-  if(typeof fmt === "string") fmt = split_fmt(fmt);
-  var l = fmt.length;
-  if(l<4 && fmt[l-1].indexOf("@")>-1) --l;
+function choose_fmt(f, v) {
+  var fmt = split_fmt(f);
+  var l = fmt.length, lat = fmt[l-1].indexOf("@");
+  if(l<4 && lat>-1) --l;
+  if(fmt.length > 4) throw "cannot find right format for |" + fmt + "|";
+```
+
+Short-circuit the string case by using the last format if it has "@":
+
+```
+  if(typeof v !== "number") return [4, fmt.length === 4 || lat>-1?fmt[fmt.length-1]:"@"];
   switch(fmt.length) {
 ```
 
@@ -1047,43 +1324,49 @@ In the case of one format, if it contains an "@" then it is a text format.
 There is a big TODO here regarding how to best handle this case.
 
 ```
-    case 1: fmt = fmt[0].indexOf("@")>-1 ? ["General", "General", "General", fmt[0]] : [fmt[0], fmt[0], fmt[0], "@"]; break;
+    case 1: fmt = lat>-1 ? ["General", "General", "General", fmt[0]] : [fmt[0], fmt[0], fmt[0], "@"]; break;
 ```
 
 In the case of 2 or 3 formats, if an `@` appears in the last field of the format
 it is treated as the text format
 
 ```
-    case 2: fmt = fmt[1].indexOf("@")>-1 ? [fmt[0], fmt[0], fmt[0], fmt[1]] : [fmt[0], fmt[1], fmt[0], "@"]; break;
-    case 3: fmt = fmt[2].indexOf("@")>-1 ? [fmt[0], fmt[1], fmt[0], fmt[2]] : [fmt[0], fmt[1], fmt[2], "@"]; break;
+    case 2: fmt = lat>-1 ? [fmt[0], fmt[0], fmt[0], fmt[1]] : [fmt[0], fmt[1], fmt[0], "@"]; break;
+    case 3: fmt = lat>-1 ? [fmt[0], fmt[1], fmt[0], fmt[2]] : [fmt[0], fmt[1], fmt[2], "@"]; break;
     case 4: break;
-    default: throw "cannot find right format for |" + fmt + "|";
   }
-  if(typeof v !== "number") return [fmt.length, fmt[3]];
 ```
 
-Here we have to scan for conditions:
+Here we have to scan for conditions.  Note that the grammar precludes decimals
+but in practice they are fair game:
 
+```js>tmp/88_cond.js
+var cfregex = /\[[=<>]/;
+var cfregex2 = /\[([=<>]*)(-?\d+\.?\d*)\]/;
+function chkcond(v, rr) {
+  if(rr == null) return false;
+  var thresh = parseFloat(rr[2]);
+  switch(rr[1]) {
+    case "=":  if(v == thresh) return true; break;
+    case ">":  if(v >  thresh) return true; break;
+    case "<":  if(v <  thresh) return true; break;
+    case "<>": if(v != thresh) return true; break;
+    case ">=": if(v >= thresh) return true; break;
+    case "<=": if(v <= thresh) return true; break;
+  }
+  return false;
+}
 ```
+
+The main function checks for conditional operators and acts accordingly:
+
+```js>tmp/90_main.js
   var ff = v > 0 ? fmt[0] : v < 0 ? fmt[1] : fmt[2];
-  if(fmt[0].match(/\[[=<>]/) || fmt[1].match(/\[[=<>]/)) {
-    var chk = function(v, rr, out) {
-      if(!rr) return null;
-      var found = false;
-      var thresh = Number(rr[2]);
-      switch(rr[1]) {
-        case "=":  if(v == thresh) found = true; break;
-        case ">":  if(v >  thresh) found = true; break;
-        case "<":  if(v <  thresh) found = true; break;
-        case "<>": if(v != thresh) found = true; break;
-        case ">=": if(v >= thresh) found = true; break;
-        case "<=": if(v <= thresh) found = true; break;
-      }
-      return found ? out : null;
-    };
-    var m1 = fmt[0].match(/\[([=<>]*)([-]?\d+)\]/);
-    var m2 = fmt[1].match(/\[([=<>]*)([-]?\d+)\]/);
-    return chk(v, m1, [l, fmt[0]]) || chk(v, m2, [l, fmt[1]]) || [l, fmt[m1&&m2?2:1]];
+  if(fmt[0].indexOf("[") === -1 && fmt[1].indexOf("[") === -1) return [l, ff];
+  if(fmt[0].match(cfregex) != null || fmt[1].match(cfregex) != null) {
+    var m1 = fmt[0].match(cfregex2);
+    var m2 = fmt[1].match(cfregex2);
+    return chkcond(v, m1) ? [l, fmt[0]] : chkcond(v, m2) ? [l, fmt[1]] : [l, fmt[m1 != null && m2 != null ? 2 : 1]];
   }
   return [l, ff];
 }
@@ -1092,50 +1375,56 @@ Here we have to scan for conditions:
 Finally, the format wrapper brings everything together:
 
 ```
-var format = function format(fmt,v,o) {
-  fixopts(o = (o||{}));
+function format(fmt,v,o) {
+  fixopts(o != null ? o : (o=[]));
+```
+
+The string format is saved to a different variable:
+
+```
+  var sfmt = "";
+  switch(typeof fmt) {
+    case "string": sfmt = fmt; break;
+    case "number": sfmt = (o.table != null ? o.table : table_fmt)[fmt]; break;
+  }
 ```
 
 LibreOffice appears to emit the format "GENERAL" for general:
 
 ```
-  if(typeof fmt === "string" && fmt.toLowerCase() === "general") return general_fmt(v, o);
-  if(typeof fmt === 'number') fmt = (o.table || table_fmt)[fmt];
-  var f = choose_fmt(fmt, v, o);
-  if(f[1].toLowerCase() === "general") return general_fmt(v,o);
+  if(isgeneral(sfmt,0)) return general_fmt(v, o);
+  var f = choose_fmt(sfmt, v);
+  if(isgeneral(f[1])) return general_fmt(v, o);
 ```
 
 The boolean TRUE and FALSE are formatted as if they are the uppercase text:
 
 ```
-  if(v === true) v = "TRUE"; if(v === false) v = "FALSE";
+  if(v === true) v = "TRUE"; else if(v === false) v = "FALSE";
 ```
 
 Empty string should always emit empty, even if there are other characters:
 
 ```
-  if(v === "" || typeof v === "undefined") return "";
+  else if(v === "" || v == null) return "";
   return eval_fmt(f[1], v, o, f[0]);
-};
-
+}
 ```
 
 The methods beginning with an underscore are subject to change and should not be
 used directly in programs.
 
-```js>tmp/90_main.js
-
-SSF._choose = choose_fmt;
+```js>tmp/98_exports.js
 SSF._table = table_fmt;
-SSF.load = function(fmt, idx) { table_fmt[idx] = fmt; };
+SSF.load = function load_entry(fmt, idx) { table_fmt[idx] = fmt; };
 SSF.format = format;
 ```
 
 To support multiple SSF tables:
 
 ```
-SSF.get_table = function() { return table_fmt; };
-SSF.load_table = function(tbl) { for(var i=0; i!=0x0188; ++i) if(tbl[i]) SSF.load(tbl[i], i); };
+SSF.get_table = function get_table() { return table_fmt; };
+SSF.load_table = function load_table(tbl) { for(var i=0; i!=0x0188; ++i) if(tbl[i] !== undefined) SSF.load(tbl[i], i); };
 ```
 
 ## Fraction Library
@@ -1143,7 +1432,7 @@ SSF.load_table = function(tbl) { for(var i=0; i!=0x0188; ++i) if(tbl[i]) SSF.loa
 The implementation is from [our frac library](https://github.com/SheetJS/frac/):
 
 ```js>tmp/30_frac.js
-var frac = function frac(x, D, mixed) {
+function frac(x, D, mixed) {
   var sgn = x < 0 ? -1 : 1;
   var B = x * sgn;
   var P_2 = 0, P_1 = 1, P = 0;
@@ -1164,19 +1453,16 @@ var frac = function frac(x, D, mixed) {
   if(Q===0) throw "Unexpected state: "+P+" "+P_1+" "+P_2+" "+Q+" "+Q_1+" "+Q_2;
   var q = Math.floor(sgn * P/Q);
   return [q, sgn*P - q*Q, Q];
-};
+}
 ```
 
 ## JS Boilerplate
 
 ```js>tmp/00_header.js
 /* ssf.js (C) 2013-2014 SheetJS -- http://sheetjs.com */
+/*jshint -W041 */
 var SSF = {};
-var make_ssf = function(SSF){
-var _strrev = function(x) { return String(x).split("").reverse().join("");};
-function fill(c,l) { return new Array(l+1).join(c); }
-function pad(v,d,c){var t=String(v);return t.length>=d?t:(fill(c||0,d-t.length)+t);}
-function rpad(v,d,c){var t=String(v);return t.length>=d?t:(t+fill(c||0,d-t.length));}
+var make_ssf = function make_ssf(SSF){
 ```
 
 ```js>tmp/99_footer.js
@@ -1205,6 +1491,8 @@ cat tmp/*.js > ssf.js
 tmp/
 node_modules/
 .vocrc
+v8.log
+perf.log
 ```
 
 ```>.npmignore
@@ -1213,6 +1501,8 @@ node_modules/
 tmp/
 .gitignore
 .vocrc
+v8.log
+perf.log
 ```
 
 ```make>Makefile
@@ -1230,6 +1520,10 @@ test_min:
 lint:
         jshint ssf.js test/
         jscs ssf.js
+
+.PHONY: perf
+perf:
+        bash misc/perf.sh
 ```
 
 Coverage tests use [blanket](http://npm.im/blanket):
@@ -1263,14 +1557,14 @@ coveralls:
 ```json>package.json
 {
   "name": "ssf",
-  "version": "0.7.1",
+  "version": "0.8.0",
   "author": "SheetJS",
-  "description": "pure-JS library to format data using ECMA-376 spreadsheet Format Codes",
+  "description": "Format data using ECMA-376 spreadsheet Format Codes",
   "keywords": [ "format", "sprintf", "spreadsheet" ],
   "main": "ssf.js",
   "dependencies": {
     "voc":"",
-    "colors":"",
+    "colors":"0.6.2",
     "frac":"0.3.1"
   },
   "devDependencies": {
@@ -1319,7 +1613,7 @@ var fs = require('fs'), assert = require('assert');
 var data = JSON.parse(fs.readFileSync('./test/implied.json','utf8'));
 var skip = [];
 function doit(d) {
-  d[1].forEach(function(r){if(!r[2])assert.equal(SSF.format(r[0],d[0]),r[1]);});
+  d[1].forEach(function(r){if(r.length === 2)assert.equal(SSF.format(r[0],d[0]),r[1]);});
 }
 describe('implied formats', function() {
   data.forEach(function(d) {
@@ -1377,25 +1671,25 @@ The dates test driver tests the date and time formats:
 
 ```js>test/date.js
 /* vim: set ts=2: */
+/*jshint -W041 */
 /*jshint loopfunc:true */
 var SSF = require('../');
 var fs = require('fs'), assert = require('assert');
 var dates = fs.readFileSync('./test/dates.tsv','utf8').split("\n");
 var date2 = fs.readFileSync('./test/cal.tsv',  'utf8').split("\n");
 var times = fs.readFileSync('./test/times.tsv','utf8').split("\n");
-
 function doit(data) {
   var step = Math.ceil(data.length/100), i = 1;
   var headers = data[0].split("\t");
   for(j=0;j<=100;++j) it(j, function() {
     for(var k = 0; k <= step; ++k,++i) {
-      if(!data[i]) return;
+      if(data[i] == null || data[i].length < 3) return;
       var d = data[i].replace(/#{255}/g,"").split("\t");
       for(var w = 1; w < headers.length; ++w) {
-        var expected = d[w], actual = SSF.format(headers[w], Number(d[0]), {});
-        if(actual != expected) throw [actual, expected, w, headers[w],d[0],d].join("|");
-        actual = SSF.format(headers[w].toUpperCase(), Number(d[0]), {});
-        if(actual != expected) throw [actual, expected, w, headers[w],d[0],d].join("|");
+        var expected = d[w], actual = SSF.format(headers[w], parseFloat(d[0]), {});
+        if(actual != expected) throw [actual, expected, w, headers[w],d[0],d,i].join("|");
+        actual = SSF.format(headers[w].toUpperCase(), parseFloat(d[0]), {});
+        if(actual != expected) throw [actual, expected, w, headers[w].toUpperCase(),d[0],d,i].join("|");
       }
     }
   });
@@ -1426,7 +1720,7 @@ function doit(d, headers) {
   it(d[0], function() {
     for(var w = 1; w < headers.length; ++w) {
       var expected = d[w].replace("|", ""), actual;
-      try { actual = SSF.format(headers[w], Number(d[0]), {}); } catch(e) { }
+      try { actual = SSF.format(headers[w], parseFloat(d[0]), {}); } catch(e) { }
       if(actual != expected && d[w][0] !== "|") throw [actual, expected, w, headers[w],d[0],d].join("|");
     }
   });
@@ -1451,7 +1745,7 @@ var data = JSON.parse(fs.readFileSync('./test/oddities.json','utf8'));
 describe('oddities', function() {
   data.forEach(function(d) {
     it(d[0], function(){
-      for(j=1;j<d.length;++j) {
+      for(var j=1;j<d.length;++j) {
         if(d[j].length == 2) {
           var expected = d[j][1], actual = SSF.format(d[0], d[j][0], {});
           assert.equal(actual, expected);
diff --git a/test/date.js b/test/date.js
index 7d8378d..4753a59 100644
--- a/test/date.js
+++ b/test/date.js
@@ -1,23 +1,23 @@
 /* vim: set ts=2: */
+/*jshint -W041 */
 /*jshint loopfunc:true */
 var SSF = require('../');
 var fs = require('fs'), assert = require('assert');
 var dates = fs.readFileSync('./test/dates.tsv','utf8').split("\n");
 var date2 = fs.readFileSync('./test/cal.tsv',  'utf8').split("\n");
 var times = fs.readFileSync('./test/times.tsv','utf8').split("\n");
-
 function doit(data) {
   var step = Math.ceil(data.length/100), i = 1;
   var headers = data[0].split("\t");
   for(j=0;j<=100;++j) it(j, function() {
     for(var k = 0; k <= step; ++k,++i) {
-      if(!data[i]) return;
+      if(data[i] == null || data[i].length < 3) return;
       var d = data[i].replace(/#{255}/g,"").split("\t");
       for(var w = 1; w < headers.length; ++w) {
-        var expected = d[w], actual = SSF.format(headers[w], Number(d[0]), {});
-        if(actual != expected) throw [actual, expected, w, headers[w],d[0],d].join("|");
-        actual = SSF.format(headers[w].toUpperCase(), Number(d[0]), {});
-        if(actual != expected) throw [actual, expected, w, headers[w],d[0],d].join("|");
+        var expected = d[w], actual = SSF.format(headers[w], parseFloat(d[0]), {});
+        if(actual != expected) throw [actual, expected, w, headers[w],d[0],d,i].join("|");
+        actual = SSF.format(headers[w].toUpperCase(), parseFloat(d[0]), {});
+        if(actual != expected) throw [actual, expected, w, headers[w].toUpperCase(),d[0],d,i].join("|");
       }
     }
   });
diff --git a/test/exp.js b/test/exp.js
index ad7b0b1..1b73676 100644
--- a/test/exp.js
+++ b/test/exp.js
@@ -7,7 +7,7 @@ function doit(d, headers) {
   it(d[0], function() {
     for(var w = 1; w < headers.length; ++w) {
       var expected = d[w].replace("|", ""), actual;
-      try { actual = SSF.format(headers[w], Number(d[0]), {}); } catch(e) { }
+      try { actual = SSF.format(headers[w], parseFloat(d[0]), {}); } catch(e) { }
       if(actual != expected && d[w][0] !== "|") throw [actual, expected, w, headers[w],d[0],d].join("|");
     }
   });
diff --git a/test/implied.js b/test/implied.js
index b3b668d..e2105ce 100644
--- a/test/implied.js
+++ b/test/implied.js
@@ -3,7 +3,7 @@ var fs = require('fs'), assert = require('assert');
 var data = JSON.parse(fs.readFileSync('./test/implied.json','utf8'));
 var skip = [];
 function doit(d) {
-  d[1].forEach(function(r){if(!r[2])assert.equal(SSF.format(r[0],d[0]),r[1]);});
+  d[1].forEach(function(r){if(r.length === 2)assert.equal(SSF.format(r[0],d[0]),r[1]);});
 }
 describe('implied formats', function() {
   data.forEach(function(d) {
diff --git a/test/oddities.js b/test/oddities.js
index 6bbbb37..8c8728a 100644
--- a/test/oddities.js
+++ b/test/oddities.js
@@ -6,7 +6,7 @@ var data = JSON.parse(fs.readFileSync('./test/oddities.json','utf8'));
 describe('oddities', function() {
   data.forEach(function(d) {
     it(d[0], function(){
-      for(j=1;j<d.length;++j) {
+      for(var j=1;j<d.length;++j) {
         if(d[j].length == 2) {
           var expected = d[j][1], actual = SSF.format(d[0], d[j][0], {});
           assert.equal(actual, expected);
diff --git a/test/oddities.json b/test/oddities.json
index 2252be1..3639263 100644
--- a/test/oddities.json
+++ b/test/oddities.json
@@ -29,11 +29,11 @@
   ["#,##0.0000000", [1000000, "1,000,000.0000000"]],
   ["#,##0.00000000", [1000000, "1,000,000.00000000"]],
   ["#,##0.000000000", [1000000, "1,000,000.000000000"]],
-  ["#,###", [1, "1"], [-1, "-1"], [0,""], ["TODO", "TODO"]],
+  ["#,###", [1, "1"], [-1, "-1"], [0,""], [12345.6789, "12,346"], ["TODO", "TODO"]],
   ["#.##", [1, "1."], [-1, "-1."], [0,"."], ["sheetjs", "sheetjs"]],
   ["0;0", [1.1, "1"], [-1.1, "-1"], [0,"0"], ["sheetjs", "sheetjs"]],
   ["0.0", [1, "1.0"], [-1, "-1.0"], [0,"0.0"], ["sheetjs", "sheetjs"]],
-  ["0.00", [1, "1.00"], [-1, "-1.00"], [0,"0.00"], ["sheetjs", "sheetjs"]],
+  ["0.00", [1.0001, "1.00"], [-1, "-1.00"], [0,"0.00"], ["sheetjs", "sheetjs"]],
   ["0.000", [1, "1.000"], [-1, "-1.000"], [0,"0.000"], ["sheetjs", "sheetjs"]],
   ["0.0000", [1, "1.0000"], [-1, "-1.0000"], [0,"0.0000"], ["sheetjs", "sheetjs"]],
   ["hh:mm AM/PM", [0.7, "04:48 PM"]],
@@ -76,11 +76,23 @@
     [123456822333333000000,    " 123,456,822,333,333,000.00"],
     [1234568223333330000000, " 1,234,568,223,333,330,000.00"]
   ],
-  ["000#0#0#0##00##00##0#########",
-    [12345, "0000000000012345"]
+  ["00,000.00,", [12345, "00,012.35"] ],
+  ["00,000.00", [12345, "12,345.00"] ],
+  ["000#0#0#0##00##00##0#########", [12345, "0000000000012345"] ],
+  ["##0.0E+0",
+    [1,           "1.0E+0"],
+    [12,         "12.0E+0"],
+    [123,       "123.0E+0"],
+    [1234,        "1.2E+3"],
+    [12345,      "12.3E+3"],
+    [123456,    "123.5E+3"],
+    [1234567,     "1.2E+6"],
+    [12345678,   "12.3E+6"],
+    [123456789, "123.5E+6"]
   ],
   ["0#######0.##0##0######00######0",
-    [12.3456789, "012.3456789000"]
+    [12.3456789, "012.3456789000"],
+    [123456789, "123456789.00000"]
   ],
   ["###\\###\\##0.00",
     [0.00101,              "##0.00"],
@@ -114,7 +126,7 @@
     [123456789.01,   "1234\\567\\890.01"]
   ],
   ["###\\\\###\\\\##\\0", [12345.6789, "\\123\\460"]],
-  ["00000-0000", [941051630, "94105-1630"]],
+  ["00000-0000", [941051630, "94105-1630"], [12345.6789, "00001-2346"]],
   ["000-00-0000", [123456789, "123-45-6789"]],
   ["00000\\-0000", [941051630, "94105-1630"]],
   ["000\\-00\\-0000", [123456789, "123-45-6789"]],
@@ -122,15 +134,18 @@
   ["# ??/??", [12.3456789, "12 28/81"]],
   ["#??/??", [12.3456789, "1000/81"]],
   ["#0#00??/??", [12.3456789, "01000/81"]],
-  ["[<=9999999]###-####;(###) ###-####", [8675309, "867-5309"],[2813308004, "(281) 330-8004"]],
+  ["[<=9999999]###-####;(###) ###-####", [8675309, "867-5309"],[2813308004, "(281) 330-8004"], [2018675309, "(201) 867-5309"]],
   ["[<=9999999]###\\-####;(###) ###\\-####", [8675309, "867-5309"],[2813308004, "(281) 330-8004"]],
-  ["[Red][<-25]General;[Blue][>25]General;[Green]General;[Yellow]General", [50, "50"],[26, "26"],[25,"25"],[1,"1"],[0,"0"],[-1,"-1"],[-25,"-25"],[-26,"26","#"],[-50,"50","#"], ["foo","foo"],["bar","bar"]],
-  ["[Red][<=-25]General;[Blue][>=25]General;[Green]General;[Yellow]General", [50, "50"],[26, "26"],[25,"25"],[1,"1"],[0,"0"],[-1,"-1"],[-25,"-25"],[-26,"26","#"],[-50,"50","#"], ["foo","foo"],["bar","bar"]],
+  ["[Red][<-25]General;[Blue][>25]General;[Green]General;[Yellow]General", [50, "50"],[26, "26"],[25,"25"],[1,"1"],[0,"0"],[-1,"-1"],[-25,"-25"],[-26,"26","#"],[-50.1,"50","#"], ["foo","foo"],["bar","bar"]],
+  ["[Red][<=-25]General;[Blue][>=25]General;[Green]General;[Yellow]General", [50, "50"],[26, "26"],[25,"25"],[1,"1"],[0,"0"],[-1,"-1"],[-25,"-25"],[-26.1,"26","#"],[-50,"50","#"], ["foo","foo"],["bar","bar"]],
   ["[Red]General ;[Blue]General\\ ;[Green]Generalp;[Yellow]General'", [50, "50 "],[0,"0p"],[-25,"-25 "],["foo","foo'"]],
   ["[Red][=50]General;[Blue]000", [50, "50"], [51, "051"], [49, "049"]],
   ["[Red][<>50]General;[Blue]000", [50, "050"], [51, "51"], [49, "49"]],
   ["b", [1,"43"], [1000,"45"], [10000,"70"]],
   ["B2yyyymmdd", [0,"13170829"], [1000,"13200624","#"], [10000,"13451117","#"]],
   ["☃", [0], [1], [-1]],
+  ["#0#######", [12345, "012345"], [12345.4321, "012345"], [12345.6789, "012346"]],
+  ["##,##", [12345, "12,345", ""], [12345.4321, "12,345", ""], [12345.6789, "12,346", ""]],
+  [0, [12345,"12345"], [4294967296.5, 4294967297]],
   ["\"foo\";\"bar\";\"baz\";\"qux\";\"foobar\"", [1], [0], [-1], ["sheetjs"]]
 ]