rectifiedIndexOf()
apz28
apz28 at hotmail.com
Fri May 5 16:47:40 UTC 2023
On Friday, 5 May 2023 at 00:19:01 UTC, Salih Dincer wrote:
> It has a traditional very beautiful function: indexOf() but it
> has an important problem: returns -1 when it can't find 'c'...
>
> ```d
> void main()
> {
> enum chr = 'c';
> auto arr = "dlang".dup;
> if(auto res = arr.indexOf(chr))
> {
> assert(arr[res] == chr);
> // core.exception.ArrayIndexError
> }
> }
> ```
>
> When you try to solve the problem with boolean logic, res is
> now a bool.
>
> ```d
> void main()
> {
> enum chr = 'd';
> auto arr = "dlang".dup;
> if(auto res = arr.indexOf(chr) > -1)
> {
> assert(arr[res] == chr);
> // core.exception.AssertError
> }
> }
> ```
>
Try below
https://gist.github.com/run-dlang/316a1b9df1c14dade4c5ff13c927cc84
More information about the Digitalmars-d
mailing list