Skip to content

Commit

Permalink
Normalizer emulation (#9970)
Browse files Browse the repository at this point in the history
Adds emulation for `java.text.Normalizer` using the native `normalize`
method on JavaScript's String.
  • Loading branch information
zbynek authored Jul 8, 2024
1 parent d8767cf commit aabb575
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
51 changes: 51 additions & 0 deletions user/super/com/google/gwt/emul/java/text/Normalizer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2024 GWT Project Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package java.text;

import javaemul.internal.JsUtils;
import jsinterop.annotations.JsType;

/**
* Emulation of <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/text/Normalizer.html">java.text.Normalizer</a>.
*/
public final class Normalizer {

public enum Form {
/** Canonical decomposition. */
NFD,
/** Canonical decomposition followed by composition. */
NFC,
/** Compatibility decomposition. */
NFKD,
/** Compatibility decomposition followed by composition. */
NFKC
}

public static String normalize(CharSequence input, Form form) {
return JsUtils.<NativeString>uncheckedCast(input.toString()).normalize(form.name());
}

public static boolean isNormalized(CharSequence input, Form form) {
String str = input.toString();
return str.equals(normalize(str, form));
}

@JsType(isNative = true, name = "String", namespace = "<window>")
private static class NativeString {
public native String normalize(String form);
}
}

4 changes: 4 additions & 0 deletions user/test/com/google/gwt/emultest/EmulSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import com.google.gwt.emultest.java.sql.SqlDateTest;
import com.google.gwt.emultest.java.sql.SqlTimeTest;
import com.google.gwt.emultest.java.sql.SqlTimestampTest;
import com.google.gwt.emultest.java.text.NormalizerTest;
import com.google.gwt.emultest.java.util.ComparatorTest;
import com.google.gwt.emultest.java.util.DateTest;
import com.google.gwt.emultest.java.util.ObjectsTest;
Expand Down Expand Up @@ -135,6 +136,9 @@
ObjectsTest.class,
RandomTest.class,

// -- java.text
NormalizerTest.class,

// Put last to reduce number of times the test framework switches modules
MathContextWithObfuscatedEnumsTest.class,
})
Expand Down
62 changes: 62 additions & 0 deletions user/test/com/google/gwt/emultest/java/text/NormalizerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2024 GWT Project Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.gwt.emultest.java.text;

import com.google.gwt.junit.client.GWTTestCase;

import java.text.Normalizer;

public class NormalizerTest extends GWTTestCase {

private String hangul = "\uD55C\uAE00";
private String hangulDecomposed = "\u1112\u1161\u11AB\u1100\u1173\u11AF";
private String ligatureFF = "\uFB00";

public void testNormalizeHangul() {
assertEquals(hangul, Normalizer.normalize(hangul, Normalizer.Form.NFC));
assertEquals(hangulDecomposed, Normalizer.normalize(hangul, Normalizer.Form.NFD));
assertEquals(hangul, Normalizer.normalize(hangul, Normalizer.Form.NFKC));
assertEquals(hangulDecomposed, Normalizer.normalize(hangul, Normalizer.Form.NFKD));
assertEquals(hangul, Normalizer.normalize(hangulDecomposed, Normalizer.Form.NFC));
}

public void testNormalizeLigature() {
assertEquals(ligatureFF, Normalizer.normalize(ligatureFF, Normalizer.Form.NFC));
assertEquals(ligatureFF, Normalizer.normalize(ligatureFF, Normalizer.Form.NFD));
assertEquals("ff", Normalizer.normalize(ligatureFF, Normalizer.Form.NFKC));
assertEquals("ff", Normalizer.normalize(ligatureFF, Normalizer.Form.NFKD));
}

public void testIsNormalizedHangul() {
assertTrue(Normalizer.isNormalized(hangul, Normalizer.Form.NFC));
assertFalse(Normalizer.isNormalized(hangul, Normalizer.Form.NFD));
assertTrue(Normalizer.isNormalized(hangul, Normalizer.Form.NFKC));
assertFalse(Normalizer.isNormalized(hangul, Normalizer.Form.NFKD));
assertFalse(Normalizer.isNormalized(hangulDecomposed, Normalizer.Form.NFC));
}

public void testIsNormalizedLigature() {
assertTrue(Normalizer.isNormalized(ligatureFF, Normalizer.Form.NFC));
assertTrue(Normalizer.isNormalized(ligatureFF, Normalizer.Form.NFD));
assertFalse(Normalizer.isNormalized(ligatureFF, Normalizer.Form.NFKC));
assertFalse(Normalizer.isNormalized(ligatureFF, Normalizer.Form.NFKD));
}

@Override
public String getModuleName() {
return "com.google.gwt.emultest.EmulSuite";
}
}

0 comments on commit aabb575

Please sign in to comment.