]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/i18n/ucoleitr.cpp
ICU-62141.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / ucoleitr.cpp
index 603ffbc41e6a4746167628114e889ba156de6643..e56ea1efe0d452f95fef60594dfe0c782f76fd70 100644 (file)
@@ -1,6 +1,8 @@
+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
 /*
 ******************************************************************************
-*   Copyright (C) 2001-2014, International Business Machines
+*   Copyright (C) 2001-2016, International Business Machines
 *   Corporation and others.  All Rights Reserved.
 ******************************************************************************
 *
@@ -34,13 +36,9 @@ U_NAMESPACE_USE
 #define DEFAULT_BUFFER_SIZE 16
 #define BUFFER_GROW 8
 
-#define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
+#define ARRAY_COPY(dst, src, count) uprv_memcpy((void *) (dst), (void *) (src), (size_t)(count) * sizeof (src)[0])
 
-#define ARRAY_COPY(dst, src, count) uprv_memcpy((void *) (dst), (void *) (src), (count) * sizeof (src)[0])
-
-#define NEW_ARRAY(type, count) (type *) uprv_malloc((count) * sizeof(type))
-
-#define GROW_ARRAY(array, newSize) uprv_realloc((void *) (array), (newSize) * sizeof (array)[0])
+#define NEW_ARRAY(type, count) (type *) uprv_malloc((size_t)(count) * sizeof(type))
 
 #define DELETE_ARRAY(array) uprv_free((void *) (array))
 
@@ -63,8 +61,8 @@ struct RCEBuffer
     RCEBuffer();
     ~RCEBuffer();
 
-    UBool empty() const;
-    void  put(uint32_t ce, int32_t ixLow, int32_t ixHigh);
+    UBool isEmpty() const;
+    void  put(uint32_t ce, int32_t ixLow, int32_t ixHigh, UErrorCode &errorCode);
     const RCEI *get();
 };
 
@@ -82,15 +80,22 @@ RCEBuffer::~RCEBuffer()
     }
 }
 
-UBool RCEBuffer::empty() const
+UBool RCEBuffer::isEmpty() const
 {
     return bufferIndex <= 0;
 }
 
-void RCEBuffer::put(uint32_t ce, int32_t ixLow, int32_t ixHigh)
+void RCEBuffer::put(uint32_t ce, int32_t ixLow, int32_t ixHigh, UErrorCode &errorCode)
 {
+    if (U_FAILURE(errorCode)) {
+        return;
+    }
     if (bufferIndex >= bufferSize) {
         RCEI *newBuffer = NEW_ARRAY(RCEI, bufferSize + BUFFER_GROW);
+        if (newBuffer == NULL) {
+            errorCode = U_MEMORY_ALLOCATION_ERROR;
+            return;
+        }
 
         ARRAY_COPY(newBuffer, buffer, bufferSize);
 
@@ -137,15 +142,22 @@ void PCEBuffer::reset()
     bufferIndex = 0;
 }
 
-UBool PCEBuffer::empty() const
+UBool PCEBuffer::isEmpty() const
 {
     return bufferIndex <= 0;
 }
 
-void PCEBuffer::put(uint64_t ce, int32_t ixLow, int32_t ixHigh)
+void PCEBuffer::put(uint64_t ce, int32_t ixLow, int32_t ixHigh, UErrorCode &errorCode)
 {
+    if (U_FAILURE(errorCode)) {
+        return;
+    }
     if (bufferIndex >= bufferSize) {
         PCEI *newBuffer = NEW_ARRAY(PCEI, bufferSize + BUFFER_GROW);
+        if (newBuffer == NULL) {
+            errorCode = U_MEMORY_ALLOCATION_ERROR;
+            return;
+        }
 
         ARRAY_COPY(newBuffer, buffer, bufferSize);
 
@@ -212,11 +224,11 @@ uint64_t UCollationPCE::processCE(uint32_t ce)
     switch(strength) {
     default:
         tertiary = ucol_tertiaryOrder(ce);
-        /* note fall-through */
+        U_FALLTHROUGH;
 
     case UCOL_SECONDARY:
         secondary = ucol_secondaryOrder(ce);
-        /* note fall-through */
+        U_FALLTHROUGH;
 
     case UCOL_PRIMARY:
         primary = ucol_primaryOrder(ce);
@@ -318,7 +330,7 @@ ucol_nextProcessed(UCollationElements *elems,
                    int32_t            *ixHigh,
                    UErrorCode         *status)
 {
-    return (UCollationPCE::UCollationPCE(elems)).nextProcessed(ixLow, ixHigh, status);
+    return (UCollationPCE(elems)).nextProcessed(ixLow, ixHigh, status);
 }
 
 
@@ -382,7 +394,7 @@ ucol_previousProcessed(UCollationElements *elems,
                    int32_t            *ixHigh,
                    UErrorCode         *status)
 {
-    return (UCollationPCE::UCollationPCE(elems)).previousProcessed(ixLow, ixHigh, status);
+    return (UCollationPCE(elems)).previousProcessed(ixLow, ixHigh, status);
 }
 
 U_NAMESPACE_BEGIN
@@ -402,7 +414,7 @@ UCollationPCE::previousProcessed(
 
     // pceBuffer.reset();
 
-    while (pceBuffer.empty()) {
+    while (pceBuffer.isEmpty()) {
         // buffer raw CEs up to non-ignorable primary
         RCEBuffer rceb;
         int32_t ce;
@@ -414,30 +426,33 @@ UCollationPCE::previousProcessed(
             low  = cei->getOffset();
 
             if (ce == UCOL_NULLORDER) {
-                if (! rceb.empty()) {
+                if (!rceb.isEmpty()) {
                     break;
                 }
 
                 goto finish;
             }
 
-            rceb.put((uint32_t)ce, low, high);
-        } while ((ce & UCOL_PRIMARYORDERMASK) == 0 || isContinuation(ce));
+            rceb.put((uint32_t)ce, low, high, *status);
+        } while (U_SUCCESS(*status) && ((ce & UCOL_PRIMARYORDERMASK) == 0 || isContinuation(ce)));
 
         // process the raw CEs
-        while (! rceb.empty()) {
+        while (U_SUCCESS(*status) && !rceb.isEmpty()) {
             const RCEI *rcei = rceb.get();
 
             result = processCE(rcei->ce);
 
             if (result != UCOL_IGNORABLE) {
-                pceBuffer.put(result, rcei->low, rcei->high);
+                pceBuffer.put(result, rcei->low, rcei->high, *status);
             }
         }
+        if (U_FAILURE(*status)) {
+            return UCOL_PROCESSED_NULLORDER;
+        }
     }
 
 finish:
-    if (pceBuffer.empty()) {
+    if (pceBuffer.isEmpty()) {
         // **** Is -1 the right value for ixLow, ixHigh? ****
        if (ixLow != NULL) {
                *ixLow = -1;