| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 99c29eb commit ceaa200
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -186,6 +186,8 @@ void BindingData::DecodeUTF8(const FunctionCallbackInfo<Value>& args) { | |||
| 186 | 186 | return node::THROW_ERR_ENCODING_INVALID_ENCODED_DATA( | |
| 187 | 187 | env->isolate(), "The encoded data was not valid for encoding utf-8"); | |
| 188 | 188 | } | |
| 189 | + | ||
| 190 | + // TODO(chalker): save on utf8 validity recheck in StringBytes::Encode() | ||
| 189 | 191 | } | |
| 190 | 192 | ||
| 191 | 193 | if (length == 0) return args.GetReturnValue().SetEmptyString(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -539,6 +539,25 @@ MaybeLocal<Value> StringBytes::Encode(Isolate* isolate, | |||
| 539 | 539 | return ExternOneByteString::NewFromCopy(isolate, buf, buflen); | |
| 540 | 540 | } | |
| 541 | 541 | ||
| 542 | + if (buflen >= 32 && simdutf::validate_utf8(buf, buflen)) { | ||
| 543 | + // We know that we are non-ASCII (and are unlikely Latin1), use 2-byte | ||
| 544 | + // In the most likely case of valid UTF-8, we can use this fast impl | ||
| 545 | + // For very short input, it is slower, so we limit min size | ||
| 546 | + size_t u16size = simdutf::utf16_length_from_utf8(buf, buflen); | ||
| 547 | + if (u16size > static_cast<size_t>(v8::String::kMaxLength)) { | ||
| 548 | + isolate->ThrowException(ERR_STRING_TOO_LONG(isolate)); | ||
| 549 | + return MaybeLocal<Value>(); | ||
| 550 | + } | ||
| 551 | + uint16_t* dst = node::UncheckedMalloc<uint16_t>(u16size); | ||
| 552 | + if (u16size != 0 && dst == nullptr) { | ||
| 553 | + THROW_ERR_MEMORY_ALLOCATION_FAILED(isolate); | ||
| 554 | + return MaybeLocal<Value>(); | ||
| 555 | + } | ||
| 556 | + size_t utf16len = simdutf::convert_valid_utf8_to_utf16( | ||
| 557 | + buf, buflen, reinterpret_cast<char16_t*>(dst)); | ||
| 558 | + return ExternTwoByteString::New(isolate, dst, utf16len); | ||
| 559 | + } | ||
| 560 | + | ||
| 542 | 561 | val = | |
| 543 | 562 | String::NewFromUtf8(isolate, buf, v8::NewStringType::kNormal, buflen); | |
| 544 | 563 | Local<String> str; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments