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-08-25 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? ZSTR_VAL(parse_error_str.s) : "");
      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             :         zend_string *u8str;
     118           1 :         const UnicodeString rules = fetch_rbbi(bio)->getRules();
     119             : 
     120           1 :         u8str = intl_charFromString(rules, BREAKITER_ERROR_CODE_P(bio));
     121           1 :         if (!u8str)
     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           1 :         RETVAL_STR(u8str);
     129             : }
     130             : 
     131           6 : U_CFUNC PHP_FUNCTION(rbbi_get_rule_status)
     132             : {
     133           6 :         BREAKITER_METHOD_INIT_VARS;
     134           6 :         object = getThis();
     135             : 
     136           6 :         if (zend_parse_parameters_none() == FAILURE) {
     137             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
     138           0 :                         "rbbi_get_rule_status: bad arguments", 0);
     139           0 :                 RETURN_FALSE;
     140             :         }
     141             : 
     142           6 :         BREAKITER_METHOD_FETCH_OBJECT;
     143             : 
     144           6 :         RETURN_LONG(fetch_rbbi(bio)->getRuleStatus());
     145             : }
     146             : 
     147           4 : U_CFUNC PHP_FUNCTION(rbbi_get_rule_status_vec)
     148             : {
     149           4 :         BREAKITER_METHOD_INIT_VARS;
     150           4 :         object = getThis();
     151             : 
     152           4 :         if (zend_parse_parameters_none() == FAILURE) {
     153             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
     154           0 :                         "rbbi_get_rule_status_vec: bad arguments", 0);
     155           0 :                 RETURN_FALSE;
     156             :         }
     157             : 
     158           4 :         BREAKITER_METHOD_FETCH_OBJECT;
     159             : 
     160             :         int32_t num_rules = fetch_rbbi(bio)->getRuleStatusVec(NULL, 0,
     161           4 :                         BREAKITER_ERROR_CODE(bio));
     162           4 :         if (BREAKITER_ERROR_CODE(bio) == U_BUFFER_OVERFLOW_ERROR) {
     163           4 :                 BREAKITER_ERROR_CODE(bio) = U_ZERO_ERROR;
     164             :         } else {
     165             :                 // should not happen
     166           0 :                 INTL_METHOD_CHECK_STATUS(bio, "rbbi_get_rule_status_vec: failed "
     167             :                                 " determining the number of status values");
     168             :         }
     169           4 :         int32_t *rules = new int32_t[num_rules];
     170             :         num_rules = fetch_rbbi(bio)->getRuleStatusVec(rules, num_rules,
     171           4 :                         BREAKITER_ERROR_CODE(bio));
     172           4 :         if (U_FAILURE(BREAKITER_ERROR_CODE(bio))) {
     173           0 :                 delete[] rules;
     174             :                 intl_errors_set(BREAKITER_ERROR_P(bio), BREAKITER_ERROR_CODE(bio),
     175             :                                 "rbbi_get_rule_status_vec: failed obtaining the status values",
     176           0 :                                 0);
     177           0 :                 RETURN_FALSE;
     178             :         }
     179             : 
     180           4 :         array_init_size(return_value, num_rules);
     181           9 :         for (int32_t i = 0; i < num_rules; i++) {
     182           5 :                 add_next_index_long(return_value, rules[i]);
     183             :         }
     184           4 :         delete[] rules;
     185             : }
     186             : 
     187             : #if U_ICU_VERSION_MAJOR_NUM * 10 + U_ICU_VERSION_MINOR_NUM >= 48
     188             : U_CFUNC PHP_FUNCTION(rbbi_get_binary_rules)
     189             : {
     190             :         BREAKITER_METHOD_INIT_VARS;
     191             :         object = getThis();
     192             : 
     193             :         if (zend_parse_parameters_none() == FAILURE) {
     194             :                 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
     195             :                         "rbbi_get_binary_rules: bad arguments", 0);
     196             :                 RETURN_FALSE;
     197             :         }
     198             : 
     199             :         BREAKITER_METHOD_FETCH_OBJECT;
     200             : 
     201             :         uint32_t                rules_len;
     202             :         const uint8_t   *rules = fetch_rbbi(bio)->getBinaryRules(rules_len);
     203             : 
     204             :         if (rules_len > INT_MAX - 1) {
     205             :                 intl_errors_set(BREAKITER_ERROR_P(bio), BREAKITER_ERROR_CODE(bio),
     206             :                                 "rbbi_get_binary_rules: the rules are too large",
     207             :                                 0);
     208             :                 RETURN_FALSE;
     209             :         }
     210             : 
     211             :         zend_string *ret_rules = zend_string_alloc(rules_len, 0);
     212             :         memcpy(ZSTR_VAL(ret_rules), rules, rules_len);
     213             :         ZSTR_VAL(ret_rules)[rules_len] = '\0';
     214             : 
     215             :         RETURN_STR(ret_rules);
     216             : }
     217             : #endif

Generated by: LCOV version 1.10

Generated at Tue, 25 Aug 2015 07:24:18 +0000 (3 days ago)

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