#include "unicode/ulistformatter.h"
#include "unicode/listformatter.h"
#include "unicode/localpointer.h"
+#include "cmemory.h"
U_NAMESPACE_USE
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]);
}
}
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);
}
* 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.
* 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
* 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