Finding chars in strings

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Sep 5 13:00:44 PDT 2017


On Tuesday, September 05, 2017 17:55:20 ag0aep6g via Digitalmars-d-learn 
wrote:
> On 09/05/2017 05:43 PM, Per Nordlöw wrote:
> > If a character literal has type char, always below 128, can we always
> > search for it's first byte offset in a string without decoding the
> > string to a range of dchars?
>
> Yes. You can search for ASCII characters (< 128) without decoding. The
> values in multibyte sequences are always above 127.

Unfortunately, you'll have to use something like std.utf.byCodeUnit or
std.string.representation to do it; otherwise, you get hit with the
autodecoding. But yeah, UTF-8 is designed to be compatible with ASCII, so
all ASCII characters are valid UTF-8 code units and don't require decoding.
The decoding is just required if you're dealing with non-ASCII characters,
which is another reason why the autodecoding is annoying.

- Jonathan M Davis




More information about the Digitalmars-d-learn mailing list