authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-09-25 11:24:33-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-09-25 11:28:58-07:00
log97bef50dc32e332839efe1a9c5cbf51364a431dc
treee984ca904e3091bd631e88ba46c89c435e1b974a
parent3411b5e4995db2b432a50842a665127e65ae1f1b

std.mem: rename all "index of" functions

Moving towards our function naming convention of having one word per concept and constructing function names out of concatenated concepts. In `std.mem` the concepts are: * "find" - return index of substring * "pos" - starting index parameter * "last" - search from the end * "linear" - simple for loop rather than fancy algo * "scalar" - substring is a single element

1 files changed, 119 insertions(+), 65 deletions(-)

lib/std/mem.zig+119-65
...@@ -806,9 +806,12 @@ fn eqlBytes(a: []const u8, b: []const u8) bool {...@@ -806,9 +806,12 @@ fn eqlBytes(a: []const u8, b: []const u8) bool {
806 return !Scan.isNotEqual(last_a_chunk, last_b_chunk);806 return !Scan.isNotEqual(last_a_chunk, last_b_chunk);
807}807}
808808
809/// Deprecated in favor of `findDiff`.
810pub const indexOfDiff = findDiff;
811
809/// Compares two slices and returns the index of the first inequality.812/// Compares two slices and returns the index of the first inequality.
810/// Returns null if the slices are equal.813/// Returns null if the slices are equal.
811pub fn indexOfDiff(comptime T: type, a: []const T, b: []const T) ?usize {814pub fn findDiff(comptime T: type, a: []const T, b: []const T) ?usize {
812 const shortest = @min(a.len, b.len);815 const shortest = @min(a.len, b.len);
813 if (a.ptr == b.ptr)816 if (a.ptr == b.ptr)
814 return if (a.len == b.len) null else shortest;817 return if (a.len == b.len) null else shortest;
...@@ -817,12 +820,12 @@ pub fn indexOfDiff(comptime T: type, a: []const T, b: []const T) ?usize {...@@ -817,12 +820,12 @@ pub fn indexOfDiff(comptime T: type, a: []const T, b: []const T) ?usize {
817 return if (a.len == b.len) null else shortest;820 return if (a.len == b.len) null else shortest;
818}821}
819822
820test indexOfDiff {823test findDiff {
821 try testing.expectEqual(indexOfDiff(u8, "one", "one"), null);824 try testing.expectEqual(findDiff(u8, "one", "one"), null);
822 try testing.expectEqual(indexOfDiff(u8, "one two", "one"), 3);825 try testing.expectEqual(findDiff(u8, "one two", "one"), 3);
823 try testing.expectEqual(indexOfDiff(u8, "one", "one two"), 3);826 try testing.expectEqual(findDiff(u8, "one", "one two"), 3);
824 try testing.expectEqual(indexOfDiff(u8, "one twx", "one two"), 6);827 try testing.expectEqual(findDiff(u8, "one twx", "one two"), 6);
825 try testing.expectEqual(indexOfDiff(u8, "xne", "one"), 0);828 try testing.expectEqual(findDiff(u8, "xne", "one"), 0);
826}829}
827830
828/// Takes a sentinel-terminated pointer and returns a slice preserving pointer attributes.831/// Takes a sentinel-terminated pointer and returns a slice preserving pointer attributes.
...@@ -1109,9 +1112,12 @@ test len {...@@ -1109,9 +1112,12 @@ test len {
1109 try testing.expect(len(c_ptr) == 2);1112 try testing.expect(len(c_ptr) == 2);
1110}1113}
11111114
1115/// Deprecated in favor of `findSentinel`.
1116pub const indexOfSentinel = findSentinel;
1117
1112/// Returns the index of the sentinel value in a sentinel-terminated pointer.1118/// Returns the index of the sentinel value in a sentinel-terminated pointer.
1113/// Linear search through memory until the sentinel is found.1119/// Linear search through memory until the sentinel is found.
1114pub fn indexOfSentinel(comptime T: type, comptime sentinel: T, p: [*:sentinel]const T) usize {1120pub fn findSentinel(comptime T: type, comptime sentinel: T, p: [*:sentinel]const T) usize {
1115 var i: usize = 0;1121 var i: usize = 0;
11161122
1117 if (use_vectors_for_comparison and1123 if (use_vectors_for_comparison and
...@@ -1262,13 +1268,19 @@ test trim {...@@ -1262,13 +1268,19 @@ test trim {
1262 try testing.expectEqualSlices(u8, "foo", trim(u8, "foo", " \n"));1268 try testing.expectEqualSlices(u8, "foo", trim(u8, "foo", " \n"));
1263}1269}
12641270
1271/// Deprecated in favor of `findScalar`.
1272pub const indexOfScalar = findScalar;
1273
1265/// Linear search for the index of a scalar value inside a slice.1274/// Linear search for the index of a scalar value inside a slice.
1266pub fn indexOfScalar(comptime T: type, slice: []const T, value: T) ?usize {1275pub fn findScalar(comptime T: type, slice: []const T, value: T) ?usize {
1267 return indexOfScalarPos(T, slice, 0, value);1276 return indexOfScalarPos(T, slice, 0, value);
1268}1277}
12691278
1279/// Deprecated in favor of `findScalarLast`.
1280pub const lastIndexOfScalar = findScalarLast;
1281
1270/// Linear search for the last index of a scalar value inside a slice.1282/// Linear search for the last index of a scalar value inside a slice.
1271pub fn lastIndexOfScalar(comptime T: type, slice: []const T, value: T) ?usize {1283pub fn findScalarLast(comptime T: type, slice: []const T, value: T) ?usize {
1272 var i: usize = slice.len;1284 var i: usize = slice.len;
1273 while (i != 0) {1285 while (i != 0) {
1274 i -= 1;1286 i -= 1;
...@@ -1277,9 +1289,12 @@ pub fn lastIndexOfScalar(comptime T: type, slice: []const T, value: T) ?usize {...@@ -1277,9 +1289,12 @@ pub fn lastIndexOfScalar(comptime T: type, slice: []const T, value: T) ?usize {
1277 return null;1289 return null;
1278}1290}
12791291
1292/// Deprecated in favor of `findScalarPos`.
1293pub const indexOfScalarPos = findScalarPos;
1294
1280/// Linear search for the index of a scalar value inside a slice, starting from a given position.1295/// Linear search for the index of a scalar value inside a slice, starting from a given position.
1281/// Returns null if the value is not found.1296/// Returns null if the value is not found.
1282pub fn indexOfScalarPos(comptime T: type, slice: []const T, start_index: usize, value: T) ?usize {1297pub fn findScalarPos(comptime T: type, slice: []const T, start_index: usize, value: T) ?usize {
1283 if (start_index >= slice.len) return null;1298 if (start_index >= slice.len) return null;
12841299
1285 var i: usize = start_index;1300 var i: usize = start_index;
...@@ -1355,15 +1370,21 @@ test indexOfScalarPos {...@@ -1355,15 +1370,21 @@ test indexOfScalarPos {
1355 }1370 }
1356}1371}
13571372
1373/// Deprecated in favor of `findAny`.
1374pub const indexOfAny = findAny;
1375
1358/// Linear search for the index of any value in the provided list inside a slice.1376/// Linear search for the index of any value in the provided list inside a slice.
1359/// Returns null if no values are found.1377/// Returns null if no values are found.
1360pub fn indexOfAny(comptime T: type, slice: []const T, values: []const T) ?usize {1378pub fn findAny(comptime T: type, slice: []const T, values: []const T) ?usize {
1361 return indexOfAnyPos(T, slice, 0, values);1379 return indexOfAnyPos(T, slice, 0, values);
1362}1380}
13631381
1382/// Deprecated in favor of `findLastAny`.
1383pub const lastIndexOfAny = findLastAny;
1384
1364/// Linear search for the last index of any value in the provided list inside a slice.1385/// Linear search for the last index of any value in the provided list inside a slice.
1365/// Returns null if no values are found.1386/// Returns null if no values are found.
1366pub fn lastIndexOfAny(comptime T: type, slice: []const T, values: []const T) ?usize {1387pub fn findLastAny(comptime T: type, slice: []const T, values: []const T) ?usize {
1367 var i: usize = slice.len;1388 var i: usize = slice.len;
1368 while (i != 0) {1389 while (i != 0) {
1369 i -= 1;1390 i -= 1;
...@@ -1374,9 +1395,12 @@ pub fn lastIndexOfAny(comptime T: type, slice: []const T, values: []const T) ?us...@@ -1374,9 +1395,12 @@ pub fn lastIndexOfAny(comptime T: type, slice: []const T, values: []const T) ?us
1374 return null;1395 return null;
1375}1396}
13761397
1398/// Deprecated in favor of `findAnyPos`.
1399pub const indexOfAnyPos = findAnyPos;
1400
1377/// Linear search for the index of any value in the provided list inside a slice, starting from a given position.1401/// Linear search for the index of any value in the provided list inside a slice, starting from a given position.
1378/// Returns null if no values are found.1402/// Returns null if no values are found.
1379pub fn indexOfAnyPos(comptime T: type, slice: []const T, start_index: usize, values: []const T) ?usize {1403pub fn findAnyPos(comptime T: type, slice: []const T, start_index: usize, values: []const T) ?usize {
1380 if (start_index >= slice.len) return null;1404 if (start_index >= slice.len) return null;
1381 for (slice[start_index..], start_index..) |c, i| {1405 for (slice[start_index..], start_index..) |c, i| {
1382 for (values) |value| {1406 for (values) |value| {
...@@ -1386,17 +1410,34 @@ pub fn indexOfAnyPos(comptime T: type, slice: []const T, start_index: usize, val...@@ -1386,17 +1410,34 @@ pub fn indexOfAnyPos(comptime T: type, slice: []const T, start_index: usize, val
1386 return null;1410 return null;
1387}1411}
13881412
1413/// Deprecated in favor of `findNone`.
1414pub const indexOfNone = findNone;
1415
1389/// Find the first item in `slice` which is not contained in `values`.1416/// Find the first item in `slice` which is not contained in `values`.
1390///1417///
1391/// Comparable to `strspn` in the C standard library.1418/// Comparable to `strspn` in the C standard library.
1392pub fn indexOfNone(comptime T: type, slice: []const T, values: []const T) ?usize {1419pub fn findNone(comptime T: type, slice: []const T, values: []const T) ?usize {
1393 return indexOfNonePos(T, slice, 0, values);1420 return indexOfNonePos(T, slice, 0, values);
1394}1421}
13951422
1423test findNone {
1424 try testing.expect(findNone(u8, "abc123", "123").? == 0);
1425 try testing.expect(findLastNone(u8, "abc123", "123").? == 2);
1426 try testing.expect(findNone(u8, "123abc", "123").? == 3);
1427 try testing.expect(findLastNone(u8, "123abc", "123").? == 5);
1428 try testing.expect(findNone(u8, "123123", "123") == null);
1429 try testing.expect(findNone(u8, "333333", "123") == null);
1430
1431 try testing.expect(indexOfNonePos(u8, "abc123", 3, "321") == null);
1432}
1433
1434/// Deprecated in favor of `findLastNone`.
1435pub const lastIndexOfNone = findLastNone;
1436
1396/// Find the last item in `slice` which is not contained in `values`.1437/// Find the last item in `slice` which is not contained in `values`.
1397///1438///
1398/// Like `strspn` in the C standard library, but searches from the end.1439/// Like `strspn` in the C standard library, but searches from the end.
1399pub fn lastIndexOfNone(comptime T: type, slice: []const T, values: []const T) ?usize {1440pub fn findLastNone(comptime T: type, slice: []const T, values: []const T) ?usize {
1400 var i: usize = slice.len;1441 var i: usize = slice.len;
1401 outer: while (i != 0) {1442 outer: while (i != 0) {
1402 i -= 1;1443 i -= 1;
...@@ -1408,11 +1449,13 @@ pub fn lastIndexOfNone(comptime T: type, slice: []const T, values: []const T) ?u...@@ -1408,11 +1449,13 @@ pub fn lastIndexOfNone(comptime T: type, slice: []const T, values: []const T) ?u
1408 return null;1449 return null;
1409}1450}
14101451
1452pub const indexOfNonePos = findNonePos;
1453
1411/// Find the first item in `slice[start_index..]` which is not contained in `values`.1454/// Find the first item in `slice[start_index..]` which is not contained in `values`.
1412/// The returned index will be relative to the start of `slice`, and never less than `start_index`.1455/// The returned index will be relative to the start of `slice`, and never less than `start_index`.
1413///1456///
1414/// Comparable to `strspn` in the C standard library.1457/// Comparable to `strspn` in the C standard library.
1415pub fn indexOfNonePos(comptime T: type, slice: []const T, start_index: usize, values: []const T) ?usize {1458pub fn findNonePos(comptime T: type, slice: []const T, start_index: usize, values: []const T) ?usize {
1416 if (start_index >= slice.len) return null;1459 if (start_index >= slice.len) return null;
1417 outer: for (slice[start_index..], start_index..) |c, i| {1460 outer: for (slice[start_index..], start_index..) |c, i| {
1418 for (values) |value| {1461 for (values) |value| {
...@@ -1423,29 +1466,24 @@ pub fn indexOfNonePos(comptime T: type, slice: []const T, start_index: usize, va...@@ -1423,29 +1466,24 @@ pub fn indexOfNonePos(comptime T: type, slice: []const T, start_index: usize, va
1423 return null;1466 return null;
1424}1467}
14251468
1426test indexOfNone {1469/// Deprecated in favor of `find`.
1427 try testing.expect(indexOfNone(u8, "abc123", "123").? == 0);1470pub const indexOf = find;
1428 try testing.expect(lastIndexOfNone(u8, "abc123", "123").? == 2);
1429 try testing.expect(indexOfNone(u8, "123abc", "123").? == 3);
1430 try testing.expect(lastIndexOfNone(u8, "123abc", "123").? == 5);
1431 try testing.expect(indexOfNone(u8, "123123", "123") == null);
1432 try testing.expect(indexOfNone(u8, "333333", "123") == null);
1433
1434 try testing.expect(indexOfNonePos(u8, "abc123", 3, "321") == null);
1435}
14361471
1437/// Search for needle in haystack and return the index of the first occurrence.1472/// Search for needle in haystack and return the index of the first occurrence.
1438/// Uses Boyer-Moore-Horspool algorithm on large inputs; linear search on small inputs.1473/// Uses Boyer-Moore-Horspool algorithm on large inputs; linear search on small inputs.
1439/// Returns null if needle is not found.1474/// Returns null if needle is not found.
1440pub fn indexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize {1475pub fn find(comptime T: type, haystack: []const T, needle: []const T) ?usize {
1441 return indexOfPos(T, haystack, 0, needle);1476 return indexOfPos(T, haystack, 0, needle);
1442}1477}
14431478
1479/// Deprecated in favor of `findLastLinear`.
1480pub const lastIndexOfLinear = findLastLinear;
1481
1444/// Find the index in a slice of a sub-slice, searching from the end backwards.1482/// Find the index in a slice of a sub-slice, searching from the end backwards.
1445/// To start looking at a different index, slice the haystack first.1483/// To start looking at a different index, slice the haystack first.
1446/// Consider using `lastIndexOf` instead of this, which will automatically use a1484/// Consider using `lastIndexOf` instead of this, which will automatically use a
1447/// more sophisticated algorithm on larger inputs.1485/// more sophisticated algorithm on larger inputs.
1448pub fn lastIndexOfLinear(comptime T: type, haystack: []const T, needle: []const T) ?usize {1486pub fn findLastLinear(comptime T: type, haystack: []const T, needle: []const T) ?usize {
1449 if (needle.len > haystack.len) return null;1487 if (needle.len > haystack.len) return null;
1450 var i: usize = haystack.len - needle.len;1488 var i: usize = haystack.len - needle.len;
1451 while (true) : (i -= 1) {1489 while (true) : (i -= 1) {
...@@ -1454,9 +1492,11 @@ pub fn lastIndexOfLinear(comptime T: type, haystack: []const T, needle: []const...@@ -1454,9 +1492,11 @@ pub fn lastIndexOfLinear(comptime T: type, haystack: []const T, needle: []const
1454 }1492 }
1455}1493}
14561494
1495pub const indexOfPosLinear = findPosLinear;
1496
1457/// Consider using `indexOfPos` instead of this, which will automatically use a1497/// Consider using `indexOfPos` instead of this, which will automatically use a
1458/// more sophisticated algorithm on larger inputs.1498/// more sophisticated algorithm on larger inputs.
1459pub fn indexOfPosLinear(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize {1499pub fn findPosLinear(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize {
1460 if (needle.len > haystack.len) return null;1500 if (needle.len > haystack.len) return null;
1461 var i: usize = start_index;1501 var i: usize = start_index;
1462 const end = haystack.len - needle.len;1502 const end = haystack.len - needle.len;
...@@ -1466,24 +1506,24 @@ pub fn indexOfPosLinear(comptime T: type, haystack: []const T, start_index: usiz...@@ -1466,24 +1506,24 @@ pub fn indexOfPosLinear(comptime T: type, haystack: []const T, start_index: usiz
1466 return null;1506 return null;
1467}1507}
14681508
1469test indexOfPosLinear {1509test findPosLinear {
1470 try testing.expectEqual(0, indexOfPosLinear(u8, "", 0, ""));1510 try testing.expectEqual(0, findPosLinear(u8, "", 0, ""));
1471 try testing.expectEqual(0, indexOfPosLinear(u8, "123", 0, ""));1511 try testing.expectEqual(0, findPosLinear(u8, "123", 0, ""));
14721512
1473 try testing.expectEqual(null, indexOfPosLinear(u8, "", 0, "1"));1513 try testing.expectEqual(null, findPosLinear(u8, "", 0, "1"));
1474 try testing.expectEqual(0, indexOfPosLinear(u8, "1", 0, "1"));1514 try testing.expectEqual(0, findPosLinear(u8, "1", 0, "1"));
1475 try testing.expectEqual(null, indexOfPosLinear(u8, "2", 0, "1"));1515 try testing.expectEqual(null, findPosLinear(u8, "2", 0, "1"));
1476 try testing.expectEqual(1, indexOfPosLinear(u8, "21", 0, "1"));1516 try testing.expectEqual(1, findPosLinear(u8, "21", 0, "1"));
1477 try testing.expectEqual(null, indexOfPosLinear(u8, "222", 0, "1"));1517 try testing.expectEqual(null, findPosLinear(u8, "222", 0, "1"));
14781518
1479 try testing.expectEqual(null, indexOfPosLinear(u8, "", 0, "12"));1519 try testing.expectEqual(null, findPosLinear(u8, "", 0, "12"));
1480 try testing.expectEqual(null, indexOfPosLinear(u8, "1", 0, "12"));1520 try testing.expectEqual(null, findPosLinear(u8, "1", 0, "12"));
1481 try testing.expectEqual(null, indexOfPosLinear(u8, "2", 0, "12"));1521 try testing.expectEqual(null, findPosLinear(u8, "2", 0, "12"));
1482 try testing.expectEqual(0, indexOfPosLinear(u8, "12", 0, "12"));1522 try testing.expectEqual(0, findPosLinear(u8, "12", 0, "12"));
1483 try testing.expectEqual(null, indexOfPosLinear(u8, "21", 0, "12"));1523 try testing.expectEqual(null, findPosLinear(u8, "21", 0, "12"));
1484 try testing.expectEqual(1, indexOfPosLinear(u8, "212", 0, "12"));1524 try testing.expectEqual(1, findPosLinear(u8, "212", 0, "12"));
1485 try testing.expectEqual(0, indexOfPosLinear(u8, "122", 0, "12"));1525 try testing.expectEqual(0, findPosLinear(u8, "122", 0, "12"));
1486 try testing.expectEqual(1, indexOfPosLinear(u8, "212112", 0, "12"));1526 try testing.expectEqual(1, findPosLinear(u8, "212112", 0, "12"));
1487}1527}
14881528
1489fn boyerMooreHorspoolPreprocessReverse(pattern: []const u8, table: *[256]usize) void {1529fn boyerMooreHorspoolPreprocessReverse(pattern: []const u8, table: *[256]usize) void {
...@@ -1512,11 +1552,14 @@ fn boyerMooreHorspoolPreprocess(pattern: []const u8, table: *[256]usize) void {...@@ -1512,11 +1552,14 @@ fn boyerMooreHorspoolPreprocess(pattern: []const u8, table: *[256]usize) void {
1512 }1552 }
1513}1553}
15141554
1555/// Deprecated in favor of `find`.
1556pub const lastIndexOf = findLast;
1557
1515/// Find the index in a slice of a sub-slice, searching from the end backwards.1558/// Find the index in a slice of a sub-slice, searching from the end backwards.
1516/// To start looking at a different index, slice the haystack first.1559/// To start looking at a different index, slice the haystack first.
1517/// Uses the Reverse Boyer-Moore-Horspool algorithm on large inputs;1560/// Uses the Reverse Boyer-Moore-Horspool algorithm on large inputs;
1518/// `lastIndexOfLinear` on small inputs.1561/// `lastIndexOfLinear` on small inputs.
1519pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize {1562pub fn findLast(comptime T: type, haystack: []const T, needle: []const T) ?usize {
1520 if (needle.len > haystack.len) return null;1563 if (needle.len > haystack.len) return null;
1521 if (needle.len == 0) return haystack.len;1564 if (needle.len == 0) return haystack.len;
15221565
...@@ -1542,8 +1585,11 @@ pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?us...@@ -1542,8 +1585,11 @@ pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?us
1542 return null;1585 return null;
1543}1586}
15441587
1588/// Deprecated in favor of `findPos`.
1589pub const indexOfPos = findPos;
1590
1545/// Uses Boyer-Moore-Horspool algorithm on large inputs; `indexOfPosLinear` on small inputs.1591/// Uses Boyer-Moore-Horspool algorithm on large inputs; `indexOfPosLinear` on small inputs.
1546pub fn indexOfPos(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize {1592pub fn findPos(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize {
1547 if (needle.len > haystack.len) return null;1593 if (needle.len > haystack.len) return null;
1548 if (needle.len < 2) {1594 if (needle.len < 2) {
1549 if (needle.len == 0) return start_index;1595 if (needle.len == 0) return start_index;
...@@ -1593,7 +1639,7 @@ test indexOf {...@@ -1593,7 +1639,7 @@ test indexOf {
1593 try testing.expect(indexOf(u8, "foo foo", "foo").? == 0);1639 try testing.expect(indexOf(u8, "foo foo", "foo").? == 0);
1594 try testing.expect(lastIndexOf(u8, "foo foo", "foo").? == 4);1640 try testing.expect(lastIndexOf(u8, "foo foo", "foo").? == 4);
1595 try testing.expect(lastIndexOfAny(u8, "boo, cat", "abo").? == 6);1641 try testing.expect(lastIndexOfAny(u8, "boo, cat", "abo").? == 6);
1596 try testing.expect(lastIndexOfScalar(u8, "boo", 'o').? == 2);1642 try testing.expect(findScalarLast(u8, "boo", 'o').? == 2);
1597}1643}
15981644
1599test "indexOf multibyte" {1645test "indexOf multibyte" {
...@@ -3306,7 +3352,7 @@ pub fn SplitBackwardsIterator(comptime T: type, comptime delimiter_type: Delimit...@@ -3306,7 +3352,7 @@ pub fn SplitBackwardsIterator(comptime T: type, comptime delimiter_type: Delimit
3306 const start = if (switch (delimiter_type) {3352 const start = if (switch (delimiter_type) {
3307 .sequence => lastIndexOf(T, self.buffer[0..end], self.delimiter),3353 .sequence => lastIndexOf(T, self.buffer[0..end], self.delimiter),
3308 .any => lastIndexOfAny(T, self.buffer[0..end], self.delimiter),3354 .any => lastIndexOfAny(T, self.buffer[0..end], self.delimiter),
3309 .scalar => lastIndexOfScalar(T, self.buffer[0..end], self.delimiter),3355 .scalar => findScalarLast(T, self.buffer[0..end], self.delimiter),
3310 }) |delim_start| blk: {3356 }) |delim_start| blk: {
3311 self.index = delim_start;3357 self.index = delim_start;
3312 break :blk delim_start + switch (delimiter_type) {3358 break :blk delim_start + switch (delimiter_type) {
...@@ -3620,9 +3666,12 @@ test minMax {...@@ -3620,9 +3666,12 @@ test minMax {
3620 }3666 }
3621}3667}
36223668
3669/// Deprecated in favor of `findMin`.
3670pub const indexOfMin = findMin;
3671
3623/// Returns the index of the smallest number in a slice. O(n).3672/// Returns the index of the smallest number in a slice. O(n).
3624/// `slice` must not be empty.3673/// `slice` must not be empty.
3625pub fn indexOfMin(comptime T: type, slice: []const T) usize {3674pub fn findMin(comptime T: type, slice: []const T) usize {
3626 assert(slice.len > 0);3675 assert(slice.len > 0);
3627 var best = slice[0];3676 var best = slice[0];
3628 var index: usize = 0;3677 var index: usize = 0;
...@@ -3635,15 +3684,17 @@ pub fn indexOfMin(comptime T: type, slice: []const T) usize {...@@ -3635,15 +3684,17 @@ pub fn indexOfMin(comptime T: type, slice: []const T) usize {
3635 return index;3684 return index;
3636}3685}
36373686
3638test indexOfMin {3687test findMin {
3639 try testing.expectEqual(indexOfMin(u8, "abcdefg"), 0);3688 try testing.expectEqual(findMin(u8, "abcdefg"), 0);
3640 try testing.expectEqual(indexOfMin(u8, "bcdefga"), 6);3689 try testing.expectEqual(findMin(u8, "bcdefga"), 6);
3641 try testing.expectEqual(indexOfMin(u8, "a"), 0);3690 try testing.expectEqual(findMin(u8, "a"), 0);
3642}3691}
36433692
3693pub const indexOfMax = findMax;
3694
3644/// Returns the index of the largest number in a slice. O(n).3695/// Returns the index of the largest number in a slice. O(n).
3645/// `slice` must not be empty.3696/// `slice` must not be empty.
3646pub fn indexOfMax(comptime T: type, slice: []const T) usize {3697pub fn findMax(comptime T: type, slice: []const T) usize {
3647 assert(slice.len > 0);3698 assert(slice.len > 0);
3648 var best = slice[0];3699 var best = slice[0];
3649 var index: usize = 0;3700 var index: usize = 0;
...@@ -3656,16 +3707,19 @@ pub fn indexOfMax(comptime T: type, slice: []const T) usize {...@@ -3656,16 +3707,19 @@ pub fn indexOfMax(comptime T: type, slice: []const T) usize {
3656 return index;3707 return index;
3657}3708}
36583709
3659test indexOfMax {3710test findMax {
3660 try testing.expectEqual(indexOfMax(u8, "abcdefg"), 6);3711 try testing.expectEqual(findMax(u8, "abcdefg"), 6);
3661 try testing.expectEqual(indexOfMax(u8, "gabcdef"), 0);3712 try testing.expectEqual(findMax(u8, "gabcdef"), 0);
3662 try testing.expectEqual(indexOfMax(u8, "a"), 0);3713 try testing.expectEqual(findMax(u8, "a"), 0);
3663}3714}
36643715
3716/// Deprecated in favor of `findMinMax`.
3717pub const indexOfMinMax = findMinMax;
3718
3665/// Finds the indices of the smallest and largest number in a slice. O(n).3719/// Finds the indices of the smallest and largest number in a slice. O(n).
3666/// Returns the indices of the smallest and largest numbers in that order.3720/// Returns the indices of the smallest and largest numbers in that order.
3667/// `slice` must not be empty.3721/// `slice` must not be empty.
3668pub fn indexOfMinMax(comptime T: type, slice: []const T) struct { usize, usize } {3722pub fn findMinMax(comptime T: type, slice: []const T) struct { usize, usize } {
3669 assert(slice.len > 0);3723 assert(slice.len > 0);
3670 var minVal = slice[0];3724 var minVal = slice[0];
3671 var maxVal = slice[0];3725 var maxVal = slice[0];
...@@ -3684,10 +3738,10 @@ pub fn indexOfMinMax(comptime T: type, slice: []const T) struct { usize, usize }...@@ -3684,10 +3738,10 @@ pub fn indexOfMinMax(comptime T: type, slice: []const T) struct { usize, usize }
3684 return .{ minIdx, maxIdx };3738 return .{ minIdx, maxIdx };
3685}3739}
36863740
3687test indexOfMinMax {3741test findMinMax {
3688 try testing.expectEqual(.{ 0, 6 }, indexOfMinMax(u8, "abcdefg"));3742 try testing.expectEqual(.{ 0, 6 }, findMinMax(u8, "abcdefg"));
3689 try testing.expectEqual(.{ 1, 0 }, indexOfMinMax(u8, "gabcdef"));3743 try testing.expectEqual(.{ 1, 0 }, findMinMax(u8, "gabcdef"));
3690 try testing.expectEqual(.{ 0, 0 }, indexOfMinMax(u8, "a"));3744 try testing.expectEqual(.{ 0, 0 }, findMinMax(u8, "a"));
3691}3745}
36923746
3693/// Exchanges contents of two memory locations.3747/// Exchanges contents of two memory locations.