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.
|
|
|
|
**********************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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"
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a transliterator.
|
|
|
|
*/
|
|
|
|
UnicodeNameTransliterator::UnicodeNameTransliterator(
|
|
|
|
UChar32 openDelim, UChar32 closeDelim,
|
|
|
|
UnicodeFilter* adoptedFilter) :
|
|
|
|
Transliterator(_ID, adoptedFilter),
|
|
|
|
openDelimiter(openDelim),
|
|
|
|
closeDelimiter(closeDelim) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a transliterator with the default delimiters '{' and
|
|
|
|
* '}'.
|
|
|
|
*/
|
|
|
|
UnicodeNameTransliterator::UnicodeNameTransliterator(UnicodeFilter* adoptedFilter) :
|
|
|
|
Transliterator(_ID, adoptedFilter),
|
|
|
|
openDelimiter((UChar) 0x007B /*{*/),
|
|
|
|
closeDelimiter((UChar) 0x007D /*}*/) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*/
|
|
|
|
UnicodeNameTransliterator::~UnicodeNameTransliterator() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy constructor.
|
|
|
|
*/
|
|
|
|
UnicodeNameTransliterator::UnicodeNameTransliterator(const UnicodeNameTransliterator& o) :
|
|
|
|
Transliterator(o),
|
|
|
|
openDelimiter(o.openDelimiter),
|
|
|
|
closeDelimiter(o.closeDelimiter) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assignment operator.
|
|
|
|
*/
|
|
|
|
UnicodeNameTransliterator& UnicodeNameTransliterator::operator=(
|
|
|
|
const UnicodeNameTransliterator& o) {
|
|
|
|
Transliterator::operator=(o);
|
|
|
|
openDelimiter = o.openDelimiter;
|
|
|
|
closeDelimiter = o.closeDelimiter;
|
|
|
|
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 {
|
2001-06-11 23:38:54 +00:00
|
|
|
// As of Unicode 3.0.0, the longest name is 83 characters long.
|
|
|
|
// Adjust this buffer size as needed.
|
2002-02-06 02:05:49 +00:00
|
|
|
|
2001-06-11 23:38:54 +00:00
|
|
|
char buf[128];
|
|
|
|
|
|
|
|
int32_t cursor = offsets.start;
|
|
|
|
int32_t limit = offsets.limit;
|
|
|
|
|
|
|
|
UnicodeString str(openDelimiter);
|
|
|
|
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;
|
|
|
|
if ((len = u_charName(c, U_EXTENDED_CHAR_NAME, buf, sizeof(buf), &status)) >0 && !U_FAILURE(status)) {
|
|
|
|
str.truncate(1);
|
|
|
|
str.append(UnicodeString(buf, len, "")).append(closeDelimiter);
|
|
|
|
text.handleReplaceBetween(cursor, cursor+clen, str);
|
|
|
|
len += 2; // adjust for delimiters
|
|
|
|
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;
|
|
|
|
}
|
2001-10-08 23:26:58 +00:00
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|