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-07-21 Functions: 15 25 60.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Tue, 22 Jul 2014 01:33:10 +0000 (3 days ago)

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