The specification for strtol conceptually divides the input string into "leading spaces", "sequence of subjects" and "ending string" and defines a "sequence of subjects" as:
The longest initial subsequence of the input string, starting with the first character of a non-white space that has the expected shape. The subject sequence must not contain characters if the input string is empty or consists entirely of space characters or if the first character of a non-white space is different from a sign or a valid letter or number.
At one time I thought that the business with the longest initial subsequence is akin to how scanf works, where " 0x@ " will scan as "0x" , a bad match, followed by "@" as the next unread character, However after some discussion, I'm basically convinced that strtol handles the longest starting subsequence that has the expected shape, and not the longest starting string, which is the starting subsequence of some possible string of the expected shape.
What still confuses me is this language in the spec:
If the sequence of objects is empty or does not have the expected shape, the conversion is not performed; the str value is stored in the object pointed to by endptr, provided that endptr is not a null pointer.
If we accept what seems to be the correct definition of “subject sequence,” there is no such thing as a non-empty subject sequence that does not have the expected shape, and instead (to avoid redundancy and confusion) the text should simply read:
If the sequence of objects is empty, no conversion is performed; the str value is stored in the object pointed to by endptr, provided that endptr is not a null pointer.
Can someone clarify these issues for me? Perhaps a reference to past discussions or any relevant defect reports would be helpful.
source share