mirror of
https://github.com/dlang/phobos.git
synced 2025-04-26 13:10:35 +03:00
overhaul documentation of std.string indexOf-like functions (#8560)
This commit is contained in:
parent
2852e84aa9
commit
54cfc22c73
1 changed files with 131 additions and 97 deletions
228
std/string.d
228
std/string.d
|
@ -457,26 +457,26 @@ pure nothrow @system unittest // https://issues.dlang.org/show_bug.cgi?id=15136
|
||||||
alias CaseSensitive = Flag!"caseSensitive";
|
alias CaseSensitive = Flag!"caseSensitive";
|
||||||
|
|
||||||
/++
|
/++
|
||||||
Searches for character in range.
|
Searches for a character in a string or range.
|
||||||
|
|
||||||
Params:
|
Params:
|
||||||
s = string or InputRange of characters to search in correct UTF format
|
s = string or InputRange of characters to search for `c` in
|
||||||
c = character to search for
|
c = character to search for in `s`
|
||||||
startIdx = starting index to a well-formed code point
|
startIdx = index to a well-formed code point in `s` to start
|
||||||
cs = `Yes.caseSensitive` or `No.caseSensitive`
|
searching from; defaults to 0
|
||||||
|
cs = specifies whether comparisons are case-sensitive
|
||||||
|
(`Yes.caseSensitive`) or not (`No.caseSensitive`).
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
the index of the first occurrence of `c` in `s` with
|
If `c` is found in `s`, then the index of its first occurrence is
|
||||||
respect to the start index `startIdx`. If `c`
|
returned. If `c` is not found or `startIdx` is greater than or equal to
|
||||||
is not found, then `-1` is returned.
|
`s.length`, then -1 is returned. If the parameters are not valid UTF,
|
||||||
If `c` is found the value of the returned index is at least
|
the result will still be either -1 or in the range [`startIdx` ..
|
||||||
`startIdx`.
|
`s.length`], but will not be reliable otherwise.
|
||||||
If the parameters are not valid UTF, the result will still
|
|
||||||
be in the range [-1 .. s.length], but will not be reliable otherwise.
|
|
||||||
|
|
||||||
Throws:
|
Throws:
|
||||||
If the sequence starting at `startIdx` does not represent a well
|
If the sequence starting at `startIdx` does not represent a well-formed
|
||||||
formed codepoint, then a $(REF UTFException, std,utf) may be thrown.
|
code point, then a $(REF UTFException, std,utf) may be thrown.
|
||||||
|
|
||||||
See_Also: $(REF countUntil, std,algorithm,searching)
|
See_Also: $(REF countUntil, std,algorithm,searching)
|
||||||
+/
|
+/
|
||||||
|
@ -901,30 +901,30 @@ private template _indexOfStr(CaseSensitive cs)
|
||||||
}
|
}
|
||||||
|
|
||||||
/++
|
/++
|
||||||
Searches for substring in `s`.
|
Searches for a substring in a string or range.
|
||||||
|
|
||||||
Params:
|
Params:
|
||||||
s = string or ForwardRange of characters to search in correct UTF format
|
s = string or ForwardRange of characters to search for `sub` in
|
||||||
sub = substring to search for
|
sub = substring to search for in `s`
|
||||||
startIdx = the index into s to start searching from
|
startIdx = index to a well-formed code point in `s` to start
|
||||||
cs = `Yes.caseSensitive` (default) or `No.caseSensitive`
|
searching from; defaults to 0
|
||||||
|
cs = specifies whether comparisons are case-sensitive
|
||||||
|
(`Yes.caseSensitive`) or not (`No.caseSensitive`)
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
the index of the first occurrence of `sub` in `s` with
|
The index of the first occurrence of `sub` in `s`. If `sub` is not found
|
||||||
respect to the start index `startIdx`. If `sub` is not found,
|
or `startIdx` is greater than or equal to `s.length`, then -1 is
|
||||||
then `-1` is returned.
|
returned. If the arguments are not valid UTF, the result will still be
|
||||||
If the arguments are not valid UTF, the result will still
|
either -1 or in the range [`startIdx` .. `s.length`], but will not be
|
||||||
be in the range [-1 .. s.length], but will not be reliable otherwise.
|
reliable otherwise.
|
||||||
If `sub` is found the value of the returned index is at least
|
|
||||||
`startIdx`.
|
|
||||||
|
|
||||||
Throws:
|
Throws:
|
||||||
If the sequence starting at `startIdx` does not represent a well
|
If the sequence starting at `startIdx` does not represent a well-formed
|
||||||
formed codepoint, then a $(REF UTFException, std,utf) may be thrown.
|
code point, then a $(REF UTFException, std,utf) may be thrown.
|
||||||
|
|
||||||
Bugs:
|
Bugs:
|
||||||
Does not work with case insensitive strings where the mapping of
|
Does not work with case-insensitive strings where the mapping of
|
||||||
tolower and toupper is not 1:1.
|
$(REF toLower, std,uni) and $(REF toUpper, std,uni) is not 1:1.
|
||||||
+/
|
+/
|
||||||
ptrdiff_t indexOf(Range, Char)(Range s, const(Char)[] sub)
|
ptrdiff_t indexOf(Range, Char)(Range s, const(Char)[] sub)
|
||||||
if (isForwardRange!Range && isSomeChar!(ElementEncodingType!Range) &&
|
if (isForwardRange!Range && isSomeChar!(ElementEncodingType!Range) &&
|
||||||
|
@ -1156,23 +1156,26 @@ unittest
|
||||||
}
|
}
|
||||||
|
|
||||||
/++
|
/++
|
||||||
|
Searches for the last occurrence of a character in a string.
|
||||||
|
|
||||||
Params:
|
Params:
|
||||||
s = string to search
|
s = string to search for `c` in
|
||||||
c = character to search for
|
c = character to search for in `s`
|
||||||
startIdx = the index into s to start searching from
|
startIdx = index of a well-formed code point in `s` to start searching
|
||||||
cs = `Yes.caseSensitive` or `No.caseSensitive`
|
from; defaults to 0
|
||||||
|
cs = specifies whether comparisons are case-sensitive
|
||||||
|
(`Yes.caseSensitive`) or not (`No.caseSensitive`)
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
The index of the last occurrence of `c` in `s`. If `c` is not
|
If `c` is found in `s`, then the index of its last occurrence is
|
||||||
found, then `-1` is returned. The `startIdx` slices `s` in
|
returned. If `c` is not found or `startIdx` is greater than or equal to
|
||||||
the following way $(D s[0 .. startIdx]). `startIdx` represents a
|
`s.length`, then -1 is returned. If the parameters are not valid UTF,
|
||||||
codeunit index in `s`.
|
the result will still be either -1 or in the range [`startIdx` ..
|
||||||
|
`s.length`], but will not be reliable otherwise.
|
||||||
|
|
||||||
Throws:
|
Throws:
|
||||||
If the sequence ending at `startIdx` does not represent a well
|
If the sequence ending at `startIdx` does not represent a well-formed
|
||||||
formed codepoint, then a $(REF UTFException, std,utf) may be thrown.
|
code point, then a $(REF UTFException, std,utf) may be thrown.
|
||||||
|
|
||||||
`cs` indicates whether the comparisons are case sensitive.
|
|
||||||
+/
|
+/
|
||||||
ptrdiff_t lastIndexOf(Char)(const(Char)[] s, in dchar c,
|
ptrdiff_t lastIndexOf(Char)(const(Char)[] s, in dchar c,
|
||||||
in CaseSensitive cs = Yes.caseSensitive) @safe pure
|
in CaseSensitive cs = Yes.caseSensitive) @safe pure
|
||||||
|
@ -1345,23 +1348,30 @@ if (isSomeChar!Char)
|
||||||
}
|
}
|
||||||
|
|
||||||
/++
|
/++
|
||||||
|
Searches for the last occurrence of a substring in a string.
|
||||||
|
|
||||||
Params:
|
Params:
|
||||||
s = string to search
|
s = string to search for `sub` in
|
||||||
sub = substring to search for
|
sub = substring to search for in `s`
|
||||||
startIdx = the index into s to start searching from
|
startIdx = index to a well-formed code point in `s` to start
|
||||||
cs = `Yes.caseSensitive` or `No.caseSensitive`
|
searching from; defaults to 0
|
||||||
|
cs = specifies whether comparisons are case-sensitive
|
||||||
|
(`Yes.caseSensitive`) or not (`No.caseSensitive`)
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
the index of the last occurrence of `sub` in `s`. If `sub` is
|
The index of the last occurrence of `sub` in `s`. If `sub` is not found
|
||||||
not found, then `-1` is returned. The `startIdx` slices `s`
|
or `startIdx` is greater than or equal to `s.length`, then -1 is
|
||||||
in the following way $(D s[0 .. startIdx]). `startIdx` represents a
|
returned. If the parameters are not valid UTF, the result will still be
|
||||||
codeunit index in `s`.
|
either -1 or in the range [`startIdx` .. `s.length`], but will not be
|
||||||
|
reliable otherwise.
|
||||||
|
|
||||||
Throws:
|
Throws:
|
||||||
If the sequence ending at `startIdx` does not represent a well
|
If the sequence starting at `startIdx` does not represent a well-formed
|
||||||
formed codepoint, then a $(REF UTFException, std,utf) may be thrown.
|
code point, then a $(REF UTFException, std,utf) may be thrown.
|
||||||
|
|
||||||
`cs` indicates whether the comparisons are case sensitive.
|
Bugs:
|
||||||
|
Does not work with case-insensitive strings where the mapping of
|
||||||
|
$(REF toLower, std,uni) and $(REF toUpper, std,uni) is not 1:1.
|
||||||
+/
|
+/
|
||||||
ptrdiff_t lastIndexOf(Char1, Char2)(const(Char1)[] s, const(Char2)[] sub,
|
ptrdiff_t lastIndexOf(Char1, Char2)(const(Char1)[] s, const(Char2)[] sub,
|
||||||
in CaseSensitive cs = Yes.caseSensitive) @safe pure
|
in CaseSensitive cs = Yes.caseSensitive) @safe pure
|
||||||
|
@ -1747,21 +1757,28 @@ if (isSomeChar!Char && isSomeChar!Char2)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Returns the index of the first occurrence of any of the elements in $(D
|
Searches the string `haystack` for one of the characters in `needles`
|
||||||
needles) in `haystack`. If no element of `needles` is found,
|
starting at index `startIdx`. If `startIdx` is not given, it defaults to 0.
|
||||||
then `-1` is returned. The `startIdx` slices `haystack` in the
|
|
||||||
following way $(D haystack[startIdx .. $]). `startIdx` represents a
|
|
||||||
codeunit index in `haystack`. If the sequence ending at `startIdx`
|
|
||||||
does not represent a well formed codepoint, then a $(REF UTFException, std,utf)
|
|
||||||
may be thrown.
|
|
||||||
|
|
||||||
Params:
|
Params:
|
||||||
haystack = String to search for needles in.
|
haystack = string to search for needles in
|
||||||
needles = Strings to search for in haystack.
|
needles = characters to search for in `haystack`
|
||||||
startIdx = slices haystack like this $(D haystack[startIdx .. $]). If
|
startIdx = index of a well-formed code point in `haystack` to start
|
||||||
the startIdx is greater than or equal to the length of haystack the
|
searching from; defaults to 0
|
||||||
functions returns `-1`.
|
cs = specifies whether comparisons are case-sensitive
|
||||||
cs = Indicates whether the comparisons are case sensitive.
|
(`Yes.caseSensitive`) or not (`No.caseSensitive`)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The index of the first occurrence of any of the elements of `needles` in
|
||||||
|
`haystack`. If no element of `needles` is found or `startIdx` is greater
|
||||||
|
than or equal to `haystack.length`, then -1 is returned. If the
|
||||||
|
parameters are not valid UTF, the result will still be either -1 or in
|
||||||
|
the range [`startIdx` .. `haystack.length`], but will not be reliable
|
||||||
|
otherwise.
|
||||||
|
|
||||||
|
Throws:
|
||||||
|
If the sequence starting at `startIdx` does not represent a well-formed
|
||||||
|
code point, then a $(REF UTFException, std,utf) may be thrown.
|
||||||
*/
|
*/
|
||||||
ptrdiff_t indexOfAny(Char,Char2)(const(Char)[] haystack, const(Char2)[] needles,
|
ptrdiff_t indexOfAny(Char,Char2)(const(Char)[] haystack, const(Char2)[] needles,
|
||||||
in CaseSensitive cs = Yes.caseSensitive) @safe pure
|
in CaseSensitive cs = Yes.caseSensitive) @safe pure
|
||||||
|
@ -1914,21 +1931,23 @@ if (isSomeChar!Char && isSomeChar!Char2)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Returns the index of the last occurrence of any of the elements in $(D
|
Searches `haystack` for the last occurrence of any of the
|
||||||
needles) in `haystack`. If no element of `needles` is found,
|
characters in `needles`.
|
||||||
then `-1` is returned. The `stopIdx` slices `haystack` in the
|
|
||||||
following way $(D s[0 .. stopIdx]). `stopIdx` represents a codeunit
|
|
||||||
index in `haystack`. If the sequence ending at `startIdx` does not
|
|
||||||
represent a well formed codepoint, then a $(REF UTFException, std,utf) may be
|
|
||||||
thrown.
|
|
||||||
|
|
||||||
Params:
|
Params:
|
||||||
haystack = String to search for needles in.
|
haystack = string to search needles in
|
||||||
needles = Strings to search for in haystack.
|
needles = characters to search for in `haystack`
|
||||||
stopIdx = slices haystack like this $(D haystack[0 .. stopIdx]). If
|
stopIdx = index in `haystack` to stop searching at (exclusive); defaults
|
||||||
the stopIdx is greater than or equal to the length of haystack the
|
to `haystack.length`
|
||||||
functions returns `-1`.
|
cs = specifies whether comparisons are case-sensitive
|
||||||
cs = Indicates whether the comparisons are case sensitive.
|
(`Yes.caseSensitive`) or not (`No.caseSensitive`)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The index of the last occurrence of any of the characters of `needles`
|
||||||
|
in `haystack`. If no character of `needles` is found or `stopIdx` is 0,
|
||||||
|
then -1 is returned. If the parameters are not valid UTF, the result
|
||||||
|
will still be in the range [-1 .. `stopIdx`], but will not be reliable
|
||||||
|
otherwise.
|
||||||
*/
|
*/
|
||||||
ptrdiff_t lastIndexOfAny(Char,Char2)(const(Char)[] haystack,
|
ptrdiff_t lastIndexOfAny(Char,Char2)(const(Char)[] haystack,
|
||||||
const(Char2)[] needles, in CaseSensitive cs = Yes.caseSensitive)
|
const(Char2)[] needles, in CaseSensitive cs = Yes.caseSensitive)
|
||||||
|
@ -2097,17 +2116,27 @@ if (isSomeChar!Char && isSomeChar!Char2)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Returns the index of the first occurrence of any character not an elements
|
Searches `haystack` for a character not in `needles`.
|
||||||
in `needles` in `haystack`. If all element of `haystack` are
|
|
||||||
element of `needles` `-1` is returned.
|
|
||||||
|
|
||||||
Params:
|
Params:
|
||||||
haystack = String to search for needles in.
|
haystack = string to search for needles in
|
||||||
needles = Strings to search for in haystack.
|
needles = characters to search for in `haystack`
|
||||||
startIdx = slices haystack like this $(D haystack[startIdx .. $]). If
|
startIdx = index of a well-formed code point in `haystack` to start
|
||||||
the startIdx is greater than or equal to the length of haystack the
|
searching from; defaults to 0
|
||||||
functions returns `-1`.
|
cs = specifies whether comparisons are case-sensitive
|
||||||
cs = Indicates whether the comparisons are case sensitive.
|
(`Yes.caseSensitive`) or not (`No.caseSensitive`)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The index of the first character in `haystack` that is not an element of
|
||||||
|
`needles`. If all characters of `haystack` are elements of `needles` or
|
||||||
|
`startIdx` is greater than or equal to `haystack.length`, then -1 is
|
||||||
|
returned. If the parameters are not valid UTF, the result will still be
|
||||||
|
either -1 or in the range [`startIdx` .. `haystack.length`], but will
|
||||||
|
not be reliable otherwise.
|
||||||
|
|
||||||
|
Throws:
|
||||||
|
If the sequence starting at `startIdx` does not represent a well-formed
|
||||||
|
code point, then a $(REF UTFException, std,utf) may be thrown.
|
||||||
*/
|
*/
|
||||||
ptrdiff_t indexOfNeither(Char,Char2)(const(Char)[] haystack,
|
ptrdiff_t indexOfNeither(Char,Char2)(const(Char)[] haystack,
|
||||||
const(Char2)[] needles, in CaseSensitive cs = Yes.caseSensitive)
|
const(Char2)[] needles, in CaseSensitive cs = Yes.caseSensitive)
|
||||||
|
@ -2257,17 +2286,22 @@ if (isSomeChar!Char && isSomeChar!Char2)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Returns the last index of the first occurence of any character that is not
|
Searches for the last character in `haystack` that is not in `needles`.
|
||||||
an elements in `needles` in `haystack`. If all element of
|
|
||||||
`haystack` are element of `needles` `-1` is returned.
|
|
||||||
|
|
||||||
Params:
|
Params:
|
||||||
haystack = String to search for needles in.
|
haystack = string to search for needles in
|
||||||
needles = Strings to search for in haystack.
|
needles = characters to search for in `haystack`
|
||||||
stopIdx = slices haystack like this $(D haystack[0 .. stopIdx]) If
|
stopIdx = index in `haystack` to stop searching at (exclusive);
|
||||||
the stopIdx is greater than or equal to the length of haystack the
|
defaults to `haystack.length`
|
||||||
functions returns `-1`.
|
cs = specifies whether comparisons are case-sensitive
|
||||||
cs = Indicates whether the comparisons are case sensitive.
|
(`Yes.caseSensitive`) or not (`No.caseSensitive`)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The index of the last character in `haystack` that is not an element of
|
||||||
|
`needles`. If all characters of `haystack` are in `needles` or `stopIdx`
|
||||||
|
is 0, then -1 is returned. If the parameters are not valid UTF, the
|
||||||
|
result will still be in the range [-1 .. `stopIdx`], but will not be
|
||||||
|
reliable otherwise.
|
||||||
*/
|
*/
|
||||||
ptrdiff_t lastIndexOfNeither(Char,Char2)(const(Char)[] haystack,
|
ptrdiff_t lastIndexOfNeither(Char,Char2)(const(Char)[] haystack,
|
||||||
const(Char2)[] needles, in CaseSensitive cs = Yes.caseSensitive)
|
const(Char2)[] needles, in CaseSensitive cs = Yes.caseSensitive)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue