Improve toLanguageTag()'s "und" locale handling

This CL is improves handling of und locale: instead of hacky replacement
of underscore with dash to avoid adding likely subtags to canonicalized
language tag it now calls uloc_addLikelySubtags() only for language tags
that need that.

Bug: 125283072
Test: mmm -j frameworks/minikin/tests/unittest && \
      adb sync data && \
      adb shell /data/nativetest/minikin_tests/minikin_tests
Change-Id: Ida08f908a207f9b9ef344677e0b6298c6824de0c
diff --git a/libs/minikin/LocaleListCache.cpp b/libs/minikin/LocaleListCache.cpp
index 15156cd..9ed5bd6 100644
--- a/libs/minikin/LocaleListCache.cpp
+++ b/libs/minikin/LocaleListCache.cpp
@@ -18,6 +18,7 @@
 
 #include "LocaleListCache.h"
 
+#include <cstring>
 #include <unordered_set>
 
 #include <log/log.h>
@@ -49,24 +50,20 @@
         return 0;
     }
 
+    char likelyChars[ULOC_FULLNAME_CAPACITY];
     // Preserve "" and "_****" since uloc_addLikelySubtags changes "" to "en_Latn_US".
     if (outLength == 0 || (outLength == 5 && output[0] == '_')) {
-        if (output[0] == '_') {
-            output[0] = '-';
+        size_t len = outLength < ULOC_FULLNAME_CAPACITY ? outLength : ULOC_FULLNAME_CAPACITY;
+        strncpy(likelyChars, output, len);
+    } else {
+        uErr = U_ZERO_ERROR;
+        uloc_addLikelySubtags(output, likelyChars, ULOC_FULLNAME_CAPACITY, &uErr);
+        if (U_FAILURE(uErr)) {
+            // unable to build a proper locale identifier
+            ALOGD("uloc_addLikelySubtags(\"%s\") failed: %s", output, u_errorName(uErr));
+            output[0] = '\0';
+            return 0;
         }
-        std::string buf(output, outLength);
-        outLength = (size_t)snprintf(output, outSize, "und%s", buf.c_str());
-        return outLength;
-    }
-
-    char likelyChars[ULOC_FULLNAME_CAPACITY];
-    uErr = U_ZERO_ERROR;
-    uloc_addLikelySubtags(output, likelyChars, ULOC_FULLNAME_CAPACITY, &uErr);
-    if (U_FAILURE(uErr)) {
-        // unable to build a proper locale identifier
-        ALOGD("uloc_addLikelySubtags(\"%s\") failed: %s", output, u_errorName(uErr));
-        output[0] = '\0';
-        return 0;
     }
 
     uErr = U_ZERO_ERROR;