1
0
mirror of https://github.com/halaxa/json-machine.git synced 2025-01-17 13:08:16 +01:00

Ext reads streams

This commit is contained in:
Filip Halaxa 2023-04-30 17:43:38 +02:00
parent fad30b98a4
commit 21453601a5
4 changed files with 22 additions and 34 deletions

View File

@ -18,40 +18,29 @@
PHP_FUNCTION(jsonmachine_next_token)
{
unsigned short int boundary[256] = {0};
zend_string *jsonChunk;
bool finish = 0;
zend_array tokens[2];
zval *resource;
php_stream *stream;
ssize_t bytes_read;
char buffer[1024];
// (string $bytes, $finish = false)
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_STR(jsonChunk)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(finish)
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_RESOURCE(resource)
ZEND_PARSE_PARAMETERS_END();
// Treat UTF-8 BOM bytes as whitespace
boundary[0xEF] = 1;
boundary[0xBB] = 1;
boundary[0xBF] = 1;
php_stream_from_zval_no_verify(stream, resource);
boundary[' '] = 1;
boundary['\n'] = 1;
boundary['\r'] = 1;
boundary['\t'] = 1;
if (stream == NULL) {
php_error_docref(NULL, E_WARNING, "Invalid stream resource");
RETURN_NULL();
}
boundary['{'] = 2;
boundary['}'] = 2;
boundary['['] = 2;
boundary[']'] = 2;
boundary[':'] = 2;
boundary[','] = 2;
bytes_read = php_stream_read(stream, buffer, sizeof(buffer) - 1);
buffer[bytes_read] = '\0';
RETURN_ARR(tokens);
RETURN_STRING(buffer);
}
/* {{{ void test1() */
PHP_FUNCTION(test1)
{

View File

@ -6,4 +6,4 @@ function test1(): void {}
function test2(string $str = ""): string {}
function jsonmachine_next_token(string $jsonChunk, bool $finish = false): array {}
function jsonmachine_next_token(resource $jsonChunk): string {}

View File

@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 58d3cb761235950c70cafa30f73e24dad6845530 */
* Stub hash: 5ab3feae5bd01fa0681248e24d3b4c2cd7c66e96 */
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_test1, 0, 0, IS_VOID, 0)
ZEND_END_ARG_INFO()
@ -8,9 +8,8 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_test2, 0, 0, IS_STRING, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, str, IS_STRING, 0, "\"\"")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_jsonmachine_next_token, 0, 1, IS_ARRAY, 0)
ZEND_ARG_TYPE_INFO(0, jsonChunk, IS_STRING, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, finish, _IS_BOOL, 0, "false")
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_jsonmachine_next_token, 0, 1, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, resource, IS_RESOURCE, 0)
ZEND_END_ARG_INFO()

View File

@ -6,11 +6,11 @@ use PHPUnit\Framework\TestCase;
class ExtJsonmachineTest extends TestCase
{
public function xtestExtensionLoaded()
public function testExtensionLoaded()
{
$this->assertTrue(function_exists('jsonmachine_next_token'));
jsonmachine_next_token('{}');
jsonmachine_next_token('{}', true);
jsonmachine_next_token('{}', false);
var_dump(jsonmachine_next_token(fopen('data://text/plain,XXX', 'r')));
// jsonmachine_next_token('{}', true);
// jsonmachine_next_token('{}', false);
}
}