summaryrefslogtreecommitdiff
path: root/src/node_buffer.cc
diff options
context:
space:
mode:
authorYagiz Nizipli <yagiz@nizipli.com>2023-01-19 22:24:40 -0500
committerGitHub <noreply@github.com>2023-01-20 03:24:40 +0000
commitbabe6d7c84b9cefe0b9bf9f1929d19c80cc30c7e (patch)
treef553aad2285989c02fa9aa890f1fe0d724db4295 /src/node_buffer.cc
parent49413ad8ae3c1c6c25caa60e150939c1cbb50094 (diff)
downloadnode-new-babe6d7c84b9cefe0b9bf9f1929d19c80cc30c7e.tar.gz
buffer: add isAscii method
PR-URL: https://github.com/nodejs/node/pull/46046 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Diffstat (limited to 'src/node_buffer.cc')
-rw-r--r--src/node_buffer.cc17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/node_buffer.cc b/src/node_buffer.cc
index f7b008af36..b144b95ab4 100644
--- a/src/node_buffer.cc
+++ b/src/node_buffer.cc
@@ -1238,6 +1238,21 @@ static void IsUtf8(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(simdutf::validate_utf8(abv.data(), abv.length()));
}
+static void IsAscii(const FunctionCallbackInfo<Value>& args) {
+ Environment* env = Environment::GetCurrent(args);
+ CHECK_EQ(args.Length(), 1);
+ CHECK(args[0]->IsTypedArray() || args[0]->IsArrayBuffer() ||
+ args[0]->IsSharedArrayBuffer());
+ ArrayBufferViewContents<char> abv(args[0]);
+
+ if (abv.WasDetached()) {
+ return node::THROW_ERR_INVALID_STATE(
+ env, "Cannot validate on a detached buffer");
+ }
+
+ args.GetReturnValue().Set(simdutf::validate_ascii(abv.data(), abv.length()));
+}
+
void SetBufferPrototype(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
@@ -1373,6 +1388,7 @@ void Initialize(Local<Object> target,
SetMethodNoSideEffect(context, target, "encodeUtf8String", EncodeUtf8String);
SetMethodNoSideEffect(context, target, "isUtf8", IsUtf8);
+ SetMethodNoSideEffect(context, target, "isAscii", IsAscii);
target
->Set(context,
@@ -1430,6 +1446,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(EncodeUtf8String);
registry->Register(IsUtf8);
+ registry->Register(IsAscii);
registry->Register(StringSlice<ASCII>);
registry->Register(StringSlice<BASE64>);