2016-06-15 18:58:17 +00:00
|
|
|
// Copyright (C) 2016 and later: Unicode, Inc. and others.
|
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
2001-06-11 23:38:54 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
2016-05-31 21:45:07 +00:00
|
|
|
* Copyright (C) 2001-2011, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
2001-06-11 23:38:54 +00:00
|
|
|
**********************************************************************
|
|
|
|
* 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"
|
2011-07-27 05:53:56 +00:00
|
|
|
#include "unicode/utf16.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
|
|
|
|
|
2003-08-31 20:53:46 +00:00
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(UnicodeNameTransliterator)
|
2002-06-29 00:04:16 +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) :
|
2004-10-13 06:24:06 +00:00
|
|
|
Transliterator(UNICODE_STRING("Any-Name", 8), 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.
|
|
|
|
*/
|
2007-06-08 22:31:20 +00:00
|
|
|
/*UnicodeNameTransliterator& UnicodeNameTransliterator::operator=(
|
2001-06-11 23:38:54 +00:00
|
|
|
const UnicodeNameTransliterator& o) {
|
|
|
|
Transliterator::operator=(o);
|
|
|
|
return *this;
|
2007-06-08 22:31:20 +00:00
|
|
|
}*/
|
2001-06-11 23:38:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
2011-07-27 05:53:56 +00:00
|
|
|
int32_t clen = U16_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);
|
2004-12-04 07:22:23 +00:00
|
|
|
str.append(UnicodeString(buf, len, US_INV)).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 */
|