-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
doc: improve 'hex'
Buffer decoding description and examples
#41598
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please run the linter 🙏 ( the word 'hexadecimal' needs to drop a line)
doc/api/buffer.md
Outdated
may occur when decoding strings that do exclusively contain valid hexadecimal | ||
characters. See below for an example. | ||
may occur when decoding strings that do not exclusively contain valid | ||
hexadecimal characters. See below for an example. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, it doesn't matter much if the strings exclusively contain hexadecimal characters
. In fact, it might be more surprising that Buffer.from
truncates data even when it only contains hex digits:
// only hex, still truncated :(
Buffer.from('000', 'hex').toString('hex') === '00'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it because it reads 2 characters at a time?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nvm, read the docs, which state:
Buffer.from('1a7g', 'hex');
// Prints <Buffer 1a>, data truncated when data ends in single digit ('7').
So the last '0'
is truncated because it's a single character, and the hex decoder expects pairs of characters.
So since the third '0'
does not have a pairing character, it is truncated.
Perhaps the description should state that? You know, not only in the code example?
doc/api/buffer.md
Outdated
may occur when decoding strings that do not exclusively contain valid | ||
hexadecimal characters. See below for an example. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
may occur when decoding strings that do not exclusively contain valid | |
hexadecimal characters. See below for an example. | |
may occur when decoding strings that do not exclusively consist of an even | |
number of hexadecimal characters. See below for an example. |
Maybe something like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that this, plus another code example (something like 123
-> 12
) would be very nice
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tniessen WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SGTM. These are unfortunate oddities that we need to maintain for backward compatibility :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took a better look at the examples and figured that I could achieve the same thing by altering the strings used in the examples.
* `'hex'`: Encode each byte as two hexadecimal characters. Data truncation | ||
may occur when decoding strings that do exclusively contain valid hexadecimal | ||
characters. See below for an example. | ||
may occur when decoding strings that do not exclusively consist of an even | ||
number of hexadecimal characters. See below for an example. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional nit-pick suggestion: The Microsoft style guide we follow suggests might
instead of may
in this context. But I wonder if it's actually will occur
rather than might occur
? All told:
* `'hex'`: Encode each byte as two hexadecimal characters. Data truncation | |
may occur when decoding strings that do exclusively contain valid hexadecimal | |
characters. See below for an example. | |
may occur when decoding strings that do not exclusively consist of an even | |
number of hexadecimal characters. See below for an example. | |
* `'hex'`: Encode each byte as two hexadecimal characters. Data is truncated | |
when decoding strings that do not exclusively consist of an even | |
number of hexadecimal characters. See below for an example. |
'hex'
Buffer decoding description'hex'
Buffer decoding description and examples
Landed in dbe60a2 |
fixes nodejs#41594 PR-URL: nodejs#41598 Fixes: nodejs#41594 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Mestery <mestery@protonmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
PR-URL: #41598 Fixes: #41594 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Mestery <mestery@protonmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
PR-URL: nodejs#41598 Fixes: nodejs#41594 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Mestery <mestery@protonmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
PR-URL: #41598 Fixes: #41594 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Mestery <mestery@protonmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
PR-URL: #41598 Fixes: #41594 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Mestery <mestery@protonmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
PR-URL: #41598 Fixes: #41594 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Mestery <mestery@protonmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
PR-URL: #41598 Fixes: #41594 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Mestery <mestery@protonmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
Fixes: #41594