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 - codepointiterator_internal.cpp (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 55 131 42.0 %
Date: 2014-10-22 Functions: 15 25 60.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 "codepointiterator_internal.h"
      18             : #include <unicode/uchriter.h>
      19             : #include <typeinfo>
      20             : 
      21             : #include "php.h"
      22             : 
      23             : //copied from cmemory.h, which is not public
      24             : typedef union {
      25             :     zend_long    t1;
      26             :     double  t2;
      27             :     void   *t3;
      28             : } UAlignedMemory;
      29             : 
      30             : #define U_POINTER_MASK_LSB(ptr, mask) (((ptrdiff_t)(char *)(ptr)) & (mask))
      31             : #define U_ALIGNMENT_OFFSET(ptr) U_POINTER_MASK_LSB(ptr, sizeof(UAlignedMemory) - 1)
      32             : #define U_ALIGNMENT_OFFSET_UP(ptr) (sizeof(UAlignedMemory) - U_ALIGNMENT_OFFSET(ptr))
      33             : 
      34             : using namespace PHP;
      35             : 
      36          15 : UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CodePointBreakIterator);
      37             : 
      38           5 : CodePointBreakIterator::CodePointBreakIterator()
      39           5 : : BreakIterator(), fCharIter(NULL), lastCodePoint(U_SENTINEL)
      40             : {
      41           5 :         UErrorCode uec = UErrorCode();
      42           5 :         this->fText = utext_openUChars(NULL, NULL, 0, &uec);
      43           5 : }
      44             : 
      45           1 : CodePointBreakIterator::CodePointBreakIterator(const PHP::CodePointBreakIterator &other)
      46           1 : : BreakIterator(other), fText(NULL), fCharIter(NULL), lastCodePoint(U_SENTINEL)
      47             : {
      48           1 :         *this = other;
      49           0 : }
      50             : 
      51           1 : CodePointBreakIterator& CodePointBreakIterator::operator=(const CodePointBreakIterator& that)
      52             : {
      53           1 :         UErrorCode uec = UErrorCode();
      54           1 :         UText *ut_clone = NULL;
      55             : 
      56           1 :         if (this == &that) {
      57           0 :                 return *this;
      58             :         }
      59             : 
      60           1 :         this->fText = utext_clone(this->fText, that.fText, FALSE, TRUE, &uec);
      61             : 
      62             :         //don't bother copying the character iterator, getText() is deprecated
      63           1 :         clearCurrentCharIter();
      64             : 
      65           1 :         this->lastCodePoint = that.lastCodePoint;
      66           1 :         return *this;
      67             : }
      68             : 
      69          12 : CodePointBreakIterator::~CodePointBreakIterator()
      70             : {
      71           6 :         if (this->fText) {
      72           6 :                 utext_close(this->fText);
      73             :         }
      74           6 :         clearCurrentCharIter();
      75           6 : }
      76             : 
      77           3 : UBool CodePointBreakIterator::operator==(const BreakIterator& that) const
      78             : {
      79           3 :         if (typeid(*this) != typeid(that)) {
      80           0 :                 return FALSE;
      81             :         }
      82             : 
      83             :         const CodePointBreakIterator& that2 =
      84           3 :                 static_cast<const CodePointBreakIterator&>(that);
      85             : 
      86           3 :         if (!utext_equals(this->fText, that2.fText)) {
      87           1 :                 return FALSE;
      88             :         }
      89             : 
      90           2 :         return TRUE;
      91             : }
      92             : 
      93           1 : CodePointBreakIterator* CodePointBreakIterator::clone(void) const
      94             : {
      95           1 :         return new CodePointBreakIterator(*this);
      96             : }
      97             : 
      98           0 : CharacterIterator& CodePointBreakIterator::getText(void) const
      99             : {
     100           0 :         if (this->fCharIter == NULL) {
     101             :                 //this method is deprecated anyway; setup bogus iterator
     102             :                 static const UChar c = 0;
     103           0 :                 this->fCharIter = new UCharCharacterIterator(&c, 0);
     104             :         }
     105             : 
     106           0 :         return *this->fCharIter;
     107             : }
     108             : 
     109           0 : UText *CodePointBreakIterator::getUText(UText *fillIn, UErrorCode &status) const
     110             : {
     111           0 :         return utext_clone(fillIn, this->fText, FALSE, TRUE, &status);
     112             : }
     113             : 
     114           0 : void CodePointBreakIterator::setText(const UnicodeString &text)
     115             : {
     116           0 :         UErrorCode uec = UErrorCode();
     117             : 
     118             :         //this closes the previous utext, if any
     119           0 :         this->fText = utext_openConstUnicodeString(this->fText, &text, &uec);
     120             : 
     121           0 :         clearCurrentCharIter();
     122           0 : }
     123             : 
     124           6 : void CodePointBreakIterator::setText(UText *text, UErrorCode &status)
     125             : {
     126           6 :         if (U_FAILURE(status)) {
     127           0 :                 return;
     128             :         }
     129             : 
     130           6 :         this->fText = utext_clone(this->fText, text, FALSE, TRUE, &status);
     131             : 
     132           6 :         clearCurrentCharIter();
     133             : }
     134             : 
     135           0 : void CodePointBreakIterator::adoptText(CharacterIterator* it)
     136             : {
     137           0 :         UErrorCode uec = UErrorCode();
     138           0 :         clearCurrentCharIter();
     139             : 
     140           0 :         this->fCharIter = it;
     141           0 :         this->fText = utext_openCharacterIterator(this->fText, it, &uec);
     142           0 : }
     143             : 
     144           3 : int32_t CodePointBreakIterator::first(void)
     145             : {
     146           3 :         UTEXT_SETNATIVEINDEX(this->fText, 0);
     147           3 :         this->lastCodePoint = U_SENTINEL;
     148             : 
     149           3 :         return 0;
     150             : }
     151             : 
     152           1 : int32_t CodePointBreakIterator::last(void)
     153             : {
     154           1 :         int32_t pos = (int32_t)utext_nativeLength(this->fText);
     155           1 :         UTEXT_SETNATIVEINDEX(this->fText, pos);
     156           1 :         this->lastCodePoint = U_SENTINEL;
     157             : 
     158           1 :         return pos;
     159             : }
     160             : 
     161          16 : int32_t CodePointBreakIterator::previous(void)
     162             : {
     163          16 :         this->lastCodePoint = UTEXT_PREVIOUS32(this->fText);
     164          16 :         if (this->lastCodePoint == U_SENTINEL) {
     165           1 :                 return BreakIterator::DONE;
     166             :         }
     167             : 
     168          15 :         return (int32_t)UTEXT_GETNATIVEINDEX(this->fText);
     169             : }
     170             : 
     171          48 : int32_t CodePointBreakIterator::next(void)
     172             : {
     173          48 :         this->lastCodePoint = UTEXT_NEXT32(this->fText);
     174          48 :         if (this->lastCodePoint == U_SENTINEL) {
     175           3 :                 return BreakIterator::DONE;
     176             :         }
     177             : 
     178          45 :         return (int32_t)UTEXT_GETNATIVEINDEX(this->fText);
     179             : }
     180             : 
     181          31 : int32_t CodePointBreakIterator::current(void) const
     182             : {
     183          31 :         return (int32_t)UTEXT_GETNATIVEINDEX(this->fText);
     184             : }
     185             : 
     186           0 : int32_t CodePointBreakIterator::following(int32_t offset)
     187             : {
     188           0 :         this->lastCodePoint = utext_next32From(this->fText, offset);
     189           0 :         if (this->lastCodePoint == U_SENTINEL) {
     190           0 :                 return BreakIterator::DONE;
     191             :         }
     192             : 
     193           0 :         return (int32_t)UTEXT_GETNATIVEINDEX(this->fText);
     194             : }
     195             : 
     196           0 : int32_t CodePointBreakIterator::preceding(int32_t offset)
     197             : {
     198           0 :         this->lastCodePoint = utext_previous32From(this->fText, offset);
     199           0 :         if (this->lastCodePoint == U_SENTINEL) {
     200           0 :                 return BreakIterator::DONE;
     201             :         }
     202             : 
     203           0 :         return (int32_t)UTEXT_GETNATIVEINDEX(this->fText);
     204             : }
     205             : 
     206           0 : UBool CodePointBreakIterator::isBoundary(int32_t offset)
     207             : {
     208             :         //this function has side effects, and it's supposed to
     209           0 :         utext_setNativeIndex(this->fText, offset);
     210           0 :         return (offset == utext_getNativeIndex(this->fText));
     211             : }
     212             : 
     213           0 : int32_t CodePointBreakIterator::next(int32_t n)
     214             : {
     215           0 :         UBool res = utext_moveIndex32(this->fText, n);
     216             : 
     217             : #ifndef UTEXT_CURRENT32
     218             : #define UTEXT_CURRENT32 utext_current32
     219             : #endif
     220             : 
     221           0 :         if (res) {
     222           0 :                 this->lastCodePoint = UTEXT_CURRENT32(this->fText);
     223           0 :                 return (int32_t)UTEXT_GETNATIVEINDEX(this->fText);
     224             :         } else {
     225           0 :                 this->lastCodePoint = U_SENTINEL;
     226           0 :                 return BreakIterator::DONE;
     227             :         }
     228             : }
     229             : 
     230           0 : CodePointBreakIterator *CodePointBreakIterator::createBufferClone(
     231             :         void *stackBuffer, int32_t &bufferSize, UErrorCode &status)
     232             : {
     233             :         //see implementation of RuleBasedBreakIterator::createBufferClone()
     234           0 :         if (U_FAILURE(status)) {
     235           0 :                 return NULL;
     236             :         }
     237             : 
     238           0 :         if (bufferSize <= 0) {
     239           0 :                 bufferSize = sizeof(CodePointBreakIterator) + U_ALIGNMENT_OFFSET_UP(0);
     240           0 :                 return NULL;
     241             :         }
     242             : 
     243           0 :         char *buf = (char*)stackBuffer;
     244           0 :         uint32_t s = bufferSize;
     245             : 
     246           0 :         if (stackBuffer == NULL) {
     247           0 :                  s = 0;
     248             :         }
     249             : 
     250           0 :         if (U_ALIGNMENT_OFFSET(stackBuffer) != 0) {
     251           0 :                 uint32_t offsetUp = (uint32_t)U_ALIGNMENT_OFFSET_UP(buf);
     252           0 :                 s -= offsetUp;
     253           0 :                 buf += offsetUp;
     254             :         }
     255             : 
     256           0 :         if (s < sizeof(CodePointBreakIterator)) {
     257           0 :                 CodePointBreakIterator *clonedBI = new CodePointBreakIterator(*this);
     258           0 :                 if (clonedBI == NULL) {
     259           0 :                         status = U_MEMORY_ALLOCATION_ERROR;
     260             :                 } else {
     261           0 :                         status = U_SAFECLONE_ALLOCATED_WARNING;
     262             :                 }
     263             : 
     264           0 :                 return clonedBI;
     265             :         }
     266             : 
     267           0 :         return new(buf) CodePointBreakIterator(*this);
     268             : }
     269             : 
     270           0 : CodePointBreakIterator &CodePointBreakIterator::refreshInputText(UText *input, UErrorCode &status)
     271             : {
     272             :         //see implementation of RuleBasedBreakIterator::createBufferClone()
     273           0 :         if (U_FAILURE(status)) {
     274           0 :                 return *this;
     275             :         }
     276           0 :         if (input == NULL) {
     277           0 :                 status = U_ILLEGAL_ARGUMENT_ERROR;
     278           0 :                 return *this;
     279             :         }
     280             : 
     281           0 :         int64_t pos = utext_getNativeIndex(this->fText);
     282           0 :         this->fText = utext_clone(this->fText, input, FALSE, TRUE, &status);
     283           0 :         if (U_FAILURE(status)) {
     284           0 :                 return *this;
     285             :         }
     286             : 
     287           0 :         utext_setNativeIndex(this->fText, pos);
     288           0 :         if (utext_getNativeIndex(fText) != pos) {
     289           0 :                 status = U_ILLEGAL_ARGUMENT_ERROR;
     290             :         }
     291             : 
     292           0 :         return *this;
     293             : }

Generated by: LCOV version 1.10

Generated at Wed, 22 Oct 2014 07:24:49 +0000 (2 days ago)

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