]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/i18n/coleitr.cpp
ICU-491.11.1.tar.gz
[apple/icu.git] / icuSources / i18n / coleitr.cpp
index 90e875a44dfaa1283987e5a8e2484eb17ae90725..7db3e5f752733a0df6dd47ec890e7a14261af6a9 100644 (file)
@@ -1,6 +1,6 @@
 /*
 *******************************************************************************
-* Copyright (C) 1996-2008, International Business Machines Corporation and    *
+* Copyright (C) 1996-2011, International Business Machines Corporation and    *
 * others. All Rights Reserved.                                                *
 *******************************************************************************
 */
@@ -31,6 +31,7 @@
 #include "unicode/coleitr.h"
 #include "unicode/ustring.h"
 #include "ucol_imp.h"
+#include "uassert.h"
 #include "cmemory.h"
 
 
@@ -99,10 +100,8 @@ UBool CollationElementIterator::operator==(
     // the constructor and setText always sets a length
     // and we only compare the string not the contents of the normalization
     // buffer
-    int thislength = m_data_->iteratordata_.endp -
-                     m_data_->iteratordata_.string;
-    int thatlength = that.m_data_->iteratordata_.endp -
-                     that.m_data_->iteratordata_.string;
+    int thislength = (int)(m_data_->iteratordata_.endp - m_data_->iteratordata_.string);
+    int thatlength = (int)(that.m_data_->iteratordata_.endp - that.m_data_->iteratordata_.string);
     
     if (thislength != thatlength) {
         return FALSE;
@@ -124,9 +123,9 @@ UBool CollationElementIterator::operator==(
         }
         // both are in the normalization buffer
         if (m_data_->iteratordata_.pos 
-            - m_data_->iteratordata_.writableBuffer 
+            - m_data_->iteratordata_.writableBuffer.getBuffer()
             != that.m_data_->iteratordata_.pos 
-            - that.m_data_->iteratordata_.writableBuffer) {
+            - that.m_data_->iteratordata_.writableBuffer.getBuffer()) {
             // not in the same position in the normalization buffer
             return FALSE;
         }
@@ -178,7 +177,7 @@ void CollationElementIterator::setText(const UnicodeString& source,
     int32_t length = source.length();
     UChar *string = NULL;
     if (m_data_->isWritable && m_data_->iteratordata_.string != NULL) {
-        uprv_free(m_data_->iteratordata_.string);
+        uprv_free((UChar *)m_data_->iteratordata_.string);
     }
     m_data_->isWritable = TRUE;
     if (length > 0) {
@@ -200,9 +199,9 @@ void CollationElementIterator::setText(const UnicodeString& source,
         *string = 0;
     }
     /* Free offsetBuffer before initializing it. */
-    freeOffsetBuffer(&(m_data_->iteratordata_));
+    ucol_freeOffsetBuffer(&(m_data_->iteratordata_));
     uprv_init_collIterate(m_data_->iteratordata_.coll, string, length, 
-        &m_data_->iteratordata_);
+        &m_data_->iteratordata_, &status);
 
     m_data_->reset_   = TRUE;
 }
@@ -243,13 +242,13 @@ void CollationElementIterator::setText(CharacterIterator& source,
     }
 
     if (m_data_->isWritable && m_data_->iteratordata_.string != NULL) {
-        uprv_free(m_data_->iteratordata_.string);
+        uprv_free((UChar *)m_data_->iteratordata_.string);
     }
     m_data_->isWritable = TRUE;
     /* Free offsetBuffer before initializing it. */
-    freeOffsetBuffer(&(m_data_->iteratordata_));
+    ucol_freeOffsetBuffer(&(m_data_->iteratordata_));
     uprv_init_collIterate(m_data_->iteratordata_.coll, buffer, length, 
-        &m_data_->iteratordata_);
+        &m_data_->iteratordata_, &status);
     m_data_->reset_   = TRUE;
 }
 
@@ -400,7 +399,7 @@ const CollationElementIterator& CollationElementIterator::operator=(
 
         // checking only UCOL_ITER_HASLEN is not enough here as we may be in 
         // the normalization buffer
-        length = othercoliter->endp - othercoliter->string;
+        length = (int)(othercoliter->endp - othercoliter->string);
 
         ucolelem->reset_         = otherucolelem->reset_;
         ucolelem->isWritable     = TRUE;
@@ -409,7 +408,7 @@ const CollationElementIterator& CollationElementIterator::operator=(
         if (length > 0) {
             coliter->string = (UChar *)uprv_malloc(length * U_SIZEOF_UCHAR);
             if(coliter->string != NULL) {
-                uprv_memcpy(coliter->string, othercoliter->string,
+                uprv_memcpy((UChar *)coliter->string, othercoliter->string,
                     length * U_SIZEOF_UCHAR);
             } else { // Error: couldn't allocate memory. No copying should be done
                 length = 0;
@@ -420,48 +419,26 @@ const CollationElementIterator& CollationElementIterator::operator=(
         }
 
         /* start and end of string */
-        coliter->endp = coliter->string + length;
+        coliter->endp = coliter->string == NULL ? NULL : coliter->string + length;
 
         /* handle writable buffer here */
 
         if (othercoliter->flags & UCOL_ITER_INNORMBUF) {
-            uint32_t wlength = u_strlen(othercoliter->writableBuffer) + 1;
-            if (wlength < coliter->writableBufSize) {
-                uprv_memcpy(coliter->stackWritableBuffer, 
-                    othercoliter->stackWritableBuffer, 
-                    wlength * U_SIZEOF_UCHAR);
-            }
-            else {
-                if (coliter->writableBuffer != coliter->stackWritableBuffer) {
-                    uprv_free(coliter->writableBuffer);
-                }
-                coliter->writableBuffer = (UChar *)uprv_malloc(
-                    wlength * U_SIZEOF_UCHAR);
-                if(coliter->writableBuffer != NULL) {
-                    uprv_memcpy(coliter->writableBuffer, 
-                        othercoliter->writableBuffer,
-                        wlength * U_SIZEOF_UCHAR);
-                    coliter->writableBufSize = wlength;
-                } else { // Error: couldn't allocate memory for writableBuffer
-                    coliter->writableBufSize = 0;
-                }
-            }
+            coliter->writableBuffer = othercoliter->writableBuffer;
+            coliter->writableBuffer.getTerminatedBuffer();
         }
 
         /* current position */
         if (othercoliter->pos >= othercoliter->string && 
             othercoliter->pos <= othercoliter->endp)
         {
+            U_ASSERT(coliter->string != NULL);
             coliter->pos = coliter->string + 
                 (othercoliter->pos - othercoliter->string);
         }
-        else if (coliter->writableBuffer != NULL) {
-            coliter->pos = coliter->writableBuffer + 
-                (othercoliter->pos - othercoliter->writableBuffer);
-        }
         else {
-            // Error: couldn't allocate memory for writableBuffer
-            coliter->pos = NULL;
+            coliter->pos = coliter->writableBuffer.getTerminatedBuffer() + 
+                (othercoliter->pos - othercoliter->writableBuffer.getBuffer());
         }
 
         /* CE buffer */
@@ -487,6 +464,7 @@ const CollationElementIterator& CollationElementIterator::operator=(
         }
 
         if (othercoliter->fcdPosition != NULL) {
+            U_ASSERT(coliter->string != NULL);
             coliter->fcdPosition = coliter->string + 
                 (othercoliter->fcdPosition 
                 - othercoliter->string);