| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent c9845fc commit f1dcbe7
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,13 +5,14 @@ const common = require('../common.js'); | |||
| 5 | 5 | const bench = common.createBenchmark(main, { | |
| 6 | 6 | encoding: ['utf-8', 'latin1', 'iso-8859-3'], | |
| 7 | 7 | ignoreBOM: [0, 1], | |
| 8 | + fatal: [0, 1], | ||
| 8 | 9 | len: [256, 1024 * 16, 1024 * 512], | |
| 9 | 10 | n: [1e2], | |
| 10 | 11 | type: ['SharedArrayBuffer', 'ArrayBuffer', 'Buffer'] | |
| 11 | 12 | }); | |
| 12 | 13 | ||
| 13 | - function main({ encoding, len, n, ignoreBOM, type }) { | ||
| 14 | - const decoder = new TextDecoder(encoding, { ignoreBOM }); | ||
| 14 | + function main({ encoding, len, n, ignoreBOM, type, fatal }) { | ||
| 15 | + const decoder = new TextDecoder(encoding, { ignoreBOM, fatal }); | ||
| 15 | 16 | let buf; | |
| 16 | 17 | ||
| 17 | 18 | switch (type) { | |
@@ -31,7 +32,11 @@ function main({ encoding, len, n, ignoreBOM, type }) { | |||
| 31 | 32 | ||
| 32 | 33 | bench.start(); | |
| 33 | 34 | for (let i = 0; i < n; i++) { | |
| 34 | - decoder.decode(buf); | ||
| 35 | + try { | ||
| 36 | + decoder.decode(buf); | ||
| 37 | + } catch { | ||
| 38 | + // eslint-disable no-empty | ||
| 39 | + } | ||
| 35 | 40 | } | |
| 36 | 41 | bench.end(n); | |
| 37 | 42 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,6 +29,7 @@ const kFlags = Symbol('flags'); | |||
| 29 | 29 | const kEncoding = Symbol('encoding'); | |
| 30 | 30 | const kDecoder = Symbol('decoder'); | |
| 31 | 31 | const kEncoder = Symbol('encoder'); | |
| 32 | + const kFatal = Symbol('kFatal'); | ||
| 32 | 33 | const kUTF8FastPath = Symbol('kUTF8FastPath'); | |
| 33 | 34 | const kIgnoreBOM = Symbol('kIgnoreBOM'); | |
| 34 | 35 | ||
@@ -401,17 +402,16 @@ function makeTextDecoderICU() { | |||
| 401 | 402 | flags |= options.ignoreBOM ? CONVERTER_FLAGS_IGNORE_BOM : 0; | |
| 402 | 403 | } | |
| 403 | 404 | ||
| 404 | - // Only support fast path for UTF-8 without FATAL flag | ||
| 405 | - const fastPathAvailable = enc === 'utf-8' && !(options?.fatal); | ||
| 406 | - | ||
| 407 | 405 | this[kDecoder] = true; | |
| 408 | 406 | this[kFlags] = flags; | |
| 409 | 407 | this[kEncoding] = enc; | |
| 410 | 408 | this[kIgnoreBOM] = Boolean(options?.ignoreBOM); | |
| 411 | - this[kUTF8FastPath] = fastPathAvailable; | ||
| 409 | + this[kFatal] = Boolean(options?.fatal); | ||
| 410 | + // Only support fast path for UTF-8. | ||
| 411 | + this[kUTF8FastPath] = enc === 'utf-8'; | ||
| 412 | 412 | this[kHandle] = undefined; | |
| 413 | 413 | ||
| 414 | - if (!fastPathAvailable) { | ||
| 414 | + if (!this[kUTF8FastPath]) { | ||
| 415 | 415 | this.#prepareConverter(); | |
| 416 | 416 | } | |
| 417 | 417 | } | |
@@ -430,7 +430,7 @@ function makeTextDecoderICU() { | |||
| 430 | 430 | this[kUTF8FastPath] &&= !(options?.stream); | |
| 431 | 431 | ||
| 432 | 432 | if (this[kUTF8FastPath]) { | |
| 433 | - return decodeUTF8(input, this[kIgnoreBOM]); | ||
| 433 | + return decodeUTF8(input, this[kIgnoreBOM], this[kFatal]); | ||
| 434 | 434 | } | |
| 435 | 435 | ||
| 436 | 436 | this.#prepareConverter(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -28,6 +28,7 @@ | |||
| 28 | 28 | #include "node_internals.h" | |
| 29 | 29 | ||
| 30 | 30 | #include "env-inl.h" | |
| 31 | + #include "simdutf.h" | ||
| 31 | 32 | #include "string_bytes.h" | |
| 32 | 33 | #include "string_search.h" | |
| 33 | 34 | #include "util-inl.h" | |
@@ -583,10 +584,20 @@ void DecodeUTF8(const FunctionCallbackInfo<Value>& args) { | |||
| 583 | 584 | ArrayBufferViewContents<char> buffer(args[0]); | |
| 584 | 585 | ||
| 585 | 586 | bool ignore_bom = args[1]->IsTrue(); | |
| 587 | + bool has_fatal = args[2]->IsTrue(); | ||
| 586 | 588 | ||
| 587 | 589 | const char* data = buffer.data(); | |
| 588 | 590 | size_t length = buffer.length(); | |
| 589 | 591 | ||
| 592 | + if (has_fatal) { | ||
| 593 | + auto result = simdutf::validate_utf8_with_errors(data, length); | ||
| 594 | + | ||
| 595 | + if (result.error) { | ||
| 596 | + return node::THROW_ERR_ENCODING_INVALID_ENCODED_DATA( | ||
| 597 | + env->isolate(), "The encoded data was not valid for encoding utf-8"); | ||
| 598 | + } | ||
| 599 | + } | ||
| 600 | + | ||
| 590 | 601 | if (!ignore_bom && length >= 3) { | |
| 591 | 602 | if (memcmp(data, "\xEF\xBB\xBF", 3) == 0) { | |
| 592 | 603 | data += 3; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments