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 {
806806 return !Scan.isNotEqual(last_a_chunk, last_b_chunk);
807807}
808808
809/// Deprecated in favor of `findDiff`.
810pub const indexOfDiff = findDiff;
811
809812/// Compares two slices and returns the index of the first inequality.
810813/// 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 {
812815 const shortest = @min(a.len, b.len);
813816 if (a.ptr == b.ptr)
814817 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 {
817820 return if (a.len == b.len) null else shortest;
818821}
819822
820test indexOfDiff {
821 try testing.expectEqual(indexOfDiff(u8, "one", "one"), null);
822 try testing.expectEqual(indexOfDiff(u8, "one two", "one"), 3);
823 try testing.expectEqual(indexOfDiff(u8, "one", "one two"), 3);
824 try testing.expectEqual(indexOfDiff(u8, "one twx", "one two"), 6);
825 try testing.expectEqual(indexOfDiff(u8, "xne", "one"), 0);
823test findDiff {
824 try testing.expectEqual(findDiff(u8, "one", "one"), null);
825 try testing.expectEqual(findDiff(u8, "one two", "one"), 3);
826 try testing.expectEqual(findDiff(u8, "one", "one two"), 3);
827 try testing.expectEqual(findDiff(u8, "one twx", "one two"), 6);
828 try testing.expectEqual(findDiff(u8, "xne", "one"), 0);
826829}
827830
828831/// Takes a sentinel-terminated pointer and returns a slice preserving pointer attributes.
......@@ -1109,9 +1112,12 @@ test len {
11091112 try testing.expect(len(c_ptr) == 2);
11101113}
11111114
1115/// Deprecated in favor of `findSentinel`.
1116pub const indexOfSentinel = findSentinel;
1117
11121118/// Returns the index of the sentinel value in a sentinel-terminated pointer.
11131119/// 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 {
11151121 var i: usize = 0;
11161122
11171123 if (use_vectors_for_comparison and
......@@ -1262,13 +1268,19 @@ test trim {
12621268 try testing.expectEqualSlices(u8, "foo", trim(u8, "foo", " \n"));
12631269}
12641270
1271/// Deprecated in favor of `findScalar`.
1272pub const indexOfScalar = findScalar;
1273
12651274/// 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 {
12671276 return indexOfScalarPos(T, slice, 0, value);
12681277}
12691278
1279/// Deprecated in favor of `findScalarLast`.
1280pub const lastIndexOfScalar = findScalarLast;
1281
12701282/// 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 {
12721284 var i: usize = slice.len;
12731285 while (i != 0) {
12741286 i -= 1;
......@@ -1277,9 +1289,12 @@ pub fn lastIndexOfScalar(comptime T: type, slice: []const T, value: T) ?usize {
12771289 return null;
12781290}
12791291
1292/// Deprecated in favor of `findScalarPos`.
1293pub const indexOfScalarPos = findScalarPos;
1294
12801295/// Linear search for the index of a scalar value inside a slice, starting from a given position.
12811296/// 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 {
12831298 if (start_index >= slice.len) return null;
12841299
12851300 var i: usize = start_index;
......@@ -1355,15 +1370,21 @@ test indexOfScalarPos {
13551370 }
13561371}
13571372
1373/// Deprecated in favor of `findAny`.
1374pub const indexOfAny = findAny;
1375
13581376/// Linear search for the index of any value in the provided list inside a slice.
13591377/// 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 {
13611379 return indexOfAnyPos(T, slice, 0, values);
13621380}
13631381
1382/// Deprecated in favor of `findLastAny`.
1383pub const lastIndexOfAny = findLastAny;
1384
13641385/// Linear search for the last index of any value in the provided list inside a slice.
13651386/// 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 {
13671388 var i: usize = slice.len;
13681389 while (i != 0) {
13691390 i -= 1;
......@@ -1374,9 +1395,12 @@ pub fn lastIndexOfAny(comptime T: type, slice: []const T, values: []const T) ?us
13741395 return null;
13751396}
13761397
1398/// Deprecated in favor of `findAnyPos`.
1399pub const indexOfAnyPos = findAnyPos;
1400
13771401/// Linear search for the index of any value in the provided list inside a slice, starting from a given position.
13781402/// 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 {
13801404 if (start_index >= slice.len) return null;
13811405 for (slice[start_index..], start_index..) |c, i| {
13821406 for (values) |value| {
......@@ -1386,17 +1410,34 @@ pub fn indexOfAnyPos(comptime T: type, slice: []const T, start_index: usize, val
13861410 return null;
13871411}
13881412
1413/// Deprecated in favor of `findNone`.
1414pub const indexOfNone = findNone;
1415
13891416/// Find the first item in `slice` which is not contained in `values`.
13901417///
13911418/// 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 {
13931420 return indexOfNonePos(T, slice, 0, values);
13941421}
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
13961437/// Find the last item in `slice` which is not contained in `values`.
13971438///
13981439/// 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 {
14001441 var i: usize = slice.len;
14011442 outer: while (i != 0) {
14021443 i -= 1;
......@@ -1408,11 +1449,13 @@ pub fn lastIndexOfNone(comptime T: type, slice: []const T, values: []const T) ?u
14081449 return null;
14091450}
14101451
1452pub const indexOfNonePos = findNonePos;
1453
14111454/// Find the first item in `slice[start_index..]` which is not contained in `values`.
14121455/// The returned index will be relative to the start of `slice`, and never less than `start_index`.
14131456///
14141457/// 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 {
14161459 if (start_index >= slice.len) return null;
14171460 outer: for (slice[start_index..], start_index..) |c, i| {
14181461 for (values) |value| {
......@@ -1423,29 +1466,24 @@ pub fn indexOfNonePos(comptime T: type, slice: []const T, start_index: usize, va
14231466 return null;
14241467}
14251468
1426test indexOfNone {
1427 try testing.expect(indexOfNone(u8, "abc123", "123").? == 0);
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}
1469/// Deprecated in favor of `find`.
1470pub const indexOf = find;
14361471
14371472/// Search for needle in haystack and return the index of the first occurrence.
14381473/// Uses Boyer-Moore-Horspool algorithm on large inputs; linear search on small inputs.
14391474/// 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 {
14411476 return indexOfPos(T, haystack, 0, needle);
14421477}
14431478
1479/// Deprecated in favor of `findLastLinear`.
1480pub const lastIndexOfLinear = findLastLinear;
1481
14441482/// Find the index in a slice of a sub-slice, searching from the end backwards.
14451483/// To start looking at a different index, slice the haystack first.
14461484/// Consider using `lastIndexOf` instead of this, which will automatically use a
14471485/// 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 {
14491487 if (needle.len > haystack.len) return null;
14501488 var i: usize = haystack.len - needle.len;
14511489 while (true) : (i -= 1) {
......@@ -1454,9 +1492,11 @@ pub fn lastIndexOfLinear(comptime T: type, haystack: []const T, needle: []const
14541492 }
14551493}
14561494
1495pub const indexOfPosLinear = findPosLinear;
1496
14571497/// Consider using `indexOfPos` instead of this, which will automatically use a
14581498/// 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 {
14601500 if (needle.len > haystack.len) return null;
14611501 var i: usize = start_index;
14621502 const end = haystack.len - needle.len;
......@@ -1466,24 +1506,24 @@ pub fn indexOfPosLinear(comptime T: type, haystack: []const T, start_index: usiz
14661506 return null;
14671507}
14681508
1469test indexOfPosLinear {
1470 try testing.expectEqual(0, indexOfPosLinear(u8, "", 0, ""));
1471 try testing.expectEqual(0, indexOfPosLinear(u8, "123", 0, ""));
1509test findPosLinear {
1510 try testing.expectEqual(0, findPosLinear(u8, "", 0, ""));
1511 try testing.expectEqual(0, findPosLinear(u8, "123", 0, ""));
14721512
1473 try testing.expectEqual(null, indexOfPosLinear(u8, "", 0, "1"));
1474 try testing.expectEqual(0, indexOfPosLinear(u8, "1", 0, "1"));
1475 try testing.expectEqual(null, indexOfPosLinear(u8, "2", 0, "1"));
1476 try testing.expectEqual(1, indexOfPosLinear(u8, "21", 0, "1"));
1477 try testing.expectEqual(null, indexOfPosLinear(u8, "222", 0, "1"));
1513 try testing.expectEqual(null, findPosLinear(u8, "", 0, "1"));
1514 try testing.expectEqual(0, findPosLinear(u8, "1", 0, "1"));
1515 try testing.expectEqual(null, findPosLinear(u8, "2", 0, "1"));
1516 try testing.expectEqual(1, findPosLinear(u8, "21", 0, "1"));
1517 try testing.expectEqual(null, findPosLinear(u8, "222", 0, "1"));
14781518
1479 try testing.expectEqual(null, indexOfPosLinear(u8, "", 0, "12"));
1480 try testing.expectEqual(null, indexOfPosLinear(u8, "1", 0, "12"));
1481 try testing.expectEqual(null, indexOfPosLinear(u8, "2", 0, "12"));
1482 try testing.expectEqual(0, indexOfPosLinear(u8, "12", 0, "12"));
1483 try testing.expectEqual(null, indexOfPosLinear(u8, "21", 0, "12"));
1484 try testing.expectEqual(1, indexOfPosLinear(u8, "212", 0, "12"));
1485 try testing.expectEqual(0, indexOfPosLinear(u8, "122", 0, "12"));
1486 try testing.expectEqual(1, indexOfPosLinear(u8, "212112", 0, "12"));
1519 try testing.expectEqual(null, findPosLinear(u8, "", 0, "12"));
1520 try testing.expectEqual(null, findPosLinear(u8, "1", 0, "12"));
1521 try testing.expectEqual(null, findPosLinear(u8, "2", 0, "12"));
1522 try testing.expectEqual(0, findPosLinear(u8, "12", 0, "12"));
1523 try testing.expectEqual(null, findPosLinear(u8, "21", 0, "12"));
1524 try testing.expectEqual(1, findPosLinear(u8, "212", 0, "12"));
1525 try testing.expectEqual(0, findPosLinear(u8, "122", 0, "12"));
1526 try testing.expectEqual(1, findPosLinear(u8, "212112", 0, "12"));
14871527}
14881528
14891529fn boyerMooreHorspoolPreprocessReverse(pattern: []const u8, table: *[256]usize) void {
......@@ -1512,11 +1552,14 @@ fn boyerMooreHorspoolPreprocess(pattern: []const u8, table: *[256]usize) void {
15121552 }
15131553}
15141554
1555/// Deprecated in favor of `find`.
1556pub const lastIndexOf = findLast;
1557
15151558/// Find the index in a slice of a sub-slice, searching from the end backwards.
15161559/// To start looking at a different index, slice the haystack first.
15171560/// Uses the Reverse Boyer-Moore-Horspool algorithm on large inputs;
15181561/// `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 {
15201563 if (needle.len > haystack.len) return null;
15211564 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
15421585 return null;
15431586}
15441587
1588/// Deprecated in favor of `findPos`.
1589pub const indexOfPos = findPos;
1590
15451591/// 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 {
15471593 if (needle.len > haystack.len) return null;
15481594 if (needle.len < 2) {
15491595 if (needle.len == 0) return start_index;
......@@ -1593,7 +1639,7 @@ test indexOf {
15931639 try testing.expect(indexOf(u8, "foo foo", "foo").? == 0);
15941640 try testing.expect(lastIndexOf(u8, "foo foo", "foo").? == 4);
15951641 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);
15971643}
15981644
15991645test "indexOf multibyte" {
......@@ -3306,7 +3352,7 @@ pub fn SplitBackwardsIterator(comptime T: type, comptime delimiter_type: Delimit
33063352 const start = if (switch (delimiter_type) {
33073353 .sequence => lastIndexOf(T, self.buffer[0..end], self.delimiter),
33083354 .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),
33103356 }) |delim_start| blk: {
33113357 self.index = delim_start;
33123358 break :blk delim_start + switch (delimiter_type) {
......@@ -3620,9 +3666,12 @@ test minMax {
36203666 }
36213667}
36223668
3669/// Deprecated in favor of `findMin`.
3670pub const indexOfMin = findMin;
3671
36233672/// Returns the index of the smallest number in a slice. O(n).
36243673/// `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 {
36263675 assert(slice.len > 0);
36273676 var best = slice[0];
36283677 var index: usize = 0;
......@@ -3635,15 +3684,17 @@ pub fn indexOfMin(comptime T: type, slice: []const T) usize {
36353684 return index;
36363685}
36373686
3638test indexOfMin {
3639 try testing.expectEqual(indexOfMin(u8, "abcdefg"), 0);
3640 try testing.expectEqual(indexOfMin(u8, "bcdefga"), 6);
3641 try testing.expectEqual(indexOfMin(u8, "a"), 0);
3687test findMin {
3688 try testing.expectEqual(findMin(u8, "abcdefg"), 0);
3689 try testing.expectEqual(findMin(u8, "bcdefga"), 6);
3690 try testing.expectEqual(findMin(u8, "a"), 0);
36423691}
36433692
3693pub const indexOfMax = findMax;
3694
36443695/// Returns the index of the largest number in a slice. O(n).
36453696/// `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 {
36473698 assert(slice.len > 0);
36483699 var best = slice[0];
36493700 var index: usize = 0;
......@@ -3656,16 +3707,19 @@ pub fn indexOfMax(comptime T: type, slice: []const T) usize {
36563707 return index;
36573708}
36583709
3659test indexOfMax {
3660 try testing.expectEqual(indexOfMax(u8, "abcdefg"), 6);
3661 try testing.expectEqual(indexOfMax(u8, "gabcdef"), 0);
3662 try testing.expectEqual(indexOfMax(u8, "a"), 0);
3710test findMax {
3711 try testing.expectEqual(findMax(u8, "abcdefg"), 6);
3712 try testing.expectEqual(findMax(u8, "gabcdef"), 0);
3713 try testing.expectEqual(findMax(u8, "a"), 0);
36633714}
36643715
3716/// Deprecated in favor of `findMinMax`.
3717pub const indexOfMinMax = findMinMax;
3718
36653719/// Finds the indices of the smallest and largest number in a slice. O(n).
36663720/// Returns the indices of the smallest and largest numbers in that order.
36673721/// `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 } {
36693723 assert(slice.len > 0);
36703724 var minVal = slice[0];
36713725 var maxVal = slice[0];
......@@ -3684,10 +3738,10 @@ pub fn indexOfMinMax(comptime T: type, slice: []const T) struct { usize, usize }
36843738 return .{ minIdx, maxIdx };
36853739}
36863740
3687test indexOfMinMax {
3688 try testing.expectEqual(.{ 0, 6 }, indexOfMinMax(u8, "abcdefg"));
3689 try testing.expectEqual(.{ 1, 0 }, indexOfMinMax(u8, "gabcdef"));
3690 try testing.expectEqual(.{ 0, 0 }, indexOfMinMax(u8, "a"));
3741test findMinMax {
3742 try testing.expectEqual(.{ 0, 6 }, findMinMax(u8, "abcdefg"));
3743 try testing.expectEqual(.{ 1, 0 }, findMinMax(u8, "gabcdef"));
3744 try testing.expectEqual(.{ 0, 0 }, findMinMax(u8, "a"));
36913745}
36923746
36933747/// Exchanges contents of two memory locations.