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: 121 141 85.8 %
Date: 2015-07-31 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)
      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);
      56          13 :                 zoiwc->destroy_it(iter);
      57             :         }
      58          13 : }
      59             : 
      60        1394 : U_CFUNC int zoi_with_current_valid(zend_object_iterator *iter)
      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)
      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)
      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)
      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);
      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));
      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);
      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)
     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);
     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));
     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);
     120             :         } else {
     121          10 :                 iter->funcs->move_forward(iter);
     122             :         }
     123          10 : }
     124             : 
     125           6 : static void string_enum_destroy_it(zend_object_iterator *iter)
     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)
     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);
     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)
     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);
     163             :         }
     164          12 :         intl_error_reset(INTLITERATOR_ERROR_P(ii));
     165             : 
     166          12 :         zend_object_std_dtor(&ii->zo);
     167          12 : }
     168             : 
     169           8 : static zend_object_iterator *IntlIterator_get_iterator(
     170             :         zend_class_entry *ce, zval *object, int by_ref)
     171             : {
     172           8 :         if (by_ref) {
     173             :                 zend_throw_exception(NULL,
     174           0 :                         "Iteration by reference is not supported", 0);
     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);
     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)
     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);
     198          12 :     object_properties_init(&intern->zo, ce);
     199          12 :         intl_error_init(INTLITERATOR_ERROR_P(intern));
     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);
     216           0 :                 return;
     217             :         }
     218             : 
     219          33 :         INTLITERATOR_METHOD_FETCH_OBJECT;
     220          33 :         data = ii->iterator->funcs->get_current_data(ii->iterator);
     221          33 :         if (data) {
     222          33 :                 ZVAL_DEREF(data);
     223          33 :                 ZVAL_COPY(return_value, data);
     224             :         }
     225             : }
     226             : 
     227          33 : static PHP_METHOD(IntlIterator, key)
     228             : {
     229          33 :         INTLITERATOR_METHOD_INIT_VARS;
     230             : 
     231          33 :         if (zend_parse_parameters_none() == FAILURE) {
     232             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
     233           0 :                         "IntlIterator::key: bad arguments", 0);
     234           0 :                 return;
     235             :         }
     236             : 
     237          33 :         INTLITERATOR_METHOD_FETCH_OBJECT;
     238             : 
     239          33 :         if (ii->iterator->funcs->get_current_key) {
     240          33 :                 ii->iterator->funcs->get_current_key(ii->iterator, return_value);
     241             :         } else {
     242           0 :                 RETURN_LONG(ii->iterator->index);
     243             :         }
     244             : }
     245             : 
     246          33 : static PHP_METHOD(IntlIterator, next)
     247             : {
     248          33 :         INTLITERATOR_METHOD_INIT_VARS;
     249             : 
     250          33 :         if (zend_parse_parameters_none() == FAILURE) {
     251             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
     252           0 :                         "IntlIterator::next: bad arguments", 0);
     253           0 :                 return;
     254             :         }
     255             : 
     256          33 :         INTLITERATOR_METHOD_FETCH_OBJECT;
     257          33 :         ii->iterator->funcs->move_forward(ii->iterator);
     258             :         /* foreach also advances the index after the last iteration,
     259             :          * so I see no problem in incrementing the index here unconditionally */
     260          33 :         ii->iterator->index++;
     261             : }
     262             : 
     263           8 : static PHP_METHOD(IntlIterator, rewind)
     264             : {
     265           8 :         INTLITERATOR_METHOD_INIT_VARS;
     266             : 
     267           8 :         if (zend_parse_parameters_none() == FAILURE) {
     268             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
     269           0 :                         "IntlIterator::rewind: bad arguments", 0);
     270           0 :                 return;
     271             :         }
     272             : 
     273           8 :         INTLITERATOR_METHOD_FETCH_OBJECT;
     274           8 :         if (ii->iterator->funcs->rewind) {
     275           8 :                 ii->iterator->funcs->rewind(ii->iterator);
     276             :         } else {
     277             :                 intl_errors_set(INTLITERATOR_ERROR_P(ii), U_UNSUPPORTED_ERROR,
     278           0 :                         "IntlIterator::rewind: rewind not supported", 0);
     279             :         }
     280             : }
     281             : 
     282          39 : static PHP_METHOD(IntlIterator, valid)
     283             : {
     284          39 :         INTLITERATOR_METHOD_INIT_VARS;
     285             : 
     286          39 :         if (zend_parse_parameters_none() == FAILURE) {
     287             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
     288           0 :                         "IntlIterator::valid: bad arguments", 0);
     289           0 :                 return;
     290             :         }
     291             : 
     292          39 :         INTLITERATOR_METHOD_FETCH_OBJECT;
     293          39 :         RETURN_BOOL(ii->iterator->funcs->valid(ii->iterator) == SUCCESS);
     294             : }
     295             : 
     296             : ZEND_BEGIN_ARG_INFO_EX(ainfo_se_void, 0, 0, 0)
     297             : ZEND_END_ARG_INFO()
     298             : 
     299             : static zend_function_entry IntlIterator_class_functions[] = {
     300             :         PHP_ME(IntlIterator,    current,        ainfo_se_void,                  ZEND_ACC_PUBLIC)
     301             :         PHP_ME(IntlIterator,    key,            ainfo_se_void,                  ZEND_ACC_PUBLIC)
     302             :         PHP_ME(IntlIterator,    next,           ainfo_se_void,                  ZEND_ACC_PUBLIC)
     303             :         PHP_ME(IntlIterator,    rewind,         ainfo_se_void,                  ZEND_ACC_PUBLIC)
     304             :         PHP_ME(IntlIterator,    valid,          ainfo_se_void,                  ZEND_ACC_PUBLIC)
     305             :         PHP_FE_END
     306             : };
     307             : 
     308             : 
     309             : /* {{{ intl_register_IntlIterator_class
     310             :  * Initialize 'IntlIterator' class
     311             :  */
     312       21049 : U_CFUNC void intl_register_IntlIterator_class(void)
     313             : {
     314             :         zend_class_entry ce;
     315             : 
     316             :         /* Create and register 'IntlIterator' class. */
     317       21049 :         INIT_CLASS_ENTRY(ce, "IntlIterator", IntlIterator_class_functions);
     318       21049 :         ce.create_object = IntlIterator_object_create;
     319       21049 :         IntlIterator_ce_ptr = zend_register_internal_class(&ce);
     320       21049 :         IntlIterator_ce_ptr->get_iterator = IntlIterator_get_iterator;
     321             :         zend_class_implements(IntlIterator_ce_ptr, 1,
     322       21049 :                 zend_ce_iterator);
     323             : 
     324             :         memcpy(&IntlIterator_handlers, zend_get_std_object_handlers(),
     325       21049 :                 sizeof IntlIterator_handlers);
     326       21049 :         IntlIterator_handlers.offset = XtOffsetOf(IntlIterator_object, zo);
     327       21049 :         IntlIterator_handlers.clone_obj = NULL;
     328       21049 :         IntlIterator_handlers.dtor_obj = zend_objects_destroy_object;
     329       21049 :         IntlIterator_handlers.free_obj = IntlIterator_objects_free;
     330             : 
     331       21049 : }

Generated by: LCOV version 1.10

Generated at Fri, 31 Jul 2015 08:58:50 +0000 (10 hours ago)

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