]> granicus.if.org Git - icu/commitdiff
ICU-20148 fix dup variant and dup ext singleton
authorFrank Tang <ftang@chromium.org>
Wed, 6 Mar 2019 20:35:17 +0000 (12:35 -0800)
committerFrank Yung-Fong Tang <41213225+FrankYFTang@users.noreply.github.com>
Thu, 7 Mar 2019 00:12:06 +0000 (16:12 -0800)
icu4j/main/classes/core/src/com/ibm/icu/impl/locale/LanguageTag.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/util/ULocaleTest.java

index d812ae2c6de59a59a2c7c6b3eaf0e54e67fba864..6b12c8171732e7af81bc8086ab16d74a723f0a0b 100644 (file)
@@ -324,7 +324,11 @@ public class LanguageTag {
             if (_variants.isEmpty()) {
                 _variants = new ArrayList<String>(3);
             }
-            _variants.add(s);
+            // Ignore repeated variant
+            s = s.toUpperCase();
+            if (!_variants.contains(s)) {
+                _variants.add(s);
+            }
             sts._parseLength = itr.currentEnd();
             itr.next();
         }
@@ -343,7 +347,7 @@ public class LanguageTag {
             String s = itr.current();
             if (isExtensionSingleton(s)) {
                 int start = itr.currentStart();
-                String singleton = s;
+                String singleton = s.toLowerCase();
                 StringBuilder sb = new StringBuilder(singleton);
 
                 itr.next();
@@ -367,7 +371,14 @@ public class LanguageTag {
                 if (_extensions.size() == 0) {
                     _extensions = new ArrayList<String>(4);
                 }
-                _extensions.add(sb.toString());
+                // Ignore the extension if it is already in _extensions.
+                boolean alreadyHas = false;
+                for (String extension : _extensions) {
+                    alreadyHas |= extension.charAt(0) == sb.charAt(0);
+                }
+                if (!alreadyHas) {
+                  _extensions.add(sb.toString());
+                }
                 found = true;
             } else {
                 break;
index 56dd2a2d9a17061ae9bc9167b8df93221ecce978..be4564a9e751044551bd8e9096fe4e77d45ae9b3 100644 (file)
@@ -4099,6 +4099,25 @@ public class ULocaleTest extends TestFmwk {
         }
     }
 
+    @Test
+    public void TestForLanguageTagBug20148() {
+        ULocale uloc = ULocale.forLanguageTag("de-DE-1901-1901");
+        assertEquals("ULocale.forLanguageTag(\"de-DE-1901-1901\") ",
+                     "de_DE_1901", uloc.getName());
+
+        uloc = ULocale.forLanguageTag("de-DE-1aBc-1AbC");
+        assertEquals("ULocale.forLanguageTag(\"de-DE-1aBc-1AbC\") ",
+                     "de_DE_1ABC", uloc.getName());
+
+        uloc = ULocale.forLanguageTag("en-a-bbb-a-ccc");
+        assertEquals("ULocale.forLanguageTag(\"en-a-bbb-a-ccc\") ",
+                     "en@a=bbb", uloc.getName());
+
+        uloc = ULocale.forLanguageTag("en-A-bbb-a-ccc");
+        assertEquals("ULocale.forLanguageTag(\"en-A-bbb-a-ccc\") ",
+                     "en@a=bbb", uloc.getName());
+    }
+
     @Test
     public void TestForLanguageTagBug13776() {
         final Locale backupDefault = Locale.getDefault();