2001-06-11 23:38:54 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
2006-03-25 23:53:31 +00:00
|
|
|
* Copyright (C) 2001-2006, International Business Machines
|
2001-06-11 23:38:54 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
**********************************************************************
|
|
|
|
* Date Name Description
|
|
|
|
* 06/07/01 aliu Creation.
|
|
|
|
**********************************************************************
|
|
|
|
*/
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_TRANSLITERATION
|
|
|
|
|
2001-06-11 23:38:54 +00:00
|
|
|
#include "unicode/unifilt.h"
|
2001-10-04 16:36:32 +00:00
|
|
|
#include "unicode/uchar.h"
|
2002-09-06 23:30:29 +00:00
|
|
|
#include "unicode/uniset.h"
|
2002-07-12 21:42:24 +00:00
|
|
|
#include "name2uni.h"
|
2002-09-06 23:30:29 +00:00
|
|
|
#include "cmemory.h"
|
2002-07-26 22:07:49 +00:00
|
|
|
#include "uprops.h"
|
2005-01-15 04:46:14 +00:00
|
|
|
#include "uinvchar.h"
|
2002-09-06 23:30:29 +00:00
|
|
|
#include "util.h"
|
2001-06-11 23:38:54 +00:00
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2003-08-31 20:53:46 +00:00
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(NameUnicodeTransliterator)
|
2002-06-29 00:04:16 +00:00
|
|
|
|
2002-09-06 23:30:29 +00:00
|
|
|
static const UChar OPEN[] = {92,78,126,123,126,0}; // "\N~{~"
|
|
|
|
static const UChar OPEN_DELIM = 92; // '\\' first char of OPEN
|
|
|
|
static const UChar CLOSE_DELIM = 125; // '}'
|
|
|
|
static const UChar SPACE = 32; // ' '
|
2001-06-11 23:38:54 +00:00
|
|
|
|
2004-09-08 14:14:35 +00:00
|
|
|
U_CDECL_BEGIN
|
|
|
|
|
2004-09-07 18:05:01 +00:00
|
|
|
// USetAdder implementation
|
|
|
|
// Does not use uset.h to reduce code dependencies
|
|
|
|
static void U_CALLCONV
|
|
|
|
_set_add(USet *set, UChar32 c) {
|
|
|
|
((UnicodeSet *)set)->add(c);
|
|
|
|
}
|
|
|
|
|
2006-09-22 18:38:46 +00:00
|
|
|
// These functions aren't used.
|
|
|
|
/*static void U_CALLCONV
|
2004-09-07 18:05:01 +00:00
|
|
|
_set_addRange(USet *set, UChar32 start, UChar32 end) {
|
|
|
|
((UnicodeSet *)set)->add(start, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void U_CALLCONV
|
|
|
|
_set_addString(USet *set, const UChar *str, int32_t length) {
|
|
|
|
((UnicodeSet *)set)->add(UnicodeString((UBool)(length<0), str, length));
|
2006-09-22 18:38:46 +00:00
|
|
|
}*/
|
2004-09-07 18:05:01 +00:00
|
|
|
|
2004-09-08 14:14:35 +00:00
|
|
|
U_CDECL_END
|
|
|
|
|
2001-06-11 23:38:54 +00:00
|
|
|
/**
|
|
|
|
* Constructs a transliterator with the default delimiters '{' and
|
|
|
|
* '}'.
|
|
|
|
*/
|
|
|
|
NameUnicodeTransliterator::NameUnicodeTransliterator(UnicodeFilter* adoptedFilter) :
|
2004-09-07 18:05:01 +00:00
|
|
|
Transliterator(UNICODE_STRING("Name-Any", 8), adoptedFilter) {
|
|
|
|
|
2006-03-25 23:53:31 +00:00
|
|
|
UnicodeSet *legalPtr = &legal;
|
2004-09-07 18:05:01 +00:00
|
|
|
// Get the legal character set
|
|
|
|
USetAdder sa = {
|
2006-03-25 23:53:31 +00:00
|
|
|
(USet *)legalPtr, // USet* == UnicodeSet*
|
2004-09-07 18:05:01 +00:00
|
|
|
_set_add,
|
2006-09-22 18:38:46 +00:00
|
|
|
NULL, // Don't need _set_addRange
|
|
|
|
NULL, // Don't need _set_addString
|
|
|
|
NULL // Don't need remove()
|
2004-09-07 18:05:01 +00:00
|
|
|
};
|
|
|
|
uprv_getCharNameCharacters(&sa);
|
2001-06-11 23:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*/
|
|
|
|
NameUnicodeTransliterator::~NameUnicodeTransliterator() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy constructor.
|
|
|
|
*/
|
|
|
|
NameUnicodeTransliterator::NameUnicodeTransliterator(const NameUnicodeTransliterator& o) :
|
2004-09-07 18:05:01 +00:00
|
|
|
Transliterator(o), legal(o.legal) {}
|
2001-06-11 23:38:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Assignment operator.
|
|
|
|
*/
|
|
|
|
NameUnicodeTransliterator& NameUnicodeTransliterator::operator=(
|
|
|
|
const NameUnicodeTransliterator& o) {
|
|
|
|
Transliterator::operator=(o);
|
2004-09-07 18:05:01 +00:00
|
|
|
// not necessary: the legal sets should all be the same -- legal=o.legal;
|
2001-06-11 23:38:54 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transliterator API.
|
|
|
|
*/
|
|
|
|
Transliterator* NameUnicodeTransliterator::clone(void) const {
|
|
|
|
return new NameUnicodeTransliterator(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements {@link Transliterator#handleTransliterate}.
|
|
|
|
*/
|
|
|
|
void NameUnicodeTransliterator::handleTransliterate(Replaceable& text, UTransPosition& offsets,
|
|
|
|
UBool isIncremental) const {
|
2002-09-06 23:30:29 +00:00
|
|
|
// The failure mode, here and below, is to behave like Any-Null,
|
|
|
|
// if either there is no name data (max len == 0) or there is no
|
|
|
|
// memory (malloc() => NULL).
|
|
|
|
|
|
|
|
int32_t maxLen = uprv_getMaxCharNameLength();
|
|
|
|
if (maxLen == 0) {
|
|
|
|
offsets.start = offsets.limit;
|
|
|
|
return;
|
|
|
|
}
|
2001-06-11 23:38:54 +00:00
|
|
|
|
2002-09-06 23:30:29 +00:00
|
|
|
// Accomodate the longest possible name
|
|
|
|
++maxLen; // allow for temporary trailing space
|
|
|
|
char* cbuf = (char*) uprv_malloc(maxLen);
|
|
|
|
if (cbuf == NULL) {
|
|
|
|
offsets.start = offsets.limit;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString openPat(TRUE, OPEN, -1);
|
|
|
|
UnicodeString str, name;
|
|
|
|
|
2001-06-11 23:38:54 +00:00
|
|
|
int32_t cursor = offsets.start;
|
|
|
|
int32_t limit = offsets.limit;
|
|
|
|
|
|
|
|
// Modes:
|
|
|
|
// 0 - looking for open delimiter
|
|
|
|
// 1 - after open delimiter
|
|
|
|
int32_t mode = 0;
|
2002-09-06 23:30:29 +00:00
|
|
|
int32_t openPos = -1; // open delim candidate pos
|
2001-06-11 23:38:54 +00:00
|
|
|
|
2001-12-03 21:43:13 +00:00
|
|
|
UChar32 c;
|
2002-09-06 23:30:29 +00:00
|
|
|
while (cursor < limit) {
|
2001-12-03 21:43:13 +00:00
|
|
|
c = text.char32At(cursor);
|
2001-06-11 23:38:54 +00:00
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
case 0: // looking for open delimiter
|
2002-09-06 23:30:29 +00:00
|
|
|
if (c == OPEN_DELIM) { // quick check first
|
2001-06-11 23:38:54 +00:00
|
|
|
openPos = cursor;
|
2002-09-06 23:30:29 +00:00
|
|
|
int32_t i =
|
|
|
|
ICU_Utility::parsePattern(openPat, text, cursor, limit);
|
|
|
|
if (i >= 0 && i < limit) {
|
|
|
|
mode = 1;
|
|
|
|
name.truncate(0);
|
|
|
|
cursor = i;
|
|
|
|
continue; // *** reprocess char32At(cursor)
|
|
|
|
}
|
2001-06-11 23:38:54 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: // after open delimiter
|
2002-09-06 23:30:29 +00:00
|
|
|
// Look for legal chars. If \s+ is found, convert it
|
2001-06-11 23:38:54 +00:00
|
|
|
// to a single space. If closeDelimiter is found, exit
|
|
|
|
// the loop. If any other character is found, exit the
|
2002-09-06 23:30:29 +00:00
|
|
|
// loop. If the limit is reached, exit the loop.
|
|
|
|
|
|
|
|
// Convert \s+ => SPACE. This assumes there are no
|
|
|
|
// runs of >1 space characters in names.
|
2002-07-26 22:07:49 +00:00
|
|
|
if (uprv_isRuleWhiteSpace(c)) {
|
2001-06-11 23:38:54 +00:00
|
|
|
// Ignore leading whitespace
|
2002-09-06 23:30:29 +00:00
|
|
|
if (name.length() > 0 &&
|
|
|
|
name.charAt(name.length()-1) != SPACE) {
|
|
|
|
name.append(SPACE);
|
|
|
|
// If we are too long then abort. maxLen includes
|
|
|
|
// temporary trailing space, so use '>'.
|
|
|
|
if (name.length() > maxLen) {
|
2001-06-11 23:38:54 +00:00
|
|
|
mode = 0;
|
|
|
|
}
|
|
|
|
}
|
2002-09-06 23:30:29 +00:00
|
|
|
break;
|
2001-06-11 23:38:54 +00:00
|
|
|
}
|
|
|
|
|
2002-09-06 23:30:29 +00:00
|
|
|
if (c == CLOSE_DELIM) {
|
|
|
|
int32_t len = name.length();
|
|
|
|
|
2001-06-11 23:38:54 +00:00
|
|
|
// Delete trailing space, if any
|
2002-09-06 23:30:29 +00:00
|
|
|
if (len > 0 &&
|
|
|
|
name.charAt(len-1) == SPACE) {
|
|
|
|
--len;
|
2001-06-11 23:38:54 +00:00
|
|
|
}
|
2001-07-09 23:26:09 +00:00
|
|
|
|
2005-01-15 04:46:14 +00:00
|
|
|
if (uprv_isInvariantUString(name.getBuffer(), len)) {
|
|
|
|
name.extract(0, len, cbuf, maxLen, US_INV);
|
2002-02-14 05:45:39 +00:00
|
|
|
|
2005-01-15 04:46:14 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
c = u_charFromName(U_EXTENDED_CHAR_NAME, cbuf, &status);
|
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
// Lookup succeeded
|
2002-09-06 23:30:29 +00:00
|
|
|
|
2005-01-15 04:46:14 +00:00
|
|
|
// assert(UTF_CHAR_LENGTH(CLOSE_DELIM) == 1);
|
|
|
|
cursor++; // advance over CLOSE_DELIM
|
2002-09-06 23:30:29 +00:00
|
|
|
|
2005-01-15 04:46:14 +00:00
|
|
|
str.truncate(0);
|
|
|
|
str.append(c);
|
|
|
|
text.handleReplaceBetween(openPos, cursor, str);
|
2001-06-11 23:38:54 +00:00
|
|
|
|
2005-01-15 04:46:14 +00:00
|
|
|
// Adjust indices for the change in the length of
|
|
|
|
// the string. Do not assume that str.length() ==
|
|
|
|
// 1, in case of surrogates.
|
|
|
|
int32_t delta = cursor - openPos - str.length();
|
|
|
|
cursor -= delta;
|
|
|
|
limit -= delta;
|
|
|
|
// assert(cursor == openPos + str.length());
|
|
|
|
}
|
2001-06-11 23:38:54 +00:00
|
|
|
}
|
|
|
|
// If the lookup failed, we leave things as-is and
|
|
|
|
// still switch to mode 0 and continue.
|
|
|
|
mode = 0;
|
2002-09-06 23:30:29 +00:00
|
|
|
openPos = -1; // close off candidate
|
|
|
|
continue; // *** reprocess char32At(cursor)
|
2001-06-11 23:38:54 +00:00
|
|
|
}
|
|
|
|
|
2002-09-06 23:30:29 +00:00
|
|
|
// Check if c is a legal char. We assume here that
|
|
|
|
// legal.contains(OPEN_DELIM) is FALSE, so when we abort a
|
|
|
|
// name, we don't have to go back to openPos+1.
|
|
|
|
if (legal.contains(c)) {
|
|
|
|
name.append(c);
|
|
|
|
// If we go past the longest possible name then abort.
|
|
|
|
// maxLen includes temporary trailing space, so use '>='.
|
|
|
|
if (name.length() >= maxLen) {
|
2001-06-11 23:38:54 +00:00
|
|
|
mode = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invalid character
|
|
|
|
else {
|
|
|
|
--cursor; // Backup and reprocess this character
|
|
|
|
mode = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2002-09-06 23:30:29 +00:00
|
|
|
|
|
|
|
cursor += UTF_CHAR_LENGTH(c);
|
2001-06-11 23:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
offsets.contextLimit += limit - offsets.limit;
|
|
|
|
offsets.limit = limit;
|
|
|
|
// In incremental mode, only advance the cursor up to the last
|
2002-09-06 23:30:29 +00:00
|
|
|
// open delimiter candidate.
|
|
|
|
offsets.start = (isIncremental && openPos >= 0) ? openPos : cursor;
|
|
|
|
|
|
|
|
uprv_free(cbuf);
|
2001-06-11 23:38:54 +00:00
|
|
|
}
|
2001-10-08 23:26:58 +00:00
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#endif /* #if !UCONFIG_NO_TRANSLITERATION */
|