1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 * Copyright (C) 2011, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: ucasemap_titlecase_brkiter.cpp
9 * encoding: US-ASCII
10 * tab size: 8 (not used)
11 * indentation:4
12 *
13 * created on: 2011jun02
14 * created by: Markus W. Scherer
15 *
16 * Titlecasing functions that are based on BreakIterator
17 * were moved here to break dependency cycles among parts of the common library.
18 */
19
20 #include "unicode/utypes.h"
21
22 #if !UCONFIG_NO_BREAK_ITERATION
23
24 #include "unicode/brkiter.h"
25 #include "unicode/ubrk.h"
26 #include "unicode/ucasemap.h"
27 #include "cmemory.h"
28 #include "ucase.h"
29 #include "ustr_imp.h"
30
31 U_NAMESPACE_USE
32
33 U_CAPI const UBreakIterator * U_EXPORT2
ucasemap_getBreakIterator(const UCaseMap * csm)34 ucasemap_getBreakIterator(const UCaseMap *csm) {
35 return csm->iter;
36 }
37
38 U_CAPI void U_EXPORT2
ucasemap_setBreakIterator(UCaseMap * csm,UBreakIterator * iterToAdopt,UErrorCode *)39 ucasemap_setBreakIterator(UCaseMap *csm, UBreakIterator *iterToAdopt, UErrorCode * /*pErrorCode*/) {
40 // Do not call ubrk_close() so that we do not depend on all of the BreakIterator code.
41 delete reinterpret_cast<BreakIterator *>(csm->iter);
42 csm->iter=iterToAdopt;
43 }
44
45 U_CAPI int32_t U_EXPORT2
ucasemap_utf8ToTitle(UCaseMap * csm,char * dest,int32_t destCapacity,const char * src,int32_t srcLength,UErrorCode * pErrorCode)46 ucasemap_utf8ToTitle(UCaseMap *csm,
47 char *dest, int32_t destCapacity,
48 const char *src, int32_t srcLength,
49 UErrorCode *pErrorCode) {
50 UText utext=UTEXT_INITIALIZER;
51 utext_openUTF8(&utext, (const char *)src, srcLength, pErrorCode);
52 if(U_FAILURE(*pErrorCode)) {
53 return 0;
54 }
55 if(csm->iter==NULL) {
56 csm->iter=ubrk_open(UBRK_WORD, csm->locale,
57 NULL, 0,
58 pErrorCode);
59 }
60 ubrk_setUText(csm->iter, &utext, pErrorCode);
61 int32_t length=ucasemap_mapUTF8(csm,
62 (uint8_t *)dest, destCapacity,
63 (const uint8_t *)src, srcLength,
64 ucasemap_internalUTF8ToTitle, pErrorCode);
65 utext_close(&utext);
66 return length;
67 }
68
69 #endif // !UCONFIG_NO_BREAK_ITERATION
70