Skip to content

Commit 25ab0e8

Browse files
authored
Merge pull request #3760 from dkorpel/hexstring-revive
Document hex strings Signed-off-by: Petar Kirov <PetarKirov@users.noreply.github.com> Merged-on-behalf-of: Petar Kirov <PetarKirov@users.noreply.github.com>
2 parents 3a27d31 + 07c7264 commit 25ab0e8

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

spec/expression.dd

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,6 +2006,43 @@ $(H3 $(LEGACY_LNAME2 StringLiteral, string_literals, String Literals))
20062006
C style implicit concatenation without an intervening operator is
20072007
error prone and not supported in D.)
20082008

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+
20092046
$(H3 $(LNAME2 array_literals, Array Literals))
20102047

20112048
$(GRAMMAR

spec/lex.dd

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,11 @@ $(GNAME StringLiteral):
291291
$(GLINK DoubleQuotedString)
292292
$(GLINK DelimitedString)
293293
$(GLINK TokenString)
294+
$(GLINK HexString)
294295
)
295296
$(P
296297
A string literal is either a wysiwyg quoted string, a double quoted
297-
string, a delimited string, or a token string.
298+
string, a delimited string, a token string, or a hex string.
298299
)
299300

300301
$(P In all string literal forms, an $(GLINK EndOfLine) is regarded as a single
@@ -491,6 +492,33 @@ $(GNAME TokenStringToken):
491492
// __EOF__ is not a token, it's end of file
492493
---
493494

495+
$(H3 $(LNAME2 hex_strings, Hex Strings))
496+
$(GRAMMAR
497+
$(GNAME HexString):
498+
$(B x") $(GLINK HexStringChars)$(OPT) $(B ") $(GLINK StringPostfix)$(OPT)
499+
500+
$(GNAME HexStringChars):
501+
$(GLINK HexStringChar)
502+
$(GLINK HexStringChar) $(GSELF HexStringChars)
503+
504+
$(GNAME HexStringChar):
505+
$(GLINK HexDigit)
506+
$(GLINK WhiteSpace)
507+
$(GLINK EndOfLine)
508+
)
509+
510+
$(P Hex strings allow string literals to be created using hex data.
511+
The hex data need not form valid UTF characters.
512+
)
513+
514+
---
515+
x"0A" // same as "\x0A"
516+
x"00 FBCD 32FD 0A" // same as "\x00\xFB\xCD\x32\xFD\x0A"
517+
---
518+
519+
$(P Whitespace and newlines are ignored, so the hex data can be easily
520+
formatted. The number of hex characters must be a multiple of 2.)
521+
494522
$(H3 $(LNAME2 string_postfix, String Postfix))
495523

496524
$(GRAMMAR_LEX

0 commit comments

Comments
 (0)