| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9f5bf55 commit 49198d2
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -995,8 +995,8 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) { | |||
| 995 | 995 | if (!StringBytes::Size(isolate, needle, enc).To(&needle_length)) return; | |
| 996 | 996 | ||
| 997 | 997 | // search_end is the exclusive upper bound of the search range. | |
| 998 | - size_t search_end = static_cast<size_t>( | ||
| 999 | - std::min(end_i64, static_cast<int64_t>(haystack_length))); | ||
| 998 | + size_t search_end = static_cast<size_t>(std::min( | ||
| 999 | + std::max(end_i64, int64_t{0}), static_cast<int64_t>(haystack_length))); | ||
| 1000 | 1000 | if (enc == UCS2) search_end &= ~static_cast<size_t>(1); | |
| 1001 | 1001 | ||
| 1002 | 1002 | int64_t opt_offset = IndexOfOffset(haystack_length, | |
@@ -1005,8 +1005,10 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) { | |||
| 1005 | 1005 | is_forward); | |
| 1006 | 1006 | ||
| 1007 | 1007 | if (needle_length == 0) { | |
| 1008 | - // Match String#indexOf() and String#lastIndexOf() behavior. | ||
| 1009 | - args.GetReturnValue().Set(static_cast<double>(opt_offset)); | ||
| 1008 | + // Match String#indexOf() and String#lastIndexOf() behavior, | ||
| 1009 | + // but clamp to search_end. | ||
| 1010 | + int64_t clamped = std::min(opt_offset, static_cast<int64_t>(search_end)); | ||
| 1011 | + args.GetReturnValue().Set(static_cast<double>(clamped)); | ||
| 1010 | 1012 | return; | |
| 1011 | 1013 | } | |
| 1012 | 1014 | ||
@@ -1120,8 +1122,8 @@ void IndexOfBuffer(const FunctionCallbackInfo<Value>& args) { | |||
| 1120 | 1122 | const size_t needle_length = needle_contents.length(); | |
| 1121 | 1123 | ||
| 1122 | 1124 | // search_end is the exclusive upper bound of the search range. | |
| 1123 | - size_t search_end = static_cast<size_t>( | ||
| 1124 | - std::min(end_i64, static_cast<int64_t>(haystack_length))); | ||
| 1125 | + size_t search_end = static_cast<size_t>(std::min( | ||
| 1126 | + std::max(end_i64, int64_t{0}), static_cast<int64_t>(haystack_length))); | ||
| 1125 | 1127 | if (enc == UCS2) search_end &= ~static_cast<size_t>(1); | |
| 1126 | 1128 | ||
| 1127 | 1129 | int64_t opt_offset = IndexOfOffset(haystack_length, | |
@@ -1130,8 +1132,10 @@ void IndexOfBuffer(const FunctionCallbackInfo<Value>& args) { | |||
| 1130 | 1132 | is_forward); | |
| 1131 | 1133 | ||
| 1132 | 1134 | if (needle_length == 0) { | |
| 1133 | - // Match String#indexOf() and String#lastIndexOf() behavior. | ||
| 1134 | - args.GetReturnValue().Set(static_cast<double>(opt_offset)); | ||
| 1135 | + // Match String#indexOf() and String#lastIndexOf() behavior, | ||
| 1136 | + // but clamp to search_end. | ||
| 1137 | + int64_t clamped = std::min(opt_offset, static_cast<int64_t>(search_end)); | ||
| 1138 | + args.GetReturnValue().Set(static_cast<double>(clamped)); | ||
| 1135 | 1139 | return; | |
| 1136 | 1140 | } | |
| 1137 | 1141 | ||
@@ -1196,8 +1200,8 @@ int32_t IndexOfNumberImpl(Local<Value> buffer_obj, | |||
| 1196 | 1200 | } | |
| 1197 | 1201 | size_t offset = static_cast<size_t>(opt_offset); | |
| 1198 | 1202 | // search_end is the exclusive upper bound of the search range. | |
| 1199 | - size_t search_end = static_cast<size_t>( | ||
| 1200 | - std::min(end_i64, static_cast<int64_t>(buffer_length))); | ||
| 1203 | + size_t search_end = static_cast<size_t>(std::min( | ||
| 1204 | + std::max(end_i64, int64_t{0}), static_cast<int64_t>(buffer_length))); | ||
| 1201 | 1205 | ||
| 1202 | 1206 | const void* ptr; | |
| 1203 | 1207 | if (is_forward) { | |
@@ -1234,8 +1238,8 @@ int32_t FastIndexOfNumber(Local<Value>, | |||
| 1234 | 1238 | Local<Value> buffer_obj, | |
| 1235 | 1239 | uint32_t needle, | |
| 1236 | 1240 | int64_t offset_i64, | |
| 1237 | - int64_t end_i64, | ||
| 1238 | 1241 | bool is_forward, | |
| 1242 | + int64_t end_i64, | ||
| 1239 | 1243 | // NOLINTNEXTLINE(runtime/references) | |
| 1240 | 1244 | FastApiCallbackOptions& options) { | |
| 1241 | 1245 | HandleScope scope(options.isolate); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -691,3 +691,38 @@ assert.strictEqual(reallyLong.lastIndexOf(pattern), 0); | |||
| 691 | 691 | ||
| 692 | 692 | assert.strictEqual(buf.includes('c'), true); | |
| 693 | 693 | } | |
| 694 | + | ||
| 695 | + { | ||
| 696 | + const buf = Buffer.from('abcabc'); | ||
| 697 | + | ||
| 698 | + // Negative end should be treated as 0 (no match possible). | ||
| 699 | + assert.strictEqual(buf.indexOf('a', 0, -1), -1); | ||
| 700 | + assert.strictEqual(buf.indexOf('a', 0, -100), -1); | ||
| 701 | + assert.strictEqual(buf.indexOf(0x61, 0, -1), -1); | ||
| 702 | + assert.strictEqual(buf.lastIndexOf('a', 5, -1), -1); | ||
| 703 | + assert.strictEqual(buf.lastIndexOf(0x61, 5, -1), -1); | ||
| 704 | + assert.strictEqual(buf.includes('a', 0, -1), false); | ||
| 705 | + assert.strictEqual(buf.indexOf(Buffer.from('a'), 0, -1), -1); | ||
| 706 | + assert.strictEqual(buf.lastIndexOf(Buffer.from('a'), 5, -1), -1); | ||
| 707 | + | ||
| 708 | + // End = 0 means empty search range. | ||
| 709 | + assert.strictEqual(buf.indexOf('a', 0, 0), -1); | ||
| 710 | + assert.strictEqual(buf.indexOf(0x61, 0, 0), -1); | ||
| 711 | + assert.strictEqual(buf.lastIndexOf('a', 5, 0), -1); | ||
| 712 | + assert.strictEqual(buf.lastIndexOf(0x61, 5, 0), -1); | ||
| 713 | + | ||
| 714 | + // End greater than buffer length should be clamped. | ||
| 715 | + assert.strictEqual(buf.indexOf('c', 0, 100), 2); | ||
| 716 | + assert.strictEqual(buf.indexOf(0x63, 0, 100), 2); | ||
| 717 | + assert.strictEqual(buf.lastIndexOf('c', 5, 100), 5); | ||
| 718 | + assert.strictEqual(buf.lastIndexOf(0x63, 5, 100), 5); | ||
| 719 | + assert.strictEqual(buf.indexOf(Buffer.from('c'), 0, 100), 2); | ||
| 720 | + | ||
| 721 | + // Empty needle with end parameter should clamp to search_end. | ||
| 722 | + assert.strictEqual(buf.indexOf('', 0, 3), 0); | ||
| 723 | + assert.strictEqual(buf.indexOf('', 5, 3), 3); | ||
| 724 | + assert.strictEqual(buf.indexOf(Buffer.from(''), 5, 3), 3); | ||
| 725 | + assert.strictEqual(buf.indexOf('', 0, 0), 0); | ||
| 726 | + assert.strictEqual(buf.lastIndexOf('', 5, 3), 3); | ||
| 727 | + assert.strictEqual(buf.lastIndexOf(Buffer.from(''), 5, 3), 3); | ||
| 728 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments