From 5336fc9b3c5df84246137cfac8090e667f3c09d3 Mon Sep 17 00:00:00 2001 From: Ivan Hetman Date: Tue, 14 Nov 2023 17:57:09 +0200 Subject: [PATCH] classlib: implement String.to__Case with locale parameter in JS BE (#766) --- .../impl/string/JSStringInjector.java | 36 +++++++++++++++---- .../org/teavm/classlib/java/lang/TString.java | 14 ++++++++ .../org/teavm/classlib/java/util/TLocale.java | 29 +++++++++++---- .../teavm/classlib/java/lang/StringTest.java | 19 +++++++++- .../teavm/classlib/java/util/LocaleTest.java | 10 ++++++ 5 files changed, 94 insertions(+), 14 deletions(-) diff --git a/classlib/src/main/java/org/teavm/classlib/impl/string/JSStringInjector.java b/classlib/src/main/java/org/teavm/classlib/impl/string/JSStringInjector.java index 67b174b00..d971920e8 100644 --- a/classlib/src/main/java/org/teavm/classlib/impl/string/JSStringInjector.java +++ b/classlib/src/main/java/org/teavm/classlib/impl/string/JSStringInjector.java @@ -195,15 +195,39 @@ public class JSStringInjector implements Injector, Function 0) { + if (!languageCode.isEmpty()) { buffer.append(getDisplayLanguage(locale)); count++; } - if (countryCode.length() > 0) { + if (!countryCode.isEmpty()) { if (count == 1) { buffer.append(" ("); } buffer.append(getDisplayCountry(locale)); count++; } - if (variantCode.length() > 0) { + if (!variantCode.isEmpty()) { if (count == 1) { buffer.append(" ("); } else if (count == 2) { @@ -260,12 +261,12 @@ public final class TLocale implements TCloneable, TSerializable { public String toString() { StringBuilder result = new StringBuilder(); result.append(languageCode); - if (countryCode.length() > 0) { + if (!countryCode.isEmpty()) { result.append('_'); result.append(countryCode); } - if (variantCode.length() > 0 && result.length() > 0) { - if (0 == countryCode.length()) { + if (!variantCode.isEmpty() && result.length() > 0) { + if (countryCode.isEmpty()) { result.append("__"); } else { result.append('_'); @@ -274,4 +275,18 @@ public final class TLocale implements TCloneable, TSerializable { } return result.toString(); } + + public TString toLanguageTag() { + StringBuilder result = new StringBuilder(); + result.append(languageCode); + if (!countryCode.isEmpty()) { + result.append('-'); + result.append(countryCode); + } + if (!variantCode.isEmpty() && result.length() > 0) { + result.append('-'); + result.append(variantCode); + } + return new TString(result.toString().toCharArray()); + } } diff --git a/tests/src/test/java/org/teavm/classlib/java/lang/StringTest.java b/tests/src/test/java/org/teavm/classlib/java/lang/StringTest.java index 2471ab616..c24bf7231 100644 --- a/tests/src/test/java/org/teavm/classlib/java/lang/StringTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/lang/StringTest.java @@ -22,6 +22,7 @@ import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import java.io.UnsupportedEncodingException; +import java.util.Locale; import org.junit.Test; import org.junit.runner.RunWith; import org.teavm.junit.EachTestCompiledSeparately; @@ -309,8 +310,24 @@ public class StringTest { } @Test - public void makesLowerCase() { + public void convertsCase() { assertEquals("foo bar", "FoO bAr".toLowerCase()); + assertEquals("FOO BAR", "FoO bAr".toUpperCase()); + } + + private String common = "i̇stanbul"; + private String turkish = "istanbul"; + + @Test + @SkipPlatform({ TestPlatform.C, TestPlatform.WEBASSEMBLY, TestPlatform.WASI }) + public void convertsCaseLocaled() { + assertEquals(turkish, "İstanbul".toLowerCase(new Locale("tr", "TR"))); + assertEquals(common, "İstanbul".toLowerCase(Locale.US)); + assertNotEquals(turkish, common); + assertEquals("İSTANBUL", common.toUpperCase(Locale.US)); + assertEquals("İSTANBUL", turkish.toUpperCase(new Locale("tr", "TR"))); + assertNotEquals(common.toUpperCase(Locale.US), turkish.toUpperCase(new Locale("tr", "TR"))); + assertEquals(common.toUpperCase(Locale.US), common.toUpperCase(Locale.CANADA)); } @Test diff --git a/tests/src/test/java/org/teavm/classlib/java/util/LocaleTest.java b/tests/src/test/java/org/teavm/classlib/java/util/LocaleTest.java index 908ebc485..90fea72da 100644 --- a/tests/src/test/java/org/teavm/classlib/java/util/LocaleTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/util/LocaleTest.java @@ -61,4 +61,14 @@ public class LocaleTest { assertEquals("Соединенные Штаты", usEnglish.getDisplayCountry(russian)); assertEquals("Россия", russian.getDisplayCountry(russian)); } + + @Test + public void testLanguageTag() { + assertEquals("fr-CA", Locale.CANADA_FRENCH.toLanguageTag()); + assertEquals("zh", Locale.CHINESE.toLanguageTag()); + assertEquals("zh-TW", Locale.TRADITIONAL_CHINESE.toLanguageTag()); + assertEquals("zh-CN", Locale.SIMPLIFIED_CHINESE.toLanguageTag()); + assertEquals("en-GB", Locale.UK.toLanguageTag()); + assertEquals("en-US", Locale.US.toLanguageTag()); + } }