2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-08-11 11:51:20 +00:00
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
2022-08-11 11:51:20 +00:00
|
|
|
* {@linkcode sprintf} and {@linkcode printf} for printing formatted strings to
|
|
|
|
* stdout.
|
|
|
|
*
|
docs(assert,cli,data-structures,expect,fmt,front-matter,html,http,jsonc,semver,streams,text,toml,webgpu): add snippet checks in module, function and class docs to doc checker (#4855)
* chore: add snippet checks to module docs
* fix
* work
* tweak
2024-05-31 02:01:46 +00:00
|
|
|
* ```ts
|
|
|
|
* import { sprintf } from "@std/fmt/printf";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
docs(assert,cli,data-structures,expect,fmt,front-matter,html,http,jsonc,semver,streams,text,toml,webgpu): add snippet checks in module, function and class docs to doc checker (#4855)
* chore: add snippet checks to module docs
* fix
* work
* tweak
2024-05-31 02:01:46 +00:00
|
|
|
*
|
|
|
|
* assertEquals(sprintf("%d", 9), "9");
|
|
|
|
* assertEquals(sprintf("%o", 9), "11");
|
|
|
|
* assertEquals(sprintf("%f", 4), "4.000000");
|
|
|
|
* assertEquals(sprintf("%.3f", 0.9999), "1.000");
|
|
|
|
* ```
|
|
|
|
*
|
2020-11-07 14:05:37 +00:00
|
|
|
* This implementation is inspired by POSIX and Golang but does not port
|
2022-08-11 11:51:20 +00:00
|
|
|
* implementation code.
|
|
|
|
*
|
2022-11-25 11:40:23 +00:00
|
|
|
* sprintf converts and formats a variable number of arguments as is specified
|
|
|
|
* by a `format string`. In it's basic form, a format string may just be a
|
|
|
|
* literal. In case arguments are meant to be formatted, a `directive` is
|
|
|
|
* contained in the format string, preceded by a '%' character:
|
|
|
|
*
|
|
|
|
* %<verb>
|
|
|
|
*
|
|
|
|
* E.g. the verb `s` indicates the directive should be replaced by the string
|
|
|
|
* representation of the argument in the corresponding position of the argument
|
|
|
|
* list. E.g.:
|
|
|
|
*
|
|
|
|
* Hello %s!
|
|
|
|
*
|
|
|
|
* applied to the arguments "World" yields "Hello World!".
|
|
|
|
*
|
|
|
|
* The meaning of the format string is modelled after [POSIX][1] format strings
|
|
|
|
* as well as well as [Golang format strings][2]. Both contain elements specific
|
|
|
|
* to the respective programming language that don't apply to JavaScript, so
|
|
|
|
* they can not be fully supported. Furthermore we implement some functionality
|
|
|
|
* that is specific to JS.
|
|
|
|
*
|
|
|
|
* ## Verbs
|
|
|
|
*
|
|
|
|
* The following verbs are supported:
|
|
|
|
*
|
|
|
|
* | Verb | Meaning |
|
|
|
|
* | ----- | -------------------------------------------------------------- |
|
|
|
|
* | `%` | print a literal percent |
|
|
|
|
* | `t` | evaluate arg as boolean, print `true` or `false` |
|
|
|
|
* | `b` | eval as number, print binary |
|
|
|
|
* | `c` | eval as number, print character corresponding to the codePoint |
|
|
|
|
* | `o` | eval as number, print octal |
|
|
|
|
* | `x X` | print as hex (ff FF), treat string as list of bytes |
|
|
|
|
* | `e E` | print number in scientific/exponent format 1.123123e+01 |
|
|
|
|
* | `f F` | print number as float with decimal point and no exponent |
|
|
|
|
* | `g G` | use %e %E or %f %F depending on size of argument |
|
|
|
|
* | `s` | interpolate string |
|
|
|
|
* | `T` | type of arg, as returned by `typeof` |
|
|
|
|
* | `v` | value of argument in 'default' format (see below) |
|
|
|
|
* | `j` | argument as formatted by `JSON.stringify` |
|
2023-01-17 09:42:53 +00:00
|
|
|
* | `i` | argument as formatted by `Deno.inspect` |
|
|
|
|
* | `I` | argument as formatted by `Deno.inspect` in compact format |
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
|
|
|
* ## Width and Precision
|
|
|
|
*
|
|
|
|
* Verbs may be modified by providing them with width and precision, either or
|
|
|
|
* both may be omitted:
|
|
|
|
*
|
|
|
|
* %9f width 9, default precision
|
|
|
|
* %.9f default width, precision 9
|
|
|
|
* %8.9f width 8, precision 9
|
2024-10-24 04:58:50 +00:00
|
|
|
* %8.f width 8, precision 0
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
|
|
|
* In general, 'width' describes the minimum length of the output, while
|
|
|
|
* 'precision' limits the output.
|
|
|
|
*
|
|
|
|
* | verb | precision |
|
|
|
|
* | --------- | --------------------------------------------------------------- |
|
|
|
|
* | `t` | n/a |
|
|
|
|
* | `b c o` | n/a |
|
|
|
|
* | `x X` | n/a for number, strings are truncated to p bytes(!) |
|
|
|
|
* | `e E f F` | number of places after decimal, default 6 |
|
|
|
|
* | `g G` | set maximum number of digits |
|
|
|
|
* | `s` | truncate input |
|
|
|
|
* | `T` | truncate |
|
|
|
|
* | `v` | truncate, or depth if used with # see "'default' format", below |
|
|
|
|
* | `j` | n/a |
|
|
|
|
*
|
|
|
|
* Numerical values for width and precision can be substituted for the `*` char,
|
|
|
|
* in which case the values are obtained from the next args, e.g.:
|
|
|
|
*
|
|
|
|
* sprintf("%*.*f", 9, 8, 456.0)
|
|
|
|
*
|
|
|
|
* is equivalent to:
|
|
|
|
*
|
|
|
|
* sprintf("%9.8f", 456.0)
|
|
|
|
*
|
|
|
|
* ## Flags
|
|
|
|
*
|
|
|
|
* The effects of the verb may be further influenced by using flags to modify
|
|
|
|
* the directive:
|
|
|
|
*
|
|
|
|
* | Flag | Verb | Meaning |
|
|
|
|
* | ----- | --------- | -------------------------------------------------------------------------- |
|
|
|
|
* | `+` | numeric | always print sign |
|
|
|
|
* | `-` | all | pad to the right (left justify) |
|
|
|
|
* | `#` | | alternate format |
|
|
|
|
* | `#` | `b o x X` | prefix with `0b 0 0x` |
|
|
|
|
* | `#` | `g G` | don't remove trailing zeros |
|
2023-06-21 16:27:37 +00:00
|
|
|
* | `#` | `v` | use output of `inspect` instead of `toString` |
|
2022-11-25 11:40:23 +00:00
|
|
|
* | `' '` | | space character |
|
|
|
|
* | `' '` | `x X` | leave spaces between bytes when printing string |
|
|
|
|
* | `' '` | `d` | insert space for missing `+` sign character |
|
|
|
|
* | `0` | all | pad with zero, `-` takes precedence, sign is appended in front of padding |
|
|
|
|
* | `<` | all | format elements of the passed array according to the directive (extension) |
|
|
|
|
*
|
|
|
|
* ## 'default' format
|
|
|
|
*
|
|
|
|
* The default format used by `%v` is the result of calling `toString()` on the
|
|
|
|
* relevant argument. If the `#` flags is used, the result of calling `inspect()`
|
|
|
|
* is interpolated. In this case, the precision, if set is passed to `inspect()`
|
|
|
|
* as the 'depth' config parameter.
|
|
|
|
*
|
|
|
|
* ## Positional arguments
|
|
|
|
*
|
|
|
|
* Arguments do not need to be consumed in the order they are provided and may
|
|
|
|
* be consumed more than once. E.g.:
|
|
|
|
*
|
|
|
|
* sprintf("%[2]s %[1]s", "World", "Hello")
|
|
|
|
*
|
|
|
|
* returns "Hello World". The presence of a positional indicator resets the arg
|
|
|
|
* counter allowing args to be reused:
|
|
|
|
*
|
|
|
|
* sprintf("dec[%d]=%d hex[%[1]d]=%x oct[%[1]d]=%#o %s", 1, 255, "Third")
|
|
|
|
*
|
|
|
|
* returns `dec[1]=255 hex[1]=0xff oct[1]=0377 Third`
|
|
|
|
*
|
|
|
|
* Width and precision my also use positionals:
|
|
|
|
*
|
|
|
|
* "%[2]*.[1]*d", 1, 2
|
|
|
|
*
|
|
|
|
* This follows the golang conventions and not POSIX.
|
|
|
|
*
|
|
|
|
* ## Errors
|
|
|
|
*
|
|
|
|
* The following errors are handled:
|
|
|
|
*
|
|
|
|
* Incorrect verb:
|
|
|
|
*
|
|
|
|
* S("%h", "") %!(BAD VERB 'h')
|
|
|
|
*
|
|
|
|
* Too few arguments:
|
|
|
|
*
|
|
|
|
* S("%d") %!(MISSING 'd')"
|
|
|
|
*
|
|
|
|
* [1]: https://pubs.opengroup.org/onlinepubs/009695399/functions/fprintf.html
|
|
|
|
* [2]: https://golang.org/pkg/fmt/
|
|
|
|
*
|
2022-08-11 11:51:20 +00:00
|
|
|
* @module
|
|
|
|
*/
|
2020-05-21 09:12:37 +00:00
|
|
|
|
2023-11-23 13:24:14 +00:00
|
|
|
const State = {
|
|
|
|
PASSTHROUGH: 0,
|
|
|
|
PERCENT: 1,
|
|
|
|
POSITIONAL: 2,
|
|
|
|
PRECISION: 3,
|
|
|
|
WIDTH: 4,
|
|
|
|
} as const;
|
2020-05-21 09:12:37 +00:00
|
|
|
|
2023-11-23 13:24:14 +00:00
|
|
|
type State = typeof State[keyof typeof State];
|
|
|
|
|
|
|
|
const WorP = {
|
|
|
|
WIDTH: 0,
|
|
|
|
PRECISION: 1,
|
|
|
|
} as const;
|
|
|
|
|
|
|
|
type WorP = typeof WorP[keyof typeof WorP];
|
|
|
|
|
|
|
|
const F = {
|
|
|
|
sign: 1,
|
|
|
|
mantissa: 2,
|
|
|
|
fractional: 3,
|
|
|
|
esign: 4,
|
|
|
|
exponent: 5,
|
|
|
|
} as const;
|
2019-08-15 15:57:24 +00:00
|
|
|
|
|
|
|
class Flags {
|
refactor(archive,async,cli,csv,dotenv,encoding,expect,fmt,front-matter,fs,http,internal,log,net,path,semver,testing,text,webgpu,yaml): enable `"exactOptionalPropertyTypes"` option (#5892)
2024-09-04 05:15:01 +00:00
|
|
|
plus: boolean | undefined;
|
|
|
|
dash: boolean | undefined;
|
|
|
|
sharp: boolean | undefined;
|
|
|
|
space: boolean | undefined;
|
|
|
|
zero: boolean | undefined;
|
|
|
|
lessthan: boolean | undefined;
|
2019-10-05 16:02:34 +00:00
|
|
|
width = -1;
|
|
|
|
precision = -1;
|
2019-08-15 15:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const min = Math.min;
|
|
|
|
const UNICODE_REPLACEMENT_CHARACTER = "\ufffd";
|
|
|
|
const DEFAULT_PRECISION = 6;
|
|
|
|
const FLOAT_REGEXP = /(-?)(\d)\.?(\d*)e([+-])(\d+)/;
|
2020-05-21 09:12:37 +00:00
|
|
|
|
2019-08-15 15:57:24 +00:00
|
|
|
class Printf {
|
|
|
|
format: string;
|
2020-05-21 09:12:37 +00:00
|
|
|
args: unknown[];
|
2019-08-15 15:57:24 +00:00
|
|
|
i: number;
|
|
|
|
|
|
|
|
state: State = State.PASSTHROUGH;
|
2019-10-05 16:02:34 +00:00
|
|
|
verb = "";
|
|
|
|
buf = "";
|
|
|
|
argNum = 0;
|
2019-08-15 15:57:24 +00:00
|
|
|
flags: Flags = new Flags();
|
|
|
|
|
|
|
|
haveSeen: boolean[];
|
|
|
|
|
|
|
|
// barf, store precision and width errors for later processing ...
|
refactor(archive,async,cli,csv,dotenv,encoding,expect,fmt,front-matter,fs,http,internal,log,net,path,semver,testing,text,webgpu,yaml): enable `"exactOptionalPropertyTypes"` option (#5892)
2024-09-04 05:15:01 +00:00
|
|
|
tmpError: string | undefined;
|
2019-08-15 15:57:24 +00:00
|
|
|
|
2020-05-21 09:12:37 +00:00
|
|
|
constructor(format: string, ...args: unknown[]) {
|
2019-08-15 15:57:24 +00:00
|
|
|
this.format = format;
|
|
|
|
this.args = args;
|
2021-11-11 09:21:06 +00:00
|
|
|
this.haveSeen = Array.from({ length: args.length });
|
2019-08-15 15:57:24 +00:00
|
|
|
this.i = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
doPrintf(): string {
|
|
|
|
for (; this.i < this.format.length; ++this.i) {
|
2019-10-05 16:02:34 +00:00
|
|
|
const c = this.format[this.i];
|
2019-08-15 15:57:24 +00:00
|
|
|
switch (this.state) {
|
|
|
|
case State.PASSTHROUGH:
|
|
|
|
if (c === "%") {
|
|
|
|
this.state = State.PERCENT;
|
|
|
|
} else {
|
|
|
|
this.buf += c;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case State.PERCENT:
|
|
|
|
if (c === "%") {
|
|
|
|
this.buf += c;
|
|
|
|
this.state = State.PASSTHROUGH;
|
|
|
|
} else {
|
|
|
|
this.handleFormat();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2024-08-28 22:55:24 +00:00
|
|
|
throw new Error(
|
2024-08-21 05:42:02 +00:00
|
|
|
`State ${this.state} should be unreachable, please file a bug report against Deno at https://github.com/denoland/std/issues`,
|
|
|
|
);
|
2019-08-15 15:57:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// check for unhandled args
|
|
|
|
let extras = false;
|
|
|
|
let err = "%!(EXTRA";
|
|
|
|
for (let i = 0; i !== this.haveSeen.length; ++i) {
|
|
|
|
if (!this.haveSeen[i]) {
|
|
|
|
extras = true;
|
|
|
|
err += ` '${Deno.inspect(this.args[i])}'`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err += ")";
|
|
|
|
if (extras) {
|
|
|
|
this.buf += err;
|
|
|
|
}
|
|
|
|
return this.buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
// %[<positional>]<flag>...<verb>
|
2022-08-24 01:21:57 +00:00
|
|
|
handleFormat() {
|
2019-08-15 15:57:24 +00:00
|
|
|
this.flags = new Flags();
|
2019-10-05 16:02:34 +00:00
|
|
|
const flags = this.flags;
|
2019-08-15 15:57:24 +00:00
|
|
|
for (; this.i < this.format.length; ++this.i) {
|
2024-03-05 19:15:47 +00:00
|
|
|
const c = this.format[this.i]!;
|
2019-08-15 15:57:24 +00:00
|
|
|
switch (this.state) {
|
|
|
|
case State.PERCENT:
|
|
|
|
switch (c) {
|
|
|
|
case "[":
|
|
|
|
this.handlePositional();
|
|
|
|
this.state = State.POSITIONAL;
|
|
|
|
break;
|
|
|
|
case "+":
|
|
|
|
flags.plus = true;
|
|
|
|
break;
|
|
|
|
case "<":
|
|
|
|
flags.lessthan = true;
|
|
|
|
break;
|
|
|
|
case "-":
|
|
|
|
flags.dash = true;
|
|
|
|
flags.zero = false; // only left pad zeros, dash takes precedence
|
|
|
|
break;
|
|
|
|
case "#":
|
|
|
|
flags.sharp = true;
|
|
|
|
break;
|
|
|
|
case " ":
|
|
|
|
flags.space = true;
|
|
|
|
break;
|
|
|
|
case "0":
|
|
|
|
// only left pad zeros, dash takes precedence
|
|
|
|
flags.zero = !flags.dash;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (("1" <= c && c <= "9") || c === "." || c === "*") {
|
|
|
|
if (c === ".") {
|
|
|
|
this.flags.precision = 0;
|
|
|
|
this.state = State.PRECISION;
|
|
|
|
this.i++;
|
|
|
|
} else {
|
|
|
|
this.state = State.WIDTH;
|
|
|
|
}
|
|
|
|
this.handleWidthAndPrecision(flags);
|
|
|
|
} else {
|
|
|
|
this.handleVerb();
|
|
|
|
return; // always end in verb
|
|
|
|
}
|
|
|
|
} // switch c
|
|
|
|
break;
|
2021-01-16 23:32:59 +00:00
|
|
|
case State.POSITIONAL:
|
|
|
|
// TODO(bartlomieju): either a verb or * only verb for now
|
2019-08-15 15:57:24 +00:00
|
|
|
if (c === "*") {
|
2020-07-14 19:24:17 +00:00
|
|
|
const worp = this.flags.precision === -1
|
|
|
|
? WorP.WIDTH
|
|
|
|
: WorP.PRECISION;
|
2019-08-15 15:57:24 +00:00
|
|
|
this.handleWidthOrPrecisionRef(worp);
|
|
|
|
this.state = State.PERCENT;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
this.handleVerb();
|
|
|
|
return; // always end in verb
|
|
|
|
}
|
|
|
|
default:
|
2024-08-21 05:42:02 +00:00
|
|
|
throw new Error(
|
|
|
|
`State ${this.state} should be unreachable, please file a bug report against Deno at https://github.com/denoland/std/issues`,
|
|
|
|
);
|
2019-08-15 15:57:24 +00:00
|
|
|
} // switch state
|
|
|
|
}
|
|
|
|
}
|
2020-05-21 09:12:37 +00:00
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Handle width or precision
|
|
|
|
* @param wOrP
|
|
|
|
*/
|
2022-08-24 01:21:57 +00:00
|
|
|
handleWidthOrPrecisionRef(wOrP: WorP) {
|
2019-08-15 15:57:24 +00:00
|
|
|
if (this.argNum >= this.args.length) {
|
|
|
|
// handle Positional should have already taken care of it...
|
|
|
|
return;
|
|
|
|
}
|
2019-10-05 16:02:34 +00:00
|
|
|
const arg = this.args[this.argNum];
|
2019-08-15 15:57:24 +00:00
|
|
|
this.haveSeen[this.argNum] = true;
|
|
|
|
if (typeof arg === "number") {
|
|
|
|
switch (wOrP) {
|
|
|
|
case WorP.WIDTH:
|
|
|
|
this.flags.width = arg;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
this.flags.precision = arg;
|
|
|
|
}
|
|
|
|
} else {
|
2019-10-05 16:02:34 +00:00
|
|
|
const tmp = wOrP === WorP.WIDTH ? "WIDTH" : "PREC";
|
2019-08-15 15:57:24 +00:00
|
|
|
this.tmpError = `%!(BAD ${tmp} '${this.args[this.argNum]}')`;
|
|
|
|
}
|
|
|
|
this.argNum++;
|
|
|
|
}
|
2020-05-21 09:12:37 +00:00
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Handle width and precision
|
|
|
|
* @param flags
|
|
|
|
*/
|
2022-08-24 01:21:57 +00:00
|
|
|
handleWidthAndPrecision(flags: Flags) {
|
2019-08-15 15:57:24 +00:00
|
|
|
const fmt = this.format;
|
|
|
|
for (; this.i !== this.format.length; ++this.i) {
|
2024-03-05 19:15:47 +00:00
|
|
|
const c = fmt[this.i]!;
|
2019-08-15 15:57:24 +00:00
|
|
|
switch (this.state) {
|
|
|
|
case State.WIDTH:
|
|
|
|
switch (c) {
|
|
|
|
case ".":
|
|
|
|
// initialize precision, %9.f -> precision=0
|
|
|
|
this.flags.precision = 0;
|
|
|
|
this.state = State.PRECISION;
|
|
|
|
break;
|
|
|
|
case "*":
|
|
|
|
this.handleWidthOrPrecisionRef(WorP.WIDTH);
|
|
|
|
// force . or flag at this point
|
|
|
|
break;
|
2020-10-02 11:51:37 +00:00
|
|
|
default: {
|
2019-08-15 15:57:24 +00:00
|
|
|
const val = parseInt(c);
|
|
|
|
// most likely parseInt does something stupid that makes
|
2020-05-14 04:38:42 +00:00
|
|
|
// it unusable for this scenario ...
|
2019-08-15 15:57:24 +00:00
|
|
|
// if we encounter a non (number|*|.) we're done with prec & wid
|
|
|
|
if (isNaN(val)) {
|
|
|
|
this.i--;
|
|
|
|
this.state = State.PERCENT;
|
|
|
|
return;
|
|
|
|
}
|
2023-08-25 09:04:43 +00:00
|
|
|
flags.width = flags.width === -1 ? 0 : flags.width;
|
2019-08-15 15:57:24 +00:00
|
|
|
flags.width *= 10;
|
|
|
|
flags.width += val;
|
2020-10-02 11:51:37 +00:00
|
|
|
}
|
2019-08-15 15:57:24 +00:00
|
|
|
} // switch c
|
|
|
|
break;
|
2020-06-12 13:19:29 +00:00
|
|
|
case State.PRECISION: {
|
2019-08-15 15:57:24 +00:00
|
|
|
if (c === "*") {
|
|
|
|
this.handleWidthOrPrecisionRef(WorP.PRECISION);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const val = parseInt(c);
|
|
|
|
if (isNaN(val)) {
|
|
|
|
// one too far, rewind
|
|
|
|
this.i--;
|
|
|
|
this.state = State.PERCENT;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
flags.precision *= 10;
|
|
|
|
flags.precision += val;
|
|
|
|
break;
|
2020-06-12 13:19:29 +00:00
|
|
|
}
|
2019-08-15 15:57:24 +00:00
|
|
|
default:
|
2024-08-21 05:42:02 +00:00
|
|
|
throw new Error(
|
|
|
|
`State ${this.state} should be unreachable, please file a bug report against Deno at https://github.com/denoland/std/issues`,
|
|
|
|
);
|
2019-08-15 15:57:24 +00:00
|
|
|
} // switch state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/** Handle positional */
|
2022-08-24 01:21:57 +00:00
|
|
|
handlePositional() {
|
2019-08-15 15:57:24 +00:00
|
|
|
if (this.format[this.i] !== "[") {
|
|
|
|
// sanity only
|
2024-08-21 05:42:02 +00:00
|
|
|
throw new Error(
|
|
|
|
"Should be unreachable, please file a bug report against Deno at https://github.com/denoland/std/issues",
|
|
|
|
);
|
2019-08-15 15:57:24 +00:00
|
|
|
}
|
|
|
|
let positional = 0;
|
|
|
|
const format = this.format;
|
|
|
|
this.i++;
|
|
|
|
let err = false;
|
|
|
|
for (; this.i !== this.format.length; ++this.i) {
|
|
|
|
if (format[this.i] === "]") {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
positional *= 10;
|
2024-03-05 19:15:47 +00:00
|
|
|
const val = parseInt(format[this.i]!, 10);
|
2019-08-15 15:57:24 +00:00
|
|
|
if (isNaN(val)) {
|
|
|
|
//throw new Error(
|
|
|
|
// `invalid character in positional: ${format}[${format[this.i]}]`
|
|
|
|
//);
|
|
|
|
this.tmpError = "%!(BAD INDEX)";
|
|
|
|
err = true;
|
|
|
|
}
|
|
|
|
positional += val;
|
|
|
|
}
|
|
|
|
if (positional - 1 >= this.args.length) {
|
|
|
|
this.tmpError = "%!(BAD INDEX)";
|
|
|
|
err = true;
|
|
|
|
}
|
|
|
|
this.argNum = err ? this.argNum : positional - 1;
|
|
|
|
}
|
2020-05-21 09:12:37 +00:00
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/** Handle less than */
|
2019-08-15 15:57:24 +00:00
|
|
|
handleLessThan(): string {
|
2020-11-03 15:19:29 +00:00
|
|
|
// deno-lint-ignore no-explicit-any
|
2020-05-21 09:12:37 +00:00
|
|
|
const arg = this.args[this.argNum] as any;
|
2019-08-15 15:57:24 +00:00
|
|
|
if ((arg || {}).constructor.name !== "Array") {
|
2024-08-21 05:42:02 +00:00
|
|
|
throw new Error(
|
|
|
|
`Cannot handle less than '<' flag: 'arg' is not an array`,
|
|
|
|
);
|
2019-08-15 15:57:24 +00:00
|
|
|
}
|
|
|
|
let str = "[ ";
|
|
|
|
for (let i = 0; i !== arg.length; ++i) {
|
|
|
|
if (i !== 0) str += ", ";
|
|
|
|
str += this._handleVerb(arg[i]);
|
|
|
|
}
|
|
|
|
return str + " ]";
|
|
|
|
}
|
2020-05-21 09:12:37 +00:00
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/** Handle verb */
|
2022-08-24 01:21:57 +00:00
|
|
|
handleVerb() {
|
2019-08-15 15:57:24 +00:00
|
|
|
const verb = this.format[this.i];
|
2024-03-05 19:15:47 +00:00
|
|
|
this.verb = verb || this.verb;
|
2019-08-15 15:57:24 +00:00
|
|
|
if (this.tmpError) {
|
|
|
|
this.buf += this.tmpError;
|
|
|
|
this.tmpError = undefined;
|
|
|
|
if (this.argNum < this.haveSeen.length) {
|
|
|
|
this.haveSeen[this.argNum] = true; // keep track of used args
|
|
|
|
}
|
|
|
|
} else if (this.args.length <= this.argNum) {
|
|
|
|
this.buf += `%!(MISSING '${verb}')`;
|
|
|
|
} else {
|
2019-10-05 16:02:34 +00:00
|
|
|
const arg = this.args[this.argNum]; // check out of range
|
2019-08-15 15:57:24 +00:00
|
|
|
this.haveSeen[this.argNum] = true; // keep track of used args
|
|
|
|
if (this.flags.lessthan) {
|
|
|
|
this.buf += this.handleLessThan();
|
|
|
|
} else {
|
|
|
|
this.buf += this._handleVerb(arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.argNum++; // if there is a further positional, it will reset.
|
|
|
|
this.state = State.PASSTHROUGH;
|
|
|
|
}
|
|
|
|
|
2020-11-03 15:19:29 +00:00
|
|
|
// deno-lint-ignore no-explicit-any
|
2019-08-15 15:57:24 +00:00
|
|
|
_handleVerb(arg: any): string {
|
|
|
|
switch (this.verb) {
|
|
|
|
case "t":
|
|
|
|
return this.pad(arg.toString());
|
|
|
|
case "b":
|
|
|
|
return this.fmtNumber(arg as number, 2);
|
|
|
|
case "c":
|
|
|
|
return this.fmtNumberCodePoint(arg as number);
|
|
|
|
case "d":
|
|
|
|
return this.fmtNumber(arg as number, 10);
|
|
|
|
case "o":
|
|
|
|
return this.fmtNumber(arg as number, 8);
|
|
|
|
case "x":
|
|
|
|
return this.fmtHex(arg);
|
|
|
|
case "X":
|
|
|
|
return this.fmtHex(arg, true);
|
|
|
|
case "e":
|
|
|
|
return this.fmtFloatE(arg as number);
|
|
|
|
case "E":
|
|
|
|
return this.fmtFloatE(arg as number, true);
|
|
|
|
case "f":
|
|
|
|
case "F":
|
|
|
|
return this.fmtFloatF(arg as number);
|
|
|
|
case "g":
|
|
|
|
return this.fmtFloatG(arg as number);
|
|
|
|
case "G":
|
|
|
|
return this.fmtFloatG(arg as number, true);
|
|
|
|
case "s":
|
|
|
|
return this.fmtString(arg as string);
|
|
|
|
case "T":
|
|
|
|
return this.fmtString(typeof arg);
|
|
|
|
case "v":
|
|
|
|
return this.fmtV(arg);
|
|
|
|
case "j":
|
|
|
|
return this.fmtJ(arg);
|
2023-01-17 09:42:53 +00:00
|
|
|
case "i":
|
|
|
|
return this.fmtI(arg, false);
|
|
|
|
case "I":
|
|
|
|
return this.fmtI(arg, true);
|
2019-08-15 15:57:24 +00:00
|
|
|
default:
|
|
|
|
return `%!(BAD VERB '${this.verb}')`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Pad a string
|
|
|
|
* @param s text to pad
|
|
|
|
*/
|
2019-08-15 15:57:24 +00:00
|
|
|
pad(s: string): string {
|
|
|
|
const padding = this.flags.zero ? "0" : " ";
|
2019-09-04 14:32:28 +00:00
|
|
|
|
|
|
|
if (this.flags.dash) {
|
|
|
|
return s.padEnd(this.flags.width, padding);
|
2019-08-15 15:57:24 +00:00
|
|
|
}
|
2019-09-04 14:32:28 +00:00
|
|
|
|
|
|
|
return s.padStart(this.flags.width, padding);
|
2019-08-15 15:57:24 +00:00
|
|
|
}
|
2020-11-07 14:05:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pad a number
|
|
|
|
* @param nStr
|
|
|
|
* @param neg
|
|
|
|
*/
|
2019-08-15 15:57:24 +00:00
|
|
|
padNum(nStr: string, neg: boolean): string {
|
|
|
|
let sign: string;
|
|
|
|
if (neg) {
|
|
|
|
sign = "-";
|
|
|
|
} else if (this.flags.plus || this.flags.space) {
|
|
|
|
sign = this.flags.plus ? "+" : " ";
|
|
|
|
} else {
|
|
|
|
sign = "";
|
|
|
|
}
|
|
|
|
const zero = this.flags.zero;
|
|
|
|
if (!zero) {
|
|
|
|
// sign comes in front of padding when padding w/ zero,
|
|
|
|
// in from of value if padding with spaces.
|
|
|
|
nStr = sign + nStr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const pad = zero ? "0" : " ";
|
|
|
|
const len = zero ? this.flags.width - sign.length : this.flags.width;
|
|
|
|
|
2019-09-04 14:32:28 +00:00
|
|
|
if (this.flags.dash) {
|
|
|
|
nStr = nStr.padEnd(len, pad);
|
|
|
|
} else {
|
|
|
|
nStr = nStr.padStart(len, pad);
|
2019-08-15 15:57:24 +00:00
|
|
|
}
|
2019-09-04 14:32:28 +00:00
|
|
|
|
2019-08-15 15:57:24 +00:00
|
|
|
if (zero) {
|
|
|
|
// see above
|
|
|
|
nStr = sign + nStr;
|
|
|
|
}
|
|
|
|
return nStr;
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Format a number
|
|
|
|
* @param n
|
|
|
|
* @param radix
|
|
|
|
* @param upcase
|
|
|
|
*/
|
2019-10-05 16:02:34 +00:00
|
|
|
fmtNumber(n: number, radix: number, upcase = false): string {
|
2019-08-15 15:57:24 +00:00
|
|
|
let num = Math.abs(n).toString(radix);
|
|
|
|
const prec = this.flags.precision;
|
|
|
|
if (prec !== -1) {
|
|
|
|
this.flags.zero = false;
|
|
|
|
num = n === 0 && prec === 0 ? "" : num;
|
|
|
|
while (num.length < prec) {
|
|
|
|
num = "0" + num;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let prefix = "";
|
|
|
|
if (this.flags.sharp) {
|
|
|
|
switch (radix) {
|
|
|
|
case 2:
|
|
|
|
prefix += "0b";
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
// don't annotate octal 0 with 0...
|
|
|
|
prefix += num.startsWith("0") ? "" : "0";
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
prefix += "0x";
|
|
|
|
break;
|
|
|
|
default:
|
2024-08-21 05:42:02 +00:00
|
|
|
throw new Error(
|
|
|
|
`Cannot handle the radix ${radix}: only 2, 8, 16 are supported`,
|
|
|
|
);
|
2019-08-15 15:57:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// don't add prefix in front of value truncated by precision=0, val=0
|
|
|
|
num = num.length === 0 ? num : prefix + num;
|
|
|
|
if (upcase) {
|
|
|
|
num = num.toUpperCase();
|
|
|
|
}
|
|
|
|
return this.padNum(num, n < 0);
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Format number with code points
|
|
|
|
* @param n
|
|
|
|
*/
|
2019-08-15 15:57:24 +00:00
|
|
|
fmtNumberCodePoint(n: number): string {
|
|
|
|
let s = "";
|
|
|
|
try {
|
|
|
|
s = String.fromCodePoint(n);
|
2021-03-02 06:31:37 +00:00
|
|
|
} catch {
|
2019-08-15 15:57:24 +00:00
|
|
|
s = UNICODE_REPLACEMENT_CHARACTER;
|
|
|
|
}
|
|
|
|
return this.pad(s);
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Format special float
|
|
|
|
* @param n
|
|
|
|
*/
|
2019-08-15 15:57:24 +00:00
|
|
|
fmtFloatSpecial(n: number): string {
|
|
|
|
// formatting of NaN and Inf are pants-on-head
|
|
|
|
// stupid and more or less arbitrary.
|
|
|
|
|
|
|
|
if (isNaN(n)) {
|
|
|
|
this.flags.zero = false;
|
|
|
|
return this.padNum("NaN", false);
|
|
|
|
}
|
|
|
|
if (n === Number.POSITIVE_INFINITY) {
|
|
|
|
this.flags.zero = false;
|
|
|
|
this.flags.plus = true;
|
|
|
|
return this.padNum("Inf", false);
|
|
|
|
}
|
|
|
|
if (n === Number.NEGATIVE_INFINITY) {
|
|
|
|
this.flags.zero = false;
|
|
|
|
return this.padNum("Inf", true);
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Round fraction to precision
|
|
|
|
* @param fractional
|
|
|
|
* @param precision
|
2021-11-22 16:04:13 +00:00
|
|
|
* @returns tuple of fractional and round
|
2020-11-07 14:05:37 +00:00
|
|
|
*/
|
2021-11-22 16:04:13 +00:00
|
|
|
roundFractionToPrecision(
|
|
|
|
fractional: string,
|
|
|
|
precision: number,
|
|
|
|
): [string, boolean] {
|
|
|
|
let round = false;
|
2019-08-15 15:57:24 +00:00
|
|
|
if (fractional.length > precision) {
|
|
|
|
fractional = "1" + fractional; // prepend a 1 in case of leading 0
|
2022-12-12 06:21:59 +00:00
|
|
|
let tmp = parseInt(fractional.slice(0, precision + 2)) / 10;
|
2019-08-15 15:57:24 +00:00
|
|
|
tmp = Math.round(tmp);
|
|
|
|
fractional = Math.floor(tmp).toString();
|
2021-11-22 16:04:13 +00:00
|
|
|
round = fractional[0] === "2";
|
2022-12-12 06:21:59 +00:00
|
|
|
fractional = fractional.slice(1); // remove extra 1
|
2019-08-15 15:57:24 +00:00
|
|
|
} else {
|
|
|
|
while (fractional.length < precision) {
|
|
|
|
fractional += "0";
|
|
|
|
}
|
|
|
|
}
|
2021-11-22 16:04:13 +00:00
|
|
|
return [fractional, round];
|
2019-08-15 15:57:24 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Format float E
|
|
|
|
* @param n
|
|
|
|
* @param upcase
|
|
|
|
*/
|
2019-10-05 16:02:34 +00:00
|
|
|
fmtFloatE(n: number, upcase = false): string {
|
2019-08-15 15:57:24 +00:00
|
|
|
const special = this.fmtFloatSpecial(n);
|
|
|
|
if (special !== "") {
|
|
|
|
return special;
|
|
|
|
}
|
|
|
|
|
|
|
|
const m = n.toExponential().match(FLOAT_REGEXP);
|
|
|
|
if (!m) {
|
2024-08-21 05:42:02 +00:00
|
|
|
throw new Error(
|
|
|
|
"Should be unreachable, please file a bug report against Deno at https://github.com/denoland/std/issues",
|
|
|
|
);
|
2019-08-15 15:57:24 +00:00
|
|
|
}
|
2020-07-14 19:24:17 +00:00
|
|
|
const precision = this.flags.precision !== -1
|
|
|
|
? this.flags.precision
|
|
|
|
: DEFAULT_PRECISION;
|
2024-03-05 19:15:47 +00:00
|
|
|
const [fractional, rounding] = this.roundFractionToPrecision(
|
refactor(archive,async,cli,csv,dotenv,encoding,expect,fmt,front-matter,fs,http,internal,log,net,path,semver,testing,text,webgpu,yaml): enable `"exactOptionalPropertyTypes"` option (#5892)
2024-09-04 05:15:01 +00:00
|
|
|
m[F.fractional] ?? "",
|
2021-11-22 16:04:13 +00:00
|
|
|
precision,
|
|
|
|
);
|
2019-08-15 15:57:24 +00:00
|
|
|
|
2024-03-05 19:15:47 +00:00
|
|
|
let e = m[F.exponent]!;
|
|
|
|
let esign = m[F.esign]!;
|
2019-08-15 15:57:24 +00:00
|
|
|
// scientific notation output with exponent padded to minlen 2
|
2024-03-05 19:15:47 +00:00
|
|
|
let mantissa = parseInt(m[F.mantissa]!);
|
2021-11-22 16:04:13 +00:00
|
|
|
if (rounding) {
|
|
|
|
mantissa += 1;
|
|
|
|
if (10 <= mantissa) {
|
|
|
|
mantissa = 1;
|
|
|
|
const r = parseInt(esign + e) + 1;
|
2024-07-04 05:29:31 +00:00
|
|
|
e = Math.abs(r).toString();
|
2021-11-22 16:04:13 +00:00
|
|
|
esign = r < 0 ? "-" : "+";
|
|
|
|
}
|
|
|
|
}
|
2023-08-25 09:04:43 +00:00
|
|
|
e = e.length === 1 ? "0" + e : e;
|
2021-11-22 16:04:13 +00:00
|
|
|
const val = `${mantissa}.${fractional}${upcase ? "E" : "e"}${esign}${e}`;
|
2019-08-15 15:57:24 +00:00
|
|
|
return this.padNum(val, n < 0);
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Format float F
|
|
|
|
* @param n
|
|
|
|
*/
|
2019-08-15 15:57:24 +00:00
|
|
|
fmtFloatF(n: number): string {
|
|
|
|
const special = this.fmtFloatSpecial(n);
|
|
|
|
if (special !== "") {
|
|
|
|
return special;
|
|
|
|
}
|
|
|
|
|
|
|
|
// stupid helper that turns a number into a (potentially)
|
|
|
|
// VERY long string.
|
|
|
|
function expandNumber(n: number): string {
|
|
|
|
if (Number.isSafeInteger(n)) {
|
|
|
|
return n.toString() + ".";
|
|
|
|
}
|
|
|
|
|
|
|
|
const t = n.toExponential().split("e");
|
2024-03-05 19:15:47 +00:00
|
|
|
let m = t[0]!.replace(".", "");
|
|
|
|
const e = parseInt(t[1]!);
|
2019-08-15 15:57:24 +00:00
|
|
|
if (e < 0) {
|
|
|
|
let nStr = "0.";
|
|
|
|
for (let i = 0; i !== Math.abs(e) - 1; ++i) {
|
|
|
|
nStr += "0";
|
|
|
|
}
|
|
|
|
return (nStr += m);
|
|
|
|
} else {
|
|
|
|
const splIdx = e + 1;
|
|
|
|
while (m.length < splIdx) {
|
|
|
|
m += "0";
|
|
|
|
}
|
2022-12-12 06:21:59 +00:00
|
|
|
return m.slice(0, splIdx) + "." + m.slice(splIdx);
|
2019-08-15 15:57:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// avoiding sign makes padding easier
|
|
|
|
const val = expandNumber(Math.abs(n)) as string;
|
2024-03-05 19:15:47 +00:00
|
|
|
let [dig, fractional] = val.split(".") as [string, string];
|
2019-08-15 15:57:24 +00:00
|
|
|
|
2020-07-14 19:24:17 +00:00
|
|
|
const precision = this.flags.precision !== -1
|
|
|
|
? this.flags.precision
|
|
|
|
: DEFAULT_PRECISION;
|
2021-11-22 16:04:13 +00:00
|
|
|
let round = false;
|
|
|
|
[fractional, round] = this.roundFractionToPrecision(fractional, precision);
|
|
|
|
if (round) {
|
|
|
|
dig = (parseInt(dig) + 1).toString();
|
|
|
|
}
|
2019-08-15 15:57:24 +00:00
|
|
|
return this.padNum(`${dig}.${fractional}`, n < 0);
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Format float G
|
|
|
|
* @param n
|
|
|
|
* @param upcase
|
|
|
|
*/
|
2019-10-05 16:02:34 +00:00
|
|
|
fmtFloatG(n: number, upcase = false): string {
|
2019-08-15 15:57:24 +00:00
|
|
|
const special = this.fmtFloatSpecial(n);
|
|
|
|
if (special !== "") {
|
|
|
|
return special;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The double argument representing a floating-point number shall be
|
|
|
|
// converted in the style f or e (or in the style F or E in
|
|
|
|
// the case of a G conversion specifier), depending on the
|
|
|
|
// value converted and the precision. Let P equal the
|
|
|
|
// precision if non-zero, 6 if the precision is omitted, or 1
|
|
|
|
// if the precision is zero. Then, if a conversion with style E would
|
|
|
|
// have an exponent of X:
|
|
|
|
|
|
|
|
// - If P > X>=-4, the conversion shall be with style f (or F )
|
|
|
|
// and precision P -( X+1).
|
|
|
|
|
|
|
|
// - Otherwise, the conversion shall be with style e (or E )
|
|
|
|
// and precision P -1.
|
|
|
|
|
|
|
|
// Finally, unless the '#' flag is used, any trailing zeros shall be
|
|
|
|
// removed from the fractional portion of the result and the
|
|
|
|
// decimal-point character shall be removed if there is no
|
|
|
|
// fractional portion remaining.
|
|
|
|
|
|
|
|
// A double argument representing an infinity or NaN shall be
|
|
|
|
// converted in the style of an f or F conversion specifier.
|
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fprintf.html
|
|
|
|
|
2020-07-14 19:24:17 +00:00
|
|
|
let P = this.flags.precision !== -1
|
|
|
|
? this.flags.precision
|
|
|
|
: DEFAULT_PRECISION;
|
2019-08-15 15:57:24 +00:00
|
|
|
P = P === 0 ? 1 : P;
|
|
|
|
|
|
|
|
const m = n.toExponential().match(FLOAT_REGEXP);
|
|
|
|
if (!m) {
|
2024-08-21 05:42:02 +00:00
|
|
|
throw new Error(
|
|
|
|
"Should be unreachable, please file a bug report against Deno at https://github.com/denoland/std/issues",
|
|
|
|
);
|
2019-08-15 15:57:24 +00:00
|
|
|
}
|
|
|
|
|
2024-03-05 19:15:47 +00:00
|
|
|
const X = parseInt(m[F.exponent]!) * (m[F.esign] === "-" ? -1 : 1);
|
2019-08-15 15:57:24 +00:00
|
|
|
let nStr = "";
|
|
|
|
if (P > X && X >= -4) {
|
|
|
|
this.flags.precision = P - (X + 1);
|
|
|
|
nStr = this.fmtFloatF(n);
|
|
|
|
if (!this.flags.sharp) {
|
|
|
|
nStr = nStr.replace(/\.?0*$/, "");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.flags.precision = P - 1;
|
|
|
|
nStr = this.fmtFloatE(n);
|
|
|
|
if (!this.flags.sharp) {
|
|
|
|
nStr = nStr.replace(/\.?0*e/, upcase ? "E" : "e");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nStr;
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Format string
|
|
|
|
* @param s
|
|
|
|
*/
|
2019-08-15 15:57:24 +00:00
|
|
|
fmtString(s: string): string {
|
|
|
|
if (this.flags.precision !== -1) {
|
2022-12-12 06:21:59 +00:00
|
|
|
s = s.slice(0, this.flags.precision);
|
2019-08-15 15:57:24 +00:00
|
|
|
}
|
|
|
|
return this.pad(s);
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Format hex
|
|
|
|
* @param val
|
|
|
|
* @param upper
|
|
|
|
*/
|
2019-10-05 16:02:34 +00:00
|
|
|
fmtHex(val: string | number, upper = false): string {
|
2019-08-15 15:57:24 +00:00
|
|
|
// allow others types ?
|
|
|
|
switch (typeof val) {
|
|
|
|
case "number":
|
|
|
|
return this.fmtNumber(val as number, 16, upper);
|
2020-06-12 13:19:29 +00:00
|
|
|
case "string": {
|
2019-10-05 16:02:34 +00:00
|
|
|
const sharp = this.flags.sharp && val.length !== 0;
|
2019-08-15 15:57:24 +00:00
|
|
|
let hex = sharp ? "0x" : "";
|
|
|
|
const prec = this.flags.precision;
|
|
|
|
const end = prec !== -1 ? min(prec, val.length) : val.length;
|
|
|
|
for (let i = 0; i !== end; ++i) {
|
|
|
|
if (i !== 0 && this.flags.space) {
|
|
|
|
hex += sharp ? " 0x" : " ";
|
|
|
|
}
|
2021-01-16 23:32:59 +00:00
|
|
|
// TODO(bartlomieju): for now only taking into account the
|
2019-08-15 15:57:24 +00:00
|
|
|
// lower half of the codePoint, ie. as if a string
|
|
|
|
// is a list of 8bit values instead of UCS2 runes
|
2019-10-05 16:02:34 +00:00
|
|
|
const c = (val.charCodeAt(i) & 0xff).toString(16);
|
2019-08-15 15:57:24 +00:00
|
|
|
hex += c.length === 1 ? `0${c}` : c;
|
|
|
|
}
|
|
|
|
if (upper) {
|
|
|
|
hex = hex.toUpperCase();
|
|
|
|
}
|
|
|
|
return this.pad(hex);
|
2020-06-12 13:19:29 +00:00
|
|
|
}
|
2019-08-15 15:57:24 +00:00
|
|
|
default:
|
|
|
|
throw new Error(
|
2024-08-21 05:42:02 +00:00
|
|
|
`Cannot format hex, only number and string are supported for hex formatting: ${typeof val} is given`,
|
2019-08-15 15:57:24 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Format value
|
|
|
|
* @param val
|
|
|
|
*/
|
2020-08-24 23:43:54 +00:00
|
|
|
fmtV(val: Record<string, unknown>): string {
|
2019-08-15 15:57:24 +00:00
|
|
|
if (this.flags.sharp) {
|
2020-07-14 19:24:17 +00:00
|
|
|
const options = this.flags.precision !== -1
|
|
|
|
? { depth: this.flags.precision }
|
|
|
|
: {};
|
2019-08-15 15:57:24 +00:00
|
|
|
return this.pad(Deno.inspect(val, options));
|
|
|
|
} else {
|
|
|
|
const p = this.flags.precision;
|
2022-12-12 06:21:59 +00:00
|
|
|
return p === -1 ? val.toString() : val.toString().slice(0, p);
|
2019-08-15 15:57:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:05:37 +00:00
|
|
|
/**
|
|
|
|
* Format JSON
|
|
|
|
* @param val
|
|
|
|
*/
|
2020-05-21 09:12:37 +00:00
|
|
|
fmtJ(val: unknown): string {
|
2019-08-15 15:57:24 +00:00
|
|
|
return JSON.stringify(val);
|
|
|
|
}
|
2023-01-17 09:42:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Format inspect
|
|
|
|
* @param val
|
|
|
|
* @param compact Whether or not the output should be compact.
|
|
|
|
*/
|
|
|
|
fmtI(val: unknown, compact: boolean): string {
|
|
|
|
return Deno.inspect(val, {
|
2023-09-01 02:38:32 +00:00
|
|
|
colors: !Deno?.noColor,
|
2023-01-17 09:42:53 +00:00
|
|
|
compact,
|
|
|
|
depth: Infinity,
|
|
|
|
iterableLimit: Infinity,
|
|
|
|
});
|
|
|
|
}
|
2019-08-15 15:57:24 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 02:33:59 +00:00
|
|
|
/**
|
2024-05-28 06:14:34 +00:00
|
|
|
* Converts and formats a variable number of `args` as is specified by `format`.
|
2020-11-06 02:33:59 +00:00
|
|
|
* `sprintf` returns the formatted string.
|
2020-11-07 14:05:37 +00:00
|
|
|
*
|
2024-05-28 06:14:34 +00:00
|
|
|
* See the module documentation for the available format strings.
|
|
|
|
*
|
|
|
|
* @example Usage
|
|
|
|
* ```ts
|
|
|
|
* import { sprintf } from "@std/fmt/printf";
|
2024-06-03 04:10:27 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* assertEquals(sprintf("%d", 9), "9");
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
2024-05-28 06:14:34 +00:00
|
|
|
* assertEquals(sprintf("%o", 9), "11");
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
2024-05-28 06:14:34 +00:00
|
|
|
* assertEquals(sprintf("%f", 4), "4.000000");
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
2024-05-28 06:14:34 +00:00
|
|
|
* assertEquals(sprintf("%.3f", 0.9999), "1.000");
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param format The format string to use
|
|
|
|
* @param args The arguments to format
|
|
|
|
* @returns The formatted string
|
2020-11-06 02:33:59 +00:00
|
|
|
*/
|
2020-05-21 09:12:37 +00:00
|
|
|
export function sprintf(format: string, ...args: unknown[]): string {
|
2019-10-05 16:02:34 +00:00
|
|
|
const printf = new Printf(format, ...args);
|
2019-08-15 15:57:24 +00:00
|
|
|
return printf.doPrintf();
|
|
|
|
}
|
2020-05-21 09:12:37 +00:00
|
|
|
|
2020-11-06 02:33:59 +00:00
|
|
|
/**
|
|
|
|
* Converts and format a variable number of `args` as is specified by `format`.
|
|
|
|
* `printf` writes the formatted string to standard output.
|
2024-05-28 06:14:34 +00:00
|
|
|
*
|
|
|
|
* See the module documentation for the available format strings.
|
|
|
|
*
|
|
|
|
* @example Usage
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 06:14:34 +00:00
|
|
|
* import { printf } from "@std/fmt/printf";
|
|
|
|
*
|
2024-06-03 04:10:27 +00:00
|
|
|
* printf("%d", 9); // Prints "9"
|
|
|
|
*
|
|
|
|
* printf("%o", 9); // Prints "11"
|
|
|
|
*
|
|
|
|
* printf("%f", 4); // Prints "4.000000"
|
|
|
|
*
|
|
|
|
* printf("%.3f", 0.9999); // Prints "1.000"
|
2024-05-28 06:14:34 +00:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param format The format string to use
|
|
|
|
* @param args The arguments to format
|
2020-11-06 02:33:59 +00:00
|
|
|
*/
|
2022-08-24 01:21:57 +00:00
|
|
|
export function printf(format: string, ...args: unknown[]) {
|
2020-05-21 09:12:37 +00:00
|
|
|
const s = sprintf(format, ...args);
|
|
|
|
Deno.stdout.writeSync(new TextEncoder().encode(s));
|
|
|
|
}
|