• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 *******************************************************************************
3 *
4 *   Copyright (C) 2001-2010, International Business Machines
5 *   Corporation and others.  All Rights Reserved.
6 *
7 *******************************************************************************
8 *   file name:  casetrn.cpp
9 *   encoding:   US-ASCII
10 *   tab size:   8 (not used)
11 *   indentation:4
12 *
13 *   created on: 2004sep03
14 *   created by: Markus W. Scherer
15 *
16 *   Implementation class for lower-/upper-/title-casing transliterators.
17 */
18 
19 #include "unicode/utypes.h"
20 
21 #if !UCONFIG_NO_TRANSLITERATION
22 
23 #include "unicode/uchar.h"
24 #include "unicode/ustring.h"
25 #include "tolowtrn.h"
26 #include "ucase.h"
27 #include "cpputils.h"
28 
29 /* case context iterator using a Replaceable */
30 U_CFUNC UChar32 U_CALLCONV
utrans_rep_caseContextIterator(void * context,int8_t dir)31 utrans_rep_caseContextIterator(void *context, int8_t dir)
32 {
33     U_NAMESPACE_USE
34 
35     UCaseContext *csc=(UCaseContext *)context;
36     Replaceable *rep=(Replaceable *)csc->p;
37     UChar32 c;
38 
39     if(dir<0) {
40         /* reset for backward iteration */
41         csc->index=csc->cpStart;
42         csc->dir=dir;
43     } else if(dir>0) {
44         /* reset for forward iteration */
45         csc->index=csc->cpLimit;
46         csc->dir=dir;
47     } else {
48         /* continue current iteration direction */
49         dir=csc->dir;
50     }
51 
52     // automatically adjust start and limit if the Replaceable disagrees
53     // with the original values
54     if(dir<0) {
55         if(csc->start<csc->index) {
56             c=rep->char32At(csc->index-1);
57             if(c<0) {
58                 csc->start=csc->index;
59             } else {
60                 csc->index-=U16_LENGTH(c);
61                 return c;
62             }
63         }
64     } else {
65         // detect, and store in csc->b1, if we hit the limit
66         if(csc->index<csc->limit) {
67             c=rep->char32At(csc->index);
68             if(c<0) {
69                 csc->limit=csc->index;
70                 csc->b1=TRUE;
71             } else {
72                 csc->index+=U16_LENGTH(c);
73                 return c;
74             }
75         } else {
76             csc->b1=TRUE;
77         }
78     }
79     return U_SENTINEL;
80 }
81 
82 U_NAMESPACE_BEGIN
83 
UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(CaseMapTransliterator)84 UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(CaseMapTransliterator)
85 
86 /**
87  * Constructs a transliterator.
88  */
89 CaseMapTransliterator::CaseMapTransliterator(const UnicodeString &id, UCaseMapFull *map) :
90     Transliterator(id, 0),
91     fCsp(ucase_getSingleton()),
92     fMap(map)
93 {
94     // TODO test incremental mode with context-sensitive text (e.g. greek sigma)
95     // TODO need to call setMaximumContextLength()?!
96 }
97 
98 /**
99  * Destructor.
100  */
~CaseMapTransliterator()101 CaseMapTransliterator::~CaseMapTransliterator() {
102 }
103 
104 /**
105  * Copy constructor.
106  */
CaseMapTransliterator(const CaseMapTransliterator & o)107 CaseMapTransliterator::CaseMapTransliterator(const CaseMapTransliterator& o) :
108     Transliterator(o),
109     fCsp(o.fCsp), fMap(o.fMap)
110 {
111 }
112 
113 /**
114  * Assignment operator.
115  */
116 /*CaseMapTransliterator& CaseMapTransliterator::operator=(const CaseMapTransliterator& o) {
117     Transliterator::operator=(o);
118     fCsp = o.fCsp;
119     fMap = o.fMap;
120     return *this;
121 }*/
122 
123 /**
124  * Transliterator API.
125  */
126 /*Transliterator* CaseMapTransliterator::clone(void) const {
127     return new CaseMapTransliterator(*this);
128 }*/
129 
130 /**
131  * Implements {@link Transliterator#handleTransliterate}.
132  */
handleTransliterate(Replaceable & text,UTransPosition & offsets,UBool isIncremental) const133 void CaseMapTransliterator::handleTransliterate(Replaceable& text,
134                                  UTransPosition& offsets,
135                                  UBool isIncremental) const
136 {
137     if (offsets.start >= offsets.limit) {
138         return;
139     }
140 
141     UCaseContext csc;
142     uprv_memset(&csc, 0, sizeof(csc));
143     csc.p = &text;
144     csc.start = offsets.contextStart;
145     csc.limit = offsets.contextLimit;
146 
147     UnicodeString tmp;
148     const UChar *s;
149     UChar32 c;
150     int32_t textPos, delta, result, locCache=0;
151 
152     for(textPos=offsets.start; textPos<offsets.limit;) {
153         csc.cpStart=textPos;
154         c=text.char32At(textPos);
155         csc.cpLimit=textPos+=U16_LENGTH(c);
156 
157         result=fMap(fCsp, c, utrans_rep_caseContextIterator, &csc, &s, "", &locCache);
158 
159         if(csc.b1 && isIncremental) {
160             // fMap() tried to look beyond the context limit
161             // wait for more input
162             offsets.start=csc.cpStart;
163             return;
164         }
165 
166         if(result>=0) {
167             // replace the current code point with its full case mapping result
168             // see UCASE_MAX_STRING_LENGTH
169             if(result<=UCASE_MAX_STRING_LENGTH) {
170                 // string s[result]
171                 tmp.setTo(FALSE, s, result);
172                 delta=result-U16_LENGTH(c);
173             } else {
174                 // single code point
175                 tmp.setTo(result);
176                 delta=tmp.length()-U16_LENGTH(c);
177             }
178             text.handleReplaceBetween(csc.cpStart, textPos, tmp);
179             if(delta!=0) {
180                 textPos+=delta;
181                 csc.limit=offsets.contextLimit+=delta;
182                 offsets.limit+=delta;
183             }
184         }
185     }
186     offsets.start=textPos;
187 }
188 
189 U_NAMESPACE_END
190 
191 #endif /* #if !UCONFIG_NO_TRANSLITERATION */
192