]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/i18n/anytrans.cpp
ICU-491.11.2.tar.gz
[apple/icu.git] / icuSources / i18n / anytrans.cpp
index cdfea1b4ff0ee602bf49295bc1dcb8b579f4940f..c3b3b67c25ce89940e210fcc4375590bf99834d0 100644 (file)
@@ -1,6 +1,6 @@
 /*
 *****************************************************************
-* Copyright (c) 2002-2003, International Business Machines Corporation
+* Copyright (c) 2002-2011, International Business Machines Corporation
 * and others.  All Rights Reserved.
 *****************************************************************
 * Date        Name        Description
@@ -19,6 +19,8 @@
 #include "uvector.h"
 #include "tridpars.h"
 #include "hash.h"
+#include "putilimp.h"
+#include "uinvchar.h"
 
 //------------------------------------------------------------
 // Constants
@@ -37,7 +39,7 @@ U_CDECL_BEGIN
  */
 static void U_CALLCONV
 _deleteTransliterator(void *obj) {
-    delete (Transliterator*) obj;    
+    delete (icu::Transliterator*) obj;    
 }
 U_CDECL_END
 
@@ -170,7 +172,7 @@ void ScriptRunIterator::adjustLimit(int32_t delta) {
 //------------------------------------------------------------
 // AnyTransliterator
 
-const char AnyTransliterator::fgClassID=0;
+UOBJECT_DEFINE_RTTI_IMPLEMENTATION(AnyTransliterator)
 
 AnyTransliterator::AnyTransliterator(const UnicodeString& id,
                                      const UnicodeString& theTarget,
@@ -180,7 +182,10 @@ AnyTransliterator::AnyTransliterator(const UnicodeString& id,
     Transliterator(id, NULL),
     targetScript(theTargetScript) 
 {
-    cache = uhash_open(uhash_hashLong, uhash_compareLong, &ec);
+    cache = uhash_open(uhash_hashLong, uhash_compareLong, NULL, &ec);
+    if (U_FAILURE(ec)) {
+        return;
+    }
     uhash_setValueDeleter(cache, _deleteTransliterator);
 
     target = theTarget;
@@ -203,7 +208,10 @@ AnyTransliterator::AnyTransliterator(const AnyTransliterator& o) :
 {
     // Don't copy the cache contents
     UErrorCode ec = U_ZERO_ERROR;
-    cache = uhash_open(uhash_hashLong, uhash_compareLong, &ec);
+    cache = uhash_open(uhash_hashLong, uhash_compareLong, NULL, &ec);
+    if (U_FAILURE(ec)) {
+        return;
+    }
     uhash_setValueDeleter(cache, _deleteTransliterator);
 }
 
@@ -270,7 +278,7 @@ Transliterator* AnyTransliterator::getTransliterator(UScriptCode source) const {
     Transliterator* t = (Transliterator*) uhash_iget(cache, (int32_t) source);
     if (t == NULL) {
         UErrorCode ec = U_ZERO_ERROR;
-        UnicodeString sourceName(uscript_getName(source), "");
+        UnicodeString sourceName(uscript_getName(source), -1, US_INV);
         UnicodeString id(sourceName);
         id.append(TARGET_SEP).append(target);
         
@@ -280,7 +288,7 @@ Transliterator* AnyTransliterator::getTransliterator(UScriptCode source) const {
             
             // Try to pivot around Latin, our most common script
             id = sourceName;
-            id.append(LATIN_PIVOT).append(target);
+            id.append(LATIN_PIVOT, -1).append(target);
             t = Transliterator::createInstance(id, UTRANS_FORWARD, ec);
             if (U_FAILURE(ec) || t == NULL) {
                 delete t;
@@ -299,14 +307,19 @@ Transliterator* AnyTransliterator::getTransliterator(UScriptCode source) const {
 /**
  * Return the script code for a given name, or -1 if not found.
  */
-UScriptCode AnyTransliterator::scriptNameToCode(const UnicodeString& name) {
+static UScriptCode scriptNameToCode(const UnicodeString& name) {
     char buf[128];
     UScriptCode code;
     UErrorCode ec = U_ZERO_ERROR;
-
-    name.extract(0, 128, buf, 128, "");
-    if (uscript_getCode(buf, &code, 1, &ec) != 1 ||
-        U_FAILURE(ec)) {
+    int32_t nameLen = name.length();
+    UBool isInvariant = uprv_isInvariantUString(name.getBuffer(), nameLen);
+    
+    if (isInvariant) {
+        name.extract(0, nameLen, buf, (int32_t)sizeof(buf), US_INV);
+        buf[127] = 0;   // Make sure that we NULL terminate the string.
+    }
+    if (!isInvariant || uscript_getCode(buf, &code, 1, &ec) != 1 || U_FAILURE(ec))
+    {
         code = USCRIPT_INVALID_CODE;
     }
     return code;
@@ -319,8 +332,8 @@ UScriptCode AnyTransliterator::scriptNameToCode(const UnicodeString& name) {
  */
 void AnyTransliterator::registerIDs() {
 
-    UErrorCode ec;
-    Hashtable seen(TRUE);
+    UErrorCode ec = U_ZERO_ERROR;
+    Hashtable seen(TRUE, ec);
 
     int32_t sourceCount = Transliterator::_countAvailableSources();
     for (int32_t s=0; s<sourceCount; ++s) {
@@ -328,7 +341,7 @@ void AnyTransliterator::registerIDs() {
         Transliterator::_getAvailableSource(s, source);
 
         // Ignore the "Any" source
-        if (source.caseCompare(ANY, 0 /*U_FOLD_CASE_DEFAULT*/) == 0) continue;
+        if (source.caseCompare(ANY, 3, 0 /*U_FOLD_CASE_DEFAULT*/) == 0) continue;
 
         int32_t targetCount = Transliterator::_countAvailableTargets(source);
         for (int32_t t=0; t<targetCount; ++t) {
@@ -351,7 +364,7 @@ void AnyTransliterator::registerIDs() {
                 Transliterator::_getAvailableVariant(v, source, target, variant);
                 
                 UnicodeString id;
-                TransliteratorIDParser::STVtoID(ANY, target, variant, id);
+                TransliteratorIDParser::STVtoID(UnicodeString(TRUE, ANY, 3), target, variant, id);
                 ec = U_ZERO_ERROR;
                 AnyTransliterator* t = new AnyTransliterator(id, target, variant,
                                                              targetScript, ec);
@@ -359,7 +372,7 @@ void AnyTransliterator::registerIDs() {
                     delete t;
                 } else {
                     Transliterator::_registerInstance(t);
-                    Transliterator::_registerSpecialInverse(target, NULL_ID, FALSE);
+                    Transliterator::_registerSpecialInverse(target, UnicodeString(TRUE, NULL_ID, 4), FALSE);
                 }
             }
         }