Searching strings with indexOf vs countUntil

Anonymouse via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 25 11:13:15 PDT 2017


On Thursday, 25 May 2017 at 11:55:21 UTC, Kagamin wrote:
> I would guess indexOf returns a value suitable for indexing, 
> therefore it counts code units, while countUntil counts range 
> elements - code points in case of a string. Also number of code 
> points is not suitable for indexing an utf8 string, it can be 
> used to allocate a dstring, but not so much for anything else. 
> What do you use the resulting value for?

I see. I would have thought indexOf would be more keen to decode, 
but that's bias talking.

The project is an IRC bot. I use indexOf/used countUntil to slice 
up strings into one part leading up to some separator (" :" in 
that example), and another into everything after it. See 
https://dpaste.dzfl.pl/326e450058c1.


On Thursday, 25 May 2017 at 12:46:43 UTC, Basile B. wrote:
> To get rid of decoding he can cast to ubyte[]. I would do that 
> if sure that the input is only made of ascii chars.

Part of the strings I'm working with can be assumed to be only 
ASCII, yes. indexOf only wants strings or char[]s, but 
interestingly if I use the same benchmark but have countUntil 
work on raw ubyte[]s, it is faster. See 
https://dpaste.dzfl.pl/2e095f7d18be.


More information about the Digitalmars-d-learn mailing list