
panda.lang.escape.JavaUnicodeEscaper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of panda-core Show documentation
Show all versions of panda-core Show documentation
Panda Core is the core module of Panda Framework, it contains commonly used utility classes similar to apache-commons.
package panda.lang.escape;
/**
* Translates codepoints to their Unicode escaped value suitable for Java source.
*
*/
public class JavaUnicodeEscaper extends UnicodeEscaper {
/**
*
* Constructs a JavaUnicodeEscaper
above the specified value (exclusive).
*
*
* @param codepoint above which to escape
* @return the newly created {@code UnicodeEscaper} instance
*/
public static JavaUnicodeEscaper above(final int codepoint) {
return outsideOf(0, codepoint);
}
/**
*
* Constructs a JavaUnicodeEscaper
below the specified value (exclusive).
*
*
* @param codepoint below which to escape
* @return the newly created {@code UnicodeEscaper} instance
*/
public static JavaUnicodeEscaper below(final int codepoint) {
return outsideOf(codepoint, Integer.MAX_VALUE);
}
/**
*
* Constructs a JavaUnicodeEscaper
between the specified values (inclusive).
*
*
* @param codepointLow above which to escape
* @param codepointHigh below which to escape
* @return the newly created {@code UnicodeEscaper} instance
*/
public static JavaUnicodeEscaper between(final int codepointLow, final int codepointHigh) {
return new JavaUnicodeEscaper(codepointLow, codepointHigh, true);
}
/**
*
* Constructs a JavaUnicodeEscaper
outside of the specified values (exclusive).
*
*
* @param codepointLow below which to escape
* @param codepointHigh above which to escape
* @return the newly created {@code UnicodeEscaper} instance
*/
public static JavaUnicodeEscaper outsideOf(final int codepointLow, final int codepointHigh) {
return new JavaUnicodeEscaper(codepointLow, codepointHigh, false);
}
/**
*
* Constructs a JavaUnicodeEscaper
for the specified range. This is the underlying
* method for the other constructors/builders. The below
and above
* boundaries are inclusive when between
is true
and exclusive when it
* is false
.
*
*
* @param below int value representing the lowest codepoint boundary
* @param above int value representing the highest codepoint boundary
* @param between whether to escape between the boundaries or outside them
*/
public JavaUnicodeEscaper(final int below, final int above, final boolean between) {
super(below, above, between);
}
/**
* Converts the given codepoint to a hex string of the form {@code "\\uXXXX\\uXXXX"}
*
* @param codepoint a Unicode code point
* @return the hex string for the given codepoint
*/
@Override
protected String toUtf16Escape(final int codepoint) {
final char[] surrogatePair = Character.toChars(codepoint);
return "\\u" + hex(surrogatePair[0]) + "\\u" + hex(surrogatePair[1]);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy