@@ -2006,6 +2006,43 @@ $(H3 $(LEGACY_LNAME2 StringLiteral, string_literals, String Literals))
2006
2006
C style implicit concatenation without an intervening operator is
2007
2007
error prone and not supported in D.)
2008
2008
2009
+
2010
+ $(H3 $(LNAME2 hex_string_literals, Hex String Literals))
2011
+ $(P Because hex string literals contain binary data not limited to textual data, they allow additional conversions over other string literals.)
2012
+
2013
+ $(P A hex string literal implicitly converts to a constant `byte[]` or `ubyte[]`.)
2014
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
2015
+ -------------
2016
+ immutable ubyte[] b = x"3F 80 00 00";
2017
+ const byte[] c = x"3F 80 00 00";
2018
+ -------------
2019
+ )
2020
+
2021
+ $(P A hex string literal can be explicitly cast to an array of integers with a larger size than 1.
2022
+ A big endian byte order in the hex string will be assumed.)
2023
+
2024
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
2025
+ -------------
2026
+ static immutable uint[] data = cast(immutable uint[]) x"AABBCCDD";
2027
+ static assert(data[0] == 0xAABBCCDD);
2028
+ -------------
2029
+ )
2030
+
2031
+ $(P This requires the length of the hex string to be a multiple of the array element's size in bytes.)
2032
+ $(SPEC_RUNNABLE_EXAMPLE_FAIL
2033
+ -------------
2034
+ static e = cast(immutable ushort[]) x"AA BB CC";
2035
+ // Error, length of 3 bytes is not a multiple of 2, the size of a `ushort`
2036
+ -------------
2037
+ )
2038
+
2039
+ $(P When a hex string literal gets constant folded, the result is no longer considered a hex string literal)
2040
+ $(SPEC_RUNNABLE_EXAMPLE_FAIL
2041
+ -------------
2042
+ static immutable byte[] b = x"AA" ~ "G"; // Error: cannot convert `string` to `immutable byte[]`
2043
+ -------------
2044
+ )
2045
+
2009
2046
$(H3 $(LNAME2 array_literals, Array Literals))
2010
2047
2011
2048
$(GRAMMAR
0 commit comments