On Sunday, October 27, 2019 6:44:05 AM MDT Per Nordlöw via Digitalmars-d- learn wrote: > In which circumstances can a `char` be initialized a non-7-bit > value (>= 128)? Is it possible only in non-@safe code? > > And, if so, what will be the result of casting such a value to > `dchar`? Will that result in an exception or will it interpret > the `char` using a 8-bit character encoding? > > I'm asking because I'm pondering about how to specialize the > non-7-bit `needle`-case of the following array-overload of > `startsWith` when `T` is `char`: > > bool startsWith(T)(scope const(T)[] haystack, > scope const T needle) > { > static if (is(T : char)) { assert(needle < 128); } // TODO > convert needle to `char[]` and call itself > if (haystack.length >= 1) > { > return haystack[0] == needle; > } > return false; > }
char is a value above 127 all the time, because specific values above 127 are used as the first byte in a multibyte code point in UTF-8. Also, as Adam points out, the default value for char is 255 (in order to specifically give it an invalid value). That being said, it doesn't make sense to use startsWith with a single char which isn't ASCII, because no such char would be valid UTF-8 on its own. - Jonathan M Davis