12e5b6d6dSopenharmony_ci// © 2016 and later: Unicode, Inc. and others.
22e5b6d6dSopenharmony_ci// License & terms of use: http://www.unicode.org/copyright.html
32e5b6d6dSopenharmony_ci/*
42e5b6d6dSopenharmony_ci*******************************************************************************
52e5b6d6dSopenharmony_ci*   Copyright (C) 2011, International Business Machines
62e5b6d6dSopenharmony_ci*   Corporation and others.  All Rights Reserved.
72e5b6d6dSopenharmony_ci*******************************************************************************
82e5b6d6dSopenharmony_ci*   file name:  ucasemap_titlecase_brkiter.cpp
92e5b6d6dSopenharmony_ci*   encoding:   UTF-8
102e5b6d6dSopenharmony_ci*   tab size:   8 (not used)
112e5b6d6dSopenharmony_ci*   indentation:4
122e5b6d6dSopenharmony_ci*
132e5b6d6dSopenharmony_ci*   created on: 2011jun02
142e5b6d6dSopenharmony_ci*   created by: Markus W. Scherer
152e5b6d6dSopenharmony_ci*
162e5b6d6dSopenharmony_ci*   Titlecasing functions that are based on BreakIterator
172e5b6d6dSopenharmony_ci*   were moved here to break dependency cycles among parts of the common library.
182e5b6d6dSopenharmony_ci*/
192e5b6d6dSopenharmony_ci
202e5b6d6dSopenharmony_ci#include "unicode/utypes.h"
212e5b6d6dSopenharmony_ci
222e5b6d6dSopenharmony_ci#if !UCONFIG_NO_BREAK_ITERATION
232e5b6d6dSopenharmony_ci
242e5b6d6dSopenharmony_ci#include "unicode/brkiter.h"
252e5b6d6dSopenharmony_ci#include "unicode/ubrk.h"
262e5b6d6dSopenharmony_ci#include "unicode/casemap.h"
272e5b6d6dSopenharmony_ci#include "unicode/ucasemap.h"
282e5b6d6dSopenharmony_ci#include "cmemory.h"
292e5b6d6dSopenharmony_ci#include "ucase.h"
302e5b6d6dSopenharmony_ci#include "ucasemap_imp.h"
312e5b6d6dSopenharmony_ci
322e5b6d6dSopenharmony_ciU_NAMESPACE_BEGIN
332e5b6d6dSopenharmony_ci
342e5b6d6dSopenharmony_civoid CaseMap::utf8ToTitle(
352e5b6d6dSopenharmony_ci        const char *locale, uint32_t options, BreakIterator *iter,
362e5b6d6dSopenharmony_ci        StringPiece src, ByteSink &sink, Edits *edits,
372e5b6d6dSopenharmony_ci        UErrorCode &errorCode) {
382e5b6d6dSopenharmony_ci    if (U_FAILURE(errorCode)) {
392e5b6d6dSopenharmony_ci        return;
402e5b6d6dSopenharmony_ci    }
412e5b6d6dSopenharmony_ci    UText utext = UTEXT_INITIALIZER;
422e5b6d6dSopenharmony_ci    utext_openUTF8(&utext, src.data(), src.length(), &errorCode);
432e5b6d6dSopenharmony_ci    LocalPointer<BreakIterator> ownedIter;
442e5b6d6dSopenharmony_ci    iter = ustrcase_getTitleBreakIterator(nullptr, locale, options, iter, ownedIter, errorCode);
452e5b6d6dSopenharmony_ci    if (iter == nullptr) {
462e5b6d6dSopenharmony_ci        utext_close(&utext);
472e5b6d6dSopenharmony_ci        return;
482e5b6d6dSopenharmony_ci    }
492e5b6d6dSopenharmony_ci    iter->setText(&utext, errorCode);
502e5b6d6dSopenharmony_ci    ucasemap_mapUTF8(
512e5b6d6dSopenharmony_ci        ustrcase_getCaseLocale(locale), options, iter,
522e5b6d6dSopenharmony_ci        src.data(), src.length(),
532e5b6d6dSopenharmony_ci        ucasemap_internalUTF8ToTitle, sink, edits, errorCode);
542e5b6d6dSopenharmony_ci    utext_close(&utext);
552e5b6d6dSopenharmony_ci}
562e5b6d6dSopenharmony_ci
572e5b6d6dSopenharmony_ciint32_t CaseMap::utf8ToTitle(
582e5b6d6dSopenharmony_ci        const char *locale, uint32_t options, BreakIterator *iter,
592e5b6d6dSopenharmony_ci        const char *src, int32_t srcLength,
602e5b6d6dSopenharmony_ci        char *dest, int32_t destCapacity, Edits *edits,
612e5b6d6dSopenharmony_ci        UErrorCode &errorCode) {
622e5b6d6dSopenharmony_ci    if (U_FAILURE(errorCode)) {
632e5b6d6dSopenharmony_ci        return 0;
642e5b6d6dSopenharmony_ci    }
652e5b6d6dSopenharmony_ci    UText utext=UTEXT_INITIALIZER;
662e5b6d6dSopenharmony_ci    utext_openUTF8(&utext, src, srcLength, &errorCode);
672e5b6d6dSopenharmony_ci    LocalPointer<BreakIterator> ownedIter;
682e5b6d6dSopenharmony_ci    iter = ustrcase_getTitleBreakIterator(nullptr, locale, options, iter, ownedIter, errorCode);
692e5b6d6dSopenharmony_ci    if(iter==NULL) {
702e5b6d6dSopenharmony_ci        utext_close(&utext);
712e5b6d6dSopenharmony_ci        return 0;
722e5b6d6dSopenharmony_ci    }
732e5b6d6dSopenharmony_ci    iter->setText(&utext, errorCode);
742e5b6d6dSopenharmony_ci    int32_t length=ucasemap_mapUTF8(
752e5b6d6dSopenharmony_ci        ustrcase_getCaseLocale(locale), options, iter,
762e5b6d6dSopenharmony_ci        dest, destCapacity,
772e5b6d6dSopenharmony_ci        src, srcLength,
782e5b6d6dSopenharmony_ci        ucasemap_internalUTF8ToTitle, edits, errorCode);
792e5b6d6dSopenharmony_ci    utext_close(&utext);
802e5b6d6dSopenharmony_ci    return length;
812e5b6d6dSopenharmony_ci}
822e5b6d6dSopenharmony_ci
832e5b6d6dSopenharmony_ciU_NAMESPACE_END
842e5b6d6dSopenharmony_ci
852e5b6d6dSopenharmony_ciU_NAMESPACE_USE
862e5b6d6dSopenharmony_ci
872e5b6d6dSopenharmony_ciU_CAPI const UBreakIterator * U_EXPORT2
882e5b6d6dSopenharmony_ciucasemap_getBreakIterator(const UCaseMap *csm) {
892e5b6d6dSopenharmony_ci    return reinterpret_cast<UBreakIterator *>(csm->iter);
902e5b6d6dSopenharmony_ci}
912e5b6d6dSopenharmony_ci
922e5b6d6dSopenharmony_ciU_CAPI void U_EXPORT2
932e5b6d6dSopenharmony_ciucasemap_setBreakIterator(UCaseMap *csm, UBreakIterator *iterToAdopt, UErrorCode *pErrorCode) {
942e5b6d6dSopenharmony_ci    if(U_FAILURE(*pErrorCode)) {
952e5b6d6dSopenharmony_ci        return;
962e5b6d6dSopenharmony_ci    }
972e5b6d6dSopenharmony_ci    delete csm->iter;
982e5b6d6dSopenharmony_ci    csm->iter=reinterpret_cast<BreakIterator *>(iterToAdopt);
992e5b6d6dSopenharmony_ci}
1002e5b6d6dSopenharmony_ci
1012e5b6d6dSopenharmony_ciU_CAPI int32_t U_EXPORT2
1022e5b6d6dSopenharmony_ciucasemap_utf8ToTitle(UCaseMap *csm,
1032e5b6d6dSopenharmony_ci                     char *dest, int32_t destCapacity,
1042e5b6d6dSopenharmony_ci                     const char *src, int32_t srcLength,
1052e5b6d6dSopenharmony_ci                     UErrorCode *pErrorCode) {
1062e5b6d6dSopenharmony_ci    if (U_FAILURE(*pErrorCode)) {
1072e5b6d6dSopenharmony_ci        return 0;
1082e5b6d6dSopenharmony_ci    }
1092e5b6d6dSopenharmony_ci    UText utext=UTEXT_INITIALIZER;
1102e5b6d6dSopenharmony_ci    utext_openUTF8(&utext, (const char *)src, srcLength, pErrorCode);
1112e5b6d6dSopenharmony_ci    if (U_FAILURE(*pErrorCode)) {
1122e5b6d6dSopenharmony_ci        return 0;
1132e5b6d6dSopenharmony_ci    }
1142e5b6d6dSopenharmony_ci    if(csm->iter==NULL) {
1152e5b6d6dSopenharmony_ci        LocalPointer<BreakIterator> ownedIter;
1162e5b6d6dSopenharmony_ci        BreakIterator *iter = ustrcase_getTitleBreakIterator(
1172e5b6d6dSopenharmony_ci            nullptr, csm->locale, csm->options, nullptr, ownedIter, *pErrorCode);
1182e5b6d6dSopenharmony_ci        if (iter == nullptr) {
1192e5b6d6dSopenharmony_ci            utext_close(&utext);
1202e5b6d6dSopenharmony_ci            return 0;
1212e5b6d6dSopenharmony_ci        }
1222e5b6d6dSopenharmony_ci        csm->iter = ownedIter.orphan();
1232e5b6d6dSopenharmony_ci    }
1242e5b6d6dSopenharmony_ci    csm->iter->setText(&utext, *pErrorCode);
1252e5b6d6dSopenharmony_ci    int32_t length=ucasemap_mapUTF8(
1262e5b6d6dSopenharmony_ci            csm->caseLocale, csm->options, csm->iter,
1272e5b6d6dSopenharmony_ci            dest, destCapacity,
1282e5b6d6dSopenharmony_ci            src, srcLength,
1292e5b6d6dSopenharmony_ci            ucasemap_internalUTF8ToTitle, NULL, *pErrorCode);
1302e5b6d6dSopenharmony_ci    utext_close(&utext);
1312e5b6d6dSopenharmony_ci    return length;
1322e5b6d6dSopenharmony_ci}
1332e5b6d6dSopenharmony_ci
1342e5b6d6dSopenharmony_ci#endif  // !UCONFIG_NO_BREAK_ITERATION
135