]> granicus.if.org Git - icu/commitdiff
ICU-11549 Review comments: Better param checks/docs; use UnicodeStrings[] stack array...
authorPeter Edberg <pedberg@unicode.org>
Mon, 9 Mar 2015 06:17:19 +0000 (06:17 +0000)
committerPeter Edberg <pedberg@unicode.org>
Mon, 9 Mar 2015 06:17:19 +0000 (06:17 +0000)
X-SVN-Rev: 37192

icu4c/source/common/ulistformatter.cpp
icu4c/source/common/unicode/ulistformatter.h

index bab49462d635badeb6afdad122480d8145b64c7f..6931cddfe07c060196adca478e6e87b43682ec02 100644 (file)
@@ -12,6 +12,7 @@
 #include "unicode/ulistformatter.h"
 #include "unicode/listformatter.h"
 #include "unicode/localpointer.h"
+#include "cmemory.h"
 
 U_NAMESPACE_USE
 
@@ -49,22 +50,25 @@ ulistfmt_format(const UListFormatter* listfmt,
     if (U_FAILURE(*status)) {
         return -1;
     }
-    if (strings == NULL || stringCount < 1 || ((result == NULL)? resultCapacity != 0 : resultCapacity < 0)) {
+    if (stringCount < 0 || (strings == NULL && stringCount > 0) || ((result == NULL)? resultCapacity != 0 : resultCapacity < 0)) {
         *status = U_ILLEGAL_ARGUMENT_ERROR;
         return -1;
     }
-    UnicodeString* ustrings = new UnicodeString[stringCount];
-    if (ustrings == NULL) {
-        *status = U_MEMORY_ALLOCATION_ERROR;
-        return -1;
+    UnicodeString ustringsStackBuf[4];
+    UnicodeString* ustrings = ustringsStackBuf;
+    if (stringCount > UPRV_LENGTHOF(ustringsStackBuf)) {
+        ustrings = new UnicodeString[stringCount];
+        if (ustrings == NULL) {
+            *status = U_MEMORY_ALLOCATION_ERROR;
+            return -1;
+        }
     }
-    int32_t stringIndex;
     if (stringLengths == NULL) {
-        for (stringIndex = 0; stringIndex < stringCount; stringIndex++) {
+        for (int32_t stringIndex = 0; stringIndex < stringCount; stringIndex++) {
             ustrings[stringIndex].setTo(TRUE, strings[stringIndex], -1);
         }
     } else {
-        for (stringIndex = 0; stringIndex < stringCount; stringIndex++) {
+        for (int32_t stringIndex = 0; stringIndex < stringCount; stringIndex++) {
             ustrings[stringIndex].setTo(stringLengths[stringIndex] < 0, strings[stringIndex], stringLengths[stringIndex]);
         }
     }
@@ -75,9 +79,8 @@ ulistfmt_format(const UListFormatter* listfmt,
         res.setTo(result, 0, resultCapacity);
     }
     ((const ListFormatter*)listfmt)->format( ustrings, stringCount, res, *status );
-    delete[] ustrings;
-    if (U_FAILURE(*status)) {
-        return -1;
+    if (ustrings != ustringsStackBuf) {
+        delete[] ustrings;
     }
     return res.extract(result, resultCapacity, *status);
 }
index 5990f3108c8a9976c838e5d06a5c7b71105db2d7..1c6cb2d7b12395402cb21d6269bf44449f5bdeb9 100644 (file)
@@ -38,7 +38,11 @@ typedef struct UListFormatter UListFormatter;  /**< C typedef for struct UListFo
  *            The locale whose rules should be used; may be NULL for
  *            default locale.
  * @param status
- *            A pointer to a UErrorCode to receive any errors.
+ *            A pointer to a standard ICU UErrorCode (input/output parameter).
+ *            Its input value must pass the U_SUCCESS() test, or else the
+ *            function returns immediately. The caller should check its output
+ *            value with U_FAILURE(), or use with function chaining (see User
+ *            Guide for details).
  * @return
  *            A pointer to a UListFormatter object for the specified locale,
  *            or NULL if an error occurred.
@@ -84,7 +88,7 @@ U_NAMESPACE_END
  *            The UListFormatter object specifying the list conventions.
  * @param strings
  *            An array of pointers to UChar strings; the array length is
- *            specified by stringCount. Must not be NULL.
+ *            specified by stringCount. Must be non-NULL if stringCount > 0.
  * @param stringLengths
  *            An array of string lengths corresponding to the strings[]
  *            parameter; any individual length value may be negative to indicate
@@ -94,13 +98,17 @@ U_NAMESPACE_END
  *            stringCount entries.
  * @param stringCount
  *            the number of entries in strings[], and the number of entries
- *            in the stringLengths array if it is not NULL. Must be >0.
+ *            in the stringLengths array if it is not NULL. Must be >0.
  * @param result
  *            A pointer to a buffer to receive the formatted list.
  * @param resultCapacity
  *            The maximum size of result.
  * @param status
- *            A pointer to a UErrorCode to receive any errors.
+ *            A pointer to a standard ICU UErrorCode (input/output parameter).
+ *            Its input value must pass the U_SUCCESS() test, or else the
+ *            function returns immediately. The caller should check its output
+ *            value with U_FAILURE(), or use with function chaining (see User
+ *            Guide for details).
  * @return
  *            The total buffer size needed; if greater than resultLength, the
  *            output was truncated. May be <=0 if unable to determine the