From 6f52ad9052a02f354011d2727b2ad4a3d73b08d5 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Thu, 21 May 2020 05:12:37 -0400 Subject: [PATCH] Move std/fmt/sprintf.ts to std/fmt/printf.ts (#4567) --- std/fmt/README.md | 16 --------- std/fmt/mod.ts | 2 -- std/fmt/{sprintf.ts => printf.ts} | 38 +++++++++++++-------- std/fmt/{sprintf_test.ts => printf_test.ts} | 8 ++++- 4 files changed, 30 insertions(+), 34 deletions(-) delete mode 100644 std/fmt/mod.ts rename std/fmt/{sprintf.ts => printf.ts} (96%) rename std/fmt/{sprintf_test.ts => printf_test.ts} (98%) diff --git a/std/fmt/README.md b/std/fmt/README.md index 6f9a67a18c..8d76cda2a2 100644 --- a/std/fmt/README.md +++ b/std/fmt/README.md @@ -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 diff --git a/std/fmt/mod.ts b/std/fmt/mod.ts deleted file mode 100644 index a0f8feb8b6..0000000000 --- a/std/fmt/mod.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from "./colors.ts"; -export * from "./sprintf.ts"; diff --git a/std/fmt/sprintf.ts b/std/fmt/printf.ts similarity index 96% rename from std/fmt/sprintf.ts rename to std/fmt/printf.ts index fb59b2925c..8c2f8d0344 100644 --- a/std/fmt/sprintf.ts +++ b/std/fmt/printf.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)); +} diff --git a/std/fmt/sprintf_test.ts b/std/fmt/printf_test.ts similarity index 98% rename from std/fmt/sprintf_test.ts rename to std/fmt/printf_test.ts index 8955487ac5..a10fdf5163 100644 --- a/std/fmt/sprintf_test.ts +++ b/std/fmt/printf_test.ts @@ -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";