Skip to content

Commit fbf55b2

Browse files
authored
simplify
1 parent a71d5b6 commit fbf55b2

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

src/node_zlib.cc

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,19 +1660,21 @@ T CallOnSequence(v8::Isolate* isolate, Local<Value> value, F callback) {
16601660
}
16611661
}
16621662

1663+
static inline uint32_t CRC32Impl(Isolate* isolate,
1664+
Local<Value> data,
1665+
uint32_t value) {
1666+
return CallOnSequence<uint32_t>(
1667+
isolate, data, [&](const char* ptr, size_t size) -> uint32_t {
1668+
return static_cast<uint32_t>(
1669+
crc32(value, reinterpret_cast<const Bytef*>(ptr), size));
1670+
});
1671+
}
1672+
16631673
static void CRC32(const FunctionCallbackInfo<Value>& args) {
16641674
CHECK(args[0]->IsArrayBufferView() || args[0]->IsString());
16651675
CHECK(args[1]->IsUint32());
16661676
uint32_t value = args[1].As<v8::Uint32>()->Value();
1667-
1668-
uint32_t result = CallOnSequence<uint32_t>(
1669-
args.GetIsolate(),
1670-
args[0],
1671-
[&](const char* data, size_t size) -> uint32_t {
1672-
return crc32(value, reinterpret_cast<const Bytef*>(data), size);
1673-
});
1674-
1675-
args.GetReturnValue().Set(result);
1677+
args.GetReturnValue().Set(CRC32Impl(args.GetIsolate(), args[0], value));
16761678
}
16771679

16781680
static uint32_t FastCRC32(v8::Local<v8::Value> receiver,
@@ -1682,16 +1684,7 @@ static uint32_t FastCRC32(v8::Local<v8::Value> receiver,
16821684
v8::FastApiCallbackOptions& options) {
16831685
TRACK_V8_FAST_API_CALL("zlib.crc32");
16841686
v8::HandleScope handle_scope(options.isolate);
1685-
CHECK(data->IsArrayBufferView() || data->IsString());
1686-
if (data->IsArrayBufferView()) {
1687-
SPREAD_BUFFER_ARG(data, buf);
1688-
return static_cast<uint32_t>(
1689-
crc32(value, reinterpret_cast<const Bytef*>(buf_data), buf_length));
1690-
}
1691-
v8::Local<v8::String> s = data.As<v8::String>();
1692-
Utf8Value utf8(options.isolate, s);
1693-
return static_cast<uint32_t>(
1694-
crc32(value, reinterpret_cast<const Bytef*>(utf8.out()), utf8.length()));
1687+
return CRC32Impl(options.isolate, data, value);
16951688
}
16961689

16971690
static CFunction fast_crc32_(CFunction::Make(FastCRC32));

0 commit comments

Comments
 (0)