]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/i18n/umsg.cpp
ICU-64260.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / umsg.cpp
index 8a58ee0442c02aa1e613fce0c63481dedbcdf192..9a5344e0191a8c8dc09c27b795907dbbf3034a3b 100644 (file)
@@ -1,12 +1,14 @@
+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
 /*
 *******************************************************************************
 *
-*   Copyright (C) 1999-2006, International Business Machines
+*   Copyright (C) 1999-2012, International Business Machines
 *   Corporation and others.  All Rights Reserved.
 *
 *******************************************************************************
 *   file name:  umsg.cpp
-*   encoding:   US-ASCII
+*   encoding:   UTF-8
 *   tab size:   8 (not used)
 *   indentation:4
 *
 #include "uassert.h"
 #include "ustr_imp.h"
 
+U_NAMESPACE_BEGIN
+/**
+ * This class isolates our access to private internal methods of
+ * MessageFormat.  It is never instantiated; it exists only for C++
+ * access management.
+ */
+class MessageFormatAdapter {
+public:
+    static const Formattable::Type* getArgTypeList(const MessageFormat& m,
+                                                   int32_t& count);
+    static UBool hasArgTypeConflicts(const MessageFormat& m) {
+        return m.hasArgTypeConflicts;
+    }
+};
+const Formattable::Type*
+MessageFormatAdapter::getArgTypeList(const MessageFormat& m,
+                                     int32_t& count) {
+    return m.getArgTypeList(count);
+}
+U_NAMESPACE_END
+
 U_NAMESPACE_USE
 
 U_CAPI int32_t
@@ -217,25 +240,23 @@ umsg_open(  const UChar     *pattern,
     }
 
     UParseError tErr;
-   
     if(parseError==NULL)
     {
         parseError = &tErr;
     }
-        
-    UMessageFormat* retVal = 0;
 
     int32_t len = (patternLength == -1 ? u_strlen(pattern) : patternLength);
-    
-    UnicodeString patString((patternLength == -1 ? TRUE:FALSE), pattern,len);
+    UnicodeString patString(patternLength == -1, pattern, len);
 
-    retVal = (UMessageFormat*) new MessageFormat(patString,Locale(locale),*parseError,*status);
-    
-    if(retVal == 0) {
+    MessageFormat* retVal = new MessageFormat(patString,Locale(locale),*parseError,*status);
+    if(retVal == NULL) {
         *status = U_MEMORY_ALLOCATION_ERROR;
-        return 0;
+        return NULL;
     }
-    return retVal;
+    if (U_SUCCESS(*status) && MessageFormatAdapter::hasArgTypeConflicts(*retVal)) {
+        *status = U_ARGUMENT_TYPE_MISMATCH;
+    }
+    return (UMessageFormat*)retVal;
 }
 
 U_CAPI void U_EXPORT2
@@ -300,7 +321,7 @@ umsg_applyPattern(UMessageFormat *fmt,
     if(status ==NULL||U_FAILURE(*status)){
         return ;
     }
-    if(fmt==NULL||pattern==NULL||patternLength<-1){
+    if(fmt==NULL || (pattern==NULL && patternLength!=0) || patternLength<-1) {
         *status=U_ILLEGAL_ARGUMENT_ERROR;
         return ;
     }
@@ -308,10 +329,8 @@ umsg_applyPattern(UMessageFormat *fmt,
     if(parseError==NULL){
       parseError = &tErr;
     }
-    if(patternLength<-1){
-        patternLength=u_strlen(pattern);
-    }
 
+    // UnicodeString(pattern, -1) calls u_strlen().
     ((MessageFormat*)fmt)->applyPattern(UnicodeString(pattern,patternLength),*parseError,*status);  
 }
 
@@ -366,24 +385,6 @@ umsg_format(    const UMessageFormat *fmt,
     return actLen;
 }
 
-U_NAMESPACE_BEGIN
-/**
- * This class isolates our access to private internal methods of
- * MessageFormat.  It is never instantiated; it exists only for C++
- * access management.
- */
-class MessageFormatAdapter {
-public:
-    static const Formattable::Type* getArgTypeList(const MessageFormat& m,
-                                                   int32_t& count);
-};
-const Formattable::Type*
-MessageFormatAdapter::getArgTypeList(const MessageFormat& m,
-                                     int32_t& count) {
-    return m.getArgTypeList(count);
-}
-U_NAMESPACE_END
-
 U_CAPI int32_t U_EXPORT2
 umsg_vformat(   const UMessageFormat *fmt,
                 UChar          *result,
@@ -441,7 +442,7 @@ umsg_vformat(   const UMessageFormat *fmt,
             // For some reason, a temporary is needed
             stringVal = va_arg(ap, UChar*);
             if(stringVal){
-                args[i].setString(stringVal);
+                args[i].setString(UnicodeString(stringVal));
             }else{
                 *status=U_ILLEGAL_ARGUMENT_ERROR;
             }
@@ -456,16 +457,17 @@ umsg_vformat(   const UMessageFormat *fmt,
             break;
 
         case Formattable::kObject:
-            // This will never happen because MessageFormat doesn't
-            // support kObject.  When MessageFormat is changed to
-            // understand MeasureFormats, modify this code to do the
-            // right thing. [alan]
-            U_ASSERT(FALSE);
+            // Unused argument number. Read and ignore a pointer argument.
+            va_arg(ap, void*);
             break;
+
+        default:
+            // Unknown/unsupported argument type.
+            UPRV_UNREACHABLE;
         }
     }
     UnicodeString resultStr;
-    FieldPosition fieldPosition(0);
+    FieldPosition fieldPosition(FieldPosition::DONT_CARE);
     
     /* format the message */
     ((const MessageFormat*)fmt)->format(args,count,resultStr,fieldPosition,*status);
@@ -523,7 +525,7 @@ umsg_vparse(const UMessageFormat *fmt,
     }
 
     UnicodeString srcString(source,sourceLength);
-    Formattable *args = ((const MessageFormat*)fmt)->parse(source,*count,*status);
+    Formattable *args = ((const MessageFormat*)fmt)->parse(srcString,*count,*status);
     UDate *aDate;
     double *aDouble;
     UChar *aString;
@@ -588,13 +590,11 @@ umsg_vparse(const UMessageFormat *fmt,
             // support kObject.  When MessageFormat is changed to
             // understand MeasureFormats, modify this code to do the
             // right thing. [alan]
-            U_ASSERT(FALSE);
-            break;
+            UPRV_UNREACHABLE;
 
         // better not happen!
         case Formattable::kArray:
-            U_ASSERT(FALSE);
-            break;
+            UPRV_UNREACHABLE;
         }
     }
 
@@ -631,6 +631,7 @@ int32_t umsg_autoQuoteApostrophe(const UChar* pattern,
         *ec = U_ILLEGAL_ARGUMENT_ERROR;
         return -1;
     }
+    U_ASSERT(destCapacity >= 0);
 
     if (patternLength == -1) {
         patternLength = u_strlen(pattern);
@@ -692,6 +693,7 @@ int32_t umsg_autoQuoteApostrophe(const UChar* pattern,
             break;
         }
 
+        U_ASSERT(len >= 0);
         MAppend(c);
     }