2 **********************************************************************
3 * Copyright (C) 2001-2004, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 * Date Name Description
7 * 06/06/01 aliu Creation.
8 **********************************************************************
11 #include "unicode/utypes.h"
13 #if !UCONFIG_NO_TRANSLITERATION
15 #include "unicode/unifilt.h"
16 #include "unicode/uchar.h"
24 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(UnicodeNameTransliterator
)
26 static const UChar OPEN_DELIM
[] = {92,78,123,0}; // "\N{"
27 static const UChar CLOSE_DELIM
= 125; // "}"
28 #define OPEN_DELIM_LEN 3
31 * Constructs a transliterator.
33 UnicodeNameTransliterator::UnicodeNameTransliterator(UnicodeFilter
* adoptedFilter
) :
34 Transliterator(UNICODE_STRING("Any-Name", 8), adoptedFilter
) {
40 UnicodeNameTransliterator::~UnicodeNameTransliterator() {}
45 UnicodeNameTransliterator::UnicodeNameTransliterator(const UnicodeNameTransliterator
& o
) :
49 * Assignment operator.
51 UnicodeNameTransliterator
& UnicodeNameTransliterator::operator=(
52 const UnicodeNameTransliterator
& o
) {
53 Transliterator::operator=(o
);
60 Transliterator
* UnicodeNameTransliterator::clone(void) const {
61 return new UnicodeNameTransliterator(*this);
65 * Implements {@link Transliterator#handleTransliterate}.
66 * Ignore isIncremental since we don't need the context, and
67 * we work on codepoints.
69 void UnicodeNameTransliterator::handleTransliterate(Replaceable
& text
, UTransPosition
& offsets
,
70 UBool
/*isIncremental*/) const {
71 // The failure mode, here and below, is to behave like Any-Null,
72 // if either there is no name data (max len == 0) or there is no
73 // memory (malloc() => NULL).
75 int32_t maxLen
= uprv_getMaxCharNameLength();
77 offsets
.start
= offsets
.limit
;
81 // Accomodate the longest possible name plus padding
82 char* buf
= (char*) uprv_malloc(maxLen
);
84 offsets
.start
= offsets
.limit
;
88 int32_t cursor
= offsets
.start
;
89 int32_t limit
= offsets
.limit
;
91 UnicodeString
str(FALSE
, OPEN_DELIM
, OPEN_DELIM_LEN
);
95 while (cursor
< limit
) {
96 UChar32 c
= text
.char32At(cursor
);
97 int32_t clen
= UTF_CHAR_LENGTH(c
);
98 status
= U_ZERO_ERROR
;
99 if ((len
= u_charName(c
, U_EXTENDED_CHAR_NAME
, buf
, maxLen
, &status
)) >0 && !U_FAILURE(status
)) {
100 str
.truncate(OPEN_DELIM_LEN
);
101 str
.append(UnicodeString(buf
, len
, "")).append(CLOSE_DELIM
);
102 text
.handleReplaceBetween(cursor
, cursor
+clen
, str
);
103 len
+= OPEN_DELIM_LEN
+ 1; // adjust for delimiters
104 cursor
+= len
; // advance cursor and adjust for new text
105 limit
+= len
-clen
; // change in length
111 offsets
.contextLimit
+= limit
- offsets
.limit
;
112 offsets
.limit
= limit
;
113 offsets
.start
= cursor
;
120 #endif /* #if !UCONFIG_NO_TRANSLITERATION */