Skip to content

Commit 54cfc22

Browse files
authored
overhaul documentation of std.string indexOf-like functions (#8560)
1 parent 2852e84 commit 54cfc22

File tree

1 file changed

+131
-97
lines changed

1 file changed

+131
-97
lines changed

std/string.d

Lines changed: 131 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -457,26 +457,26 @@ pure nothrow @system unittest // https://issues.dlang.org/show_bug.cgi?id=15136
457457
alias CaseSensitive = Flag!"caseSensitive";
458458

459459
/++
460-
Searches for character in range.
460+
Searches for a character in a string or range.
461461
462462
Params:
463-
s = string or InputRange of characters to search in correct UTF format
464-
c = character to search for
465-
startIdx = starting index to a well-formed code point
466-
cs = `Yes.caseSensitive` or `No.caseSensitive`
463+
s = string or InputRange of characters to search for `c` in
464+
c = character to search for in `s`
465+
startIdx = index to a well-formed code point in `s` to start
466+
searching from; defaults to 0
467+
cs = specifies whether comparisons are case-sensitive
468+
(`Yes.caseSensitive`) or not (`No.caseSensitive`).
467469
468470
Returns:
469-
the index of the first occurrence of `c` in `s` with
470-
respect to the start index `startIdx`. If `c`
471-
is not found, then `-1` is returned.
472-
If `c` is found the value of the returned index is at least
473-
`startIdx`.
474-
If the parameters are not valid UTF, the result will still
475-
be in the range [-1 .. s.length], but will not be reliable otherwise.
471+
If `c` is found in `s`, then the index of its first occurrence is
472+
returned. If `c` is not found or `startIdx` is greater than or equal to
473+
`s.length`, then -1 is returned. If the parameters are not valid UTF,
474+
the result will still be either -1 or in the range [`startIdx` ..
475+
`s.length`], but will not be reliable otherwise.
476476
477477
Throws:
478-
If the sequence starting at `startIdx` does not represent a well
479-
formed codepoint, then a $(REF UTFException, std,utf) may be thrown.
478+
If the sequence starting at `startIdx` does not represent a well-formed
479+
code point, then a $(REF UTFException, std,utf) may be thrown.
480480
481481
See_Also: $(REF countUntil, std,algorithm,searching)
482482
+/
@@ -901,30 +901,30 @@ private template _indexOfStr(CaseSensitive cs)
901901
}
902902

903903
/++
904-
Searches for substring in `s`.
904+
Searches for a substring in a string or range.
905905
906906
Params:
907-
s = string or ForwardRange of characters to search in correct UTF format
908-
sub = substring to search for
909-
startIdx = the index into s to start searching from
910-
cs = `Yes.caseSensitive` (default) or `No.caseSensitive`
907+
s = string or ForwardRange of characters to search for `sub` in
908+
sub = substring to search for in `s`
909+
startIdx = index to a well-formed code point in `s` to start
910+
searching from; defaults to 0
911+
cs = specifies whether comparisons are case-sensitive
912+
(`Yes.caseSensitive`) or not (`No.caseSensitive`)
911913
912914
Returns:
913-
the index of the first occurrence of `sub` in `s` with
914-
respect to the start index `startIdx`. If `sub` is not found,
915-
then `-1` is returned.
916-
If the arguments are not valid UTF, the result will still
917-
be in the range [-1 .. s.length], but will not be reliable otherwise.
918-
If `sub` is found the value of the returned index is at least
919-
`startIdx`.
915+
The index of the first occurrence of `sub` in `s`. If `sub` is not found
916+
or `startIdx` is greater than or equal to `s.length`, then -1 is
917+
returned. If the arguments are not valid UTF, the result will still be
918+
either -1 or in the range [`startIdx` .. `s.length`], but will not be
919+
reliable otherwise.
920920
921921
Throws:
922-
If the sequence starting at `startIdx` does not represent a well
923-
formed codepoint, then a $(REF UTFException, std,utf) may be thrown.
922+
If the sequence starting at `startIdx` does not represent a well-formed
923+
code point, then a $(REF UTFException, std,utf) may be thrown.
924924
925925
Bugs:
926-
Does not work with case insensitive strings where the mapping of
927-
tolower and toupper is not 1:1.
926+
Does not work with case-insensitive strings where the mapping of
927+
$(REF toLower, std,uni) and $(REF toUpper, std,uni) is not 1:1.
928928
+/
929929
ptrdiff_t indexOf(Range, Char)(Range s, const(Char)[] sub)
930930
if (isForwardRange!Range && isSomeChar!(ElementEncodingType!Range) &&
@@ -1156,23 +1156,26 @@ unittest
11561156
}
11571157

11581158
/++
1159+
Searches for the last occurrence of a character in a string.
1160+
11591161
Params:
1160-
s = string to search
1161-
c = character to search for
1162-
startIdx = the index into s to start searching from
1163-
cs = `Yes.caseSensitive` or `No.caseSensitive`
1162+
s = string to search for `c` in
1163+
c = character to search for in `s`
1164+
startIdx = index of a well-formed code point in `s` to start searching
1165+
from; defaults to 0
1166+
cs = specifies whether comparisons are case-sensitive
1167+
(`Yes.caseSensitive`) or not (`No.caseSensitive`)
11641168
11651169
Returns:
1166-
The index of the last occurrence of `c` in `s`. If `c` is not
1167-
found, then `-1` is returned. The `startIdx` slices `s` in
1168-
the following way $(D s[0 .. startIdx]). `startIdx` represents a
1169-
codeunit index in `s`.
1170+
If `c` is found in `s`, then the index of its last occurrence is
1171+
returned. If `c` is not found or `startIdx` is greater than or equal to
1172+
`s.length`, then -1 is returned. If the parameters are not valid UTF,
1173+
the result will still be either -1 or in the range [`startIdx` ..
1174+
`s.length`], but will not be reliable otherwise.
11701175
11711176
Throws:
1172-
If the sequence ending at `startIdx` does not represent a well
1173-
formed codepoint, then a $(REF UTFException, std,utf) may be thrown.
1174-
1175-
`cs` indicates whether the comparisons are case sensitive.
1177+
If the sequence ending at `startIdx` does not represent a well-formed
1178+
code point, then a $(REF UTFException, std,utf) may be thrown.
11761179
+/
11771180
ptrdiff_t lastIndexOf(Char)(const(Char)[] s, in dchar c,
11781181
in CaseSensitive cs = Yes.caseSensitive) @safe pure
@@ -1345,23 +1348,30 @@ if (isSomeChar!Char)
13451348
}
13461349

13471350
/++
1351+
Searches for the last occurrence of a substring in a string.
1352+
13481353
Params:
1349-
s = string to search
1350-
sub = substring to search for
1351-
startIdx = the index into s to start searching from
1352-
cs = `Yes.caseSensitive` or `No.caseSensitive`
1354+
s = string to search for `sub` in
1355+
sub = substring to search for in `s`
1356+
startIdx = index to a well-formed code point in `s` to start
1357+
searching from; defaults to 0
1358+
cs = specifies whether comparisons are case-sensitive
1359+
(`Yes.caseSensitive`) or not (`No.caseSensitive`)
13531360
13541361
Returns:
1355-
the index of the last occurrence of `sub` in `s`. If `sub` is
1356-
not found, then `-1` is returned. The `startIdx` slices `s`
1357-
in the following way $(D s[0 .. startIdx]). `startIdx` represents a
1358-
codeunit index in `s`.
1362+
The index of the last occurrence of `sub` in `s`. If `sub` is not found
1363+
or `startIdx` is greater than or equal to `s.length`, then -1 is
1364+
returned. If the parameters are not valid UTF, the result will still be
1365+
either -1 or in the range [`startIdx` .. `s.length`], but will not be
1366+
reliable otherwise.
13591367
13601368
Throws:
1361-
If the sequence ending at `startIdx` does not represent a well
1362-
formed codepoint, then a $(REF UTFException, std,utf) may be thrown.
1369+
If the sequence starting at `startIdx` does not represent a well-formed
1370+
code point, then a $(REF UTFException, std,utf) may be thrown.
13631371
1364-
`cs` indicates whether the comparisons are case sensitive.
1372+
Bugs:
1373+
Does not work with case-insensitive strings where the mapping of
1374+
$(REF toLower, std,uni) and $(REF toUpper, std,uni) is not 1:1.
13651375
+/
13661376
ptrdiff_t lastIndexOf(Char1, Char2)(const(Char1)[] s, const(Char2)[] sub,
13671377
in CaseSensitive cs = Yes.caseSensitive) @safe pure
@@ -1747,21 +1757,28 @@ if (isSomeChar!Char && isSomeChar!Char2)
17471757
}
17481758

17491759
/**
1750-
Returns the index of the first occurrence of any of the elements in $(D
1751-
needles) in `haystack`. If no element of `needles` is found,
1752-
then `-1` is returned. The `startIdx` slices `haystack` in the
1753-
following way $(D haystack[startIdx .. $]). `startIdx` represents a
1754-
codeunit index in `haystack`. If the sequence ending at `startIdx`
1755-
does not represent a well formed codepoint, then a $(REF UTFException, std,utf)
1756-
may be thrown.
1760+
Searches the string `haystack` for one of the characters in `needles`
1761+
starting at index `startIdx`. If `startIdx` is not given, it defaults to 0.
17571762
17581763
Params:
1759-
haystack = String to search for needles in.
1760-
needles = Strings to search for in haystack.
1761-
startIdx = slices haystack like this $(D haystack[startIdx .. $]). If
1762-
the startIdx is greater than or equal to the length of haystack the
1763-
functions returns `-1`.
1764-
cs = Indicates whether the comparisons are case sensitive.
1764+
haystack = string to search for needles in
1765+
needles = characters to search for in `haystack`
1766+
startIdx = index of a well-formed code point in `haystack` to start
1767+
searching from; defaults to 0
1768+
cs = specifies whether comparisons are case-sensitive
1769+
(`Yes.caseSensitive`) or not (`No.caseSensitive`)
1770+
1771+
Returns:
1772+
The index of the first occurrence of any of the elements of `needles` in
1773+
`haystack`. If no element of `needles` is found or `startIdx` is greater
1774+
than or equal to `haystack.length`, then -1 is returned. If the
1775+
parameters are not valid UTF, the result will still be either -1 or in
1776+
the range [`startIdx` .. `haystack.length`], but will not be reliable
1777+
otherwise.
1778+
1779+
Throws:
1780+
If the sequence starting at `startIdx` does not represent a well-formed
1781+
code point, then a $(REF UTFException, std,utf) may be thrown.
17651782
*/
17661783
ptrdiff_t indexOfAny(Char,Char2)(const(Char)[] haystack, const(Char2)[] needles,
17671784
in CaseSensitive cs = Yes.caseSensitive) @safe pure
@@ -1914,21 +1931,23 @@ if (isSomeChar!Char && isSomeChar!Char2)
19141931
}
19151932

19161933
/**
1917-
Returns the index of the last occurrence of any of the elements in $(D
1918-
needles) in `haystack`. If no element of `needles` is found,
1919-
then `-1` is returned. The `stopIdx` slices `haystack` in the
1920-
following way $(D s[0 .. stopIdx]). `stopIdx` represents a codeunit
1921-
index in `haystack`. If the sequence ending at `startIdx` does not
1922-
represent a well formed codepoint, then a $(REF UTFException, std,utf) may be
1923-
thrown.
1934+
Searches `haystack` for the last occurrence of any of the
1935+
characters in `needles`.
19241936
19251937
Params:
1926-
haystack = String to search for needles in.
1927-
needles = Strings to search for in haystack.
1928-
stopIdx = slices haystack like this $(D haystack[0 .. stopIdx]). If
1929-
the stopIdx is greater than or equal to the length of haystack the
1930-
functions returns `-1`.
1931-
cs = Indicates whether the comparisons are case sensitive.
1938+
haystack = string to search needles in
1939+
needles = characters to search for in `haystack`
1940+
stopIdx = index in `haystack` to stop searching at (exclusive); defaults
1941+
to `haystack.length`
1942+
cs = specifies whether comparisons are case-sensitive
1943+
(`Yes.caseSensitive`) or not (`No.caseSensitive`)
1944+
1945+
Returns:
1946+
The index of the last occurrence of any of the characters of `needles`
1947+
in `haystack`. If no character of `needles` is found or `stopIdx` is 0,
1948+
then -1 is returned. If the parameters are not valid UTF, the result
1949+
will still be in the range [-1 .. `stopIdx`], but will not be reliable
1950+
otherwise.
19321951
*/
19331952
ptrdiff_t lastIndexOfAny(Char,Char2)(const(Char)[] haystack,
19341953
const(Char2)[] needles, in CaseSensitive cs = Yes.caseSensitive)
@@ -2097,17 +2116,27 @@ if (isSomeChar!Char && isSomeChar!Char2)
20972116
}
20982117

20992118
/**
2100-
Returns the index of the first occurrence of any character not an elements
2101-
in `needles` in `haystack`. If all element of `haystack` are
2102-
element of `needles` `-1` is returned.
2119+
Searches `haystack` for a character not in `needles`.
21032120
21042121
Params:
2105-
haystack = String to search for needles in.
2106-
needles = Strings to search for in haystack.
2107-
startIdx = slices haystack like this $(D haystack[startIdx .. $]). If
2108-
the startIdx is greater than or equal to the length of haystack the
2109-
functions returns `-1`.
2110-
cs = Indicates whether the comparisons are case sensitive.
2122+
haystack = string to search for needles in
2123+
needles = characters to search for in `haystack`
2124+
startIdx = index of a well-formed code point in `haystack` to start
2125+
searching from; defaults to 0
2126+
cs = specifies whether comparisons are case-sensitive
2127+
(`Yes.caseSensitive`) or not (`No.caseSensitive`)
2128+
2129+
Returns:
2130+
The index of the first character in `haystack` that is not an element of
2131+
`needles`. If all characters of `haystack` are elements of `needles` or
2132+
`startIdx` is greater than or equal to `haystack.length`, then -1 is
2133+
returned. If the parameters are not valid UTF, the result will still be
2134+
either -1 or in the range [`startIdx` .. `haystack.length`], but will
2135+
not be reliable otherwise.
2136+
2137+
Throws:
2138+
If the sequence starting at `startIdx` does not represent a well-formed
2139+
code point, then a $(REF UTFException, std,utf) may be thrown.
21112140
*/
21122141
ptrdiff_t indexOfNeither(Char,Char2)(const(Char)[] haystack,
21132142
const(Char2)[] needles, in CaseSensitive cs = Yes.caseSensitive)
@@ -2257,17 +2286,22 @@ if (isSomeChar!Char && isSomeChar!Char2)
22572286
}
22582287

22592288
/**
2260-
Returns the last index of the first occurence of any character that is not
2261-
an elements in `needles` in `haystack`. If all element of
2262-
`haystack` are element of `needles` `-1` is returned.
2289+
Searches for the last character in `haystack` that is not in `needles`.
22632290
22642291
Params:
2265-
haystack = String to search for needles in.
2266-
needles = Strings to search for in haystack.
2267-
stopIdx = slices haystack like this $(D haystack[0 .. stopIdx]) If
2268-
the stopIdx is greater than or equal to the length of haystack the
2269-
functions returns `-1`.
2270-
cs = Indicates whether the comparisons are case sensitive.
2292+
haystack = string to search for needles in
2293+
needles = characters to search for in `haystack`
2294+
stopIdx = index in `haystack` to stop searching at (exclusive);
2295+
defaults to `haystack.length`
2296+
cs = specifies whether comparisons are case-sensitive
2297+
(`Yes.caseSensitive`) or not (`No.caseSensitive`)
2298+
2299+
Returns:
2300+
The index of the last character in `haystack` that is not an element of
2301+
`needles`. If all characters of `haystack` are in `needles` or `stopIdx`
2302+
is 0, then -1 is returned. If the parameters are not valid UTF, the
2303+
result will still be in the range [-1 .. `stopIdx`], but will not be
2304+
reliable otherwise.
22712305
*/
22722306
ptrdiff_t lastIndexOfNeither(Char,Char2)(const(Char)[] haystack,
22732307
const(Char2)[] needles, in CaseSensitive cs = Yes.caseSensitive)

0 commit comments

Comments
 (0)