mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 09:31:22 -05:00
Move std/fmt/sprintf.ts to std/fmt/printf.ts (#4567)
This commit is contained in:
parent
9fdc6dc435
commit
6f52ad9052
4 changed files with 30 additions and 34 deletions
|
@ -55,22 +55,6 @@ This is very much a work-in-progress. I'm actively soliciting feedback.
|
|||
are not likely useful) are missing, namely %q (print quoted), %U (unicode
|
||||
format)
|
||||
|
||||
## Author
|
||||
|
||||
Tim Becker (tim@presseverykey.com)
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
|
||||
The implementation is inspired by POSIX and Golang (see above) but does not port
|
||||
implementation code. A number of Golang test-cases based on:
|
||||
|
||||
https://golang.org/src/fmt/fmt_test.go
|
||||
( BSD: Copyright (c) 2009 The Go Authors. All rights reserved. )
|
||||
|
||||
were used.
|
||||
|
||||
# printf: prints formatted output
|
||||
|
||||
sprintf converts and formats a variable number of arguments as is specified by a
|
||||
|
|
|
@ -1,2 +0,0 @@
|
|||
export * from "./colors.ts";
|
||||
export * from "./sprintf.ts";
|
|
@ -1,3 +1,8 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
//
|
||||
// This implementation is inspired by POSIX and Golang but does not port
|
||||
// implementation code.
|
||||
|
||||
enum State {
|
||||
PASSTHROUGH,
|
||||
PERCENT,
|
||||
|
@ -5,6 +10,7 @@ enum State {
|
|||
PRECISION,
|
||||
WIDTH,
|
||||
}
|
||||
|
||||
enum WorP {
|
||||
WIDTH,
|
||||
PRECISION,
|
||||
|
@ -21,14 +27,11 @@ class Flags {
|
|||
precision = -1;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
|
||||
const min = Math.min;
|
||||
|
||||
const UNICODE_REPLACEMENT_CHARACTER = "\ufffd";
|
||||
const DEFAULT_PRECISION = 6;
|
||||
|
||||
const FLOAT_REGEXP = /(-?)(\d)\.?(\d*)e([+-])(\d+)/;
|
||||
|
||||
enum F {
|
||||
sign = 1,
|
||||
mantissa,
|
||||
|
@ -39,8 +42,7 @@ enum F {
|
|||
|
||||
class Printf {
|
||||
format: string;
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
args: any[];
|
||||
args: unknown[];
|
||||
i: number;
|
||||
|
||||
state: State = State.PASSTHROUGH;
|
||||
|
@ -54,8 +56,7 @@ class Printf {
|
|||
// barf, store precision and width errors for later processing ...
|
||||
tmpError?: string;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
constructor(format: string, ...args: any[]) {
|
||||
constructor(format: string, ...args: unknown[]) {
|
||||
this.format = format;
|
||||
this.args = args;
|
||||
this.haveSeen = new Array(args.length);
|
||||
|
@ -166,6 +167,7 @@ class Printf {
|
|||
} // switch state
|
||||
}
|
||||
}
|
||||
|
||||
handleWidthOrPrecisionRef(wOrP: WorP): void {
|
||||
if (this.argNum >= this.args.length) {
|
||||
// handle Positional should have already taken care of it...
|
||||
|
@ -187,6 +189,7 @@ class Printf {
|
|||
}
|
||||
this.argNum++;
|
||||
}
|
||||
|
||||
handleWidthAndPrecision(flags: Flags): void {
|
||||
const fmt = this.format;
|
||||
for (; this.i !== this.format.length; ++this.i) {
|
||||
|
@ -270,8 +273,10 @@ class Printf {
|
|||
this.argNum = err ? this.argNum : positional - 1;
|
||||
return;
|
||||
}
|
||||
|
||||
handleLessThan(): string {
|
||||
const arg = this.args[this.argNum];
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const arg = this.args[this.argNum] as any;
|
||||
if ((arg || {}).constructor.name !== "Array") {
|
||||
throw new Error(`arg ${arg} is not an array. Todo better error handling`);
|
||||
}
|
||||
|
@ -282,6 +287,7 @@ class Printf {
|
|||
}
|
||||
return str + " ]";
|
||||
}
|
||||
|
||||
handleVerb(): void {
|
||||
const verb = this.format[this.i];
|
||||
this.verb = verb;
|
||||
|
@ -649,8 +655,7 @@ class Printf {
|
|||
}
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
fmtV(val: any): string {
|
||||
fmtV(val: object): string {
|
||||
if (this.flags.sharp) {
|
||||
const options =
|
||||
this.flags.precision !== -1 ? { depth: this.flags.precision } : {};
|
||||
|
@ -661,14 +666,17 @@ class Printf {
|
|||
}
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
fmtJ(val: any): string {
|
||||
fmtJ(val: unknown): string {
|
||||
return JSON.stringify(val);
|
||||
}
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export function sprintf(format: string, ...args: any[]): string {
|
||||
export function sprintf(format: string, ...args: unknown[]): string {
|
||||
const printf = new Printf(format, ...args);
|
||||
return printf.doPrintf();
|
||||
}
|
||||
|
||||
export function printf(format: string, ...args: unknown[]): void {
|
||||
const s = sprintf(format, ...args);
|
||||
Deno.stdout.writeSync(new TextEncoder().encode(s));
|
||||
}
|
|
@ -1,5 +1,11 @@
|
|||
import { sprintf } from "./sprintf.ts";
|
||||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
//
|
||||
// A number of test-cases based on:
|
||||
//
|
||||
// https://golang.org/src/fmt/fmt_test.go
|
||||
// BSD: Copyright (c) 2009 The Go Authors. All rights reserved.
|
||||
|
||||
import { sprintf } from "./printf.ts";
|
||||
import { assertEquals } from "../testing/asserts.ts";
|
||||
import { cyan, yellow } from "./colors.ts";
|
||||
|
Loading…
Add table
Reference in a new issue