2001-06-11 23:38:54 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
|
|
|
* Copyright (C) 2001, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
**********************************************************************
|
|
|
|
* Date Name Description
|
|
|
|
* 06/06/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-11-30 00:06:13 +00:00
|
|
|
#include "unicode/uchar.h"
|
2002-07-12 21:42:24 +00:00
|
|
|
#include "uni2name.h"
|
|
|
|
#include "cstring.h"
|
2002-09-06 23:30:29 +00:00
|
|
|
#include "cmemory.h"
|
|
|
|
#include "uprops.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 UnicodeNameTransliterator::fgClassID=0;
|
|
|
|
|
2001-10-11 23:54:55 +00:00
|
|
|
const char UnicodeNameTransliterator::_ID[] = "Any-Name";
|
2001-06-11 23:38:54 +00:00
|
|
|
|
2002-09-06 23:30:29 +00:00
|
|
|
static const UChar OPEN_DELIM[] = {92,78,123,0}; // "\N{"
|
|
|
|
static const UChar CLOSE_DELIM = 125; // "}"
|
|
|
|
#define OPEN_DELIM_LEN 3
|
2001-06-11 23:38:54 +00:00
|
|
|
|
|
|
|
/**
|
2002-09-06 23:30:29 +00:00
|
|
|
* Constructs a transliterator.
|
2001-06-11 23:38:54 +00:00
|
|
|
*/
|
|
|
|
UnicodeNameTransliterator::UnicodeNameTransliterator(UnicodeFilter* adoptedFilter) :
|
2002-09-06 23:30:29 +00:00
|
|
|
Transliterator(_ID, adoptedFilter) {
|
2001-06-11 23:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*/
|
|
|
|
UnicodeNameTransliterator::~UnicodeNameTransliterator() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy constructor.
|
|
|
|
*/
|
|
|
|
UnicodeNameTransliterator::UnicodeNameTransliterator(const UnicodeNameTransliterator& o) :
|
2002-09-06 23:30:29 +00:00
|
|
|
Transliterator(o) {}
|
2001-06-11 23:38:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Assignment operator.
|
|
|
|
*/
|
|
|
|
UnicodeNameTransliterator& UnicodeNameTransliterator::operator=(
|
|
|
|
const UnicodeNameTransliterator& o) {
|
|
|
|
Transliterator::operator=(o);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transliterator API.
|
|
|
|
*/
|
|
|
|
Transliterator* UnicodeNameTransliterator::clone(void) const {
|
|
|
|
return new UnicodeNameTransliterator(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements {@link Transliterator#handleTransliterate}.
|
2001-11-20 18:59:58 +00:00
|
|
|
* Ignore isIncremental since we don't need the context, and
|
|
|
|
* we work on codepoints.
|
2001-06-11 23:38:54 +00:00
|
|
|
*/
|
|
|
|
void UnicodeNameTransliterator::handleTransliterate(Replaceable& text, UTransPosition& offsets,
|
2001-11-20 18:59:58 +00:00
|
|
|
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;
|
|
|
|
}
|
2002-02-06 02:05:49 +00:00
|
|
|
|
2002-09-06 23:30:29 +00:00
|
|
|
// Accomodate the longest possible name plus padding
|
|
|
|
char* buf = (char*) uprv_malloc(maxLen);
|
|
|
|
if (buf == NULL) {
|
|
|
|
offsets.start = offsets.limit;
|
|
|
|
return;
|
|
|
|
}
|
2001-06-11 23:38:54 +00:00
|
|
|
|
|
|
|
int32_t cursor = offsets.start;
|
|
|
|
int32_t limit = offsets.limit;
|
|
|
|
|
2002-09-06 23:30:29 +00:00
|
|
|
UnicodeString str(FALSE, OPEN_DELIM, OPEN_DELIM_LEN);
|
2001-06-11 23:38:54 +00:00
|
|
|
UErrorCode status;
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t len;
|
2001-06-11 23:38:54 +00:00
|
|
|
|
|
|
|
while (cursor < limit) {
|
2001-12-03 21:43:13 +00:00
|
|
|
UChar32 c = text.char32At(cursor);
|
2002-01-31 17:49:00 +00:00
|
|
|
int32_t clen = UTF_CHAR_LENGTH(c);
|
2002-02-14 05:45:39 +00:00
|
|
|
status = U_ZERO_ERROR;
|
2002-09-06 23:30:29 +00:00
|
|
|
if ((len = u_charName(c, U_EXTENDED_CHAR_NAME, buf, maxLen, &status)) >0 && !U_FAILURE(status)) {
|
|
|
|
str.truncate(OPEN_DELIM_LEN);
|
|
|
|
str.append(UnicodeString(buf, len, "")).append(CLOSE_DELIM);
|
2002-02-14 05:45:39 +00:00
|
|
|
text.handleReplaceBetween(cursor, cursor+clen, str);
|
2002-09-06 23:30:29 +00:00
|
|
|
len += OPEN_DELIM_LEN + 1; // adjust for delimiters
|
2002-02-14 05:45:39 +00:00
|
|
|
cursor += len; // advance cursor and adjust for new text
|
|
|
|
limit += len-clen; // change in length
|
|
|
|
} else {
|
|
|
|
cursor += clen;
|
|
|
|
}
|
2001-06-11 23:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
offsets.contextLimit += limit - offsets.limit;
|
|
|
|
offsets.limit = limit;
|
|
|
|
offsets.start = cursor;
|
2002-09-06 23:30:29 +00:00
|
|
|
|
|
|
|
uprv_free(buf);
|
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 */
|