Skip to content

Commit

Permalink
src: refactor macro to std::min in node_buffer.cc
Browse files Browse the repository at this point in the history
PR-URL: #25919
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Refael Ackermann <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Сковорода Никита Андреевич <[email protected]>
  • Loading branch information
gengjiawen authored and targos committed Feb 10, 2019
1 parent 237b5e6 commit 779a577
Showing 1 changed file with 12 additions and 13 deletions.
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 @@ -516,9 +514,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 @@ -549,7 +547,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 @@ -570,15 +569,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 @@ -643,7 +642,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 @@ -712,9 +711,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 @@ -734,7 +733,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

0 comments on commit 779a577

Please sign in to comment.