-
Notifications
You must be signed in to change notification settings - Fork 7.8k
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
Add array_is_list(array $array) function (rebased) #6070
Conversation
} | ||
|
||
/* Check if the list could theoretically be repacked */ | ||
ZEND_HASH_FOREACH_KEY(array, num_idx, str_idx) { |
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.
Could we rebase against the master, and try to use ZEND_HASH_REVERSE_FOREACH_KEY
, just like what you said on #4886 (comment), Thanks!
I am concerned about the name because lists do not always contain sequential integers as indices https://www.php.net/manual/en/spl.datastructures.php
|
|
This function tests if an array contains only sequential integer keys. While list isn't an official type, this usage is consistent with the community usage of "list" as an annotation type, cf. https://psalm.dev/docs/annotating_code/type_syntax/array_types/#lists Rebased and modified version of php#4886 - Use .stub.php files - Add opcache constant evaluation when argument is a constant - Change from is_list(mixed $value) to array_is_list(array $array) RFC: https://wiki.php.net/rfc/is_list Co-Authored-By: Tyson Andre <tysonandre775@hotmail.com> Co-Authored-By: Dusk <dusk@woofle.net>
@@ -1188,6 +1188,33 @@ static zend_always_inline void *zend_hash_get_current_data_ptr_ex(HashTable *ht, | |||
ZEND_HASH_FILL_FINISH(); \ | |||
} while (0) | |||
|
|||
/* Check if an array is a list */ | |||
static zend_always_inline zend_bool zend_array_is_list(zend_array *array) |
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'd split this into an inlined fast path (num_elements + packed without holes check) and an out of line slow path (that does the full table scan).
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.
Though I guess with just two usages right now making the whole thing inline isn't terrible either.
This new function is not listed on php.net https://www.php.net/manual-lookup.php?pattern=array_is_list&scope=quickref |
The `nNextFreeElement` field is initialized with `ZEND_LONG_MIN` instead of zero since PHP 8.0. Adjust our `php_mp_is_hash()` check accordingly. See [1] and [2] for details. NB: PHP 8.1 introduces `zend_array_is_list()`, which may be used here. See [3] and [4] for details. [1]: https://wiki.php.net/rfc/negative_array_index [2]: php/php-src#3772 [3]: https://wiki.php.net/rfc/is_list [4]: php/php-src#6070 Since I don't observe any other problems on PHP 8.1, closing the relevant issue. Fixes tarantool#171
The `nNextFreeElement` field is initialized with `ZEND_LONG_MIN` instead of zero since PHP 8.0. Adjust our `php_mp_is_hash()` check accordingly. See [1] and [2] for details. NB: PHP 8.1 introduces `zend_array_is_list()`, which may be used here. See [3] and [4] for details. [1]: https://wiki.php.net/rfc/negative_array_index [2]: php/php-src#3772 [3]: https://wiki.php.net/rfc/is_list [4]: php/php-src#6070 Since I don't observe any other problems on PHP 8.1, closing the relevant issue. Fixes #171
Rebased version of #4886
RFC: https://wiki.php.net/rfc/is_list
An earlier version of this returned false for non-arrays instead of throwing a TypeError - this was updated to prevent confusion with list-like object types
This accepts any type, and deliberately returns false (with no notices) for all non-arrays, e.g. see the test output in the files changed. This is similar to is_callable(), is_numeric(), is_iterable(), etc, which also accept any type: