From fbe399c75c6493d07d794d7a01977c61a55fd0f6 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Sat, 24 Dec 2022 21:32:05 -0500 Subject: [PATCH] buffer: add buffer.isUtf8 for utf8 validation PR-URL: https://github.com/nodejs/node/pull/45947 Reviewed-By: Robert Nagy Reviewed-By: Matteo Collina Reviewed-By: Luigi Pinca Reviewed-By: Rafael Gonzaga Reviewed-By: Ben Noordhuis Reviewed-By: Anna Henningsen --- doc/api/buffer.md | 11 ++++ lib/buffer.js | 13 ++++- src/node_buffer.cc | 18 ++++++ src/node_errors.h | 1 + src/util-inl.h | 1 + src/util.h | 2 + test/parallel/test-buffer-isutf8.js | 86 +++++++++++++++++++++++++++++ 7 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-buffer-isutf8.js diff --git a/doc/api/buffer.md b/doc/api/buffer.md index 978c26f734eee8..3ed750fce8a1cc 100644 --- a/doc/api/buffer.md +++ b/doc/api/buffer.md @@ -5130,6 +5130,17 @@ For code running using Node.js APIs, converting between base64-encoded strings and binary data should be performed using `Buffer.from(str, 'base64')` and `buf.toString('base64')`.** +### `buffer.isUtf8(input)` + + + +* input {Buffer | ArrayBuffer | TypedArray} The input to validate. +* Returns: {boolean} Returns `true` if and only if the input is valid UTF-8. + +This function is used to check if input contains UTF-8 code points (characters). + ### `buffer.INSPECT_MAX_BYTES`