PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/intl/common - common_enum.cpp (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 120 140 85.7 %
Date: 2014-10-14 Functions: 17 17 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | This source file is subject to version 3.01 of the PHP license,      |
       6             :    | that is bundled with this package in the file LICENSE, and is        |
       7             :    | available through the world-wide-web at the following url:           |
       8             :    | http://www.php.net/license/3_01.txt                                  |
       9             :    | If you did not receive a copy of the PHP license and are unable to   |
      10             :    | obtain it through the world-wide-web, please send a note to          |
      11             :    | license@php.net so we can mail you a copy immediately.               |
      12             :    +----------------------------------------------------------------------+
      13             :    | Authors: Gustavo Lopes <cataphract@php.net>                          |
      14             :    +----------------------------------------------------------------------+
      15             : */
      16             : 
      17             : #ifdef HAVE_CONFIG_H
      18             : #include "config.h"
      19             : #endif
      20             : 
      21             : #include "../intl_cppshims.h"
      22             : 
      23             : // Fix build on Windows/old versions of ICU
      24             : #include <stdio.h>
      25             : 
      26             : #include "common_enum.h"
      27             : 
      28             : extern "C" {
      29             : #include <zend_interfaces.h>
      30             : #include <zend_exceptions.h>
      31             : }
      32             : 
      33             : zend_class_entry *IntlIterator_ce_ptr;
      34             : zend_object_handlers IntlIterator_handlers;
      35             : 
      36          13 : void zoi_with_current_dtor(zend_object_iterator *iter TSRMLS_DC)
      37             : {
      38          13 :         zoi_with_current *zoiwc = (zoi_with_current*)iter;
      39             :         
      40          26 :         if (!Z_ISUNDEF(zoiwc->wrapping_obj)) {
      41             :                 /* we have to copy the pointer because zoiwc->wrapping_obj may be
      42             :                  * changed midway the execution of zval_ptr_dtor() */
      43           0 :                 zval *zwo = &zoiwc->wrapping_obj;
      44             :                 
      45             :                 /* object is still here, we can rely on it to call this again and
      46             :                  * destroy this object */
      47           0 :                 zval_ptr_dtor(zwo);
      48             :         } else {
      49             :                 /* Object not here anymore (we've been called by the object free handler)
      50             :                  * Note that the iterator wrapper objects (that also depend on this
      51             :                  * structure) call this function earlier, in the destruction phase, which
      52             :                  * precedes the object free phase. Therefore there's no risk on this
      53             :                  * function being called by the iterator wrapper destructor function and
      54             :                  * not finding the memory of this iterator allocated anymore. */
      55          13 :                 iter->funcs->invalidate_current(iter TSRMLS_CC);
      56          13 :                 zoiwc->destroy_it(iter TSRMLS_CC);
      57             :         }
      58          13 : }
      59             : 
      60        1394 : U_CFUNC int zoi_with_current_valid(zend_object_iterator *iter TSRMLS_DC)
      61             : {
      62        2788 :         return Z_ISUNDEF(((zoi_with_current*)iter)->current)? FAILURE : SUCCESS;
      63             : }
      64             : 
      65        1379 : U_CFUNC zval *zoi_with_current_get_current_data(zend_object_iterator *iter TSRMLS_DC)
      66             : {
      67        1379 :         return &((zoi_with_current*)iter)->current;
      68             : }
      69             : 
      70        1410 : U_CFUNC void zoi_with_current_invalidate_current(zend_object_iterator *iter TSRMLS_DC)
      71             : {
      72        1410 :         zoi_with_current *zoi_iter = (zoi_with_current*)iter;
      73        2820 :         if (!Z_ISUNDEF(zoi_iter->current)) {
      74        1381 :                 zval_ptr_dtor(&zoi_iter->current);
      75        1381 :                 ZVAL_UNDEF(&zoi_iter->current); //valid would return FAILURE now
      76             :         }
      77        1410 : }
      78             : 
      79        1340 : static void string_enum_current_move_forward(zend_object_iterator *iter TSRMLS_DC)
      80             : {
      81        1340 :         zoi_with_current *zoi_iter = (zoi_with_current*)iter;
      82        1340 :         INTLITERATOR_METHOD_INIT_VARS;
      83             : 
      84        1340 :         iter->funcs->invalidate_current(iter TSRMLS_CC);
      85             : 
      86        1340 :         object = &zoi_iter->wrapping_obj;
      87        1340 :         INTLITERATOR_METHOD_FETCH_OBJECT_NO_CHECK;
      88             : 
      89             :         int32_t result_length;
      90             :         const char *result = ((StringEnumeration*)Z_PTR(iter->data))->next(
      91        1340 :                 &result_length, INTLITERATOR_ERROR_CODE(ii));
      92             : 
      93        1340 :         intl_error_set_code(NULL, INTLITERATOR_ERROR_CODE(ii) TSRMLS_CC);
      94        1340 :         if (U_FAILURE(INTLITERATOR_ERROR_CODE(ii))) {
      95             :                 intl_errors_set_custom_msg(INTL_DATA_ERROR_P(ii),
      96           0 :                         "Error fetching next iteration element", 0 TSRMLS_CC);
      97        1340 :         } else if (result) {
      98        2664 :                 ZVAL_STRINGL(&zoi_iter->current, result, result_length);
      99             :         } //else we've reached the end of the enum, nothing more is required
     100        1340 : }
     101             : 
     102          10 : static void string_enum_rewind(zend_object_iterator *iter TSRMLS_DC)
     103             : {
     104          10 :         zoi_with_current *zoi_iter = (zoi_with_current*)iter;
     105          10 :         INTLITERATOR_METHOD_INIT_VARS;
     106             : 
     107          20 :         if (!Z_ISUNDEF(zoi_iter->current)) {
     108           2 :                 iter->funcs->invalidate_current(iter TSRMLS_CC);
     109             :         }
     110             : 
     111          10 :         object = &zoi_iter->wrapping_obj;
     112          10 :         INTLITERATOR_METHOD_FETCH_OBJECT_NO_CHECK;
     113             : 
     114          10 :         ((StringEnumeration*)Z_PTR(iter->data))->reset(INTLITERATOR_ERROR_CODE(ii));
     115             : 
     116          10 :         intl_error_set_code(NULL, INTLITERATOR_ERROR_CODE(ii) TSRMLS_CC);
     117          10 :         if (U_FAILURE(INTLITERATOR_ERROR_CODE(ii))) {
     118             :                 intl_errors_set_custom_msg(INTL_DATA_ERROR_P(ii),
     119           0 :                         "Error resetting enumeration", 0 TSRMLS_CC);
     120             :         } else {
     121          10 :                 iter->funcs->move_forward(iter TSRMLS_CC);
     122             :         }
     123          10 : }
     124             : 
     125           6 : static void string_enum_destroy_it(zend_object_iterator *iter TSRMLS_DC)
     126             : {
     127           6 :         delete (StringEnumeration*)Z_PTR(iter->data);
     128           6 : }
     129             : 
     130             : static zend_object_iterator_funcs string_enum_object_iterator_funcs = {
     131             :         zoi_with_current_dtor,
     132             :         zoi_with_current_valid,
     133             :         zoi_with_current_get_current_data,
     134             :         NULL,
     135             :         string_enum_current_move_forward,
     136             :         string_enum_rewind,
     137             :         zoi_with_current_invalidate_current
     138             : };
     139             : 
     140           6 : U_CFUNC void IntlIterator_from_StringEnumeration(StringEnumeration *se, zval *object TSRMLS_DC)
     141             : {
     142             :         IntlIterator_object *ii;
     143           6 :         object_init_ex(object, IntlIterator_ce_ptr);
     144           6 :         ii = Z_INTL_ITERATOR_P(object);
     145           6 :         ii->iterator = (zend_object_iterator*)emalloc(sizeof(zoi_with_current));
     146           6 :         zend_iterator_init(ii->iterator TSRMLS_CC);
     147           6 :         ZVAL_PTR(&ii->iterator->data, se);
     148           6 :         ii->iterator->funcs = &string_enum_object_iterator_funcs;
     149           6 :         ii->iterator->index = 0;
     150           6 :         ((zoi_with_current*)ii->iterator)->destroy_it = string_enum_destroy_it;
     151           6 :         ZVAL_COPY_VALUE(&((zoi_with_current*)ii->iterator)->wrapping_obj, object);
     152           6 :         ZVAL_UNDEF(&((zoi_with_current*)ii->iterator)->current);
     153           6 : }
     154             : 
     155          12 : static void IntlIterator_objects_free(zend_object *object TSRMLS_DC)
     156             : {
     157          12 :         IntlIterator_object     *ii = php_intl_iterator_fetch_object(object);
     158             : 
     159          12 :         if (ii->iterator) {
     160          12 :                 zval *wrapping_objp = &((zoi_with_current*)ii->iterator)->wrapping_obj;
     161          12 :                 ZVAL_UNDEF(wrapping_objp);
     162          12 :                 zend_iterator_dtor(ii->iterator TSRMLS_CC);
     163             :         }
     164          12 :         intl_error_reset(INTLITERATOR_ERROR_P(ii) TSRMLS_CC);
     165             : 
     166          12 :         zend_object_std_dtor(&ii->zo TSRMLS_CC);
     167          12 : }
     168             : 
     169           8 : static zend_object_iterator *IntlIterator_get_iterator(
     170             :         zend_class_entry *ce, zval *object, int by_ref TSRMLS_DC)
     171             : {
     172           8 :         if (by_ref) {
     173             :                 zend_throw_exception(NULL,
     174           0 :                         "Iteration by reference is not supported", 0 TSRMLS_CC);
     175           0 :                 return NULL;
     176             :         }
     177             : 
     178           8 :         IntlIterator_object *ii = Z_INTL_ITERATOR_P(object);
     179             : 
     180           8 :         if (ii->iterator == NULL) {
     181             :                 zend_throw_exception(NULL,
     182           0 :                         "The IntlIterator is not properly constructed", 0 TSRMLS_CC);
     183           0 :                 return NULL;
     184             :         }
     185             : 
     186           8 :         ++GC_REFCOUNT(ii->iterator);
     187             : 
     188           8 :         return ii->iterator;
     189             : }
     190             : 
     191          12 : static zend_object *IntlIterator_object_create(zend_class_entry *ce TSRMLS_DC)
     192             : {
     193             :         IntlIterator_object     *intern;
     194             : 
     195          12 :         intern = (IntlIterator_object*)ecalloc(1, sizeof(IntlIterator_object) + sizeof(zval) * (ce->default_properties_count - 1));
     196             :         
     197          12 :         zend_object_std_init(&intern->zo, ce TSRMLS_CC);
     198          12 :     object_properties_init(&intern->zo, ce);
     199          12 :         intl_error_init(INTLITERATOR_ERROR_P(intern) TSRMLS_CC);
     200             : 
     201          12 :         intern->iterator = NULL;
     202             : 
     203          12 :         intern->zo.handlers = &IntlIterator_handlers;
     204             : 
     205          12 :         return &intern->zo;
     206             : }
     207             : 
     208          33 : static PHP_METHOD(IntlIterator, current)
     209             : {
     210             :         zval *data;
     211          33 :         INTLITERATOR_METHOD_INIT_VARS;
     212             : 
     213          33 :         if (zend_parse_parameters_none() == FAILURE) {
     214             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
     215           0 :                         "IntlIterator::current: bad arguments", 0 TSRMLS_CC);
     216           0 :                 return;
     217             :         }
     218             : 
     219          33 :         INTLITERATOR_METHOD_FETCH_OBJECT;
     220          33 :         data = ii->iterator->funcs->get_current_data(ii->iterator TSRMLS_CC);
     221          33 :         if (data) {
     222          99 :                 RETURN_ZVAL(data, 1, 0);
     223             :         }
     224             : }
     225             : 
     226          33 : static PHP_METHOD(IntlIterator, key)
     227             : {
     228          33 :         INTLITERATOR_METHOD_INIT_VARS;
     229             : 
     230          33 :         if (zend_parse_parameters_none() == FAILURE) {
     231             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
     232           0 :                         "IntlIterator::key: bad arguments", 0 TSRMLS_CC);
     233           0 :                 return;
     234             :         }
     235             : 
     236          33 :         INTLITERATOR_METHOD_FETCH_OBJECT;
     237             : 
     238          33 :         if (ii->iterator->funcs->get_current_key) {
     239          33 :                 ii->iterator->funcs->get_current_key(ii->iterator, return_value TSRMLS_CC);
     240             :         } else {
     241           0 :                 RETURN_LONG(ii->iterator->index);
     242             :         }
     243             : }
     244             : 
     245          33 : static PHP_METHOD(IntlIterator, next)
     246             : {
     247          33 :         INTLITERATOR_METHOD_INIT_VARS;
     248             : 
     249          33 :         if (zend_parse_parameters_none() == FAILURE) {
     250             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
     251           0 :                         "IntlIterator::next: bad arguments", 0 TSRMLS_CC);
     252           0 :                 return;
     253             :         }
     254             : 
     255          33 :         INTLITERATOR_METHOD_FETCH_OBJECT;
     256          33 :         ii->iterator->funcs->move_forward(ii->iterator TSRMLS_CC);
     257             :         /* foreach also advances the index after the last iteration,
     258             :          * so I see no problem in incrementing the index here unconditionally */
     259          33 :         ii->iterator->index++;
     260             : }
     261             : 
     262           8 : static PHP_METHOD(IntlIterator, rewind)
     263             : {
     264           8 :         INTLITERATOR_METHOD_INIT_VARS;
     265             : 
     266           8 :         if (zend_parse_parameters_none() == FAILURE) {
     267             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
     268           0 :                         "IntlIterator::rewind: bad arguments", 0 TSRMLS_CC);
     269           0 :                 return;
     270             :         }
     271             : 
     272           8 :         INTLITERATOR_METHOD_FETCH_OBJECT;
     273           8 :         if (ii->iterator->funcs->rewind) {
     274           8 :                 ii->iterator->funcs->rewind(ii->iterator TSRMLS_CC);
     275             :         } else {
     276             :                 intl_errors_set(INTLITERATOR_ERROR_P(ii), U_UNSUPPORTED_ERROR,
     277           0 :                         "IntlIterator::rewind: rewind not supported", 0 TSRMLS_CC);
     278             :         }
     279             : }
     280             : 
     281          39 : static PHP_METHOD(IntlIterator, valid)
     282             : {
     283          39 :         INTLITERATOR_METHOD_INIT_VARS;
     284             : 
     285          39 :         if (zend_parse_parameters_none() == FAILURE) {
     286             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
     287           0 :                         "IntlIterator::valid: bad arguments", 0 TSRMLS_CC);
     288           0 :                 return;
     289             :         }
     290             : 
     291          39 :         INTLITERATOR_METHOD_FETCH_OBJECT;
     292          39 :         RETURN_BOOL(ii->iterator->funcs->valid(ii->iterator TSRMLS_CC) == SUCCESS);
     293             : }
     294             : 
     295             : ZEND_BEGIN_ARG_INFO_EX(ainfo_se_void, 0, 0, 0)
     296             : ZEND_END_ARG_INFO()
     297             : 
     298             : static zend_function_entry IntlIterator_class_functions[] = {
     299             :         PHP_ME(IntlIterator,    current,        ainfo_se_void,                  ZEND_ACC_PUBLIC)
     300             :         PHP_ME(IntlIterator,    key,            ainfo_se_void,                  ZEND_ACC_PUBLIC)
     301             :         PHP_ME(IntlIterator,    next,           ainfo_se_void,                  ZEND_ACC_PUBLIC)
     302             :         PHP_ME(IntlIterator,    rewind,         ainfo_se_void,                  ZEND_ACC_PUBLIC)
     303             :         PHP_ME(IntlIterator,    valid,          ainfo_se_void,                  ZEND_ACC_PUBLIC)
     304             :         PHP_FE_END
     305             : };
     306             : 
     307             : 
     308             : /* {{{ intl_register_IntlIterator_class
     309             :  * Initialize 'IntlIterator' class
     310             :  */
     311       20401 : U_CFUNC void intl_register_IntlIterator_class(TSRMLS_D)
     312             : {
     313             :         zend_class_entry ce;
     314             : 
     315             :         /* Create and register 'IntlIterator' class. */
     316       20401 :         INIT_CLASS_ENTRY(ce, "IntlIterator", IntlIterator_class_functions);
     317       20401 :         ce.create_object = IntlIterator_object_create;
     318       20401 :         IntlIterator_ce_ptr = zend_register_internal_class(&ce TSRMLS_CC);
     319       20401 :         IntlIterator_ce_ptr->get_iterator = IntlIterator_get_iterator;
     320             :         zend_class_implements(IntlIterator_ce_ptr TSRMLS_CC, 1,
     321       20401 :                 zend_ce_iterator);
     322             : 
     323             :         memcpy(&IntlIterator_handlers, zend_get_std_object_handlers(),
     324       20401 :                 sizeof IntlIterator_handlers);
     325       20401 :         IntlIterator_handlers.offset = XtOffsetOf(IntlIterator_object, zo);
     326       20401 :         IntlIterator_handlers.clone_obj = NULL;
     327       20401 :         IntlIterator_handlers.dtor_obj = zend_objects_destroy_object;
     328       20401 :         IntlIterator_handlers.free_obj = IntlIterator_objects_free;
     329             : 
     330       20401 : }

Generated by: LCOV version 1.10

Generated at Tue, 14 Oct 2014 07:25:41 +0000 (7 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.