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.
|
|
|
|
**********************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/unifilt.h"
|
2001-10-04 16:36:32 +00:00
|
|
|
#include "unicode/uchar.h"
|
2002-07-12 21:42:24 +00:00
|
|
|
#include "name2uni.h"
|
2002-07-26 22:07:49 +00:00
|
|
|
#include "uprops.h"
|
2001-06-11 23:38:54 +00:00
|
|
|
|
|
|
|
// As of Unicode 3.0.0, the longest name is 83 characters long.
|
|
|
|
#define LONGEST_NAME 83
|
|
|
|
|
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";
|
|
|
|
|
2001-06-11 23:38:54 +00:00
|
|
|
/**
|
|
|
|
* Constructs a transliterator.
|
|
|
|
*/
|
|
|
|
NameUnicodeTransliterator::NameUnicodeTransliterator(
|
|
|
|
UChar32 openDelim, UChar32 closeDelim,
|
|
|
|
UnicodeFilter* adoptedFilter) :
|
|
|
|
Transliterator(_ID, adoptedFilter),
|
|
|
|
openDelimiter(openDelim),
|
|
|
|
closeDelimiter(closeDelim) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a transliterator with the default delimiters '{' and
|
|
|
|
* '}'.
|
|
|
|
*/
|
|
|
|
NameUnicodeTransliterator::NameUnicodeTransliterator(UnicodeFilter* adoptedFilter) :
|
|
|
|
Transliterator(_ID, adoptedFilter),
|
|
|
|
openDelimiter((UChar) 0x007B /*{*/),
|
|
|
|
closeDelimiter((UChar) 0x007D /*}*/) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*/
|
|
|
|
NameUnicodeTransliterator::~NameUnicodeTransliterator() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy constructor.
|
|
|
|
*/
|
|
|
|
NameUnicodeTransliterator::NameUnicodeTransliterator(const NameUnicodeTransliterator& o) :
|
|
|
|
Transliterator(o),
|
|
|
|
openDelimiter(o.openDelimiter),
|
|
|
|
closeDelimiter(o.closeDelimiter) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assignment operator.
|
|
|
|
*/
|
|
|
|
NameUnicodeTransliterator& NameUnicodeTransliterator::operator=(
|
|
|
|
const NameUnicodeTransliterator& o) {
|
|
|
|
Transliterator::operator=(o);
|
|
|
|
openDelimiter = o.openDelimiter;
|
|
|
|
closeDelimiter = o.closeDelimiter;
|
|
|
|
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 {
|
|
|
|
// Accomodate the longest possible name plus padding
|
2001-07-09 23:26:09 +00:00
|
|
|
UChar buf[LONGEST_NAME + 8];
|
|
|
|
char cbuf[LONGEST_NAME + 8]; // Default converter
|
2001-06-11 23:38:54 +00:00
|
|
|
|
|
|
|
// The only characters used in names are (as of Unicode 3.0.0):
|
|
|
|
// -0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
|
|
|
|
// (first character is a space).
|
|
|
|
|
|
|
|
int32_t cursor = offsets.start;
|
|
|
|
int32_t limit = offsets.limit;
|
|
|
|
|
|
|
|
// Modes:
|
|
|
|
// 0 - looking for open delimiter
|
|
|
|
// 1 - after open delimiter
|
|
|
|
int32_t mode = 0;
|
|
|
|
int32_t ibuf = 0;
|
|
|
|
int32_t openPos = offsets.start; // position of openDelimiter
|
|
|
|
|
|
|
|
UnicodeString str;
|
|
|
|
|
2001-12-03 21:43:13 +00:00
|
|
|
UChar32 c;
|
|
|
|
for (; cursor < limit; cursor+=UTF_CHAR_LENGTH(c)) {
|
|
|
|
c = text.char32At(cursor);
|
2001-06-11 23:38:54 +00:00
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
case 0: // looking for open delimiter
|
|
|
|
if (c == openDelimiter) {
|
|
|
|
openPos = cursor;
|
|
|
|
mode = 1;
|
|
|
|
ibuf = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: // after open delimiter
|
2002-03-27 19:13:22 +00:00
|
|
|
// Look for [-a-zA-Z0-9<>]. 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
|
|
|
|
// loop. If the limit is found, exit the loop.
|
2002-07-26 22:07:49 +00:00
|
|
|
if (uprv_isRuleWhiteSpace(c)) {
|
2001-06-11 23:38:54 +00:00
|
|
|
// Ignore leading whitespace
|
|
|
|
if (ibuf != 0 && buf[ibuf-1] != (UChar)0x0020) {
|
|
|
|
buf[ibuf++] = (UChar)0x0020 /* */;
|
|
|
|
// If we go a bit past the longest possible name then abort
|
|
|
|
if (ibuf == (LONGEST_NAME + 4)) {
|
|
|
|
mode = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c == closeDelimiter) {
|
|
|
|
// Delete trailing space, if any
|
|
|
|
if (ibuf > 0 && buf[ibuf-1] == (UChar)0x0020) {
|
|
|
|
--ibuf;
|
|
|
|
}
|
|
|
|
buf[ibuf] = 0; // Add terminating zero
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2001-07-09 23:26:09 +00:00
|
|
|
|
2002-02-14 05:45:39 +00:00
|
|
|
UChar32 ch;
|
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
u_UCharsToChars(buf, cbuf, ibuf+1);
|
|
|
|
ch = 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
|
|
|
|
str.truncate(0);
|
|
|
|
str.append(ch);
|
|
|
|
text.handleReplaceBetween(openPos, cursor+1, str);
|
|
|
|
|
|
|
|
// 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 + 1 - openPos - str.length();
|
|
|
|
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;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2002-02-14 05:45:39 +00:00
|
|
|
// Check if c =~ [-A-Za-z0-9<> ]
|
2001-06-11 23:38:54 +00:00
|
|
|
if (c == (UChar)0x002D ||
|
|
|
|
(c >= (UChar)0x0041 && c <= (UChar)0x005A) ||
|
2002-02-14 05:45:39 +00:00
|
|
|
(c >= (UChar)0x0061 && c <= (UChar)0x007A) ||
|
2002-02-07 07:14:42 +00:00
|
|
|
(c >= (UChar)0x0030 && c <= (UChar)0x0039) ||
|
2002-02-14 20:39:27 +00:00
|
|
|
c == (UChar)0x003C || c == (UChar)0x003E) {
|
2001-06-11 23:38:54 +00:00
|
|
|
buf[ibuf++] = (char) c;
|
|
|
|
// If we go a bit past the longest possible name then abort
|
|
|
|
if (ibuf == (LONGEST_NAME + 4)) {
|
|
|
|
mode = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invalid character
|
|
|
|
else {
|
|
|
|
--cursor; // Backup and reprocess this character
|
|
|
|
mode = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
offsets.contextLimit += limit - offsets.limit;
|
|
|
|
offsets.limit = limit;
|
|
|
|
// In incremental mode, only advance the cursor up to the last
|
|
|
|
// open delimiter, if we are in mode 1.
|
|
|
|
offsets.start = (mode == 1 && isIncremental) ? openPos : cursor;
|
|
|
|
}
|
2001-10-08 23:26:58 +00:00
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|