]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/titletrn.cpp
ICU-64243.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / titletrn.cpp
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
b75a7d8f
A
3/*
4**********************************************************************
4388f060 5* Copyright (C) 2001-2011, International Business Machines
b75a7d8f
A
6* Corporation and others. All Rights Reserved.
7**********************************************************************
8* Date Name Description
9* 05/24/01 aliu Creation.
10**********************************************************************
11*/
12
13#include "unicode/utypes.h"
14
15#if !UCONFIG_NO_TRANSLITERATION
16
17#include "unicode/uchar.h"
18#include "unicode/uniset.h"
19#include "unicode/ustring.h"
4388f060 20#include "unicode/utf16.h"
b75a7d8f
A
21#include "titletrn.h"
22#include "umutex.h"
374ca955 23#include "ucase.h"
b75a7d8f
A
24#include "cpputils.h"
25
26U_NAMESPACE_BEGIN
27
374ca955 28UOBJECT_DEFINE_RTTI_IMPLEMENTATION(TitlecaseTransliterator)
b75a7d8f 29
73c04bcf
A
30TitlecaseTransliterator::TitlecaseTransliterator() :
31 CaseMapTransliterator(UNICODE_STRING("Any-Title", 9), NULL)
b75a7d8f 32{
b75a7d8f
A
33 // Need to look back 2 characters in the case of "can't"
34 setMaximumContextLength(2);
b75a7d8f
A
35}
36
37/**
38 * Destructor.
39 */
40TitlecaseTransliterator::~TitlecaseTransliterator() {
b75a7d8f
A
41}
42
43/**
44 * Copy constructor.
45 */
46TitlecaseTransliterator::TitlecaseTransliterator(const TitlecaseTransliterator& o) :
374ca955 47 CaseMapTransliterator(o)
b75a7d8f 48{
b75a7d8f
A
49}
50
51/**
52 * Assignment operator.
53 */
46f4442e 54/*TitlecaseTransliterator& TitlecaseTransliterator::operator=(
b75a7d8f 55 const TitlecaseTransliterator& o) {
374ca955 56 CaseMapTransliterator::operator=(o);
b75a7d8f 57 return *this;
46f4442e 58}*/
b75a7d8f
A
59
60/**
61 * Transliterator API.
62 */
63Transliterator* TitlecaseTransliterator::clone(void) const {
64 return new TitlecaseTransliterator(*this);
65}
66
67/**
68 * Implements {@link Transliterator#handleTransliterate}.
69 */
70void TitlecaseTransliterator::handleTransliterate(
71 Replaceable& text, UTransPosition& offsets,
374ca955
A
72 UBool isIncremental) const
73{
74 // TODO reimplement, see ustrcase.c
75 // using a real word break iterator
76 // instead of just looking for a transition between cased and uncased characters
77 // call CaseMapTransliterator::handleTransliterate() for lowercasing? (set fMap)
78 // needs to take isIncremental into account because case mappings are context-sensitive
79 // also detect when lowercasing function did not finish because of context
80
81 if (offsets.start >= offsets.limit) {
b75a7d8f
A
82 return;
83 }
84
374ca955
A
85 // case type: >0 cased (UCASE_LOWER etc.) ==0 uncased <0 case-ignorable
86 int32_t type;
87
b75a7d8f
A
88 // Our mode; we are either converting letter toTitle or
89 // toLower.
90 UBool doTitle = TRUE;
91
374ca955 92 // Determine if there is a preceding context of cased case-ignorable*,
b75a7d8f
A
93 // in which case we want to start in toLower mode. If the
94 // prior context is anything else (including empty) then start
95 // in toTitle mode.
96 UChar32 c;
97 int32_t start;
374ca955 98 for (start = offsets.start - 1; start >= offsets.contextStart; start -= U16_LENGTH(c)) {
b75a7d8f 99 c = text.char32At(start);
f3c0d7a5 100 type=ucase_getTypeOrIgnorable(c);
374ca955
A
101 if(type>0) { // cased
102 doTitle=FALSE;
103 break;
104 } else if(type==0) { // uncased but not ignorable
105 break;
b75a7d8f 106 }
374ca955 107 // else (type<0) case-ignorable: continue
b75a7d8f
A
108 }
109
374ca955
A
110 // Convert things after a cased character toLower; things
111 // after an uncased, non-case-ignorable character toTitle. Case-ignorable
b75a7d8f 112 // characters are copied directly and do not change the mode.
374ca955
A
113 UCaseContext csc;
114 uprv_memset(&csc, 0, sizeof(csc));
115 csc.p = &text;
116 csc.start = offsets.contextStart;
117 csc.limit = offsets.contextLimit;
118
119 UnicodeString tmp;
120 const UChar *s;
f3c0d7a5 121 int32_t textPos, delta, result;
374ca955
A
122
123 for(textPos=offsets.start; textPos<offsets.limit;) {
124 csc.cpStart=textPos;
125 c=text.char32At(textPos);
126 csc.cpLimit=textPos+=U16_LENGTH(c);
127
f3c0d7a5 128 type=ucase_getTypeOrIgnorable(c);
374ca955
A
129 if(type>=0) { // not case-ignorable
130 if(doTitle) {
f3c0d7a5 131 result=ucase_toFullTitle(c, utrans_rep_caseContextIterator, &csc, &s, UCASE_LOC_ROOT);
b75a7d8f 132 } else {
f3c0d7a5 133 result=ucase_toFullLower(c, utrans_rep_caseContextIterator, &csc, &s, UCASE_LOC_ROOT);
b75a7d8f 134 }
374ca955
A
135 doTitle = (UBool)(type==0); // doTitle=isUncased
136
137 if(csc.b1 && isIncremental) {
138 // fMap() tried to look beyond the context limit
139 // wait for more input
73c04bcf
A
140 offsets.start=csc.cpStart;
141 return;
374ca955
A
142 }
143
144 if(result>=0) {
145 // replace the current code point with its full case mapping result
146 // see UCASE_MAX_STRING_LENGTH
147 if(result<=UCASE_MAX_STRING_LENGTH) {
148 // string s[result]
149 tmp.setTo(FALSE, s, result);
150 delta=result-U16_LENGTH(c);
151 } else {
152 // single code point
153 tmp.setTo(result);
154 delta=tmp.length()-U16_LENGTH(c);
155 }
156 text.handleReplaceBetween(csc.cpStart, textPos, tmp);
157 if(delta!=0) {
158 textPos+=delta;
159 csc.limit=offsets.contextLimit+=delta;
160 offsets.limit+=delta;
b75a7d8f
A
161 }
162 }
163 }
b75a7d8f 164 }
374ca955 165 offsets.start=textPos;
b75a7d8f
A
166}
167
168U_NAMESPACE_END
169
170#endif /* #if !UCONFIG_NO_TRANSLITERATION */