Bug 572339: [String] Add StringUtils.formatCodePoint
Change-Id: I950c57226046583ad997247f69b4a44784a19401
diff --git a/jcommons/org.eclipse.statet.jcommons.util-tests/src/org/eclipse/statet/jcommons/string/StringUtilsTest.java b/jcommons/org.eclipse.statet.jcommons.util-tests/src/org/eclipse/statet/jcommons/string/StringUtilsTest.java
new file mode 100644
index 0000000..f52352c
--- /dev/null
+++ b/jcommons/org.eclipse.statet.jcommons.util-tests/src/org/eclipse/statet/jcommons/string/StringUtilsTest.java
@@ -0,0 +1,39 @@
+/*=============================================================================#
+ # Copyright (c) 2021 Stephan Wahlbrink and others.
+ #
+ # This program and the accompanying materials are made available under the
+ # terms of the Eclipse Public License 2.0 which is available at
+ # https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
+ # which is available at https://www.apache.org/licenses/LICENSE-2.0.
+ #
+ # SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
+ #
+ # Contributors:
+ # Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation
+ #=============================================================================*/
+
+package org.eclipse.statet.jcommons.string;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.api.Test;
+
+import org.eclipse.statet.jcommons.lang.NonNullByDefault;
+
+
+@NonNullByDefault
+public class StringUtilsTest {
+
+
+ @Test
+ public void formatCodePoint() {
+ assertEquals("U+0000", StringUtils.formatCodePoint(0)); // MIN
+ assertEquals("U+0001", StringUtils.formatCodePoint(1));
+ assertEquals("U+0041", StringUtils.formatCodePoint('A'));
+ assertEquals("U+FFFF", StringUtils.formatCodePoint(0xFFFF));
+ assertEquals("U+010000", StringUtils.formatCodePoint(0x10000));
+ assertEquals("U+10FFFF", StringUtils.formatCodePoint(0x10FFFF)); // MAX
+ assertEquals("U+FFFFFFFF", StringUtils.formatCodePoint(0xFFFFFFFF)); // <invalid>
+ }
+
+}
diff --git a/jcommons/org.eclipse.statet.jcommons.util/src/org/eclipse/statet/jcommons/string/StringUtils.java b/jcommons/org.eclipse.statet.jcommons.util/src/org/eclipse/statet/jcommons/string/StringUtils.java
new file mode 100644
index 0000000..b4273c5
--- /dev/null
+++ b/jcommons/org.eclipse.statet.jcommons.util/src/org/eclipse/statet/jcommons/string/StringUtils.java
@@ -0,0 +1,54 @@
+/*=============================================================================#
+ # Copyright (c) 2021 Stephan Wahlbrink and others.
+ #
+ # This program and the accompanying materials are made available under the
+ # terms of the Eclipse Public License 2.0 which is available at
+ # https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
+ # which is available at https://www.apache.org/licenses/LICENSE-2.0.
+ #
+ # SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
+ #
+ # Contributors:
+ # Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation
+ #=============================================================================*/
+
+package org.eclipse.statet.jcommons.string;
+
+import org.eclipse.statet.jcommons.lang.NonNullByDefault;
+
+
+@NonNullByDefault
+public class StringUtils {
+
+
+ private static final byte[] U_DIGITS= {
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
+ 'A', 'B', 'C', 'D', 'E', 'F',
+ };
+
+
+ /**
+ * Formats the Unicode code points in the standard U+XXXX notation.
+ *
+ * @param cp the code point
+ * @return a string with the formatted code point
+ */
+ @SuppressWarnings("deprecation")
+ public static String formatCodePoint(int cp) {
+ final int nDigits= Math.max(((Integer.SIZE + 7 - Integer.numberOfLeadingZeros(cp)) / 8) * 2, 4);
+ final byte[] latin1= new byte[2 + nDigits];
+ latin1[0]= 'U';
+ latin1[1]= '+';
+ int i= latin1.length;
+ do {
+ latin1[--i]= U_DIGITS[cp & 0xF];
+ cp >>>= 4;
+ } while (i > 2);
+ return new String(latin1, 0, 0, latin1.length);
+ }
+
+
+ private StringUtils() {
+ }
+
+}