diff --git a/lib/tput.js b/lib/tput.js index 6be45ee..4cec116 100644 --- a/lib/tput.js +++ b/lib/tput.js @@ -627,8 +627,8 @@ Tput.prototype._compile = function(info, key, str) { } var tkey = info.name + '.' + key - , header = 'var v, dyn = {}, stat = {}, stack = [], out = "";' - , footer = ';return out;' + , header = 'var v, dyn = {}, stat = {}, stack = [], out = [];' + , footer = ';return out.join("");' , code = header , val = str , buff = '' @@ -661,11 +661,7 @@ Tput.prototype._compile = function(info, key, str) { function echo(c) { if (c === '""') return; - if (/^"([^"]|\\")+"$/.test(c)) { - expr('out += ' + c); - } else { - expr('out += (' + c + ' || "")'); - } + expr('out.push(' + c + ')'); } function print(c) { @@ -930,7 +926,7 @@ Tput.prototype._compile = function(info, key, str) { // Perhaps implement a "pushed" counter from the time the if statement // is added, to the time the then statement is added, and pop off // the appropriate number of elements. - // while (pushed--) expr('(v = stack.pop)'); + // while (pushed--) expr('stack.pop()'); stmt(') {'); continue; } @@ -980,14 +976,14 @@ Tput.prototype._compile = function(info, key, str) { v = code.slice(header.length, -footer.length); if (!v.length) { code = 'return "";'; - } else if (v = /^out \+= ("(?:[^"]|\\")+")$/.exec(v)) { + } else if (v = /^out\.push\(("(?:[^"]|\\")+")\)$/.exec(v)) { code = 'return ' + v[1] + ';'; } else { - // Turn `(stack.push(v = params[0]), v),out += (stack.pop() || "")` - // into `out += (params[0] || "")`. + // Turn `(stack.push(v = params[0]), v),out.push(stack.pop())` + // into `out.push(params[0])`. code = code.replace( - /\(stack\.push\(v = params\[(\d+)\]\), v\),out \+= \(stack\.pop\(\) \|\| ""\)/g, - 'out += (params[$1] || "")'); + /\(stack\.push\(v = params\[(\d+)\]\), v\),out\.push\(stack\.pop\(\)\)/g, + 'out.push(params[$1])'); // Remove unnecessary variable initializations. v = code.slice(header.length, -footer.length); @@ -996,8 +992,10 @@ Tput.prototype._compile = function(info, key, str) { if (!~v.indexOf('stat')) code = code.replace('stat = {}, ', ''); if (!~v.indexOf('stack')) code = code.replace('stack = [], ', ''); - // Turn `var out = "";out += "foo",` into `var out = "foo";`. - code = code.replace(/out = "";out \+= ("(?:[^"]|\\")+"),/, 'out = $1;'); + // Turn `var out = [];out.push("foo"),` into `var out = ["foo"];`. + code = code.replace( + /out = \[\];out\.push\(("(?:[^"]|\\")+")\),/, + 'out = [$1];'); } // Terminfos `wyse350-vb`, and `wy350-w` diff --git a/test/terminfo b/test/terminfo index 7540c8f..bd3a944 100644 --- a/test/terminfo +++ b/test/terminfo @@ -431,7 +431,7 @@ return "\x07"; Compiling carriage_return: "\r" return "\r"; Compiling change_scroll_region: "\u001b[%i%p1%d;%p2%dr" -var out = "\x1b[";(params[0]++, params[1]++),out += (params[0] || ""),out += ";",out += (params[1] || ""),out += "r";return out; +var out = ["\x1b["];(params[0]++, params[1]++),out.push(params[0]),out.push(";"),out.push(params[1]),out.push("r");return out.join(""); Compiling clear_all_tabs: "\u001b[3g" return "\x1b[3g"; Compiling clear_screen: "\u001b[H\u001b[2J" @@ -441,9 +441,9 @@ return "\x1b[K"; Compiling clr_eos: "\u001b[J" return "\x1b[J"; Compiling column_address: "\u001b[%i%p1%dG" -var out = "\x1b[";(params[0]++, params[1]++),out += (params[0] || ""),out += "G";return out; +var out = ["\x1b["];(params[0]++, params[1]++),out.push(params[0]),out.push("G");return out.join(""); Compiling cursor_address: "\u001b[%i%p1%d;%p2%dH" -var out = "\x1b[";(params[0]++, params[1]++),out += (params[0] || ""),out += ";",out += (params[1] || ""),out += "H";return out; +var out = ["\x1b["];(params[0]++, params[1]++),out.push(params[0]),out.push(";"),out.push(params[1]),out.push("H");return out.join(""); Compiling cursor_down: "\n" return "\n"; Compiling cursor_home: "\u001b[H" @@ -483,7 +483,7 @@ return "\x1b[7m"; Compiling enter_underline_mode: "\u001b[4m" return "\x1b[4m"; Compiling erase_chars: "\u001b[%p1%dX" -var out = "\x1b[";out += (params[0] || ""),out += "X";return out; +var out = ["\x1b["];out.push(params[0]),out.push("X");return out.join(""); Compiling exit_alt_charset_mode: "\u001b(B" return "\x1b(B"; Compiling exit_attribute_mode: "\u001b(B\u001b[m" @@ -555,25 +555,25 @@ return "\x1b[?1034l"; Compiling meta_on: "\u001b[?1034h" return "\x1b[?1034h"; Compiling parm_dch: "\u001b[%p1%dP" -var out = "\x1b[";out += (params[0] || ""),out += "P";return out; +var out = ["\x1b["];out.push(params[0]),out.push("P");return out.join(""); Compiling parm_delete_line: "\u001b[%p1%dM" -var out = "\x1b[";out += (params[0] || ""),out += "M";return out; +var out = ["\x1b["];out.push(params[0]),out.push("M");return out.join(""); Compiling parm_down_cursor: "\u001b[%p1%dB" -var out = "\x1b[";out += (params[0] || ""),out += "B";return out; +var out = ["\x1b["];out.push(params[0]),out.push("B");return out.join(""); Compiling parm_ich: "\u001b[%p1%d@" -var out = "\x1b[";out += (params[0] || ""),out += "@";return out; +var out = ["\x1b["];out.push(params[0]),out.push("@");return out.join(""); Compiling parm_index: "\u001b[%p1%dS" -var out = "\x1b[";out += (params[0] || ""),out += "S";return out; +var out = ["\x1b["];out.push(params[0]),out.push("S");return out.join(""); Compiling parm_insert_line: "\u001b[%p1%dL" -var out = "\x1b[";out += (params[0] || ""),out += "L";return out; +var out = ["\x1b["];out.push(params[0]),out.push("L");return out.join(""); Compiling parm_left_cursor: "\u001b[%p1%dD" -var out = "\x1b[";out += (params[0] || ""),out += "D";return out; +var out = ["\x1b["];out.push(params[0]),out.push("D");return out.join(""); Compiling parm_right_cursor: "\u001b[%p1%dC" -var out = "\x1b[";out += (params[0] || ""),out += "C";return out; +var out = ["\x1b["];out.push(params[0]),out.push("C");return out.join(""); Compiling parm_rindex: "\u001b[%p1%dT" -var out = "\x1b[";out += (params[0] || ""),out += "T";return out; +var out = ["\x1b["];out.push(params[0]),out.push("T");return out.join(""); Compiling parm_up_cursor: "\u001b[%p1%dA" -var out = "\x1b[";out += (params[0] || ""),out += "A";return out; +var out = ["\x1b["];out.push(params[0]),out.push("A");return out.join(""); Compiling print_screen: "\u001b[i" return "\x1b[i"; Compiling prtr_off: "\u001b[4i" @@ -587,7 +587,7 @@ return "\x1b[!p\x1b[?3;4l\x1b[4l\x1b>"; Compiling restore_cursor: "\u001b8" return "\x1b8"; Compiling row_address: "\u001b[%i%p1%dd" -var out = "\x1b[";(params[0]++, params[1]++),out += (params[0] || ""),out += "d";return out; +var out = ["\x1b["];(params[0]++, params[1]++),out.push(params[0]),out.push("d");return out.join(""); Compiling save_cursor: "\u001b7" return "\x1b7"; Compiling scroll_forward: "\n" @@ -595,7 +595,7 @@ return "\n"; Compiling scroll_reverse: "\u001bM" return "\x1bM"; Compiling set_attributes: "%?%p9%t\u001b(0%e\u001b(B%;\u001b[0%?%p6%t;1%;%?%p2%t;4%;%?%p1%p3%|%t;7%;%?%p4%t;5%;%?%p7%t;8%;m" -var v, stack = [], out = "";;if ((stack.push(v = params[8]), v)) {out += "\x1b(0"} else {out += "\x1b(B"}out += "\x1b[0";if ((stack.push(v = params[5]), v)) {out += ";1"};if ((stack.push(v = params[1]), v)) {out += ";4"};if ((stack.push(v = params[0]), v),(stack.push(v = params[2]), v),(stack.push(v = (stack.pop() | stack.pop()) || 0), v)) {out += ";7"};if ((stack.push(v = params[3]), v)) {out += ";5"};if ((stack.push(v = params[6]), v)) {out += ";8"}out += "m";return out; +var v, stack = [], out = [];;if ((stack.push(v = params[8]), v)) {out.push("\x1b(0")} else {out.push("\x1b(B")}out.push("\x1b[0");if ((stack.push(v = params[5]), v)) {out.push(";1")};if ((stack.push(v = params[1]), v)) {out.push(";4")};if ((stack.push(v = params[0]), v),(stack.push(v = params[2]), v),(stack.push(v = (stack.pop() | stack.pop()) || 0), v)) {out.push(";7")};if ((stack.push(v = params[3]), v)) {out.push(";5")};if ((stack.push(v = params[6]), v)) {out.push(";8")}out.push("m");return out.join(""); Compiling set_tab: "\u001bH" return "\x1bH"; Compiling tab: "\t" @@ -739,7 +739,7 @@ return "\x1b[1;4R"; Compiling clr_bol: "\u001b[1K" return "\x1b[1K"; Compiling user6: "\u001b[%i%d;%dR" -var stack = [], out = "\x1b[";(params[0]++, params[1]++),out += (stack.pop() || ""),out += ";",out += (stack.pop() || ""),out += "R";return out; +var stack = [], out = ["\x1b["];(params[0]++, params[1]++),out.push(stack.pop()),out.push(";"),out.push(stack.pop()),out.push("R");return out.join(""); Compiling user7: "\u001b[6n" return "\x1b[6n"; Compiling user8: "\u001b[?1;2c" @@ -749,15 +749,15 @@ return "\x1b[c"; Compiling orig_pair: "\u001b[39;49m" return "\x1b[39;49m"; Compiling set_foreground: "\u001b[3%?%p1%{1}%=%t4%e%p1%{3}%=%t6%e%p1%{4}%=%t1%e%p1%{6}%=%t3%e%p1%d%;m" -var v, stack = [], out = "";out += "\x1b[3";if ((stack.push(v = params[0]), v),(stack.push(v = 1), v),(stack.push(v = (stack.pop() === stack.pop()) || 0), v)) {out += "4"} else if ((stack.push(v = params[0]), v),(stack.push(v = 3), v),(stack.push(v = (stack.pop() === stack.pop()) || 0), v)) {out += "6"} else if ((stack.push(v = params[0]), v),(stack.push(v = 4), v),(stack.push(v = (stack.pop() === stack.pop()) || 0), v)) {out += "1"} else if ((stack.push(v = params[0]), v),(stack.push(v = 6), v),(stack.push(v = (stack.pop() === stack.pop()) || 0), v)) {out += "3"} else {out += (params[0] || "")}out += "m";return out; +var v, stack = [], out = [];out.push("\x1b[3");if ((stack.push(v = params[0]), v),(stack.push(v = 1), v),(stack.push(v = (stack.pop() === stack.pop()) || 0), v)) {out.push("4")} else if ((stack.push(v = params[0]), v),(stack.push(v = 3), v),(stack.push(v = (stack.pop() === stack.pop()) || 0), v)) {out.push("6")} else if ((stack.push(v = params[0]), v),(stack.push(v = 4), v),(stack.push(v = (stack.pop() === stack.pop()) || 0), v)) {out.push("1")} else if ((stack.push(v = params[0]), v),(stack.push(v = 6), v),(stack.push(v = (stack.pop() === stack.pop()) || 0), v)) {out.push("3")} else {out.push(params[0])}out.push("m");return out.join(""); Compiling set_background: "\u001b[4%?%p1%{1}%=%t4%e%p1%{3}%=%t6%e%p1%{4}%=%t1%e%p1%{6}%=%t3%e%p1%d%;m" -var v, stack = [], out = "";out += "\x1b[4";if ((stack.push(v = params[0]), v),(stack.push(v = 1), v),(stack.push(v = (stack.pop() === stack.pop()) || 0), v)) {out += "4"} else if ((stack.push(v = params[0]), v),(stack.push(v = 3), v),(stack.push(v = (stack.pop() === stack.pop()) || 0), v)) {out += "6"} else if ((stack.push(v = params[0]), v),(stack.push(v = 4), v),(stack.push(v = (stack.pop() === stack.pop()) || 0), v)) {out += "1"} else if ((stack.push(v = params[0]), v),(stack.push(v = 6), v),(stack.push(v = (stack.pop() === stack.pop()) || 0), v)) {out += "3"} else {out += (params[0] || "")}out += "m";return out; +var v, stack = [], out = [];out.push("\x1b[4");if ((stack.push(v = params[0]), v),(stack.push(v = 1), v),(stack.push(v = (stack.pop() === stack.pop()) || 0), v)) {out.push("4")} else if ((stack.push(v = params[0]), v),(stack.push(v = 3), v),(stack.push(v = (stack.pop() === stack.pop()) || 0), v)) {out.push("6")} else if ((stack.push(v = params[0]), v),(stack.push(v = 4), v),(stack.push(v = (stack.pop() === stack.pop()) || 0), v)) {out.push("1")} else if ((stack.push(v = params[0]), v),(stack.push(v = 6), v),(stack.push(v = (stack.pop() === stack.pop()) || 0), v)) {out.push("3")} else {out.push(params[0])}out.push("m");return out.join(""); Compiling key_mouse: "\u001b[M" return "\x1b[M"; Compiling set_a_foreground: "\u001b[3%p1%dm" -var out = "\x1b[3";out += (params[0] || ""),out += "m";return out; +var out = ["\x1b[3"];out.push(params[0]),out.push("m");return out.join(""); Compiling set_a_background: "\u001b[4%p1%dm" -var out = "\x1b[4";out += (params[0] || ""),out += "m";return out; +var out = ["\x1b[4"];out.push(params[0]),out.push("m");return out.join(""); Compiling memory_lock: "\u001bl" return "\x1bl"; Compiling memory_unlock: "\u001bm"