| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| // Copyright (c) 2014, Ben Noordhuis <info@bnoordhuis.nl> | ||
| // | ||
| // Permission to use, copy, modify, and/or distribute this software for any | ||
| // purpose with or without fee is hereby granted, provided that the above | ||
| // copyright notice and this permission notice appear in all copies. | ||
| // | ||
| // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
| // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
| // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
| // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
| // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
| // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
| // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| var common = require('../common.js'); | ||
| var format = require('util').format; | ||
|
|
||
| var bench = common.createBenchmark(main, {}); | ||
|
|
||
| function main(conf) { | ||
| var fmts = []; | ||
|
|
||
| for (var i = 0; i < 26; i += 1) { | ||
| var k = i, fmt = ''; | ||
| do { | ||
| fmt += '%' + String.fromCharCode(97 + k); | ||
| k = (k + 1) % 26; | ||
| } while (k != i); | ||
| fmts.push(fmt); | ||
| } | ||
|
|
||
| bench.start(); | ||
|
|
||
| for (var i = 0; i < 1e5; i += 1) | ||
| for (var k = 0; k < fmts.length; k += 1) | ||
| format(fmts[k]); | ||
|
|
||
| bench.end(1e5); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| // Copyright (c) 2014, Ben Noordhuis <info@bnoordhuis.nl> | ||
| // | ||
| // Permission to use, copy, modify, and/or distribute this software for any | ||
| // purpose with or without fee is hereby granted, provided that the above | ||
| // copyright notice and this permission notice appear in all copies. | ||
| // | ||
| // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
| // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
| // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
| // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
| // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
| // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
| // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| var L = require('_linklist'); | ||
| var inspect = require('util').inspect; | ||
| var quickeval = process.binding('contextify').quickeval; | ||
|
|
||
| function LRU(maxsize) { | ||
| this.dict = Object.create(null); | ||
| this.list = L.init({}); | ||
| this.size = 0; | ||
| this.maxsize = maxsize; | ||
| } | ||
|
|
||
| LRU.prototype.find = function(key) { | ||
| var val = this.dict[key]; | ||
|
|
||
| // Move to front of the list unless it's already the first node. | ||
| if (val && val !== L.peek(this.list)) | ||
| L.append(this.list, L.remove(val)); | ||
|
|
||
| return val; | ||
| }; | ||
|
|
||
| LRU.prototype.insert = function(key, val) { | ||
| while (this.size >= this.maxsize) { | ||
| delete this.dict[L.shift(this.list).key]; | ||
| this.size -= 1; | ||
| } | ||
|
|
||
| var val = L.append(this.list, {key: key}); | ||
| this.dict[key] = val; | ||
| this.size += 1; | ||
|
|
||
| return val; | ||
| }; | ||
|
|
||
| function format(fmt /*, ... */) { | ||
| return vsprintf(fmt, arguments, compat); | ||
| } | ||
|
|
||
| function sprintf(fmt /*, ... */) { | ||
| return vsprintf(fmt, arguments, extended); | ||
| } | ||
|
|
||
| function vsprintf(fmt, argv, compile) { | ||
| if (typeof(fmt) !== 'string') { | ||
| if (compile === compat) | ||
| return concat(argv); | ||
| fmt = '' + fmt; | ||
| } | ||
|
|
||
| var lru = compile.lru; | ||
| var cached = lru.find(fmt); | ||
|
|
||
| if (cached === undefined) { | ||
| cached = lru.insert(fmt); | ||
| cached.fun = compile(fmt); | ||
| } | ||
|
|
||
| return cached.fun.apply(null, argv); | ||
| } | ||
|
|
||
| function concat(argv) { | ||
| var argc = argv.length; | ||
| var objects = new Array(argc); | ||
|
|
||
| for (var i = 0; i < argc; i += 1) | ||
| objects[i] = inspect(argv[i]); | ||
|
|
||
| return objects.join(' '); | ||
| } | ||
|
|
||
| // util.format() compatibility mode. | ||
| function compat(fmt) { | ||
| var index = 0; | ||
| var argc = 0; | ||
| var body = ''; | ||
|
|
||
| for (;;) { | ||
| var start = index; | ||
| index = fmt.indexOf('%', index); | ||
| var end = index === -1 ? fmt.length : index; | ||
|
|
||
| if (start !== end) { | ||
| body += ' s += "' + untaint(fmt.slice(start, end)) + '";'; | ||
| } | ||
|
|
||
| if (index === -1) | ||
| break; | ||
|
|
||
| index += 1; | ||
|
|
||
| var c = fmt[index]; | ||
| switch (c) { | ||
| case 'd': | ||
| // %d is really %f; it coerces the argument to a number | ||
| // before turning it into a string. | ||
| body += ' s += argc > ' + (argc + 1) + ' ? +a' + argc + ' : "%d";'; | ||
| argc += 1; | ||
| index += 1; | ||
| continue; | ||
|
|
||
| case 'j': | ||
| body += ' if (argc > ' + (argc + 1) + ')'; | ||
| body += ' try { s += JSON.stringify(a' + argc + '); }'; | ||
| body += ' catch (e) { s += "[Circular]"; }'; | ||
| body += ' else s += "%j";'; | ||
| argc += 1; | ||
| index += 1; | ||
| continue; | ||
|
|
||
| case 's': | ||
| body += ' s += argc > ' + (argc + 1) + ' ? a' + argc + ' : "%s";'; | ||
| argc += 1; | ||
| index += 1; | ||
| continue; | ||
|
|
||
| case '"': | ||
| c = '\\"'; | ||
| // Fall through. | ||
|
|
||
| default: | ||
| body += ' s += "' + c + '";'; | ||
| index += 1; | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| // The dummy argument lets format() and sprintf() call the formatter | ||
| // with .apply() without having to unshift the format argument first. | ||
| var source = 'return function(dummy'; | ||
|
|
||
| for (var i = 0; i < argc; i += 1) | ||
| source += ', a' + i; | ||
|
|
||
| source += ') { var argc = arguments.length, s = "";' + body; | ||
| source += ' for (var i = ' + argc + ' + 1; i < arguments.length; i += 1) {'; | ||
| source += ' var arg = arguments[i];'; | ||
| source += ' if (arg === null || typeof(arg) !== "object") s += " " + arg;'; | ||
| source += ' else s += " " + inspect(arg);'; | ||
| source += ' }'; | ||
| source += ' return s;'; | ||
| source += ' };'; | ||
|
|
||
| return Function('inspect', source)(inspect); | ||
|
Comment thread
Copy link
Copy Markdown
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityNote to self: the generated code gets optimized as of https://codereview.chromium.org/821553003/
Sorry, something went wrong.
All reactions
|
||
| } | ||
|
|
||
| function untaint(s) { | ||
| return s.replace(/[\\"]|[^\x20-\x7F]/g, function(c) { | ||
|
Comment thread
Copy link
Copy Markdown
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityIsn’t /[\\"\r\n\u2028\u2029]/g enough?
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityNow that you bring it up: probably not. With Harmony template strings being available now, the generated code is susceptible to template literal injection. I'll have to look at this again.
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality/[\\"\r\n\u2028\u2029]/g is enough, and template strings don’t affect the parsing of double-quoted string literals.
Sorry, something went wrong.
All reactions
|
||
| switch (c) { | ||
| case '\t': return '\\t'; | ||
| case '\n': return '\\n'; | ||
| case '\f': return '\\f'; | ||
| case '\r': return '\\r'; | ||
| case '"': return '\\"'; | ||
| case '\\': return '\\\\'; | ||
| } | ||
| return '\\u' + ('0000' + c.charCodeAt(0).toString(16)).slice(-4); | ||
| }); | ||
| } | ||
|
|
||
| compat.lru = new LRU(64); | ||
|
|
||
| function extended(fmt) { | ||
| throw Error('unimplemented'); | ||
| } | ||
|
|
||
| extended.lru = new LRU(64); | ||
|
|
||
| // Hidden export for lib/util.js | ||
| Object.defineProperty(sprintf, '_format', {value: format}); | ||
|
|
||
| module.exports = sprintf; | ||
| Back | FazBrowse Home | New Git URL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityDid you want to use an internal argc instead of arguments.length for for loop as a cached value? Otherwise argc is unused.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.