mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
src: simplify size() == 0 checks
PR-URL: https://github.com/nodejs/node/pull/53440 Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Richard Lau <rlau@redhat.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
This commit is contained in:
@@ -247,7 +247,7 @@ size_t BlobSerializer<Impl>::WriteVector(const std::vector<T>& data) {
|
||||
}
|
||||
|
||||
size_t written_total = WriteArithmetic<size_t>(data.size());
|
||||
if (data.size() == 0) {
|
||||
if (data.empty()) {
|
||||
return written_total;
|
||||
}
|
||||
|
||||
|
||||
@@ -134,7 +134,7 @@ WebCryptoCipherStatus AES_Cipher(
|
||||
//
|
||||
// Refs: https://github.com/openssl/openssl/commit/420cb707b880e4fb649094241371701013eeb15f
|
||||
// Refs: https://github.com/nodejs/node/pull/38913#issuecomment-866505244
|
||||
if (in.size() == 0) {
|
||||
if (in.empty()) {
|
||||
out_len = 0;
|
||||
} else if (!EVP_CipherUpdate(ctx.get(),
|
||||
buf.data<unsigned char>(),
|
||||
|
||||
@@ -398,8 +398,7 @@ void ECDH::ConvertKey(const FunctionCallbackInfo<Value>& args) {
|
||||
ArrayBufferOrViewContents<char> args0(args[0]);
|
||||
if (UNLIKELY(!args0.CheckSizeInt32()))
|
||||
return THROW_ERR_OUT_OF_RANGE(env, "key is too big");
|
||||
if (args0.size() == 0)
|
||||
return args.GetReturnValue().SetEmptyString();
|
||||
if (args0.empty()) return args.GetReturnValue().SetEmptyString();
|
||||
|
||||
node::Utf8Value curve(env->isolate(), args[1]);
|
||||
|
||||
|
||||
@@ -39,8 +39,7 @@ bool VerifySpkac(const ArrayBufferOrViewContents<char>& input) {
|
||||
void VerifySpkac(const FunctionCallbackInfo<Value>& args) {
|
||||
Environment* env = Environment::GetCurrent(args);
|
||||
ArrayBufferOrViewContents<char> input(args[0]);
|
||||
if (input.size() == 0)
|
||||
return args.GetReturnValue().SetEmptyString();
|
||||
if (input.empty()) return args.GetReturnValue().SetEmptyString();
|
||||
|
||||
if (UNLIKELY(!input.CheckSizeInt32()))
|
||||
return THROW_ERR_OUT_OF_RANGE(env, "spkac is too large");
|
||||
@@ -76,7 +75,7 @@ void ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
|
||||
Environment* env = Environment::GetCurrent(args);
|
||||
|
||||
ArrayBufferOrViewContents<char> input(args[0]);
|
||||
if (input.size() == 0) return args.GetReturnValue().SetEmptyString();
|
||||
if (input.empty()) return args.GetReturnValue().SetEmptyString();
|
||||
|
||||
if (UNLIKELY(!input.CheckSizeInt32()))
|
||||
return THROW_ERR_OUT_OF_RANGE(env, "spkac is too large");
|
||||
@@ -109,8 +108,7 @@ void ExportChallenge(const FunctionCallbackInfo<Value>& args) {
|
||||
Environment* env = Environment::GetCurrent(args);
|
||||
|
||||
ArrayBufferOrViewContents<char> input(args[0]);
|
||||
if (input.size() == 0)
|
||||
return args.GetReturnValue().SetEmptyString();
|
||||
if (input.empty()) return args.GetReturnValue().SetEmptyString();
|
||||
|
||||
if (UNLIKELY(!input.CheckSizeInt32()))
|
||||
return THROW_ERR_OUT_OF_RANGE(env, "spkac is too large");
|
||||
|
||||
@@ -233,6 +233,9 @@ class ByteSource {
|
||||
// Returns the (allocated) size in bytes.
|
||||
size_t size() const { return size_; }
|
||||
|
||||
// Returns if (allocated) size is zero.
|
||||
bool empty() const { return size_ == 0; }
|
||||
|
||||
// Finalizes the Builder and returns a read-only view that is optionally
|
||||
// truncated.
|
||||
ByteSource release(std::optional<size_t> resize = std::nullopt) && {
|
||||
@@ -271,6 +274,8 @@ class ByteSource {
|
||||
|
||||
size_t size() const { return size_; }
|
||||
|
||||
bool empty() const { return size_ == 0; }
|
||||
|
||||
operator bool() const { return data_ != nullptr; }
|
||||
|
||||
BignumPointer ToBN() const {
|
||||
@@ -718,8 +723,7 @@ class ArrayBufferOrViewContents {
|
||||
// Ideally, these would return nullptr if IsEmpty() or length_ is zero,
|
||||
// but some of the openssl API react badly if given a nullptr even when
|
||||
// length is zero, so we have to return something.
|
||||
if (size() == 0)
|
||||
return &buf;
|
||||
if (empty()) return &buf;
|
||||
return reinterpret_cast<T*>(data_) + offset_;
|
||||
}
|
||||
|
||||
@@ -727,13 +731,14 @@ class ArrayBufferOrViewContents {
|
||||
// Ideally, these would return nullptr if IsEmpty() or length_ is zero,
|
||||
// but some of the openssl API react badly if given a nullptr even when
|
||||
// length is zero, so we have to return something.
|
||||
if (size() == 0)
|
||||
return &buf;
|
||||
if (empty()) return &buf;
|
||||
return reinterpret_cast<T*>(data_) + offset_;
|
||||
}
|
||||
|
||||
inline size_t size() const { return length_; }
|
||||
|
||||
inline bool empty() const { return length_ == 0; }
|
||||
|
||||
// In most cases, input buffer sizes passed in to openssl need to
|
||||
// be limited to <= INT_MAX. This utility method helps us check.
|
||||
inline bool CheckSizeInt32() { return size() <= INT_MAX; }
|
||||
@@ -743,14 +748,14 @@ class ArrayBufferOrViewContents {
|
||||
}
|
||||
|
||||
inline ByteSource ToCopy() const {
|
||||
if (size() == 0) return ByteSource();
|
||||
if (empty()) return ByteSource();
|
||||
ByteSource::Builder buf(size());
|
||||
memcpy(buf.data<void>(), data(), size());
|
||||
return std::move(buf).release();
|
||||
}
|
||||
|
||||
inline ByteSource ToNullTerminatedCopy() const {
|
||||
if (size() == 0) return ByteSource();
|
||||
if (empty()) return ByteSource();
|
||||
ByteSource::Builder buf(size() + 1);
|
||||
memcpy(buf.data<void>(), data(), size());
|
||||
buf.data<char>()[size()] = 0;
|
||||
|
||||
@@ -125,7 +125,7 @@ v8::Maybe<bool> ModuleWrap::CheckUnsettledTopLevelAwait() {
|
||||
|
||||
auto stalled_messages =
|
||||
std::get<1>(module->GetStalledTopLevelAwaitMessages(isolate));
|
||||
if (stalled_messages.size() == 0) {
|
||||
if (stalled_messages.empty()) {
|
||||
return v8::Just(true);
|
||||
}
|
||||
|
||||
|
||||
@@ -1580,7 +1580,7 @@ int MKDirpSync(uv_loop_t* loop,
|
||||
// ~FSReqWrapSync():
|
||||
case 0:
|
||||
req_wrap->continuation_data()->MaybeSetFirstPath(next_path);
|
||||
if (req_wrap->continuation_data()->paths().size() == 0) {
|
||||
if (req_wrap->continuation_data()->paths().empty()) {
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
@@ -1596,7 +1596,7 @@ int MKDirpSync(uv_loop_t* loop,
|
||||
if (dirname != next_path) {
|
||||
req_wrap->continuation_data()->PushPath(std::move(next_path));
|
||||
req_wrap->continuation_data()->PushPath(std::move(dirname));
|
||||
} else if (req_wrap->continuation_data()->paths().size() == 0) {
|
||||
} else if (req_wrap->continuation_data()->paths().empty()) {
|
||||
err = UV_EEXIST;
|
||||
continue;
|
||||
}
|
||||
@@ -1653,7 +1653,7 @@ int MKDirpAsync(uv_loop_t* loop,
|
||||
// Note: uv_fs_req_cleanup in terminal paths will be called by
|
||||
// FSReqAfterScope::~FSReqAfterScope()
|
||||
case 0: {
|
||||
if (req_wrap->continuation_data()->paths().size() == 0) {
|
||||
if (req_wrap->continuation_data()->paths().empty()) {
|
||||
req_wrap->continuation_data()->MaybeSetFirstPath(path);
|
||||
req_wrap->continuation_data()->Done(0);
|
||||
} else {
|
||||
@@ -1676,7 +1676,7 @@ int MKDirpAsync(uv_loop_t* loop,
|
||||
if (dirname != path) {
|
||||
req_wrap->continuation_data()->PushPath(path);
|
||||
req_wrap->continuation_data()->PushPath(std::move(dirname));
|
||||
} else if (req_wrap->continuation_data()->paths().size() == 0) {
|
||||
} else if (req_wrap->continuation_data()->paths().empty()) {
|
||||
err = UV_EEXIST;
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -177,7 +177,7 @@ FSPermission::RadixTree::~RadixTree() {
|
||||
bool FSPermission::RadixTree::Lookup(const std::string_view& s,
|
||||
bool when_empty_return) const {
|
||||
FSPermission::RadixTree::Node* current_node = root_node_;
|
||||
if (current_node->children.size() == 0) {
|
||||
if (current_node->children.empty()) {
|
||||
return when_empty_return;
|
||||
}
|
||||
size_t parent_node_prefix_len = current_node->prefix.length();
|
||||
|
||||
@@ -129,7 +129,7 @@ class FSPermission final : public PermissionBase {
|
||||
// ---> er
|
||||
// ---> n
|
||||
bool IsEndNode() const {
|
||||
if (children.size() == 0) {
|
||||
if (children.empty()) {
|
||||
return true;
|
||||
}
|
||||
return is_leaf;
|
||||
|
||||
Reference in New Issue
Block a user