]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/common/ustr_titlecase_brkiter.cpp
ICU-59117.0.1.tar.gz
[apple/icu.git] / icuSources / common / ustr_titlecase_brkiter.cpp
index 4a2352eed507dda093a97fb444310f00af7f6cd8..0b2ba02064b3249c72783411d6ad3d064b45513d 100644 (file)
@@ -1,10 +1,12 @@
+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
 /*
 *******************************************************************************
 *   Copyright (C) 2011, International Business Machines
 *   Corporation and others.  All Rights Reserved.
 *******************************************************************************
 *   file name:  ustr_titlecase_brkiter.cpp
-*   encoding:   US-ASCII
+*   encoding:   UTF-8
 *   tab size:   8 (not used)
 *   indentation:4
 *
 #if !UCONFIG_NO_BREAK_ITERATION
 
 #include "unicode/brkiter.h"
+#include "unicode/casemap.h"
+#include "unicode/localpointer.h"
 #include "unicode/ubrk.h"
 #include "unicode/ucasemap.h"
 #include "cmemory.h"
 #include "ucase.h"
-#include "ustr_imp.h"
+#include "ucasemap_imp.h"
 
-/* functions available in the common library (for unistr_case.cpp) */
+U_NAMESPACE_USE
 
-/*
- * Set parameters on an empty UCaseMap, for UCaseMap-less API functions.
- * Do this fast because it is called with every function call.
- * Duplicate of the same function in ustrcase.cpp, to keep it inline.
- */
-static inline void
-setTempCaseMap(UCaseMap *csm, const char *locale) {
-    if(csm->csp==NULL) {
-        csm->csp=ucase_getSingleton();
-    }
-    if(locale!=NULL && locale[0]==0) {
-        csm->locale[0]=0;
-    } else {
-        ustrcase_setTempCaseMapLocale(csm, locale);
-    }
-}
+/* functions available in the common library (for unistr_case.cpp) */
 
 /* public API functions */
 
@@ -53,39 +42,73 @@ u_strToTitle(UChar *dest, int32_t destCapacity,
              UBreakIterator *titleIter,
              const char *locale,
              UErrorCode *pErrorCode) {
-    UCaseMap csm=UCASEMAP_INITIALIZER;
-    setTempCaseMap(&csm, locale);
+    LocalPointer<BreakIterator> ownedIter;
+    BreakIterator *iter;
     if(titleIter!=NULL) {
-        ubrk_setText(csm.iter=titleIter, src, srcLength, pErrorCode);
+        iter=reinterpret_cast<BreakIterator *>(titleIter);
     } else {
-        csm.iter=ubrk_open(UBRK_WORD, csm.locale, src, srcLength, pErrorCode);
+        iter=BreakIterator::createWordInstance(Locale(locale), *pErrorCode);
+        ownedIter.adoptInstead(iter);
+    }
+    if(U_FAILURE(*pErrorCode)) {
+        return 0;
     }
-    int32_t length=ustrcase_map(
-        &csm,
+    UnicodeString s(srcLength<0, src, srcLength);
+    iter->setText(s);
+    return ustrcase_mapWithOverlap(
+        ustrcase_getCaseLocale(locale), 0, iter,
         dest, destCapacity,
         src, srcLength,
-        ustrcase_internalToTitle, pErrorCode);
-    if(titleIter==NULL && csm.iter!=NULL) {
-        ubrk_close(csm.iter);
+        ustrcase_internalToTitle, *pErrorCode);
+}
+
+U_NAMESPACE_BEGIN
+
+int32_t CaseMap::toTitle(
+        const char *locale, uint32_t options, BreakIterator *iter,
+        const UChar *src, int32_t srcLength,
+        UChar *dest, int32_t destCapacity, Edits *edits,
+        UErrorCode &errorCode) {
+    LocalPointer<BreakIterator> ownedIter;
+    if(iter==NULL) {
+        iter=BreakIterator::createWordInstance(Locale(locale), errorCode);
+        ownedIter.adoptInstead(iter);
+    }
+    if(U_FAILURE(errorCode)) {
+        return 0;
     }
-    return length;
+    UnicodeString s(srcLength<0, src, srcLength);
+    iter->setText(s);
+    return ustrcase_map(
+        ustrcase_getCaseLocale(locale), options, iter,
+        dest, destCapacity,
+        src, srcLength,
+        ustrcase_internalToTitle, edits, errorCode);
 }
 
+U_NAMESPACE_END
+
 U_CAPI int32_t U_EXPORT2
 ucasemap_toTitle(UCaseMap *csm,
                  UChar *dest, int32_t destCapacity,
                  const UChar *src, int32_t srcLength,
                  UErrorCode *pErrorCode) {
-    if(csm->iter!=NULL) {
-        ubrk_setText(csm->iter, src, srcLength, pErrorCode);
-    } else {
-        csm->iter=ubrk_open(UBRK_WORD, csm->locale, src, srcLength, pErrorCode);
+    if (U_FAILURE(*pErrorCode)) {
+        return 0;
+    }
+    if (csm->iter == NULL) {
+        csm->iter = BreakIterator::createWordInstance(Locale(csm->locale), *pErrorCode);
+    }
+    if (U_FAILURE(*pErrorCode)) {
+        return 0;
     }
+    UnicodeString s(srcLength<0, src, srcLength);
+    csm->iter->setText(s);
     return ustrcase_map(
-        csm,
+        csm->caseLocale, csm->options, csm->iter,
         dest, destCapacity,
         src, srcLength,
-        ustrcase_internalToTitle, pErrorCode);
+        ustrcase_internalToTitle, NULL, *pErrorCode);
 }
 
 #endif  // !UCONFIG_NO_BREAK_ITERATION