]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/name2uni.cpp
ICU-62141.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / name2uni.cpp
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
b75a7d8f
A
3/*
4**********************************************************************
4388f060 5* Copyright (C) 2001-2011, International Business Machines
b75a7d8f
A
6* Corporation and others. All Rights Reserved.
7**********************************************************************
8* Date Name Description
9* 06/07/01 aliu Creation.
10**********************************************************************
11*/
12
13#include "unicode/utypes.h"
14
15#if !UCONFIG_NO_TRANSLITERATION
16
17#include "unicode/unifilt.h"
18#include "unicode/uchar.h"
19#include "unicode/uniset.h"
4388f060 20#include "unicode/utf16.h"
b75a7d8f 21#include "cmemory.h"
4388f060
A
22#include "name2uni.h"
23#include "patternprops.h"
b75a7d8f 24#include "uprops.h"
73c04bcf 25#include "uinvchar.h"
b75a7d8f
A
26#include "util.h"
27
28U_NAMESPACE_BEGIN
29
374ca955 30UOBJECT_DEFINE_RTTI_IMPLEMENTATION(NameUnicodeTransliterator)
b75a7d8f
A
31
32static const UChar OPEN[] = {92,78,126,123,126,0}; // "\N~{~"
33static const UChar OPEN_DELIM = 92; // '\\' first char of OPEN
34static const UChar CLOSE_DELIM = 125; // '}'
35static const UChar SPACE = 32; // ' '
36
374ca955
A
37U_CDECL_BEGIN
38
39// USetAdder implementation
40// Does not use uset.h to reduce code dependencies
41static void U_CALLCONV
42_set_add(USet *set, UChar32 c) {
46f4442e 43 uset_add(set, c);
374ca955
A
44}
45
46f4442e
A
46// These functions aren't used.
47/*static void U_CALLCONV
374ca955
A
48_set_addRange(USet *set, UChar32 start, UChar32 end) {
49 ((UnicodeSet *)set)->add(start, end);
50}
51
52static void U_CALLCONV
53_set_addString(USet *set, const UChar *str, int32_t length) {
54 ((UnicodeSet *)set)->add(UnicodeString((UBool)(length<0), str, length));
46f4442e 55}*/
374ca955
A
56
57U_CDECL_END
58
b75a7d8f
A
59/**
60 * Constructs a transliterator with the default delimiters '{' and
61 * '}'.
62 */
63NameUnicodeTransliterator::NameUnicodeTransliterator(UnicodeFilter* adoptedFilter) :
374ca955
A
64 Transliterator(UNICODE_STRING("Name-Any", 8), adoptedFilter) {
65
73c04bcf 66 UnicodeSet *legalPtr = &legal;
374ca955
A
67 // Get the legal character set
68 USetAdder sa = {
73c04bcf 69 (USet *)legalPtr, // USet* == UnicodeSet*
374ca955 70 _set_add,
46f4442e
A
71 NULL, // Don't need _set_addRange
72 NULL, // Don't need _set_addString
73 NULL, // Don't need remove()
74 NULL
374ca955
A
75 };
76 uprv_getCharNameCharacters(&sa);
b75a7d8f
A
77}
78
79/**
80 * Destructor.
81 */
82NameUnicodeTransliterator::~NameUnicodeTransliterator() {}
83
84/**
85 * Copy constructor.
86 */
87NameUnicodeTransliterator::NameUnicodeTransliterator(const NameUnicodeTransliterator& o) :
374ca955 88 Transliterator(o), legal(o.legal) {}
b75a7d8f
A
89
90/**
91 * Assignment operator.
92 */
46f4442e 93/*NameUnicodeTransliterator& NameUnicodeTransliterator::operator=(
b75a7d8f
A
94 const NameUnicodeTransliterator& o) {
95 Transliterator::operator=(o);
374ca955 96 // not necessary: the legal sets should all be the same -- legal=o.legal;
b75a7d8f 97 return *this;
46f4442e 98}*/
b75a7d8f
A
99
100/**
101 * Transliterator API.
102 */
103Transliterator* NameUnicodeTransliterator::clone(void) const {
104 return new NameUnicodeTransliterator(*this);
105}
106
107/**
108 * Implements {@link Transliterator#handleTransliterate}.
109 */
110void 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).
115
116 int32_t maxLen = uprv_getMaxCharNameLength();
117 if (maxLen == 0) {
118 offsets.start = offsets.limit;
119 return;
120 }
121
122 // Accomodate the longest possible name
123 ++maxLen; // allow for temporary trailing space
124 char* cbuf = (char*) uprv_malloc(maxLen);
125 if (cbuf == NULL) {
126 offsets.start = offsets.limit;
127 return;
128 }
129
130 UnicodeString openPat(TRUE, OPEN, -1);
131 UnicodeString str, name;
132
b75a7d8f
A
133 int32_t cursor = offsets.start;
134 int32_t limit = offsets.limit;
135
136 // Modes:
137 // 0 - looking for open delimiter
138 // 1 - after open delimiter
139 int32_t mode = 0;
140 int32_t openPos = -1; // open delim candidate pos
141
142 UChar32 c;
143 while (cursor < limit) {
144 c = text.char32At(cursor);
145
146 switch (mode) {
147 case 0: // looking for open delimiter
148 if (c == OPEN_DELIM) { // quick check first
149 openPos = cursor;
150 int32_t i =
151 ICU_Utility::parsePattern(openPat, text, cursor, limit);
152 if (i >= 0 && i < limit) {
153 mode = 1;
154 name.truncate(0);
155 cursor = i;
156 continue; // *** reprocess char32At(cursor)
157 }
158 }
159 break;
160
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.
166
167 // Convert \s+ => SPACE. This assumes there are no
168 // runs of >1 space characters in names.
4388f060 169 if (PatternProps::isWhiteSpace(c)) {
b75a7d8f
A
170 // Ignore leading whitespace
171 if (name.length() > 0 &&
172 name.charAt(name.length()-1) != SPACE) {
173 name.append(SPACE);
174 // If we are too long then abort. maxLen includes
175 // temporary trailing space, so use '>'.
176 if (name.length() > maxLen) {
177 mode = 0;
178 }
179 }
180 break;
181 }
182
183 if (c == CLOSE_DELIM) {
b75a7d8f
A
184 int32_t len = name.length();
185
186 // Delete trailing space, if any
187 if (len > 0 &&
188 name.charAt(len-1) == SPACE) {
189 --len;
190 }
191
73c04bcf
A
192 if (uprv_isInvariantUString(name.getBuffer(), len)) {
193 name.extract(0, len, cbuf, maxLen, US_INV);
b75a7d8f 194
73c04bcf
A
195 UErrorCode status = U_ZERO_ERROR;
196 c = u_charFromName(U_EXTENDED_CHAR_NAME, cbuf, &status);
197 if (U_SUCCESS(status)) {
198 // Lookup succeeded
b75a7d8f 199
4388f060 200 // assert(U16_LENGTH(CLOSE_DELIM) == 1);
73c04bcf 201 cursor++; // advance over CLOSE_DELIM
b75a7d8f 202
73c04bcf
A
203 str.truncate(0);
204 str.append(c);
205 text.handleReplaceBetween(openPos, cursor, str);
b75a7d8f 206
73c04bcf
A
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();
211 cursor -= delta;
212 limit -= delta;
213 // assert(cursor == openPos + str.length());
214 }
b75a7d8f
A
215 }
216 // If the lookup failed, we leave things as-is and
217 // still switch to mode 0 and continue.
218 mode = 0;
219 openPos = -1; // close off candidate
220 continue; // *** reprocess char32At(cursor)
221 }
222
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)) {
227 name.append(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) {
231 mode = 0;
232 }
233 }
234
235 // Invalid character
236 else {
237 --cursor; // Backup and reprocess this character
238 mode = 0;
239 }
240
241 break;
242 }
243
4388f060 244 cursor += U16_LENGTH(c);
b75a7d8f
A
245 }
246
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;
252
253 uprv_free(cbuf);
254}
255
256U_NAMESPACE_END
257
258#endif /* #if !UCONFIG_NO_TRANSLITERATION */