Using a char value >= 128
Ernesto Castellotti
erny.castell at gmail.com
Sun Oct 27 14:36:54 UTC 2019
On Sunday, 27 October 2019 at 12:44:05 UTC, Per Nordlöw wrote:
> In which circumstances can a `char` be initialized a non-7-bit
> value (>= 128)? Is it possible only in non- at 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 in D is always unsigned, it is not implementation-specific.
Therefore it can take values up to (2^8)−1, If you want a
signed 8 byte type you can use ubyte, which obviously can take up
from -(2^7) to (2^7)-1
More information about the Digitalmars-d-learn
mailing list