2001-06-11 23:38:54 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
|
|
|
* Copyright (C) 2001, International Business Machines
|
|
|
|
* 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"
|
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
|
|
|
|
|
2002-06-29 00:04:16 +00:00
|
|
|
const char NameUnicodeTransliterator::fgClassID=0;
|
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
const char NameUnicodeTransliterator::_ID[] = "Name-Any";
|
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a transliterator with the default delimiters '{' and
|
|
|
|
* '}'.
|
|
|
|
*/
|
|
|
|
NameUnicodeTransliterator::NameUnicodeTransliterator(UnicodeFilter* adoptedFilter) :
|
2002-09-06 23:30:29 +00:00
|
|
|
Transliterator(_ID, adoptedFilter) {
|
2001-06-11 23:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*/
|
|
|
|
NameUnicodeTransliterator::~NameUnicodeTransliterator() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy constructor.
|
|
|
|
*/
|
|
|
|
NameUnicodeTransliterator::NameUnicodeTransliterator(const NameUnicodeTransliterator& o) :
|
2002-09-06 23:30:29 +00:00
|
|
|
Transliterator(o) {}
|
2001-06-11 23:38:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Assignment operator.
|
|
|
|
*/
|
|
|
|
NameUnicodeTransliterator& NameUnicodeTransliterator::operator=(
|
|
|
|
const NameUnicodeTransliterator& o) {
|
|
|
|
Transliterator::operator=(o);
|
|
|
|
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;
|
|
|
|
|
|
|
|
// Get the legal character set
|
|
|
|
UnicodeSet legal;
|
|
|
|
uprv_getCharNameCharacters((USet*) &legal); // USet* == UnicodeSet*
|
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
|
|
|
|
2002-09-06 23:30:29 +00:00
|
|
|
name.extract(0, len, cbuf, "");
|
2002-02-14 05:45:39 +00:00
|
|
|
|
2002-09-06 23:30:29 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
c = u_charFromName(U_EXTENDED_CHAR_NAME, cbuf, &status);
|
2002-02-14 05:45:39 +00:00
|
|
|
if (U_SUCCESS(status)) {
|
2001-06-11 23:38:54 +00:00
|
|
|
// Lookup succeeded
|
2002-09-06 23:30:29 +00:00
|
|
|
|
|
|
|
// assert(UTF_CHAR_LENGTH(CLOSE_DELIM) == 1);
|
|
|
|
cursor++; // advance over CLOSE_DELIM
|
|
|
|
|
2001-06-11 23:38:54 +00:00
|
|
|
str.truncate(0);
|
2002-09-06 23:30:29 +00:00
|
|
|
str.append(c);
|
|
|
|
text.handleReplaceBetween(openPos, cursor, str);
|
2001-06-11 23:38:54 +00:00
|
|
|
|
|
|
|
// Adjust indices for the change in the length of
|
|
|
|
// the string. Do not assume that str.length() ==
|
|
|
|
// 1, in case of surrogates.
|
2002-09-06 23:30:29 +00:00
|
|
|
int32_t delta = cursor - openPos - str.length();
|
2001-06-11 23:38:54 +00:00
|
|
|
cursor -= delta;
|
|
|
|
limit -= delta;
|
|
|
|
// assert(cursor == openPos + str.length());
|
|
|
|
}
|
|
|
|
// 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 */
|