]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/common/unistr_case.cpp
ICU-64260.0.1.tar.gz
[apple/icu.git] / icuSources / common / unistr_case.cpp
index 1c62ce5e974e2373a5ea8172d050ca55b33b4316..2138d60c01c2d17be8dae6c51f7e1a17403e2c43 100644 (file)
@@ -19,6 +19,7 @@
 */
 
 #include "unicode/utypes.h"
+#include "unicode/brkiter.h"
 #include "unicode/casemap.h"
 #include "unicode/edits.h"
 #include "unicode/putil.h"
@@ -104,6 +105,12 @@ UnicodeString::caseMap(int32_t caseLocale, uint32_t options, UCASEMAP_BREAK_ITER
   UBool writable = isBufferWritable();
   UErrorCode errorCode = U_ZERO_ERROR;
 
+#if !UCONFIG_NO_BREAK_ITERATION
+  // Read-only alias to the original string contents for the titlecasing BreakIterator.
+  // We cannot set the iterator simply to *this because *this is being modified.
+  UnicodeString oldString;
+#endif
+
   // Try to avoid heap-allocating a new character array for this string.
   if (writable ? oldLength <= UPRV_LENGTHOF(oldBuffer) : oldLength < US_STACKBUF_SIZE) {
     // Short string: Copy the contents into a temporary buffer and
@@ -123,6 +130,12 @@ UnicodeString::caseMap(int32_t caseLocale, uint32_t options, UCASEMAP_BREAK_ITER
       buffer = fUnion.fStackFields.fBuffer;
       capacity = US_STACKBUF_SIZE;
     }
+#if !UCONFIG_NO_BREAK_ITERATION
+    if (iter != nullptr) {
+      oldString.setTo(FALSE, oldArray, oldLength);
+      iter->setText(oldString);
+    }
+#endif
     newLength = stringCaseMapper(caseLocale, options, UCASEMAP_BREAK_ITERATOR
                                  buffer, capacity,
                                  oldArray, oldLength, NULL, errorCode);
@@ -143,7 +156,13 @@ UnicodeString::caseMap(int32_t caseLocale, uint32_t options, UCASEMAP_BREAK_ITER
     oldArray = getArrayStart();
     Edits edits;
     UChar replacementChars[200];
-    stringCaseMapper(caseLocale, options | UCASEMAP_OMIT_UNCHANGED_TEXT, UCASEMAP_BREAK_ITERATOR
+#if !UCONFIG_NO_BREAK_ITERATION
+    if (iter != nullptr) {
+      oldString.setTo(FALSE, oldArray, oldLength);
+      iter->setText(oldString);
+    }
+#endif
+    stringCaseMapper(caseLocale, options | U_OMIT_UNCHANGED_TEXT, UCASEMAP_BREAK_ITERATOR
                      replacementChars, UPRV_LENGTHOF(replacementChars),
                      oldArray, oldLength, &edits, errorCode);
     if (U_SUCCESS(errorCode)) {
@@ -179,6 +198,7 @@ UnicodeString::caseMap(int32_t caseLocale, uint32_t options, UCASEMAP_BREAK_ITER
     return *this;
   }
   errorCode = U_ZERO_ERROR;
+  // No need to iter->setText() again: The case mapper restarts via iter->first().
   newLength = stringCaseMapper(caseLocale, options, UCASEMAP_BREAK_ITERATOR
                                getArrayStart(), getCapacity(),
                                oldArray, oldLength, NULL, errorCode);