Skip to content

Commit 1f8eaea

Browse files
committed
src: simplify size() == 0 checks
1 parent 1839eb2 commit 1f8eaea

File tree

9 files changed

+26
-25
lines changed

9 files changed

+26
-25
lines changed

src/blob_serializer_deserializer-inl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ size_t BlobSerializer<Impl>::WriteVector(const std::vector<T>& data) {
246246
}
247247

248248
size_t written_total = WriteArithmetic<size_t>(data.size());
249-
if (data.size() == 0) {
249+
if (data.empty()) {
250250
return written_total;
251251
}
252252

src/crypto/crypto_aes.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ WebCryptoCipherStatus AES_Cipher(
134134
//
135135
// Refs: https://github.com/openssl/openssl/commit/420cb707b880e4fb649094241371701013eeb15f
136136
// Refs: https://github.com/nodejs/node/pull/38913#issuecomment-866505244
137-
if (in.size() == 0) {
137+
if (in.empty()) {
138138
out_len = 0;
139139
} else if (!EVP_CipherUpdate(ctx.get(),
140140
buf.data<unsigned char>(),

src/crypto/crypto_ec.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,7 @@ void ECDH::ConvertKey(const FunctionCallbackInfo<Value>& args) {
398398
ArrayBufferOrViewContents<char> args0(args[0]);
399399
if (UNLIKELY(!args0.CheckSizeInt32()))
400400
return THROW_ERR_OUT_OF_RANGE(env, "key is too big");
401-
if (args0.size() == 0)
402-
return args.GetReturnValue().SetEmptyString();
401+
if (args0.empty()) return args.GetReturnValue().SetEmptyString();
403402

404403
node::Utf8Value curve(env->isolate(), args[1]);
405404

src/crypto/crypto_spkac.cc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ bool VerifySpkac(const ArrayBufferOrViewContents<char>& input) {
3939
void VerifySpkac(const FunctionCallbackInfo<Value>& args) {
4040
Environment* env = Environment::GetCurrent(args);
4141
ArrayBufferOrViewContents<char> input(args[0]);
42-
if (input.size() == 0)
43-
return args.GetReturnValue().SetEmptyString();
42+
if (input.empty()) return args.GetReturnValue().SetEmptyString();
4443

4544
if (UNLIKELY(!input.CheckSizeInt32()))
4645
return THROW_ERR_OUT_OF_RANGE(env, "spkac is too large");
@@ -76,7 +75,7 @@ void ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
7675
Environment* env = Environment::GetCurrent(args);
7776

7877
ArrayBufferOrViewContents<char> input(args[0]);
79-
if (input.size() == 0) return args.GetReturnValue().SetEmptyString();
78+
if (input.empty()) return args.GetReturnValue().SetEmptyString();
8079

8180
if (UNLIKELY(!input.CheckSizeInt32()))
8281
return THROW_ERR_OUT_OF_RANGE(env, "spkac is too large");
@@ -109,8 +108,7 @@ void ExportChallenge(const FunctionCallbackInfo<Value>& args) {
109108
Environment* env = Environment::GetCurrent(args);
110109

111110
ArrayBufferOrViewContents<char> input(args[0]);
112-
if (input.size() == 0)
113-
return args.GetReturnValue().SetEmptyString();
111+
if (input.empty()) return args.GetReturnValue().SetEmptyString();
114112

115113
if (UNLIKELY(!input.CheckSizeInt32()))
116114
return THROW_ERR_OUT_OF_RANGE(env, "spkac is too large");

src/crypto/crypto_util.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,9 @@ class ByteSource {
231231
}
232232

233233
// Returns the (allocated) size in bytes.
234-
size_t size() const { return size_; }
234+
constexpr size_t size() const noexcept { return size_; }
235+
236+
constexpr bool empty() const noexcept { return size_ == 0; }
235237

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

272-
size_t size() const { return size_; }
274+
constexpr size_t size() const noexcept { return size_; }
275+
276+
constexpr bool empty() const noexcept { return size_ == 0; }
273277

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

@@ -718,21 +722,21 @@ class ArrayBufferOrViewContents {
718722
// Ideally, these would return nullptr if IsEmpty() or length_ is zero,
719723
// but some of the openssl API react badly if given a nullptr even when
720724
// length is zero, so we have to return something.
721-
if (size() == 0)
722-
return &buf;
725+
if (empty()) return &buf;
723726
return reinterpret_cast<T*>(data_) + offset_;
724727
}
725728

726729
inline T* data() {
727730
// Ideally, these would return nullptr if IsEmpty() or length_ is zero,
728731
// but some of the openssl API react badly if given a nullptr even when
729732
// length is zero, so we have to return something.
730-
if (size() == 0)
731-
return &buf;
733+
if (empty()) return &buf;
732734
return reinterpret_cast<T*>(data_) + offset_;
733735
}
734736

735-
inline size_t size() const { return length_; }
737+
inline constexpr size_t size() const noexcept { return length_; }
738+
739+
inline constexpr bool empty() const noexcept { return length_ == 0; }
736740

737741
// In most cases, input buffer sizes passed in to openssl need to
738742
// be limited to <= INT_MAX. This utility method helps us check.
@@ -743,14 +747,14 @@ class ArrayBufferOrViewContents {
743747
}
744748

745749
inline ByteSource ToCopy() const {
746-
if (size() == 0) return ByteSource();
750+
if (empty()) return ByteSource();
747751
ByteSource::Builder buf(size());
748752
memcpy(buf.data<void>(), data(), size());
749753
return std::move(buf).release();
750754
}
751755

752756
inline ByteSource ToNullTerminatedCopy() const {
753-
if (size() == 0) return ByteSource();
757+
if (empty()) return ByteSource();
754758
ByteSource::Builder buf(size() + 1);
755759
memcpy(buf.data<void>(), data(), size());
756760
buf.data<char>()[size()] = 0;

src/module_wrap.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ v8::Maybe<bool> ModuleWrap::CheckUnsettledTopLevelAwait() {
125125

126126
auto stalled_messages =
127127
std::get<1>(module->GetStalledTopLevelAwaitMessages(isolate));
128-
if (stalled_messages.size() == 0) {
128+
if (stalled_messages.empty()) {
129129
return v8::Just(true);
130130
}
131131

src/node_file.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,7 +1580,7 @@ int MKDirpSync(uv_loop_t* loop,
15801580
// ~FSReqWrapSync():
15811581
case 0:
15821582
req_wrap->continuation_data()->MaybeSetFirstPath(next_path);
1583-
if (req_wrap->continuation_data()->paths().size() == 0) {
1583+
if (req_wrap->continuation_data()->paths().empty()) {
15841584
return 0;
15851585
}
15861586
break;
@@ -1596,7 +1596,7 @@ int MKDirpSync(uv_loop_t* loop,
15961596
if (dirname != next_path) {
15971597
req_wrap->continuation_data()->PushPath(std::move(next_path));
15981598
req_wrap->continuation_data()->PushPath(std::move(dirname));
1599-
} else if (req_wrap->continuation_data()->paths().size() == 0) {
1599+
} else if (req_wrap->continuation_data()->paths().empty()) {
16001600
err = UV_EEXIST;
16011601
continue;
16021602
}
@@ -1653,7 +1653,7 @@ int MKDirpAsync(uv_loop_t* loop,
16531653
// Note: uv_fs_req_cleanup in terminal paths will be called by
16541654
// FSReqAfterScope::~FSReqAfterScope()
16551655
case 0: {
1656-
if (req_wrap->continuation_data()->paths().size() == 0) {
1656+
if (req_wrap->continuation_data()->paths().empty()) {
16571657
req_wrap->continuation_data()->MaybeSetFirstPath(path);
16581658
req_wrap->continuation_data()->Done(0);
16591659
} else {
@@ -1676,7 +1676,7 @@ int MKDirpAsync(uv_loop_t* loop,
16761676
if (dirname != path) {
16771677
req_wrap->continuation_data()->PushPath(path);
16781678
req_wrap->continuation_data()->PushPath(std::move(dirname));
1679-
} else if (req_wrap->continuation_data()->paths().size() == 0) {
1679+
} else if (req_wrap->continuation_data()->paths().empty()) {
16801680
err = UV_EEXIST;
16811681
continue;
16821682
}

src/permission/fs_permission.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ FSPermission::RadixTree::~RadixTree() {
177177
bool FSPermission::RadixTree::Lookup(const std::string_view& s,
178178
bool when_empty_return) const {
179179
FSPermission::RadixTree::Node* current_node = root_node_;
180-
if (current_node->children.size() == 0) {
180+
if (current_node->children.empty()) {
181181
return when_empty_return;
182182
}
183183
size_t parent_node_prefix_len = current_node->prefix.length();

src/permission/fs_permission.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class FSPermission final : public PermissionBase {
129129
// ---> er
130130
// ---> n
131131
bool IsEndNode() const {
132-
if (children.size() == 0) {
132+
if (children.empty()) {
133133
return true;
134134
}
135135
return is_leaf;

0 commit comments

Comments
 (0)