| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5130,6 +5130,20 @@ For code running using Node.js APIs, converting between base64-encoded strings | |||
| 5130 | 5130 | and binary data should be performed using `Buffer.from(str, 'base64')` and | |
| 5131 | 5131 | `buf.toString('base64')`.** | |
| 5132 | 5132 | ||
| 5133 | + ### `buffer.isAscii(input)` | ||
| 5134 | + | ||
| 5135 | + <!-- YAML | ||
| 5136 | + added: REPLACEME | ||
| 5137 | + --> | ||
| 5138 | + | ||
| 5139 | + * input {Buffer | ArrayBuffer | TypedArray} The input to validate. | ||
| 5140 | + * Returns: {boolean} | ||
| 5141 | + | ||
| 5142 | + This function returns `true` if `input` contains only valid ASCII-encoded data, | ||
| 5143 | + including the case in which `input` is empty. | ||
| 5144 | + | ||
| 5145 | + Throws if the `input` is a detached array buffer. | ||
| 5146 | + | ||
| 5133 | 5147 | ### `buffer.isUtf8(input)` | |
| 5134 | 5148 | ||
| 5135 | 5149 | <!-- YAML | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -56,6 +56,7 @@ const { | |||
| 56 | 56 | compareOffset, | |
| 57 | 57 | createFromString, | |
| 58 | 58 | fill: bindingFill, | |
| 59 | + isAscii: bindingIsAscii, | ||
| 59 | 60 | isUtf8: bindingIsUtf8, | |
| 60 | 61 | indexOfBuffer, | |
| 61 | 62 | indexOfNumber, | |
@@ -1323,11 +1324,20 @@ function isUtf8(input) { | |||
| 1323 | 1324 | throw new ERR_INVALID_ARG_TYPE('input', ['TypedArray', 'Buffer'], input); | |
| 1324 | 1325 | } | |
| 1325 | 1326 | ||
| 1327 | + function isAscii(input) { | ||
| 1328 | + if (isTypedArray(input) || isAnyArrayBuffer(input)) { | ||
| 1329 | + return bindingIsAscii(input); | ||
| 1330 | + } | ||
| 1331 | + | ||
| 1332 | + throw new ERR_INVALID_ARG_TYPE('input', ['ArrayBuffer', 'Buffer', 'TypedArray'], input); | ||
| 1333 | + } | ||
| 1334 | + | ||
| 1326 | 1335 | module.exports = { | |
| 1327 | 1336 | Buffer, | |
| 1328 | 1337 | SlowBuffer, | |
| 1329 | 1338 | transcode, | |
| 1330 | 1339 | isUtf8, | |
| 1340 | + isAscii, | ||
| 1331 | 1341 | ||
| 1332 | 1342 | // Legacy | |
| 1333 | 1343 | kMaxLength, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1238,6 +1238,21 @@ static void IsUtf8(const FunctionCallbackInfo<Value>& args) { | |||
| 1238 | 1238 | args.GetReturnValue().Set(simdutf::validate_utf8(abv.data(), abv.length())); | |
| 1239 | 1239 | } | |
| 1240 | 1240 | ||
| 1241 | + static void IsAscii(const FunctionCallbackInfo<Value>& args) { | ||
| 1242 | + Environment* env = Environment::GetCurrent(args); | ||
| 1243 | + CHECK_EQ(args.Length(), 1); | ||
| 1244 | + CHECK(args[0]->IsTypedArray() || args[0]->IsArrayBuffer() || | ||
| 1245 | + args[0]->IsSharedArrayBuffer()); | ||
| 1246 | + ArrayBufferViewContents<char> abv(args[0]); | ||
| 1247 | + | ||
| 1248 | + if (abv.WasDetached()) { | ||
| 1249 | + return node::THROW_ERR_INVALID_STATE( | ||
| 1250 | + env, "Cannot validate on a detached buffer"); | ||
| 1251 | + } | ||
| 1252 | + | ||
| 1253 | + args.GetReturnValue().Set(simdutf::validate_ascii(abv.data(), abv.length())); | ||
| 1254 | + } | ||
| 1255 | + | ||
| 1241 | 1256 | void SetBufferPrototype(const FunctionCallbackInfo<Value>& args) { | |
| 1242 | 1257 | Environment* env = Environment::GetCurrent(args); | |
| 1243 | 1258 | ||
@@ -1373,6 +1388,7 @@ void Initialize(Local<Object> target, | |||
| 1373 | 1388 | SetMethodNoSideEffect(context, target, "encodeUtf8String", EncodeUtf8String); | |
| 1374 | 1389 | ||
| 1375 | 1390 | SetMethodNoSideEffect(context, target, "isUtf8", IsUtf8); | |
| 1391 | + SetMethodNoSideEffect(context, target, "isAscii", IsAscii); | ||
| 1376 | 1392 | ||
| 1377 | 1393 | target | |
| 1378 | 1394 | ->Set(context, | |
@@ -1430,6 +1446,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) { | |||
| 1430 | 1446 | registry->Register(EncodeUtf8String); | |
| 1431 | 1447 | ||
| 1432 | 1448 | registry->Register(IsUtf8); | |
| 1449 | + registry->Register(IsAscii); | ||
| 1433 | 1450 | ||
| 1434 | 1451 | registry->Register(StringSlice<ASCII>); | |
| 1435 | 1452 | registry->Register(StringSlice<BASE64>); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,42 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + require('../common'); | ||
| 4 | + const assert = require('assert'); | ||
| 5 | + const { isAscii, Buffer } = require('buffer'); | ||
| 6 | + const { TextEncoder } = require('util'); | ||
| 7 | + | ||
| 8 | + const encoder = new TextEncoder(); | ||
| 9 | + | ||
| 10 | + assert.strictEqual(isAscii(encoder.encode('hello')), true); | ||
| 11 | + assert.strictEqual(isAscii(encoder.encode('ğ')), false); | ||
| 12 | + assert.strictEqual(isAscii(Buffer.from([])), true); | ||
| 13 | + | ||
| 14 | + [ | ||
| 15 | + undefined, | ||
| 16 | + '', 'hello', | ||
| 17 | + false, true, | ||
| 18 | + 0, 1, | ||
| 19 | + 0n, 1n, | ||
| 20 | + Symbol(), | ||
| 21 | + () => {}, | ||
| 22 | + {}, [], null, | ||
| 23 | + ].forEach((input) => { | ||
| 24 | + assert.throws( | ||
| 25 | + () => { isAscii(input); }, | ||
| 26 | + { | ||
| 27 | + code: 'ERR_INVALID_ARG_TYPE', | ||
| 28 | + }, | ||
| 29 | + ); | ||
| 30 | + }); | ||
| 31 | + | ||
| 32 | + { | ||
| 33 | + // Test with detached array buffers | ||
| 34 | + const arrayBuffer = new ArrayBuffer(1024); | ||
| 35 | + structuredClone(arrayBuffer, { transfer: [arrayBuffer] }); | ||
| 36 | + assert.throws( | ||
| 37 | + () => { isAscii(arrayBuffer); }, | ||
| 38 | + { | ||
| 39 | + code: 'ERR_INVALID_STATE' | ||
| 40 | + } | ||
| 41 | + ); | ||
| 42 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments