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

Generated by: LCOV version 1.10

Generated at Sat, 27 Jun 2015 09:41:10 +0000 (6 days ago)

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