Skip to content

Commit

Permalink
src: simplify size() == 0 checks
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Jun 13, 2024
1 parent 1839eb2 commit c4219f0
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/blob_serializer_deserializer-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/crypto/crypto_aes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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>(),
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/crypto_ec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +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)
if (args0.empty())
return args.GetReturnValue().SetEmptyString();

node::Utf8Value curve(env->isolate(), args[1]);
Expand Down
6 changes: 3 additions & 3 deletions src/crypto/crypto_spkac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +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)
if (input.empty())
return args.GetReturnValue().SetEmptyString();

if (UNLIKELY(!input.CheckSizeInt32()))
Expand Down Expand Up @@ -76,7 +76,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");
Expand Down Expand Up @@ -109,7 +109,7 @@ void ExportChallenge(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

ArrayBufferOrViewContents<char> input(args[0]);
if (input.size() == 0)
if (input.empty())
return args.GetReturnValue().SetEmptyString();

if (UNLIKELY(!input.CheckSizeInt32()))
Expand Down
20 changes: 13 additions & 7 deletions src/crypto/crypto_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ class ByteSource {
}

// Returns the (allocated) size in bytes.
size_t size() const { return size_; }
constexpr size_t size() const noexcept { return size_; }

constexpr bool empty() const noexcept { return size_ == 0; }

// Finalizes the Builder and returns a read-only view that is optionally
// truncated.
Expand Down Expand Up @@ -269,7 +271,9 @@ class ByteSource {
return reinterpret_cast<const T*>(data_);
}

size_t size() const { return size_; }
constexpr size_t size() const noexcept { return size_; }

constexpr bool empty() const noexcept { return size_ == 0; }

operator bool() const { return data_ != nullptr; }

Expand Down Expand Up @@ -718,7 +722,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)
if (empty())
return &buf;
return reinterpret_cast<T*>(data_) + offset_;
}
Expand All @@ -727,12 +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)
if (empty())
return &buf;
return reinterpret_cast<T*>(data_) + offset_;
}

inline size_t size() const { return length_; }
inline constexpr size_t size() const noexcept { return length_; }

inline constexpr bool empty() const noexcept { 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.
Expand All @@ -743,14 +749,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;
Expand Down
2 changes: 1 addition & 1 deletion src/module_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
8 changes: 4 additions & 4 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/permission/fs_permission.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/permission/fs_permission.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit c4219f0

Please sign in to comment.