blob: b4273c517a8850a9a7671e10d57a27e72636a7d5 [file] [log] [blame]
/*=============================================================================#
# 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() {
}
}