| /* This Source Code Form is subject to the terms of the Mozilla Public |
| * License, v. 2.0. If a copy of the MPL was not distributed with this |
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| String.prototype.format = function string_format() { |
| // there are two modes of operation... unnamed indices are read in order; |
| // named indices using %(name)s. The two styles cannot be mixed. |
| // Unnamed indices can be passed as either a single argument to this function, |
| // multiple arguments to this function, or as a single array argument |
| if (arguments.length > 1) { |
| function r(s, key, type) { |
| throw Error("Cannot mix named and positional indices in string formatting."); |
| if (curindex == 0 && (!(d instanceof Object) || !(0 in d))) { |
| else if (!(curindex in d)) |
| throw Error("Insufficient number of items in format, requesting item %i".format(curindex)); |
| throw Error("Cannot mix named and positional indices in string formatting."); |
| throw Error("Key '%s' not present during string substitution.".format(key)); |
| throw Error("Unexpected format character '%s'.".format(type)); |
| return this.replace(/%(\([^)]+\))?(.)/g, r); |