1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (C) 2001-2011, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
8 * Date Name Description
9 * 06/07/01 aliu Creation.
10 **********************************************************************
13 #include "unicode/utypes.h"
15 #if !UCONFIG_NO_TRANSLITERATION
17 #include "unicode/unifilt.h"
18 #include "unicode/uchar.h"
19 #include "unicode/uniset.h"
20 #include "unicode/utf16.h"
23 #include "patternprops.h"
30 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(NameUnicodeTransliterator
)
32 static const UChar OPEN
[] = {92,78,126,123,126,0}; // "\N~{~"
33 static const UChar OPEN_DELIM
= 92; // '\\' first char of OPEN
34 static const UChar CLOSE_DELIM
= 125; // '}'
35 static const UChar SPACE
= 32; // ' '
39 // USetAdder implementation
40 // Does not use uset.h to reduce code dependencies
41 static void U_CALLCONV
42 _set_add(USet
*set
, UChar32 c
) {
46 // These functions aren't used.
47 /*static void U_CALLCONV
48 _set_addRange(USet *set, UChar32 start, UChar32 end) {
49 ((UnicodeSet *)set)->add(start, end);
52 static void U_CALLCONV
53 _set_addString(USet *set, const UChar *str, int32_t length) {
54 ((UnicodeSet *)set)->add(UnicodeString((UBool)(length<0), str, length));
60 * Constructs a transliterator with the default delimiters '{' and
63 NameUnicodeTransliterator::NameUnicodeTransliterator(UnicodeFilter
* adoptedFilter
) :
64 Transliterator(UNICODE_STRING("Name-Any", 8), adoptedFilter
) {
66 UnicodeSet
*legalPtr
= &legal
;
67 // Get the legal character set
69 (USet
*)legalPtr
, // USet* == UnicodeSet*
71 NULL
, // Don't need _set_addRange
72 NULL
, // Don't need _set_addString
73 NULL
, // Don't need remove()
76 uprv_getCharNameCharacters(&sa
);
82 NameUnicodeTransliterator::~NameUnicodeTransliterator() {}
87 NameUnicodeTransliterator::NameUnicodeTransliterator(const NameUnicodeTransliterator
& o
) :
88 Transliterator(o
), legal(o
.legal
) {}
91 * Assignment operator.
93 /*NameUnicodeTransliterator& NameUnicodeTransliterator::operator=(
94 const NameUnicodeTransliterator& o) {
95 Transliterator::operator=(o);
96 // not necessary: the legal sets should all be the same -- legal=o.legal;
101 * Transliterator API.
103 Transliterator
* NameUnicodeTransliterator::clone(void) const {
104 return new NameUnicodeTransliterator(*this);
108 * Implements {@link Transliterator#handleTransliterate}.
110 void NameUnicodeTransliterator::handleTransliterate(Replaceable
& text
, UTransPosition
& offsets
,
111 UBool isIncremental
) const {
112 // The failure mode, here and below, is to behave like Any-Null,
113 // if either there is no name data (max len == 0) or there is no
114 // memory (malloc() => NULL).
116 int32_t maxLen
= uprv_getMaxCharNameLength();
118 offsets
.start
= offsets
.limit
;
122 // Accomodate the longest possible name
123 ++maxLen
; // allow for temporary trailing space
124 char* cbuf
= (char*) uprv_malloc(maxLen
);
126 offsets
.start
= offsets
.limit
;
130 UnicodeString
openPat(TRUE
, OPEN
, -1);
131 UnicodeString str
, name
;
133 int32_t cursor
= offsets
.start
;
134 int32_t limit
= offsets
.limit
;
137 // 0 - looking for open delimiter
138 // 1 - after open delimiter
140 int32_t openPos
= -1; // open delim candidate pos
143 while (cursor
< limit
) {
144 c
= text
.char32At(cursor
);
147 case 0: // looking for open delimiter
148 if (c
== OPEN_DELIM
) { // quick check first
151 ICU_Utility::parsePattern(openPat
, text
, cursor
, limit
);
152 if (i
>= 0 && i
< limit
) {
156 continue; // *** reprocess char32At(cursor)
161 case 1: // after open delimiter
162 // Look for legal chars. If \s+ is found, convert it
163 // to a single space. If closeDelimiter is found, exit
164 // the loop. If any other character is found, exit the
165 // loop. If the limit is reached, exit the loop.
167 // Convert \s+ => SPACE. This assumes there are no
168 // runs of >1 space characters in names.
169 if (PatternProps::isWhiteSpace(c
)) {
170 // Ignore leading whitespace
171 if (name
.length() > 0 &&
172 name
.charAt(name
.length()-1) != SPACE
) {
174 // If we are too long then abort. maxLen includes
175 // temporary trailing space, so use '>'.
176 if (name
.length() > maxLen
) {
183 if (c
== CLOSE_DELIM
) {
184 int32_t len
= name
.length();
186 // Delete trailing space, if any
188 name
.charAt(len
-1) == SPACE
) {
192 if (uprv_isInvariantUString(name
.getBuffer(), len
)) {
193 name
.extract(0, len
, cbuf
, maxLen
, US_INV
);
195 UErrorCode status
= U_ZERO_ERROR
;
196 c
= u_charFromName(U_EXTENDED_CHAR_NAME
, cbuf
, &status
);
197 if (U_SUCCESS(status
)) {
200 // assert(U16_LENGTH(CLOSE_DELIM) == 1);
201 cursor
++; // advance over CLOSE_DELIM
205 text
.handleReplaceBetween(openPos
, cursor
, str
);
207 // Adjust indices for the change in the length of
208 // the string. Do not assume that str.length() ==
209 // 1, in case of surrogates.
210 int32_t delta
= cursor
- openPos
- str
.length();
213 // assert(cursor == openPos + str.length());
216 // If the lookup failed, we leave things as-is and
217 // still switch to mode 0 and continue.
219 openPos
= -1; // close off candidate
220 continue; // *** reprocess char32At(cursor)
223 // Check if c is a legal char. We assume here that
224 // legal.contains(OPEN_DELIM) is FALSE, so when we abort a
225 // name, we don't have to go back to openPos+1.
226 if (legal
.contains(c
)) {
228 // If we go past the longest possible name then abort.
229 // maxLen includes temporary trailing space, so use '>='.
230 if (name
.length() >= maxLen
) {
237 --cursor
; // Backup and reprocess this character
244 cursor
+= U16_LENGTH(c
);
247 offsets
.contextLimit
+= limit
- offsets
.limit
;
248 offsets
.limit
= limit
;
249 // In incremental mode, only advance the cursor up to the last
250 // open delimiter candidate.
251 offsets
.start
= (isIncremental
&& openPos
>= 0) ? openPos
: cursor
;
258 #endif /* #if !UCONFIG_NO_TRANSLITERATION */