intl/icu/source/i18n/uni2name.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 **********************************************************************
michael@0 3 * Copyright (C) 2001-2011, International Business Machines
michael@0 4 * Corporation and others. All Rights Reserved.
michael@0 5 **********************************************************************
michael@0 6 * Date Name Description
michael@0 7 * 06/06/01 aliu Creation.
michael@0 8 **********************************************************************
michael@0 9 */
michael@0 10
michael@0 11 #include "unicode/utypes.h"
michael@0 12
michael@0 13 #if !UCONFIG_NO_TRANSLITERATION
michael@0 14
michael@0 15 #include "unicode/unifilt.h"
michael@0 16 #include "unicode/uchar.h"
michael@0 17 #include "unicode/utf16.h"
michael@0 18 #include "uni2name.h"
michael@0 19 #include "cstring.h"
michael@0 20 #include "cmemory.h"
michael@0 21 #include "uprops.h"
michael@0 22
michael@0 23 U_NAMESPACE_BEGIN
michael@0 24
michael@0 25 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(UnicodeNameTransliterator)
michael@0 26
michael@0 27 static const UChar OPEN_DELIM[] = {92,78,123,0}; // "\N{"
michael@0 28 static const UChar CLOSE_DELIM = 125; // "}"
michael@0 29 #define OPEN_DELIM_LEN 3
michael@0 30
michael@0 31 /**
michael@0 32 * Constructs a transliterator.
michael@0 33 */
michael@0 34 UnicodeNameTransliterator::UnicodeNameTransliterator(UnicodeFilter* adoptedFilter) :
michael@0 35 Transliterator(UNICODE_STRING("Any-Name", 8), adoptedFilter) {
michael@0 36 }
michael@0 37
michael@0 38 /**
michael@0 39 * Destructor.
michael@0 40 */
michael@0 41 UnicodeNameTransliterator::~UnicodeNameTransliterator() {}
michael@0 42
michael@0 43 /**
michael@0 44 * Copy constructor.
michael@0 45 */
michael@0 46 UnicodeNameTransliterator::UnicodeNameTransliterator(const UnicodeNameTransliterator& o) :
michael@0 47 Transliterator(o) {}
michael@0 48
michael@0 49 /**
michael@0 50 * Assignment operator.
michael@0 51 */
michael@0 52 /*UnicodeNameTransliterator& UnicodeNameTransliterator::operator=(
michael@0 53 const UnicodeNameTransliterator& o) {
michael@0 54 Transliterator::operator=(o);
michael@0 55 return *this;
michael@0 56 }*/
michael@0 57
michael@0 58 /**
michael@0 59 * Transliterator API.
michael@0 60 */
michael@0 61 Transliterator* UnicodeNameTransliterator::clone(void) const {
michael@0 62 return new UnicodeNameTransliterator(*this);
michael@0 63 }
michael@0 64
michael@0 65 /**
michael@0 66 * Implements {@link Transliterator#handleTransliterate}.
michael@0 67 * Ignore isIncremental since we don't need the context, and
michael@0 68 * we work on codepoints.
michael@0 69 */
michael@0 70 void UnicodeNameTransliterator::handleTransliterate(Replaceable& text, UTransPosition& offsets,
michael@0 71 UBool /*isIncremental*/) const {
michael@0 72 // The failure mode, here and below, is to behave like Any-Null,
michael@0 73 // if either there is no name data (max len == 0) or there is no
michael@0 74 // memory (malloc() => NULL).
michael@0 75
michael@0 76 int32_t maxLen = uprv_getMaxCharNameLength();
michael@0 77 if (maxLen == 0) {
michael@0 78 offsets.start = offsets.limit;
michael@0 79 return;
michael@0 80 }
michael@0 81
michael@0 82 // Accomodate the longest possible name plus padding
michael@0 83 char* buf = (char*) uprv_malloc(maxLen);
michael@0 84 if (buf == NULL) {
michael@0 85 offsets.start = offsets.limit;
michael@0 86 return;
michael@0 87 }
michael@0 88
michael@0 89 int32_t cursor = offsets.start;
michael@0 90 int32_t limit = offsets.limit;
michael@0 91
michael@0 92 UnicodeString str(FALSE, OPEN_DELIM, OPEN_DELIM_LEN);
michael@0 93 UErrorCode status;
michael@0 94 int32_t len;
michael@0 95
michael@0 96 while (cursor < limit) {
michael@0 97 UChar32 c = text.char32At(cursor);
michael@0 98 int32_t clen = U16_LENGTH(c);
michael@0 99 status = U_ZERO_ERROR;
michael@0 100 if ((len = u_charName(c, U_EXTENDED_CHAR_NAME, buf, maxLen, &status)) >0 && !U_FAILURE(status)) {
michael@0 101 str.truncate(OPEN_DELIM_LEN);
michael@0 102 str.append(UnicodeString(buf, len, US_INV)).append(CLOSE_DELIM);
michael@0 103 text.handleReplaceBetween(cursor, cursor+clen, str);
michael@0 104 len += OPEN_DELIM_LEN + 1; // adjust for delimiters
michael@0 105 cursor += len; // advance cursor and adjust for new text
michael@0 106 limit += len-clen; // change in length
michael@0 107 } else {
michael@0 108 cursor += clen;
michael@0 109 }
michael@0 110 }
michael@0 111
michael@0 112 offsets.contextLimit += limit - offsets.limit;
michael@0 113 offsets.limit = limit;
michael@0 114 offsets.start = cursor;
michael@0 115
michael@0 116 uprv_free(buf);
michael@0 117 }
michael@0 118
michael@0 119 U_NAMESPACE_END
michael@0 120
michael@0 121 #endif /* #if !UCONFIG_NO_TRANSLITERATION */

mercurial