Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: refactor macro to std::min in node_buffer.cc #25919

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
#include <string.h>
#include <limits.h>

#define MIN(a, b) ((a) < (b) ? (a) : (b))

#define THROW_AND_RETURN_UNLESS_BUFFER(env, obj) \
THROW_AND_RETURN_IF_NOT_BUFFER(env, obj, "argument") \

Expand Down Expand Up @@ -518,9 +516,9 @@ void Copy(const FunctionCallbackInfo<Value> &args) {
if (source_end - source_start > target_length - target_start)
source_end = source_start + target_length - target_start;

uint32_t to_copy = MIN(MIN(source_end - source_start,
target_length - target_start),
ts_obj_length - source_start);
uint32_t to_copy = std::min(
std::min(source_end - source_start, target_length - target_start),
ts_obj_length - source_start);

memmove(target_data + target_start, ts_obj_data + source_start, to_copy);
args.GetReturnValue().Set(to_copy);
Expand Down Expand Up @@ -551,7 +549,8 @@ void Fill(const FunctionCallbackInfo<Value>& args) {
if (Buffer::HasInstance(args[1])) {
SPREAD_BUFFER_ARG(args[1], fill_obj);
str_length = fill_obj_length;
memcpy(ts_obj_data + start, fill_obj_data, MIN(str_length, fill_length));
memcpy(
ts_obj_data + start, fill_obj_data, std::min(str_length, fill_length));
goto start_fill;
}

Expand All @@ -572,15 +571,15 @@ void Fill(const FunctionCallbackInfo<Value>& args) {
if (enc == UTF8) {
str_length = str_obj->Utf8Length(env->isolate());
node::Utf8Value str(env->isolate(), args[1]);
memcpy(ts_obj_data + start, *str, MIN(str_length, fill_length));
memcpy(ts_obj_data + start, *str, std::min(str_length, fill_length));

} else if (enc == UCS2) {
str_length = str_obj->Length() * sizeof(uint16_t);
node::TwoByteValue str(env->isolate(), args[1]);
if (IsBigEndian())
SwapBytes16(reinterpret_cast<char*>(&str[0]), str_length);

memcpy(ts_obj_data + start, *str, MIN(str_length, fill_length));
memcpy(ts_obj_data + start, *str, std::min(str_length, fill_length));

} else {
// Write initial String to Buffer, then use that memory to copy remainder
Expand Down Expand Up @@ -645,7 +644,7 @@ void StringWrite(const FunctionCallbackInfo<Value>& args) {
THROW_AND_RETURN_IF_OOB(ParseArrayIndex(env, args[2], ts_obj_length - offset,
&max_length));

max_length = MIN(ts_obj_length - offset, max_length);
max_length = std::min(ts_obj_length - offset, max_length);

if (max_length == 0)
return args.GetReturnValue().Set(0);
Expand Down Expand Up @@ -714,9 +713,9 @@ void CompareOffset(const FunctionCallbackInfo<Value> &args) {
CHECK_LE(source_start, source_end);
CHECK_LE(target_start, target_end);

size_t to_cmp = MIN(MIN(source_end - source_start,
target_end - target_start),
ts_obj_length - source_start);
size_t to_cmp =
std::min(std::min(source_end - source_start, target_end - target_start),
ts_obj_length - source_start);

int val = normalizeCompareVal(to_cmp > 0 ?
memcmp(ts_obj_data + source_start,
Expand All @@ -736,7 +735,7 @@ void Compare(const FunctionCallbackInfo<Value> &args) {
SPREAD_BUFFER_ARG(args[0], obj_a);
SPREAD_BUFFER_ARG(args[1], obj_b);

size_t cmp_length = MIN(obj_a_length, obj_b_length);
size_t cmp_length = std::min(obj_a_length, obj_b_length);

int val = normalizeCompareVal(cmp_length > 0 ?
memcmp(obj_a_data, obj_b_data, cmp_length) : 0,
Expand Down