-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP]Fix calling Buffer.toString
with (offset, length, encoding)
#3467
Changes from all commits
3b53bba
98be1d9
22a72a9
2003ffd
cc0bb2f
961ec30
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -427,29 +427,29 @@ export function hexWrite(this: BufferExt, text, offset, length) { | |
return this.write(text, offset, length, "hex"); | ||
} | ||
|
||
export function utf8Slice(this: BufferExt, offset, length) { | ||
return this.toString(offset, length, "utf8"); | ||
export function utf8Slice(this: BufferExt, start, end) { | ||
return this.toString("utf8", start, end); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
export function ucs2Slice(this: BufferExt, offset, length) { | ||
return this.toString(offset, length, "ucs2"); | ||
export function ucs2Slice(this: BufferExt, start, end) { | ||
return this.toString("ucs2", start, end); | ||
} | ||
export function utf16leSlice(this: BufferExt, offset, length) { | ||
return this.toString(offset, length, "utf16le"); | ||
export function utf16leSlice(this: BufferExt, start, end) { | ||
return this.toString("utf16le", start, end); | ||
} | ||
export function latin1Slice(this: BufferExt, offset, length) { | ||
return this.toString(offset, length, "latin1"); | ||
export function latin1Slice(this: BufferExt, start, end) { | ||
return this.toString("latin1", start, end); | ||
} | ||
export function asciiSlice(this: BufferExt, offset, length) { | ||
return this.toString(offset, length, "ascii"); | ||
export function asciiSlice(this: BufferExt, start, end) { | ||
return this.toString("ascii", start, end); | ||
} | ||
export function base64Slice(this: BufferExt, offset, length) { | ||
return this.toString(offset, length, "base64"); | ||
export function base64Slice(this: BufferExt, start, end) { | ||
return this.toString("base64", start, end); | ||
} | ||
export function base64urlSlice(this: BufferExt, offset, length) { | ||
return this.toString(offset, length, "base64url"); | ||
export function base64urlSlice(this: BufferExt, start, end) { | ||
return this.toString("base64url", start, end); | ||
} | ||
export function hexSlice(this: BufferExt, offset, length) { | ||
return this.toString(offset, length, "hex"); | ||
export function hexSlice(this: BufferExt, start, end) { | ||
return this.toString("hex", start, end); | ||
} | ||
|
||
export function toJSON(this: BufferExt) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2353,6 +2353,85 @@ | |
} | ||
}); | ||
|
||
it("Buffer.toString(encoding, start, end)", () => { | ||
const buf = Buffer.from("0123456789", "utf8"); | ||
|
||
expect(buf.toString()).toStrictEqual("0123456789"); | ||
expect(buf.toString("utf8")).toStrictEqual("0123456789"); | ||
expect(buf.toString("utf8", 3)).toStrictEqual("3456789"); | ||
expect(buf.toString("utf8", 3, 4)).toStrictEqual("3"); | ||
|
||
expect(buf.toString("utf8", 3, 100)).toStrictEqual("3456789"); | ||
expect(buf.toString("utf8", 3, 1)).toStrictEqual(""); | ||
expect(buf.toString("utf8", 100, 200)).toStrictEqual(""); | ||
expect(buf.toString("utf8", 100, 1)).toStrictEqual(""); | ||
}); | ||
|
||
it("Buffer.toString(offset, length, encoding)", () => { | ||
const buf = Buffer.from("0123456789", "utf8"); | ||
|
||
expect(buf.toString(3, 6, "utf8")).toStrictEqual("345678"); | ||
expect(buf.toString(3, 100, "utf8")).toStrictEqual("3456789"); | ||
expect(buf.toString(100, 200, "utf8")).toStrictEqual(""); | ||
expect(buf.toString(100, 50, "utf8")).toStrictEqual(""); | ||
}); | ||
|
||
it("Buffer.asciiSlice())", () => { | ||
const buf = Buffer.from("0123456789", "ascii"); | ||
|
||
expect(buf.asciiSlice()).toStrictEqual("0123456789"); | ||
expect(buf.asciiSlice(3)).toStrictEqual("3456789"); | ||
expect(buf.asciiSlice(3, 4)).toStrictEqual("3"); | ||
Check failure on line 2384 in test/js/node/buffer.test.js GitHub Actions / Tests linux-x64error: expect(received).toStrictEqual(expected)
Check failure on line 2384 in test/js/node/buffer.test.js GitHub Actions / Tests linux-x64-baselineerror: expect(received).toStrictEqual(expected)
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These tests are failing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh its failing because we need to commit regenerated builtins There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you. I had mistakenly thought that the generated code was not required for commit. |
||
}); | ||
|
||
it("Buffer.latin1Slice()", () => { | ||
const buf = Buffer.from("âéö", "latin1"); | ||
|
||
expect(buf.latin1Slice()).toStrictEqual("âéö"); | ||
expect(buf.latin1Slice(1)).toStrictEqual("éö"); | ||
expect(buf.latin1Slice(1, 2)).toStrictEqual("é"); | ||
Check failure on line 2392 in test/js/node/buffer.test.js GitHub Actions / Tests linux-x64error: expect(received).toStrictEqual(expected)
Check failure on line 2392 in test/js/node/buffer.test.js GitHub Actions / Tests linux-x64-baselineerror: expect(received).toStrictEqual(expected)
|
||
}); | ||
|
||
it("Buffer.utf8Slice()", () => { | ||
const buf = Buffer.from("あいうえお", "utf8"); | ||
|
||
expect(buf.utf8Slice()).toStrictEqual("あいうえお"); | ||
expect(buf.utf8Slice(3)).toStrictEqual("いうえお"); | ||
expect(buf.utf8Slice(3, 6)).toStrictEqual("い"); | ||
Check failure on line 2400 in test/js/node/buffer.test.js GitHub Actions / Tests linux-x64error: expect(received).toStrictEqual(expected)
Check failure on line 2400 in test/js/node/buffer.test.js GitHub Actions / Tests linux-x64-baselineerror: expect(received).toStrictEqual(expected)
|
||
}); | ||
|
||
it("Buffer.hexSlice()", () => { | ||
const buf = Buffer.from("0123456789", "utf8"); | ||
|
||
expect(buf.hexSlice()).toStrictEqual("30313233343536373839"); | ||
expect(buf.hexSlice(3)).toStrictEqual("33343536373839"); | ||
expect(buf.hexSlice(3, 4)).toStrictEqual("33"); | ||
Check failure on line 2408 in test/js/node/buffer.test.js GitHub Actions / Tests linux-x64error: expect(received).toStrictEqual(expected)
Check failure on line 2408 in test/js/node/buffer.test.js GitHub Actions / Tests linux-x64-baselineerror: expect(received).toStrictEqual(expected)
|
||
}); | ||
|
||
it("Buffer.ucs2Slice()", () => { | ||
const buf = Buffer.from("あいうえお", "ucs2"); | ||
|
||
expect(buf.ucs2Slice()).toStrictEqual("あいうえお"); | ||
expect(buf.ucs2Slice(2)).toStrictEqual("いうえお"); | ||
expect(buf.ucs2Slice(2, 6)).toStrictEqual("いう"); | ||
Check failure on line 2416 in test/js/node/buffer.test.js GitHub Actions / Tests linux-x64error: expect(received).toStrictEqual(expected)
Check failure on line 2416 in test/js/node/buffer.test.js GitHub Actions / Tests linux-x64-baselineerror: expect(received).toStrictEqual(expected)
|
||
}); | ||
|
||
it("Buffer.base64Slice()", () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I get a segmentfault in the base64-related tests on my local machine. I don't know what happened here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I vaguely remember @cirospaciari running into strange issues with .toBase64URL as well... I think we should switch to using the SIMD base64 library that Node.js also uses. Our current implementation uses a mix of WebKit's internal |
||
const buf = Buffer.from("0123456789", "utf8"); | ||
|
||
expect(buf.base64Slice()).toStrictEqual("MDEyMzQ1Njc4OQ=="); | ||
expect(buf.base64Slice(3)).toStrictEqual("MzQ1Njc4OQ=="); | ||
expect(buf.base64Slice(3, 4)).toStrictEqual("Mw=="); | ||
Check failure on line 2424 in test/js/node/buffer.test.js GitHub Actions / Tests linux-x64error: expect(received).toStrictEqual(expected)
Check failure on line 2424 in test/js/node/buffer.test.js GitHub Actions / Tests linux-x64-baselineerror: expect(received).toStrictEqual(expected)
|
||
}); | ||
|
||
it("Buffer.base64urlSlice()", () => { | ||
const buf = Buffer.from("0123456789", "utf8"); | ||
|
||
expect(buf.base64urlSlice()).toStrictEqual("MDEyMzQ1Njc4OQ"); | ||
expect(buf.base64urlSlice(3)).toStrictEqual("MzQ1Njc4OQ"); | ||
expect(buf.base64urlSlice(3, 4)).toStrictEqual("Mw"); | ||
Check failure on line 2432 in test/js/node/buffer.test.js GitHub Actions / Tests linux-x64error: expect(received).toStrictEqual(expected)
Check failure on line 2432 in test/js/node/buffer.test.js GitHub Actions / Tests linux-x64-baselineerror: expect(received).toStrictEqual(expected)
|
||
}); | ||
|
||
it("should not crash on invalid UTF-8 byte sequence", () => { | ||
const buf = Buffer.from([0xc0, 0xfd]); | ||
expect(buf.length).toBe(2); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
toInt32
can throw, we should technically check for exceptions here (we did not before)