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/breakiterator - rulebasedbreakiterator_methods.cpp (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 52 81 64.2 %
Date: 2014-09-27 Functions: 6 6 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             : #include <unicode/rbbi.h>
      18             : 
      19             : extern "C" {
      20             : #define USE_BREAKITERATOR_POINTER 1
      21             : #include "breakiterator_class.h"
      22             : #include <zend_exceptions.h>
      23             : #include <limits.h>
      24             : }
      25             : 
      26             : #include "../intl_convertcpp.h"
      27             : 
      28          15 : static inline RuleBasedBreakIterator *fetch_rbbi(BreakIterator_object *bio) {
      29          15 :         return (RuleBasedBreakIterator*)bio->biter;
      30             : }
      31             : 
      32          11 : static void _php_intlrbbi_constructor_body(INTERNAL_FUNCTION_PARAMETERS)
      33             : {
      34          11 :         zval            *object         = getThis();
      35             :         char            *rules;
      36             :         size_t                  rules_len;
      37          11 :         zend_bool       compiled        = 0;
      38          11 :         UErrorCode      status          = U_ZERO_ERROR;
      39          11 :         intl_error_reset(NULL TSRMLS_CC);
      40             : 
      41          11 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b",
      42             :                         &rules, &rules_len, &compiled) == FAILURE) {
      43             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
      44           0 :                         "rbbi_create_instance: bad arguments", 0 TSRMLS_CC);
      45           0 :                 Z_OBJ_P(return_value) = NULL;
      46           0 :                 return;
      47             :         }
      48             : 
      49             :         // instantiation of ICU object
      50             :         RuleBasedBreakIterator *rbbi;
      51             : 
      52          11 :         if (!compiled) {
      53          11 :                 UnicodeString   rulesStr;
      54          11 :                 UParseError             parseError = UParseError();
      55          11 :                 if (intl_stringFromChar(rulesStr, rules, rules_len, &status)
      56             :                                 == FAILURE) {
      57             :                         intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
      58             :                                 "rbbi_create_instance: rules were not a valid UTF-8 string",
      59           0 :                                 0 TSRMLS_CC);
      60           0 :                         RETURN_NULL();
      61             :                 }
      62             : 
      63          11 :                 rbbi = new RuleBasedBreakIterator(rulesStr, parseError, status);
      64          11 :                 intl_error_set_code(NULL, status TSRMLS_CC);
      65          11 :                 if (U_FAILURE(status)) {
      66             :                         char *msg;
      67             :                         smart_str parse_error_str;
      68           0 :                         parse_error_str = intl_parse_error_to_string(&parseError);
      69             :                         spprintf(&msg, 0, "rbbi_create_instance: unable to create "
      70           0 :                                 "RuleBasedBreakIterator from rules (%s)", parse_error_str.s? parse_error_str.s->val : "");
      71             :                         smart_str_free(&parse_error_str);
      72           0 :                         intl_error_set_custom_msg(NULL, msg, 1 TSRMLS_CC);
      73           0 :                         efree(msg);
      74           0 :                         delete rbbi;
      75           0 :                         Z_OBJ_P(return_value) = NULL;
      76             :                         return;
      77           0 :                 }
      78             :         } else { // compiled
      79             : #if U_ICU_VERSION_MAJOR_NUM * 10 + U_ICU_VERSION_MINOR_NUM >= 48
      80             :                 rbbi = new RuleBasedBreakIterator((uint8_t*)rules, rules_len, status);
      81             :                 if (U_FAILURE(status)) {
      82             :                         intl_error_set(NULL, status, "rbbi_create_instance: unable to "
      83             :                                 "create instance from compiled rules", 0 TSRMLS_CC);
      84             :                         Z_OBJ_P(return_value) = NULL;
      85             :                         return;
      86             :                 }
      87             : #else
      88             :                 intl_error_set(NULL, U_UNSUPPORTED_ERROR, "rbbi_create_instance: "
      89           0 :                         "compiled rules require ICU >= 4.8", 0 TSRMLS_CC);
      90           0 :                 Z_OBJ_P(return_value) = NULL;
      91           0 :                 return;
      92             : #endif
      93             :         }
      94             : 
      95          11 :         breakiterator_object_create(return_value, rbbi, 0 TSRMLS_CC);
      96             : }
      97             : 
      98          11 : U_CFUNC PHP_METHOD(IntlRuleBasedBreakIterator, __construct)
      99             : {
     100          11 :         zval    orig_this               = *getThis();
     101             : 
     102          11 :         return_value = getThis();
     103          11 :         _php_intlrbbi_constructor_body(INTERNAL_FUNCTION_PARAM_PASSTHRU);
     104             : 
     105          11 :         if (Z_TYPE_P(return_value) == IS_OBJECT && Z_OBJ_P(return_value) == NULL) {
     106           0 :                 zend_object_store_ctor_failed(Z_OBJ(orig_this) TSRMLS_CC);
     107             :                 zval_dtor(&orig_this);
     108           0 :                 ZEND_CTOR_MAKE_NULL();
     109             :         }
     110          11 : }
     111             : 
     112           1 : U_CFUNC PHP_FUNCTION(rbbi_get_rules)
     113             : {
     114           1 :         BREAKITER_METHOD_INIT_VARS;
     115           1 :         object = getThis();
     116             : 
     117           1 :         if (zend_parse_parameters_none() == FAILURE) {
     118             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
     119           0 :                         "rbbi_get_rules: bad arguments", 0 TSRMLS_CC);
     120           0 :                 RETURN_FALSE;
     121             :         }
     122             : 
     123           1 :         BREAKITER_METHOD_FETCH_OBJECT;
     124             : 
     125             :         char *str;
     126             :         int str_len;
     127           1 :         const UnicodeString rules = fetch_rbbi(bio)->getRules();
     128             : 
     129           1 :         if (intl_charFromString(rules, &str, &str_len, BREAKITER_ERROR_CODE_P(bio)) == FAILURE)
     130             :         {
     131             :                 intl_errors_set(BREAKITER_ERROR_P(bio), BREAKITER_ERROR_CODE(bio),
     132             :                                 "rbbi_hash_code: Error converting result to UTF-8 string",
     133           0 :                                 0 TSRMLS_CC);
     134           0 :                 RETURN_FALSE;
     135             :         }
     136           2 :         RETVAL_STRINGL(str, str_len);
     137             :         //???
     138           1 :         efree(str);
     139             : }
     140             : 
     141           6 : U_CFUNC PHP_FUNCTION(rbbi_get_rule_status)
     142             : {
     143           6 :         BREAKITER_METHOD_INIT_VARS;
     144           6 :         object = getThis();
     145             : 
     146           6 :         if (zend_parse_parameters_none() == FAILURE) {
     147             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
     148           0 :                         "rbbi_get_rule_status: bad arguments", 0 TSRMLS_CC);
     149           0 :                 RETURN_FALSE;
     150             :         }
     151             : 
     152           6 :         BREAKITER_METHOD_FETCH_OBJECT;
     153             : 
     154           6 :         RETURN_LONG(fetch_rbbi(bio)->getRuleStatus());
     155             : }
     156             : 
     157           4 : U_CFUNC PHP_FUNCTION(rbbi_get_rule_status_vec)
     158             : {
     159           4 :         BREAKITER_METHOD_INIT_VARS;
     160           4 :         object = getThis();
     161             : 
     162           4 :         if (zend_parse_parameters_none() == FAILURE) {
     163             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
     164           0 :                         "rbbi_get_rule_status_vec: bad arguments", 0 TSRMLS_CC);
     165           0 :                 RETURN_FALSE;
     166             :         }
     167             : 
     168           4 :         BREAKITER_METHOD_FETCH_OBJECT;
     169             : 
     170             :         int32_t num_rules = fetch_rbbi(bio)->getRuleStatusVec(NULL, 0,
     171           4 :                         BREAKITER_ERROR_CODE(bio));
     172           4 :         if (BREAKITER_ERROR_CODE(bio) == U_BUFFER_OVERFLOW_ERROR) {
     173           4 :                 BREAKITER_ERROR_CODE(bio) = U_ZERO_ERROR;
     174             :         } else {
     175             :                 // should not happen
     176           0 :                 INTL_METHOD_CHECK_STATUS(bio, "rbbi_get_rule_status_vec: failed "
     177             :                                 " determining the number of status values");
     178             :         }
     179           4 :         int32_t *rules = new int32_t[num_rules];
     180             :         num_rules = fetch_rbbi(bio)->getRuleStatusVec(rules, num_rules,
     181           4 :                         BREAKITER_ERROR_CODE(bio));
     182           4 :         if (U_FAILURE(BREAKITER_ERROR_CODE(bio))) {
     183           0 :                 delete[] rules;
     184             :                 intl_errors_set(BREAKITER_ERROR_P(bio), BREAKITER_ERROR_CODE(bio),
     185             :                                 "rbbi_get_rule_status_vec: failed obtaining the status values",
     186           0 :                                 0 TSRMLS_CC);
     187           0 :                 RETURN_FALSE;
     188             :         }
     189             : 
     190           4 :         array_init_size(return_value, num_rules);
     191           9 :         for (int32_t i = 0; i < num_rules; i++) {
     192           5 :                 add_next_index_long(return_value, rules[i]);
     193             :         }
     194           4 :         delete[] rules;
     195             : }
     196             : 
     197             : #if U_ICU_VERSION_MAJOR_NUM * 10 + U_ICU_VERSION_MINOR_NUM >= 48
     198             : U_CFUNC PHP_FUNCTION(rbbi_get_binary_rules)
     199             : {
     200             :         BREAKITER_METHOD_INIT_VARS;
     201             :         object = getThis();
     202             : 
     203             :         if (zend_parse_parameters_none() == FAILURE) {
     204             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
     205             :                         "rbbi_get_binary_rules: bad arguments", 0 TSRMLS_CC);
     206             :                 RETURN_FALSE;
     207             :         }
     208             : 
     209             :         BREAKITER_METHOD_FETCH_OBJECT;
     210             : 
     211             :         uint32_t                rules_len;
     212             :         const uint8_t   *rules = fetch_rbbi(bio)->getBinaryRules(rules_len);
     213             : 
     214             :         if (rules_len > INT_MAX - 1) {
     215             :                 intl_errors_set(BREAKITER_ERROR_P(bio), BREAKITER_ERROR_CODE(bio),
     216             :                                 "rbbi_get_binary_rules: the rules are too large",
     217             :                                 0 TSRMLS_CC);
     218             :                 RETURN_FALSE;
     219             :         }
     220             : 
     221             :         zend_string *ret_rules = zend_string_alloc(rules_len, 0);
     222             :         memcpy(ret_rules->val, rules, rules_len);
     223             :         ret_rules->val[rules_len] = '\0';
     224             : 
     225             :         RETURN_STR(ret_rules);
     226             : }
     227             : #endif

Generated by: LCOV version 1.10

Generated at Sat, 27 Sep 2014 16:43:09 +0000 (3 days ago)

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