php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #79153 function to copy arrays
Submitted: 2020-01-21 22:04 UTC Modified: 2021-09-16 21:31 UTC
From: flip101 at gmail dot com Assigned:
Status: Open Package: FFI (PECL)
PHP Version: 7.4.1 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: flip101 at gmail dot com
New email:
PHP Version: OS:

 

 [2020-01-21 22:04 UTC] flip101 at gmail dot com
Description:
------------
On example #5 in https://www.php.net/manual/en/ffi.examples-basic.php it says "work with it like with a regular PHP array".

But when i assign values directly
$a = [1, 2, 3, 4];

The type seems to change from `FFI\CData` to PHP `array`. It would be useful to have an array copy method so a copy loop (which seems?? the only way for the moment) doesn't need to be done in php itself. Although pack() could be used as well, but is that the best way?


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-01-21 22:38 UTC] flip101 at gmail dot com
I wrote this piece of code, not saying it's the best. But perhaps some of the logic here is better implemented in C.

function arrayCreate(string $type, array $source) {
  $size = count($source);

  if ('char' === $type) {
    $format = 'c'; $bytes_multiplier = 1;
  } elseif ('unsigned char' === $type) {
    $format = 'C'; $bytes_multiplier = 1;
  } elseif ('short' === $type) {
    $format = 's'; $bytes_multiplier = 2;
  } elseif ('unsigned short' === $type) {
    $format = 'S'; $bytes_multiplier = 2;
  } elseif ('int' === $type) {
    $format = 'i'; $bytes_multiplier = 4; // assumption: https://stackoverflow.com/a/11438838/1833322
  } elseif ('unsigned int' === $type) {
    $format = 'I'; $bytes_multiplier = 4;
  } elseif ('long' === $type) {
    $format = 'l'; $bytes_multiplier = 4;
  } elseif ('unsigned long' === $type) {
    $format = 'L'; $bytes_multiplier = 4;
  } elseif ('long long' === $type) {
    $format = 'q'; $bytes_multiplier = 8;
  } elseif ('unsigned long long' === $type) {
    $format = 'Q'; $bytes_multiplier = 8;
  } elseif ('float' === $type) {
    $format = 'f'; $bytes_multiplier = 4;
  } elseif ('double' === $type) {
    $format = 'd'; $bytes_multiplier = 8;
  } else {
    throw new \InvalidArgumentException('type');
  }

  $first_type = gettype(reset($source));
  if (! array_reduce($source, function($c, $i) use ($first_type) { return $c && gettype($i) === $first_type; }, true)) {
    throw new \InvalidArgumentException('source');
  }

  $array = FFI::new($type . '[' . $size . ']');

  FFI::memcpy($array, pack($format . '*', ...$source), $size * $bytes_multiplier);

  return $array;
}
 [2020-01-23 13:33 UTC] sjon@php.net
-Package: ffi +Package: *Extensibility Functions
 [2021-09-16 21:31 UTC] cmb@php.net
-Package: *Extensibility Functions +Package: FFI
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 23:01:27 2024 UTC