Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | String.prototype.format = function string_format() { |
michael@0 | 6 | // there are two modes of operation... unnamed indices are read in order; |
michael@0 | 7 | // named indices using %(name)s. The two styles cannot be mixed. |
michael@0 | 8 | // Unnamed indices can be passed as either a single argument to this function, |
michael@0 | 9 | // multiple arguments to this function, or as a single array argument |
michael@0 | 10 | let curindex = 0; |
michael@0 | 11 | let d; |
michael@0 | 12 | |
michael@0 | 13 | if (arguments.length > 1) { |
michael@0 | 14 | d = arguments; |
michael@0 | 15 | } |
michael@0 | 16 | else |
michael@0 | 17 | d = arguments[0]; |
michael@0 | 18 | |
michael@0 | 19 | function r(s, key, type) { |
michael@0 | 20 | if (type == '%') |
michael@0 | 21 | return '%'; |
michael@0 | 22 | |
michael@0 | 23 | let v; |
michael@0 | 24 | if (key == "") { |
michael@0 | 25 | if (curindex == -1) |
michael@0 | 26 | throw Error("Cannot mix named and positional indices in string formatting."); |
michael@0 | 27 | |
michael@0 | 28 | if (curindex == 0 && (!(d instanceof Object) || !(0 in d))) { |
michael@0 | 29 | v = d; |
michael@0 | 30 | } |
michael@0 | 31 | else if (!(curindex in d)) |
michael@0 | 32 | throw Error("Insufficient number of items in format, requesting item %i".format(curindex)); |
michael@0 | 33 | else { |
michael@0 | 34 | v = d[curindex]; |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | ++curindex; |
michael@0 | 38 | } |
michael@0 | 39 | else { |
michael@0 | 40 | key = key.slice(1, -1); |
michael@0 | 41 | if (curindex > 0) |
michael@0 | 42 | throw Error("Cannot mix named and positional indices in string formatting."); |
michael@0 | 43 | curindex = -1; |
michael@0 | 44 | |
michael@0 | 45 | if (!(key in d)) |
michael@0 | 46 | throw Error("Key '%s' not present during string substitution.".format(key)); |
michael@0 | 47 | v = d[key]; |
michael@0 | 48 | } |
michael@0 | 49 | switch (type) { |
michael@0 | 50 | case "s": |
michael@0 | 51 | if (v === undefined) |
michael@0 | 52 | return "<undefined>"; |
michael@0 | 53 | return v.toString(); |
michael@0 | 54 | case "r": |
michael@0 | 55 | return uneval(v); |
michael@0 | 56 | case "i": |
michael@0 | 57 | return parseInt(v); |
michael@0 | 58 | case "f": |
michael@0 | 59 | return Number(v); |
michael@0 | 60 | default: |
michael@0 | 61 | throw Error("Unexpected format character '%s'.".format(type)); |
michael@0 | 62 | } |
michael@0 | 63 | } |
michael@0 | 64 | return this.replace(/%(\([^)]+\))?(.)/g, r); |
michael@0 | 65 | }; |