]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/common/simpleformatter.cpp
ICU-66108.tar.gz
[apple/icu.git] / icuSources / common / simpleformatter.cpp
index 6dfa5b94ab4c80f01f06e4da5758e6d2053dc2ea..1f9a169aa5d3ca6bace0b28219d1a46d843eb08c 100644 (file)
@@ -1,3 +1,5 @@
+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
 /*
 ******************************************************************************
 * Copyright (C) 2014-2016, International Business Machines
@@ -60,7 +62,7 @@ SimpleFormatter::~SimpleFormatter() {}
 
 UBool SimpleFormatter::applyPatternMinMaxArguments(
         const UnicodeString &pattern,
-        int32_t min, int32_t max,
+        int32_t min, int32_t max, UBool removeSingleQuotes,
         UErrorCode &errorCode) {
     if (U_FAILURE(errorCode)) {
         return FALSE;
@@ -85,7 +87,7 @@ UBool SimpleFormatter::applyPatternMinMaxArguments(
                 // skip the quote-ending apostrophe
                 inQuote = FALSE;
                 continue;
-            } else if (c == OPEN_BRACE || c == CLOSE_BRACE) {
+            } else if (removeSingleQuotes || c == OPEN_BRACE || c == CLOSE_BRACE) {
                 // Skip the quote-starting apostrophe, find the end of the quoted literal text.
                 ++i;
                 inQuote = TRUE;
@@ -244,15 +246,24 @@ UnicodeString &SimpleFormatter::formatAndReplace(
 }
 
 UnicodeString SimpleFormatter::getTextWithNoArguments(
-        const UChar *compiledPattern, int32_t compiledPatternLength) {
+        const UChar *compiledPattern,
+        int32_t compiledPatternLength,
+        int32_t* offsets,
+        int32_t offsetsLength) {
+    for (int32_t i = 0; i < offsetsLength; i++) {
+        offsets[i] = -1;
+    }
     int32_t capacity = compiledPatternLength - 1 -
             getArgumentLimit(compiledPattern, compiledPatternLength);
     UnicodeString sb(capacity, 0, 0);  // Java: StringBuilder
     for (int32_t i = 1; i < compiledPatternLength;) {
-        int32_t segmentLength = compiledPattern[i++] - ARG_NUM_LIMIT;
-        if (segmentLength > 0) {
-            sb.append(compiledPattern + i, segmentLength);
-            i += segmentLength;
+        int32_t n = compiledPattern[i++];
+        if (n > ARG_NUM_LIMIT) {
+            n -= ARG_NUM_LIMIT;
+            sb.append(compiledPattern + i, n);
+            i += n;
+        } else if (n < offsetsLength) {
+            offsets[n] = sb.length();
         }
     }
     return sb;