2 **********************************************************************
3 * Copyright (C) 2001-2008, 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
) {
42 // These functions aren't used.
43 /*static void U_CALLCONV
44 _set_addRange(USet *set, UChar32 start, UChar32 end) {
45 ((UnicodeSet *)set)->add(start, end);
48 static void U_CALLCONV
49 _set_addString(USet *set, const UChar *str, int32_t length) {
50 ((UnicodeSet *)set)->add(UnicodeString((UBool)(length<0), str, length));
56 * Constructs a transliterator with the default delimiters '{' and
59 NameUnicodeTransliterator::NameUnicodeTransliterator(UnicodeFilter
* adoptedFilter
) :
60 Transliterator(UNICODE_STRING("Name-Any", 8), adoptedFilter
) {
62 UnicodeSet
*legalPtr
= &legal
;
63 // Get the legal character set
65 (USet
*)legalPtr
, // USet* == UnicodeSet*
67 NULL
, // Don't need _set_addRange
68 NULL
, // Don't need _set_addString
69 NULL
, // Don't need remove()
72 uprv_getCharNameCharacters(&sa
);
78 NameUnicodeTransliterator::~NameUnicodeTransliterator() {}
83 NameUnicodeTransliterator::NameUnicodeTransliterator(const NameUnicodeTransliterator
& o
) :
84 Transliterator(o
), legal(o
.legal
) {}
87 * Assignment operator.
89 /*NameUnicodeTransliterator& NameUnicodeTransliterator::operator=(
90 const NameUnicodeTransliterator& o) {
91 Transliterator::operator=(o);
92 // not necessary: the legal sets should all be the same -- legal=o.legal;
99 Transliterator
* NameUnicodeTransliterator::clone(void) const {
100 return new NameUnicodeTransliterator(*this);
104 * Implements {@link Transliterator#handleTransliterate}.
106 void NameUnicodeTransliterator::handleTransliterate(Replaceable
& text
, UTransPosition
& offsets
,
107 UBool isIncremental
) const {
108 // The failure mode, here and below, is to behave like Any-Null,
109 // if either there is no name data (max len == 0) or there is no
110 // memory (malloc() => NULL).
112 int32_t maxLen
= uprv_getMaxCharNameLength();
114 offsets
.start
= offsets
.limit
;
118 // Accomodate the longest possible name
119 ++maxLen
; // allow for temporary trailing space
120 char* cbuf
= (char*) uprv_malloc(maxLen
);
122 offsets
.start
= offsets
.limit
;
126 UnicodeString
openPat(TRUE
, OPEN
, -1);
127 UnicodeString str
, name
;
129 int32_t cursor
= offsets
.start
;
130 int32_t limit
= offsets
.limit
;
133 // 0 - looking for open delimiter
134 // 1 - after open delimiter
136 int32_t openPos
= -1; // open delim candidate pos
139 while (cursor
< limit
) {
140 c
= text
.char32At(cursor
);
143 case 0: // looking for open delimiter
144 if (c
== OPEN_DELIM
) { // quick check first
147 ICU_Utility::parsePattern(openPat
, text
, cursor
, limit
);
148 if (i
>= 0 && i
< limit
) {
152 continue; // *** reprocess char32At(cursor)
157 case 1: // after open delimiter
158 // Look for legal chars. If \s+ is found, convert it
159 // to a single space. If closeDelimiter is found, exit
160 // the loop. If any other character is found, exit the
161 // loop. If the limit is reached, exit the loop.
163 // Convert \s+ => SPACE. This assumes there are no
164 // runs of >1 space characters in names.
165 if (uprv_isRuleWhiteSpace(c
)) {
166 // Ignore leading whitespace
167 if (name
.length() > 0 &&
168 name
.charAt(name
.length()-1) != SPACE
) {
170 // If we are too long then abort. maxLen includes
171 // temporary trailing space, so use '>'.
172 if (name
.length() > maxLen
) {
179 if (c
== CLOSE_DELIM
) {
180 int32_t len
= name
.length();
182 // Delete trailing space, if any
184 name
.charAt(len
-1) == SPACE
) {
188 if (uprv_isInvariantUString(name
.getBuffer(), len
)) {
189 name
.extract(0, len
, cbuf
, maxLen
, US_INV
);
191 UErrorCode status
= U_ZERO_ERROR
;
192 c
= u_charFromName(U_EXTENDED_CHAR_NAME
, cbuf
, &status
);
193 if (U_SUCCESS(status
)) {
196 // assert(UTF_CHAR_LENGTH(CLOSE_DELIM) == 1);
197 cursor
++; // advance over CLOSE_DELIM
201 text
.handleReplaceBetween(openPos
, cursor
, str
);
203 // Adjust indices for the change in the length of
204 // the string. Do not assume that str.length() ==
205 // 1, in case of surrogates.
206 int32_t delta
= cursor
- openPos
- str
.length();
209 // assert(cursor == openPos + str.length());
212 // If the lookup failed, we leave things as-is and
213 // still switch to mode 0 and continue.
215 openPos
= -1; // close off candidate
216 continue; // *** reprocess char32At(cursor)
219 // Check if c is a legal char. We assume here that
220 // legal.contains(OPEN_DELIM) is FALSE, so when we abort a
221 // name, we don't have to go back to openPos+1.
222 if (legal
.contains(c
)) {
224 // If we go past the longest possible name then abort.
225 // maxLen includes temporary trailing space, so use '>='.
226 if (name
.length() >= maxLen
) {
233 --cursor
; // Backup and reprocess this character
240 cursor
+= UTF_CHAR_LENGTH(c
);
243 offsets
.contextLimit
+= limit
- offsets
.limit
;
244 offsets
.limit
= limit
;
245 // In incremental mode, only advance the cursor up to the last
246 // open delimiter candidate.
247 offsets
.start
= (isIncremental
&& openPos
>= 0) ? openPos
: cursor
;
254 #endif /* #if !UCONFIG_NO_TRANSLITERATION */