intl/icu/source/common/unistr_titlecase_brkiter.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /*
     2 *******************************************************************************
     3 *   Copyright (C) 2011, International Business Machines
     4 *   Corporation and others.  All Rights Reserved.
     5 *******************************************************************************
     6 *   file name:  unistr_titlecase_brkiter.cpp
     7 *   encoding:   US-ASCII
     8 *   tab size:   8 (not used)
     9 *   indentation:2
    10 *
    11 *   created on: 2011may30
    12 *   created by: Markus W. Scherer
    13 *
    14 *   Titlecasing functions that are based on BreakIterator
    15 *   were moved here to break dependency cycles among parts of the common library.
    16 */
    18 #include "unicode/utypes.h"
    20 #if !UCONFIG_NO_BREAK_ITERATION
    22 #include "unicode/brkiter.h"
    23 #include "unicode/ubrk.h"
    24 #include "unicode/unistr.h"
    25 #include "unicode/ustring.h"
    26 #include "cmemory.h"
    27 #include "ustr_imp.h"
    29 static int32_t U_CALLCONV
    30 unistr_case_internalToTitle(const UCaseMap *csm,
    31                             UChar *dest, int32_t destCapacity,
    32                             const UChar *src, int32_t srcLength,
    33                             UErrorCode *pErrorCode) {
    34   ubrk_setText(csm->iter, src, srcLength, pErrorCode);
    35   return ustrcase_internalToTitle(csm, dest, destCapacity, src, srcLength, pErrorCode);
    36 }
    38 /*
    39  * Set parameters on an empty UCaseMap, for UCaseMap-less API functions.
    40  * Do this fast because it is called with every function call.
    41  */
    42 static inline void
    43 setTempCaseMap(UCaseMap *csm, const char *locale) {
    44     if(csm->csp==NULL) {
    45         csm->csp=ucase_getSingleton();
    46     }
    47     if(locale!=NULL && locale[0]==0) {
    48         csm->locale[0]=0;
    49     } else {
    50         ustrcase_setTempCaseMapLocale(csm, locale);
    51     }
    52 }
    54 U_NAMESPACE_BEGIN
    56 UnicodeString &
    57 UnicodeString::toTitle(BreakIterator *titleIter) {
    58   return toTitle(titleIter, Locale::getDefault(), 0);
    59 }
    61 UnicodeString &
    62 UnicodeString::toTitle(BreakIterator *titleIter, const Locale &locale) {
    63   return toTitle(titleIter, locale, 0);
    64 }
    66 UnicodeString &
    67 UnicodeString::toTitle(BreakIterator *titleIter, const Locale &locale, uint32_t options) {
    68   UCaseMap csm=UCASEMAP_INITIALIZER;
    69   csm.options=options;
    70   setTempCaseMap(&csm, locale.getName());
    71   BreakIterator *bi=titleIter;
    72   if(bi==NULL) {
    73     UErrorCode errorCode=U_ZERO_ERROR;
    74     bi=BreakIterator::createWordInstance(locale, errorCode);
    75     if(U_FAILURE(errorCode)) {
    76       setToBogus();
    77       return *this;
    78     }
    79   }
    80   csm.iter=reinterpret_cast<UBreakIterator *>(bi);
    81   caseMap(&csm, unistr_case_internalToTitle);
    82   if(titleIter==NULL) {
    83     delete bi;
    84   }
    85   return *this;
    86 }
    88 U_NAMESPACE_END
    90 #endif  // !UCONFIG_NO_BREAK_ITERATION

mercurial