printj/bits/11_switchcode.js

92 lines
2.0 KiB
JavaScript
Raw Normal View History

2016-09-19 06:33:23 +00:00
if(c >= 48 && c < 58) {
if(fmtprec.length) fmtprec += String.fromCharCode(c);
else if(c == 48 && !fmtwidth.length) fmtflags += String.fromCharCode(c);
else fmtwidth += String.fromCharCode(c);
} else switch(c) {
/* positional */
case 36:
if(fmtprec.length) fmtprec += "$";
else if(fmtwidth.charAt(0) == "*") fmtwidth += "$";
else { fmtparam = fmtwidth + "$"; fmtwidth = ""; }
break;
/* flags */
case 39: fmtflags += "'"; break;
case 45: fmtflags += "-"; break;
case 43: fmtflags += "+"; break;
case 32: fmtflags += " "; break;
case 35: fmtflags += "#"; break;
/* width and precision */
case 46: fmtprec = "."; break;
case 42:
if(fmtprec.charAt(0) == ".") fmtprec += "*";
else fmtwidth += "*";
break;
/* length */
case 104:
case 108:
if(fmtlen.length > 1) throw "bad length " + fmtlen + String(c);
fmtlen += String.fromCharCode(c);
break;
case 76:
case 106:
case 122:
case 116:
case 113:
case 90:
case 119:
if(fmtlen !== "") throw "bad length " + fmtlen + String.fromCharCode(c);
fmtlen = String.fromCharCode(c);
break;
case 73:
if(fmtlen !== "") throw "bad length " + fmtlen + 'I';
fmtlen = 'I';
break;
/* conversion */
case 100:
case 105:
case 111:
case 117:
case 120:
case 88:
case 102:
case 70:
case 101:
case 69:
case 103:
case 71:
case 97:
case 65:
case 99:
case 67:
case 115:
case 83:
case 112:
case 110:
case 68:
case 85:
case 79:
case 109:
case 98:
case 66:
case 121:
case 89:
case 74:
case 86:
case 84:
case 37:
infmt = false;
if(fmtprec.length > 1) fmtprec = fmtprec.substr(1);
out.push([String.fromCharCode(c), fmt.substring(start, i+1), fmtparam, fmtflags, fmtwidth, fmtprec, fmtlen]);
start = i+1;
fmtlen = fmtprec = fmtwidth = fmtflags = fmtparam = "";
break;
default:
throw new Error("Invalid format string starting with |" + fmt.substring(start, i+1) + "|");
}