Branch data Line data Source code
1 : 1 : // -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
2 : : // SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
3 : : // SPDX-FileCopyrightText: 2012 Red Hat, Inc.
4 : : // SPDX-FileCopyrightText: 2012 Giovanni Campagna <scampa.giovanni@gmail.com>
5 : :
6 : : /* exported format, printf, vprintf */
7 : :
8 : 1 : var {vprintf} = imports._format;
9 : :
10 : 0 : function printf(fmt, ...args) {
11 : 0 : print(vprintf(fmt, args));
12 : : }
13 : :
14 : : /**
15 : : * format:
16 : : *
17 : : * @param {...*} args - Values with which to replace `%` codes in the string
18 : : * @returns {string} The formatted string
19 : : *
20 : : * This function is intended to extend the String object and provide a
21 : : * String.format API for string formatting. It has to be set up using
22 : : *
23 : : * String.prototype.format = Format.format;
24 : : *
25 : : * Usage:
26 : : *
27 : : * "somestring %s %d".format('hello', 5);
28 : : *
29 : : * It supports %s, %d, %x and %f. For %f it also supports precisions like
30 : : * "%.2f".format(1.526). All specifiers can be prefixed with a minimum field
31 : : * width, e.g. "%5s".format("foo"). Unless the width is prefixed with '0', the
32 : : * formatted string will be padded with spaces.
33 : : */
34 : 18 : function format(...args) {
35 : 18 : return vprintf(this, args);
36 : : }
|