- 
                Notifications
    You must be signed in to change notification settings 
- Fork 8k
Add array_is_list(array $array) function (rebased) #6070
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
          
     Closed
      
      
    
  
     Closed
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -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) | ||
| { | ||
| zend_long expected_idx = 0; | ||
| zend_long num_idx; | ||
| zend_string* str_idx; | ||
| /* Empty arrays are lists */ | ||
| if (zend_hash_num_elements(array) == 0) { | ||
| return 1; | ||
| } | ||
|  | ||
| /* Packed arrays are lists */ | ||
| if (HT_IS_PACKED(array) && HT_IS_WITHOUT_HOLES(array)) { | ||
|         
                  nikic marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| return 1; | ||
| } | ||
|  | ||
| /* 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 commentThe reason will be displayed to describe this comment to others. Learn more. Could we rebase against the master, and try to use  | ||
| if (str_idx != NULL || num_idx != expected_idx++) { | ||
| return 0; | ||
| } | ||
| } ZEND_HASH_FOREACH_END(); | ||
|  | ||
| return 1; | ||
| } | ||
|  | ||
|  | ||
| static zend_always_inline zval *_zend_hash_append_ex(HashTable *ht, zend_string *key, zval *zv, bool interned) | ||
| { | ||
| uint32_t idx = ht->nNumUsed++; | ||
|  | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| --TEST-- | ||
| Test array_is_list() function | ||
| --FILE-- | ||
| <?php | ||
|  | ||
| function test_is_list(string $desc, $val) : void { | ||
| try { | ||
| printf("%s: %s\n", $desc, json_encode(array_is_list($val))); | ||
| } catch (TypeError $e) { | ||
| printf("%s: threw %s\n", $desc, $e->getMessage()); | ||
| } | ||
| } | ||
|  | ||
| test_is_list("empty", []); | ||
| test_is_list("one", [1]); | ||
| test_is_list("two", [1,2]); | ||
| test_is_list("three", [1,2,3]); | ||
| test_is_list("four", [1,2,3,4]); | ||
| test_is_list("ten", range(0, 10)); | ||
|  | ||
| test_is_list("null", null); | ||
| test_is_list("int", 123); | ||
| test_is_list("float", 1.23); | ||
| test_is_list("string", "string"); | ||
| test_is_list("object", new stdClass()); | ||
| test_is_list("true", true); | ||
| test_is_list("false", false); | ||
|  | ||
| test_is_list("string key", ["a" => 1]); | ||
| test_is_list("mixed keys", [0 => 0, "a" => 1]); | ||
| test_is_list("ordered keys", [0 => 0, 1 => 1]); | ||
| test_is_list("shuffled keys", [1 => 0, 0 => 1]); | ||
| test_is_list("skipped keys", [0 => 0, 2 => 2]); | ||
|  | ||
| $arr = [1, 2, 3]; | ||
| unset($arr[0]); | ||
| test_is_list("unset first", $arr); | ||
|  | ||
| $arr = [1, 2, 3]; | ||
| unset($arr[1]); | ||
| test_is_list("unset middle", $arr); | ||
|  | ||
| $arr = [1, 2, 3]; | ||
| unset($arr[2]); | ||
| test_is_list("unset end", $arr); | ||
|  | ||
| $arr = [1, "a" => "a", 2]; | ||
| unset($arr["a"]); | ||
| test_is_list("unset string key", $arr); | ||
|  | ||
| $arr = [1 => 1, 0 => 0]; | ||
| unset($arr[1]); | ||
| test_is_list("unset into order", $arr); | ||
|  | ||
| $arr = ["a" => 1]; | ||
| unset($arr["a"]); | ||
| test_is_list("unset to empty", $arr); | ||
|  | ||
| $arr = [1, 2, 3]; | ||
| $arr[] = 4; | ||
| test_is_list("append implicit", $arr); | ||
|  | ||
| $arr = [1, 2, 3]; | ||
| $arr[3] = 4; | ||
| test_is_list("append explicit", $arr); | ||
|  | ||
| $arr = [1, 2, 3]; | ||
| $arr[4] = 5; | ||
| test_is_list("append with gap", $arr); | ||
|  | ||
| --EXPECT-- | ||
| empty: true | ||
| one: true | ||
| two: true | ||
| three: true | ||
| four: true | ||
| ten: true | ||
| null: threw array_is_list(): Argument #1 ($array) must be of type array, null given | ||
| int: threw array_is_list(): Argument #1 ($array) must be of type array, int given | ||
| float: threw array_is_list(): Argument #1 ($array) must be of type array, float given | ||
| string: threw array_is_list(): Argument #1 ($array) must be of type array, string given | ||
| object: threw array_is_list(): Argument #1 ($array) must be of type array, stdClass given | ||
| true: threw array_is_list(): Argument #1 ($array) must be of type array, bool given | ||
| false: threw array_is_list(): Argument #1 ($array) must be of type array, bool given | ||
| string key: false | ||
| mixed keys: false | ||
| ordered keys: true | ||
| shuffled keys: false | ||
| skipped keys: false | ||
| unset first: false | ||
| unset middle: false | ||
| unset end: true | ||
| unset string key: true | ||
| unset into order: true | ||
| unset to empty: true | ||
| append implicit: true | ||
| append explicit: true | ||
| append with gap: false | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
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.