Skip to content

[RFC] Add support for endianness modifier in pack()/unpack() format codes - #21074

Merged
Girgias merged 1 commit into
php:masterfrom
alexandre-daubois:rfc-endianness-modifier-pack-unpack
Aug 24, 2026
Merged

[RFC] Add support for endianness modifier in pack()/unpack() format codes#21074
Girgias merged 1 commit into
php:masterfrom
alexandre-daubois:rfc-endianness-modifier-pack-unpack

Conversation

@alexandre-daubois

@alexandre-daubois alexandre-daubois commented Jan 29, 2026

Copy link
Copy Markdown
Member

@alexandre-daubois
alexandre-daubois force-pushed the rfc-endianness-modifier-pack-unpack branch from 8d5f2e4 to 4b714a6 Compare March 19, 2026 10:05
@alexandre-daubois alexandre-daubois changed the title [RFC] Add support for endianness modifier in pack()/unpack() integer format codes [RFC] Add support for endianness modifier in pack()/unpack( format codes Mar 19, 2026
@alexandre-daubois alexandre-daubois changed the title [RFC] Add support for endianness modifier in pack()/unpack( format codes [RFC] Add support for endianness modifier in pack()/unpack() format codes Mar 19, 2026
@alexandre-daubois
alexandre-daubois force-pushed the rfc-endianness-modifier-pack-unpack branch 2 times, most recently from 032ef28 to dfb409c Compare March 19, 2026 13:09
@alexandre-daubois

Copy link
Copy Markdown
Member Author

PR updated to include the similar, second RFC.

@jrfnl

jrfnl commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

Just checking - what's the current status of this PR ? Both RFC votes passed, but it looks like this has not been merged yet.

@alexandre-daubois

Copy link
Copy Markdown
Member Author

Oh wow, I really forgot about this one. Just asked for new review. Thanks!

@alexandre-daubois
alexandre-daubois force-pushed the rfc-endianness-modifier-pack-unpack branch from dfb409c to 82fbd91 Compare August 24, 2026 05:43
Comment thread ext/standard/tests/strings/pack_endian_modifiers.phpt Outdated
Comment thread ext/standard/tests/strings/pack_endian_modifiers.phpt Outdated
Comment thread ext/standard/tests/strings/pack_endian_modifiers_32.phpt Outdated
Comment thread ext/standard/pack.c Outdated
Comment thread ext/standard/pack.c Outdated
@alexandre-daubois
alexandre-daubois force-pushed the rfc-endianness-modifier-pack-unpack branch 2 times, most recently from 9022795 to af2d0c5 Compare August 24, 2026 13:51

@Girgias Girgias left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think splitting the 64 bit specific tests to another tests is probably a good idea.

The implementation looks reasonable to me otherwise.

Comment thread ext/standard/tests/strings/pack_endian_modifiers.phpt Outdated
@alexandre-daubois
alexandre-daubois force-pushed the rfc-endianness-modifier-pack-unpack branch from af2d0c5 to fbaaafe Compare August 24, 2026 14:22
@alexandre-daubois
alexandre-daubois force-pushed the rfc-endianness-modifier-pack-unpack branch from fbaaafe to 4d729f8 Compare August 24, 2026 14:26
@alexandre-daubois

Copy link
Copy Markdown
Member Author

Also added entires in UPGRADING and NEWS, I forgot about that

@Girgias
Girgias merged commit 02229e2 into php:master Aug 24, 2026
18 checks passed
@Girgias

Girgias commented Aug 24, 2026

Copy link
Copy Markdown
Member

Merging, if there are issues with it can always be fixed for beta3, but at least end users can test this.

@alexandre-daubois

Copy link
Copy Markdown
Member Author

Thank you Gina!

Comment thread ext/standard/pack.c
Comment on lines +820 to +831
if (formatlen > 0) {
char c = *format;

if (c == '<') {
endian = PHP_LITTLE_ENDIAN;
format++;
formatlen--;
} else if (c == '>') {
endian = PHP_BIG_ENDIAN;
format++;
formatlen--;
}

@LamentXU123 LamentXU123 Aug 24, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is a bug when reviewing. Seems like unpack silently breaks existing key names beginning with < or >.

unpack('s<value', "\x02\x01"); // ["<value" => 258]
unpack('C>name', "\x01");      // [">name" => 1]

After this fix the first becomes ["value" => 258] as well as the second it becomes ValueError.

Or is this a feature?? Anyways, this should at least be documented as a BC break (UPGRADING) and add tests.

wdyt @Girgias @alexandre-daubois

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should document this. Since the feature is already pretty niche, I doubt that discontinuing support for names starting with a chevron will really be a problem. I'll create the PR if you agree

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #23446

@jrfnl

jrfnl commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

@alexandre-daubois Now this is merged, should the RFCs be moved from "Pending implementation" to "Implemented in PHP 8.6" ?

@NattyNarwhal

Copy link
Copy Markdown
Member

Tests pass on BE, FWIW

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add pack/unpack support for signed integers with specific endianness

6 participants