2 **********************************************************************
3 * Copyright (C) 2001-2006, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 * Date Name Description
7 * 06/07/01 aliu Creation.
8 **********************************************************************
11 #include "unicode/utypes.h"
13 #if !UCONFIG_NO_TRANSLITERATION
15 #include "unicode/unifilt.h"
16 #include "unicode/uchar.h"
17 #include "unicode/uniset.h"
26 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(NameUnicodeTransliterator
)
28 static const UChar OPEN
[] = {92,78,126,123,126,0}; // "\N~{~"
29 static const UChar OPEN_DELIM
= 92; // '\\' first char of OPEN
30 static const UChar CLOSE_DELIM
= 125; // '}'
31 static const UChar SPACE
= 32; // ' '
35 // USetAdder implementation
36 // Does not use uset.h to reduce code dependencies
37 static void U_CALLCONV
38 _set_add(USet
*set
, UChar32 c
) {
39 ((UnicodeSet
*)set
)->add(c
);
42 static void U_CALLCONV
43 _set_addRange(USet
*set
, UChar32 start
, UChar32 end
) {
44 ((UnicodeSet
*)set
)->add(start
, end
);
47 static void U_CALLCONV
48 _set_addString(USet
*set
, const UChar
*str
, int32_t length
) {
49 ((UnicodeSet
*)set
)->add(UnicodeString((UBool
)(length
<0), str
, length
));
55 * Constructs a transliterator with the default delimiters '{' and
58 NameUnicodeTransliterator::NameUnicodeTransliterator(UnicodeFilter
* adoptedFilter
) :
59 Transliterator(UNICODE_STRING("Name-Any", 8), adoptedFilter
) {
61 UnicodeSet
*legalPtr
= &legal
;
62 // Get the legal character set
64 (USet
*)legalPtr
, // USet* == UnicodeSet*
68 NULL
// don't need remove()
70 uprv_getCharNameCharacters(&sa
);
76 NameUnicodeTransliterator::~NameUnicodeTransliterator() {}
81 NameUnicodeTransliterator::NameUnicodeTransliterator(const NameUnicodeTransliterator
& o
) :
82 Transliterator(o
), legal(o
.legal
) {}
85 * Assignment operator.
87 NameUnicodeTransliterator
& NameUnicodeTransliterator::operator=(
88 const NameUnicodeTransliterator
& o
) {
89 Transliterator::operator=(o
);
90 // not necessary: the legal sets should all be the same -- legal=o.legal;
97 Transliterator
* NameUnicodeTransliterator::clone(void) const {
98 return new NameUnicodeTransliterator(*this);
102 * Implements {@link Transliterator#handleTransliterate}.
104 void NameUnicodeTransliterator::handleTransliterate(Replaceable
& text
, UTransPosition
& offsets
,
105 UBool isIncremental
) const {
106 // The failure mode, here and below, is to behave like Any-Null,
107 // if either there is no name data (max len == 0) or there is no
108 // memory (malloc() => NULL).
110 int32_t maxLen
= uprv_getMaxCharNameLength();
112 offsets
.start
= offsets
.limit
;
116 // Accomodate the longest possible name
117 ++maxLen
; // allow for temporary trailing space
118 char* cbuf
= (char*) uprv_malloc(maxLen
);
120 offsets
.start
= offsets
.limit
;
124 UnicodeString
openPat(TRUE
, OPEN
, -1);
125 UnicodeString str
, name
;
127 int32_t cursor
= offsets
.start
;
128 int32_t limit
= offsets
.limit
;
131 // 0 - looking for open delimiter
132 // 1 - after open delimiter
134 int32_t openPos
= -1; // open delim candidate pos
137 while (cursor
< limit
) {
138 c
= text
.char32At(cursor
);
141 case 0: // looking for open delimiter
142 if (c
== OPEN_DELIM
) { // quick check first
145 ICU_Utility::parsePattern(openPat
, text
, cursor
, limit
);
146 if (i
>= 0 && i
< limit
) {
150 continue; // *** reprocess char32At(cursor)
155 case 1: // after open delimiter
156 // Look for legal chars. If \s+ is found, convert it
157 // to a single space. If closeDelimiter is found, exit
158 // the loop. If any other character is found, exit the
159 // loop. If the limit is reached, exit the loop.
161 // Convert \s+ => SPACE. This assumes there are no
162 // runs of >1 space characters in names.
163 if (uprv_isRuleWhiteSpace(c
)) {
164 // Ignore leading whitespace
165 if (name
.length() > 0 &&
166 name
.charAt(name
.length()-1) != SPACE
) {
168 // If we are too long then abort. maxLen includes
169 // temporary trailing space, so use '>'.
170 if (name
.length() > maxLen
) {
177 if (c
== CLOSE_DELIM
) {
178 int32_t len
= name
.length();
180 // Delete trailing space, if any
182 name
.charAt(len
-1) == SPACE
) {
186 if (uprv_isInvariantUString(name
.getBuffer(), len
)) {
187 name
.extract(0, len
, cbuf
, maxLen
, US_INV
);
189 UErrorCode status
= U_ZERO_ERROR
;
190 c
= u_charFromName(U_EXTENDED_CHAR_NAME
, cbuf
, &status
);
191 if (U_SUCCESS(status
)) {
194 // assert(UTF_CHAR_LENGTH(CLOSE_DELIM) == 1);
195 cursor
++; // advance over CLOSE_DELIM
199 text
.handleReplaceBetween(openPos
, cursor
, str
);
201 // Adjust indices for the change in the length of
202 // the string. Do not assume that str.length() ==
203 // 1, in case of surrogates.
204 int32_t delta
= cursor
- openPos
- str
.length();
207 // assert(cursor == openPos + str.length());
210 // If the lookup failed, we leave things as-is and
211 // still switch to mode 0 and continue.
213 openPos
= -1; // close off candidate
214 continue; // *** reprocess char32At(cursor)
217 // Check if c is a legal char. We assume here that
218 // legal.contains(OPEN_DELIM) is FALSE, so when we abort a
219 // name, we don't have to go back to openPos+1.
220 if (legal
.contains(c
)) {
222 // If we go past the longest possible name then abort.
223 // maxLen includes temporary trailing space, so use '>='.
224 if (name
.length() >= maxLen
) {
231 --cursor
; // Backup and reprocess this character
238 cursor
+= UTF_CHAR_LENGTH(c
);
241 offsets
.contextLimit
+= limit
- offsets
.limit
;
242 offsets
.limit
= limit
;
243 // In incremental mode, only advance the cursor up to the last
244 // open delimiter candidate.
245 offsets
.start
= (isIncremental
&& openPos
>= 0) ? openPos
: cursor
;
252 #endif /* #if !UCONFIG_NO_TRANSLITERATION */