Bug->Doc #75320 [Opn->Ver]: Pack uses "a" and "Z" differently

From: Date: Thu, 05 Oct 2017 18:32:34 +0000
Subject: Bug->Doc #75320 [Opn->Ver]: Pack uses "a" and "Z" differently
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-15031@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=75320&edit=1 ID: 75320 Updated by: requinix@php.net Reported by: andrey at kostin dot info Summary: Pack uses "a" and "Z" differently -Status: Open +Status: Verified -Type: Bug +Type: Documentation Problem Package: Strings related Operating System: Ubuntu 16.04, Gentoo PHP Version: 7.1.10 Block user comment: N Private report: N New Comment: In this case the correct docs are Perl's :D We're borrowing pack/unpack from them anyways, and their explanations go more in depth. Maybe we should link to them... https://perldoc.perl.org/functions/pack.html a A string with arbitrary binary data, will be null padded. Z A null-terminated (ASCIZ) string, will be null padded. described as The a , A , and Z types gobble just one value, but pack it as a string of length count, padding with nulls or spaces as needed. When unpacking, A strips trailing whitespace and nulls, Z strips everything after the first null, and a returns data with no stripping at all. If the value to pack is too long, the result is truncated. If it's too long and an explicit count is provided, Z packs only $count-1 bytes, followed by a null byte. Thus Z always packs a trailing null, except when the count is 0. I think we can fit the terminating NUL and truncation behavior briefly in the table. Something like "NUL-padded string; always includes at least one NUL terminator". (The "new in PHP 5.5" is covered in the Changelog already.) Or just mention the terminator and add a Note? One more small change to make: the format description says > For a, A, h, H the repeat count specifies how many characters of one data > argument are taken, That applies to Z too. Previous Comments: ------------------------------------------------------------------------ [2017-10-05 17:20:41] andrey at kostin dot info Description: ------------ I can be wrong, but documentation says that "a" and "Z" are equal since PHP 5.5 But it is not: php > var_dump(pack('a1', '1')); string(1) "1" php > var_dump(pack('Z1', '1')); string(1) "" If I'm wrong, please point to correct docs. Test script: --------------- <?php var_dump(pack('a1', '1') === pack('Z1', '1')); Expected result: ---------------- true Actual result: -------------- false ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=75320&edit=1

« previous php.doc.bugs (#15031) next »