2 **********************************************************************
3 * Copyright (C) 2001-2004, 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"
25 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(NameUnicodeTransliterator
)
27 static const UChar OPEN
[] = {92,78,126,123,126,0}; // "\N~{~"
28 static const UChar OPEN_DELIM
= 92; // '\\' first char of OPEN
29 static const UChar CLOSE_DELIM
= 125; // '}'
30 static const UChar SPACE
= 32; // ' '
34 // USetAdder implementation
35 // Does not use uset.h to reduce code dependencies
36 static void U_CALLCONV
37 _set_add(USet
*set
, UChar32 c
) {
38 ((UnicodeSet
*)set
)->add(c
);
41 static void U_CALLCONV
42 _set_addRange(USet
*set
, UChar32 start
, UChar32 end
) {
43 ((UnicodeSet
*)set
)->add(start
, end
);
46 static void U_CALLCONV
47 _set_addString(USet
*set
, const UChar
*str
, int32_t length
) {
48 ((UnicodeSet
*)set
)->add(UnicodeString((UBool
)(length
<0), str
, length
));
54 * Constructs a transliterator with the default delimiters '{' and
57 NameUnicodeTransliterator::NameUnicodeTransliterator(UnicodeFilter
* adoptedFilter
) :
58 Transliterator(UNICODE_STRING("Name-Any", 8), adoptedFilter
) {
60 // Get the legal character set
62 (USet
*)&legal
, // USet* == UnicodeSet*
67 uprv_getCharNameCharacters(&sa
);
73 NameUnicodeTransliterator::~NameUnicodeTransliterator() {}
78 NameUnicodeTransliterator::NameUnicodeTransliterator(const NameUnicodeTransliterator
& o
) :
79 Transliterator(o
), legal(o
.legal
) {}
82 * Assignment operator.
84 NameUnicodeTransliterator
& NameUnicodeTransliterator::operator=(
85 const NameUnicodeTransliterator
& o
) {
86 Transliterator::operator=(o
);
87 // not necessary: the legal sets should all be the same -- legal=o.legal;
94 Transliterator
* NameUnicodeTransliterator::clone(void) const {
95 return new NameUnicodeTransliterator(*this);
99 * Implements {@link Transliterator#handleTransliterate}.
101 void NameUnicodeTransliterator::handleTransliterate(Replaceable
& text
, UTransPosition
& offsets
,
102 UBool isIncremental
) const {
103 // The failure mode, here and below, is to behave like Any-Null,
104 // if either there is no name data (max len == 0) or there is no
105 // memory (malloc() => NULL).
107 int32_t maxLen
= uprv_getMaxCharNameLength();
109 offsets
.start
= offsets
.limit
;
113 // Accomodate the longest possible name
114 ++maxLen
; // allow for temporary trailing space
115 char* cbuf
= (char*) uprv_malloc(maxLen
);
117 offsets
.start
= offsets
.limit
;
121 UnicodeString
openPat(TRUE
, OPEN
, -1);
122 UnicodeString str
, name
;
124 int32_t cursor
= offsets
.start
;
125 int32_t limit
= offsets
.limit
;
128 // 0 - looking for open delimiter
129 // 1 - after open delimiter
131 int32_t openPos
= -1; // open delim candidate pos
134 while (cursor
< limit
) {
135 c
= text
.char32At(cursor
);
138 case 0: // looking for open delimiter
139 if (c
== OPEN_DELIM
) { // quick check first
142 ICU_Utility::parsePattern(openPat
, text
, cursor
, limit
);
143 if (i
>= 0 && i
< limit
) {
147 continue; // *** reprocess char32At(cursor)
152 case 1: // after open delimiter
153 // Look for legal chars. If \s+ is found, convert it
154 // to a single space. If closeDelimiter is found, exit
155 // the loop. If any other character is found, exit the
156 // loop. If the limit is reached, exit the loop.
158 // Convert \s+ => SPACE. This assumes there are no
159 // runs of >1 space characters in names.
160 if (uprv_isRuleWhiteSpace(c
)) {
161 // Ignore leading whitespace
162 if (name
.length() > 0 &&
163 name
.charAt(name
.length()-1) != SPACE
) {
165 // If we are too long then abort. maxLen includes
166 // temporary trailing space, so use '>'.
167 if (name
.length() > maxLen
) {
174 if (c
== CLOSE_DELIM
) {
176 int32_t len
= name
.length();
178 // Delete trailing space, if any
180 name
.charAt(len
-1) == SPACE
) {
184 name
.extract(0, len
, cbuf
, "");
186 UErrorCode status
= U_ZERO_ERROR
;
187 c
= u_charFromName(U_EXTENDED_CHAR_NAME
, cbuf
, &status
);
188 if (U_SUCCESS(status
)) {
191 // assert(UTF_CHAR_LENGTH(CLOSE_DELIM) == 1);
192 cursor
++; // advance over CLOSE_DELIM
196 text
.handleReplaceBetween(openPos
, cursor
, str
);
198 // Adjust indices for the change in the length of
199 // the string. Do not assume that str.length() ==
200 // 1, in case of surrogates.
201 int32_t delta
= cursor
- openPos
- str
.length();
204 // assert(cursor == openPos + str.length());
206 // If the lookup failed, we leave things as-is and
207 // still switch to mode 0 and continue.
209 openPos
= -1; // close off candidate
210 continue; // *** reprocess char32At(cursor)
213 // Check if c is a legal char. We assume here that
214 // legal.contains(OPEN_DELIM) is FALSE, so when we abort a
215 // name, we don't have to go back to openPos+1.
216 if (legal
.contains(c
)) {
218 // If we go past the longest possible name then abort.
219 // maxLen includes temporary trailing space, so use '>='.
220 if (name
.length() >= maxLen
) {
227 --cursor
; // Backup and reprocess this character
234 cursor
+= UTF_CHAR_LENGTH(c
);
237 offsets
.contextLimit
+= limit
- offsets
.limit
;
238 offsets
.limit
= limit
;
239 // In incremental mode, only advance the cursor up to the last
240 // open delimiter candidate.
241 offsets
.start
= (isIncremental
&& openPos
>= 0) ? openPos
: cursor
;
248 #endif /* #if !UCONFIG_NO_TRANSLITERATION */