Skip to content

Commit

Permalink
- Added QuickHash::loadFromString().
Browse files Browse the repository at this point in the history
SVN Rev: 531
  • Loading branch information
derickr committed Jan 25, 2010
1 parent 98aec44 commit a1c2733
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 5 deletions.
68 changes: 63 additions & 5 deletions qh_intset.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,19 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_qh_intset_save_to_file, 0, 0, 1)
ZEND_ARG_INFO(0, filename)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_qh_intset_load_from_string, 0, 0, 1)
ZEND_ARG_INFO(0, contents)
ZEND_ARG_INFO(0, options)
ZEND_END_ARG_INFO()

/* Class methods definition */
zend_function_entry qh_funcs_intset[] = {
PHP_ME(QuickHashIntSet, __construct, arginfo_qh_intset_construct, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC)
PHP_ME(QuickHashIntSet, add, arginfo_qh_intset_add, ZEND_ACC_PUBLIC)
PHP_ME(QuickHashIntSet, exists, arginfo_qh_intset_exists, ZEND_ACC_PUBLIC)
PHP_ME(QuickHashIntSet, loadFromFile, arginfo_qh_intset_load_from_file, ZEND_ACC_STATIC|ZEND_ACC_PUBLIC)
PHP_ME(QuickHashIntSet, saveToFile, arginfo_qh_intset_save_to_file, ZEND_ACC_PUBLIC)
PHP_ME(QuickHashIntSet, __construct, arginfo_qh_intset_construct, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC)
PHP_ME(QuickHashIntSet, add, arginfo_qh_intset_add, ZEND_ACC_PUBLIC)
PHP_ME(QuickHashIntSet, exists, arginfo_qh_intset_exists, ZEND_ACC_PUBLIC)
PHP_ME(QuickHashIntSet, loadFromFile, arginfo_qh_intset_load_from_file, ZEND_ACC_STATIC|ZEND_ACC_PUBLIC)
PHP_ME(QuickHashIntSet, saveToFile, arginfo_qh_intset_save_to_file, ZEND_ACC_PUBLIC)
PHP_ME(QuickHashIntSet, loadFromString, arginfo_qh_intset_load_from_string, ZEND_ACC_STATIC|ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};

Expand Down Expand Up @@ -351,3 +357,55 @@ PHP_METHOD(QuickHashIntSet, saveToFile)
php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC);
}
/* }}} */

static uint32_t qh_intset_initialize_from_string(php_qh_intset_obj *obj, char *contents, long length, long flags TSRMLS_DC)
{
uint32_t nr_of_elements;
qho *options = qho_create();

// deal with options
process_flags(options, flags);

// if the size is not an increment of 4, abort
if (length % 4 != 0) {
qho_free(options);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "String is in the wrong format (not a multiple of 4 bytes)");
return 0;
}
nr_of_elements = length / 4;

// override the nr of bucket lists as we know better
options->size = nr_of_elements;

// create the hash
obj->hash = qhi_create(options);
if (obj->hash == NULL) {
qho_free(options);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't create set");
return 0;
}

// read the elements and add them to the set
qhi_set_add_elements_from_buffer(obj->hash, contents, nr_of_elements);
return nr_of_elements;
}

/* {{{ proto QuickHashIntSet QuickHashIntSet::loadFromString( string contents [, int options ] )
Creates a QuickHashIntSet from data in a string */
PHP_METHOD(QuickHashIntSet, loadFromString)
{
char *contents;
int contents_len;
long options = 0;
uint32_t added_elements;

php_set_error_handling(EH_THROW, NULL TSRMLS_CC);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &contents, &contents_len, &options) == FAILURE) {
return;
}

qh_instantiate(qh_ce_intset, return_value TSRMLS_CC);
added_elements = qh_intset_initialize_from_string(zend_object_store_get_object(return_value TSRMLS_CC), contents, contents_len, options);
php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC);
}
/* }}} */
1 change: 1 addition & 0 deletions qh_intset.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ PHP_METHOD(QuickHashIntSet, add);
PHP_METHOD(QuickHashIntSet, exists);
PHP_METHOD(QuickHashIntSet, loadFromFile);
PHP_METHOD(QuickHashIntSet, saveToFile);
PHP_METHOD(QuickHashIntSet, loadFromString);

void qh_register_class_intset(TSRMLS_D);

Expand Down
65 changes: 65 additions & 0 deletions tests/int-hash-from-string-failures.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
--TEST--
Test for failure conditions for loadFromString.
--INI--
extension=quickhash.so
xdebug.default_enable=0
--FILE--
<?php
echo "\nWrong params: \n";
try
{
$hash = QuickHashIntSet::loadFromString();
}
catch( Exception $e )
{
echo $e->getMessage(), "\n";
}

try
{
$hash = QuickHashIntSet::loadFromString( 1024, 2, 'stuff' );
}
catch( Exception $e )
{
echo $e->getMessage(), "\n";
}

try
{
$hash = QuickHashIntSet::loadFromString( 1024, 'stuff' );
}
catch( Exception $e )
{
echo $e->getMessage(), "\n";
}

try
{
$hash = QuickHashIntSet::loadFromString( new StdClass );
}
catch( Exception $e )
{
echo $e->getMessage(), "\n";
}

echo "\nWrong size: \n";
try
{
$contents = file_get_contents( dirname( __FILE__ ) . "/broken-file.set" );
$hash = QuickHashIntSet::loadFromString( $contents );
}
catch( Exception $e )
{
echo $e->getMessage(), "\n";
}
?>
--EXPECT--

Wrong params:
QuickHashIntSet::loadFromString() expects at least 1 parameter, 0 given
QuickHashIntSet::loadFromString() expects at most 2 parameters, 3 given
QuickHashIntSet::loadFromString() expects parameter 2 to be long, string given
QuickHashIntSet::loadFromString() expects parameter 1 to be string, object given

Wrong size:
QuickHashIntSet::loadFromString(): String is in the wrong format (not a multiple of 4 bytes)
48 changes: 48 additions & 0 deletions tests/int-hash-from-string1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
--TEST--
Basic test for reading sets from strings.
--INI--
extension=quickhash.so
xdebug.default_enable=0
--FILE--
<?php
$file = dirname( __FILE__ ) . "/simple.set";
$contents = file_get_contents( $file );
$hash = QuickHashIntSet::loadFromString( $contents, QuickHashIntSet::DO_NOT_USE_ZEND_ALLOC );
foreach( range( 0, 0x1f ) as $key )
{
printf( "Key %3d (%2x) is %s\n", $key, $key, $hash->exists( $key ) ? 'set' : 'unset' );
}
?>
--EXPECT--
Key 0 ( 0) is unset
Key 1 ( 1) is set
Key 2 ( 2) is set
Key 3 ( 3) is set
Key 4 ( 4) is unset
Key 5 ( 5) is set
Key 6 ( 6) is unset
Key 7 ( 7) is set
Key 8 ( 8) is unset
Key 9 ( 9) is unset
Key 10 ( a) is unset
Key 11 ( b) is set
Key 12 ( c) is unset
Key 13 ( d) is set
Key 14 ( e) is unset
Key 15 ( f) is unset
Key 16 (10) is unset
Key 17 (11) is set
Key 18 (12) is unset
Key 19 (13) is set
Key 20 (14) is unset
Key 21 (15) is unset
Key 22 (16) is unset
Key 23 (17) is set
Key 24 (18) is unset
Key 25 (19) is unset
Key 26 (1a) is unset
Key 27 (1b) is set
Key 28 (1c) is unset
Key 29 (1d) is set
Key 30 (1e) is unset
Key 31 (1f) is set

0 comments on commit a1c2733

Please sign in to comment.