18.09.2015, 08:07
(
Последний раз редактировалось SkittlesAreFalling; 18.09.2015 в 10:37.
)
Quote:
and how to escape the string for use in SQLite functions? in pawn I use format and %q
|
Код:
var mysql_escape_string = function(str) { return str.replace(/[\0\x08\x09\x1a\n\r"'\\\%]/g, function (char) { switch (char) { case "\0": return "\\0"; case "\x08": return "\\b"; case "\x09": return "\\t"; case "\x1a": return "\\z"; case "\n": return "\\n"; case "\r": return "\\r"; case "\"": case "'": case "\\": case "%": return "\\"+char; // prepends a backslash to backslash, percent, // and double/single quotes } }); } String.format = function(str, arr) { var i = -1; var callback; callback = function(exp, p0, p1, p2, p3, p4) { if(exp=='%%') { return '%'; } if(arr[++i] === undefined) { return undefined; } var exp = p2 ? parseInt(p2.substr(1)) : undefined; var base = p3 ? parseInt(p3.substr(1)) : undefined; var val; switch(p4) { case 's': val = arr[i]; break; case 'c': val = arr[i][0]; break; case 'i': val = arr[i].toString(); break; case 'd': val = arr[i].toString(); break; case 'f': val = parseFloat(arr[i]).toFixed(exp); break; case 'p': val = parseFloat(arr[i]).toPrecision(exp); break; case 'e': val = parseFloat(arr[i]).toExponential(exp); break; case 'x': val = parseInt(arr[i]).toString(base?base:16); break; case 'd': val = parseFloat(parseInt(arr[i], base?base:10).toPrecision(exp)).toFixed(0); break; case 'q': val = mysql_escape_string(arr[i]); break; } val = (typeof(val) == 'object') ? JSON.stringify(val) : val.toString(base); var sz = parseInt(p1); /* padding size */ var ch = p1 && p1[0]=='0' ? '0' : ' '; /* isnull? */ while(val.length<sz) { val = p0 !== undefined ? val+ch : ch+val; /* isminus? */ } return val; } var regex = /%(-)?(0?[0-9]+)?([.][0-9]+)?([#][0-9]+)?([scfpexd])/g; return str.replace(regex, callback); } String.prototype.format = function() { return String.format(this, Array.prototype.slice.call(arguments)); }
Example:
Код:
const COLOR = { WHITE : 0xFFFFFFFF, TEAL : 0x008080FF }; SendClientMessage(player.id, COLOR.WHITE, ("Welcome to {%06x}%s{%06x}, {%06x}%s{%06x}!").format((COLOR.TEAL >>> 8), ServerData.name, (COLOR.WHITE >>> 8), (COLOR.TEAL >>> 8), player.name, (COLOR.WHITE >>> 8)));