fix: format specifier without characters in between (#9209)

* fix: format specifier without spaces in between, resolves #9208

* chore: test in console-log.test.ts
This commit is contained in:
Yash Singh
2024-03-04 17:46:19 -08:00
committed by GitHub
parent 4f0a497660
commit bd2176ffb0
4 changed files with 15 additions and 0 deletions

View File

@@ -1226,7 +1226,13 @@ pub const Formatter = struct {
var i: u32 = 0;
var len: u32 = @as(u32, @truncate(slice.len));
var any_non_ascii = false;
var hit_percent = false;
while (i < len) : (i += 1) {
if (hit_percent) {
i = 0;
hit_percent = false;
}
switch (slice[i]) {
'%' => {
i += 1;
@@ -1251,6 +1257,7 @@ pub const Formatter = struct {
any_non_ascii = false;
slice = slice[@min(slice.len, i + 1)..];
i = 0;
hit_percent = true;
len = @as(u32, @truncate(slice.len));
const next_value = this.remaining_values[0];
this.remaining_values = this.remaining_values[1..];

View File

@@ -59,7 +59,9 @@ test("no assertion failures", () => {
assert.strictEqual(util.format("%d", Infinity), "Infinity");
assert.strictEqual(util.format("%d", -Infinity), "-Infinity");
assert.strictEqual(util.format("%d %d", 42, 43), "42 43");
assert.strictEqual(util.format("%d%d", 42, 43), "4243");
assert.strictEqual(util.format("%d %d", 42), "42 %d");
assert.strictEqual(util.format("%d%d", 42), "42%d");
assert.strictEqual(util.format("%d", 1180591620717411303424), "1.1805916207174113e+21");
assert.strictEqual(util.format("%d", 1180591620717411303424n), "1180591620717411303424n");
assert.strictEqual(
@@ -151,7 +153,9 @@ test("no assertion failures", () => {
assert.strictEqual(util.format("%s", -0), "-0");
assert.strictEqual(util.format("%s", "-0.0"), "-0.0");
assert.strictEqual(util.format("%s %s", 42, 43), "42 43");
assert.strictEqual(util.format("%s%s", 42, 43), "4243");
assert.strictEqual(util.format("%s %s", 42), "42 %s");
assert.strictEqual(util.format("%s%s", 42), "42%s");
assert.strictEqual(util.format("%s", 42n), "42n");
assert.strictEqual(util.format("%s", Symbol("foo")), "Symbol(foo)");
assert.strictEqual(util.format("%s", true), "true");
@@ -239,6 +243,7 @@ test("no assertion failures", () => {
assert.strictEqual(util.format("%j", 42), "42");
assert.strictEqual(util.format("%j", "42"), '"42"');
assert.strictEqual(util.format("%j %j", 42, 43), "42 43");
assert.strictEqual(util.format("%j%j", 42, null), "42null");
assert.strictEqual(util.format("%j %j", 42), "42 %j");
// Object format specifier

View File

@@ -48,6 +48,7 @@ FooWithProp {
/FooRegex/
Is it a bug or a feature that formatting numbers like 123 is colored
String 123 should be 2nd word, 456 == 456 and percent s %s == What okay
123456 without space should work
{
foo: {
name: "baz",

View File

@@ -53,6 +53,8 @@ console.log("Is it a bug or a feature that formatting numbers like %d is colored
console.log("String %s should be 2nd word, 456 == %s and percent s %s == %s", "123", "456", "%s", "What", "okay");
console.log("%s%s without space should work", "123", "456");
const infinteLoop = {
foo: {
name: "baz",