public final class Utility extends Object
| Modifier and Type | Field and Description |
|---|---|
static String |
LINE_SEPARATOR |
| Constructor and Description |
|---|
Utility() |
| Modifier and Type | Method and Description |
|---|---|
static int |
addExact(int x,
int y)
This implementation is equivalent to Java 8+ Math#addExact(int, int)
|
static <T extends Appendable> |
appendNumber(T result,
int n,
int radix,
int minDigits)
Append a number to the given Appendable in the given radix.
|
static <A extends Appendable> |
appendTo(CharSequence string,
A appendable)
Appends a CharSequence to an Appendable, converting IOException to ICUUncheckedIOException.
|
static void |
appendToRule(StringBuffer rule,
int c,
boolean isLiteral,
boolean escapeUnprintable,
StringBuffer quoteBuf)
Append a character to a rule that is being built up.
|
static void |
appendToRule(StringBuffer rule,
String text,
boolean isLiteral,
boolean escapeUnprintable,
StringBuffer quoteBuf)
Append the given string to the rule.
|
static void |
appendToRule(StringBuffer rule,
UnicodeMatcher matcher,
boolean escapeUnprintable,
StringBuffer quoteBuf)
Given a matcher reference, which may be null, append its
pattern as a literal to the given rule.
|
static boolean |
arrayEquals(byte[] source,
Object target) |
static boolean |
arrayEquals(double[] source,
Object target)
Convenience utility to compare two double[]s
Ought to be in System
|
static boolean |
arrayEquals(int[] source,
Object target)
Convenience utility to compare two int[]s
Ought to be in System
|
static boolean |
arrayEquals(Object[] source,
Object target)
Convenience utility to compare two Object[]s.
|
static boolean |
arrayEquals(Object source,
Object target)
Convenience utility to compare two Object[]s
Ought to be in System
|
static boolean |
arrayRegionMatches(byte[] source,
int sourceStart,
byte[] target,
int targetStart,
int len) |
static boolean |
arrayRegionMatches(char[] source,
int sourceStart,
char[] target,
int targetStart,
int len)
Convenience utility to compare two Object[]s
Ought to be in System.
|
static boolean |
arrayRegionMatches(double[] source,
int sourceStart,
double[] target,
int targetStart,
int len)
Convenience utility to compare two arrays of doubles.
|
static boolean |
arrayRegionMatches(int[] source,
int sourceStart,
int[] target,
int targetStart,
int len)
Convenience utility to compare two int[]s.
|
static boolean |
arrayRegionMatches(Object[] source,
int sourceStart,
Object[] target,
int targetStart,
int len)
Convenience utility to compare two Object[]s
Ought to be in System.
|
static String |
arrayToRLEString(byte[] a)
Construct a string representing a byte array.
|
static String |
arrayToRLEString(char[] a)
Construct a string representing a char array.
|
static String |
arrayToRLEString(int[] a)
Construct a string representing an int array.
|
static String |
arrayToRLEString(short[] a)
Construct a string representing a short array.
|
static boolean |
charSequenceEquals(CharSequence a,
CharSequence b)
Returns whether the chars in the two CharSequences are equal.
|
static int |
charSequenceHashCode(CharSequence value)
Returns a hash code for a CharSequence that is equivalent to calling
charSequence.toString().hashCode()
|
static <T extends Comparable<T>> |
checkCompare(T a,
T b)
Convenience utility.
|
static int |
checkHash(Object a)
Convenience utility.
|
static int |
compareUnsigned(int source,
int target)
Compares 2 unsigned integers
|
static int |
cpFromCodePointAndLength(int cpAndLength) |
static String |
escape(String s)
Convert characters outside the range U+0020 to U+007F to
Unicode escapes, and convert backslash to a double backslash.
|
static <T extends Appendable> |
escape(T result,
int c)
Escapes one code point using
|
static <T extends Appendable> |
escapeUnprintable(T result,
int c)
Escapes one unprintable code point using
|
static String |
format1ForSource(String s)
Format a String for representation in a source file.
|
static String |
formatForSource(String s)
Format a String for representation in a source file.
|
static String |
fromHex(String string,
int minLength,
Pattern separator)
Parse a list of hex numbers and return a string
|
static String |
fromHex(String string,
int minLength,
String separator)
Parse a list of hex numbers and return a string
|
static String |
hex(byte[] o,
int start,
int end,
String separator) |
static String |
hex(CharSequence s)
Convert a string to comma-separated groups of 4 hex uppercase
digits.
|
static String |
hex(long ch)
Convert a char to 4 hex uppercase digits.
|
static String |
hex(long i,
int places)
Supplies a zero-padded hex representation of an integer (without 0x)
|
static <S extends CharSequence> |
hex(S s,
int width,
S separator)
Convert a string to comma-separated groups of 4 hex uppercase
digits.
|
static <S extends CharSequence,U extends CharSequence,T extends Appendable> |
hex(S s,
int width,
U separator,
boolean useCodePoints,
T result)
Convert a string to separated groups of hex uppercase
digits.
|
static byte |
highBit(int n)
Find the highest bit in a positive integer.
|
static boolean |
isUnprintable(int c)
Return true if the character is NOT printable ASCII.
|
static String |
joinStrings(CharSequence delimiter,
Iterable<? extends CharSequence> elements)
Java 8+ String#join(CharSequence, Iterable extends CharSequence>) compatible method for Java 7 env.
|
static int |
lengthFromCodePointAndLength(int cpAndLength) |
static int |
lookup(String source,
String[] target)
Look up a given string in a string array.
|
static boolean |
parseChar(String id,
int[] pos,
char ch)
Parse a single non-whitespace character 'ch', optionally
preceded by whitespace.
|
static int |
parseInteger(String rule,
int[] pos,
int limit)
Parse an integer at pos, either of the form \d+ or of the form
0x[0-9A-Fa-f]+ or 0[0-7]+, that is, in standard decimal, hex,
or octal format.
|
static int |
parseNumber(String text,
int[] pos,
int radix)
Parse an unsigned 31-bit integer at the given offset.
|
static int |
parsePattern(String rule,
int pos,
int limit,
String pattern,
int[] parsedInts)
Parse a pattern string starting at offset pos.
|
static int |
parsePattern(String pat,
Replaceable text,
int index,
int limit)
Parse a pattern string within the given Replaceable and a parsing
pattern.
|
static String |
parseUnicodeIdentifier(String str,
int[] pos)
Parse a Unicode identifier from the given string at the given
position.
|
static int |
quotedIndexOf(String text,
int start,
int limit,
String setOfChars)
Returns the index of the first character in a set, ignoring quoted text.
|
static String |
repeat(String s,
int count)
Utility to duplicate a string count times
|
static byte[] |
RLEStringToByteArray(String s)
Construct an array of bytes from a run-length encoded string.
|
static char[] |
RLEStringToCharArray(String s)
Construct an array of shorts from a run-length encoded string.
|
static int[] |
RLEStringToIntArray(String s)
Construct an array of ints from a run-length encoded string.
|
static short[] |
RLEStringToShortArray(String s)
Construct an array of shorts from a run-length encoded string.
|
static boolean |
sameObjects(Object a,
Object b)
Trivial reference equality.
|
static boolean |
shouldAlwaysBeEscaped(int c) |
static String[] |
split(String s,
char divider)
Split a string into pieces based on the given divider character
|
static void |
split(String s,
char divider,
String[] output)
Split a string into pieces based on the given divider character
|
static String[] |
splitString(String src,
String target) |
static String[] |
splitWhitespace(String src)
Split the string at runs of ascii whitespace characters.
|
static String |
unescape(CharSequence s)
Convert all escapes in a given string using unescapeAndLengthAt().
|
static int |
unescapeAndLengthAt(CharSequence s,
int offset)
Converts an escape to a code point value.
|
static String |
unescapeLeniently(CharSequence s)
Convert all escapes in a given string using unescapeAndLengthAt().
|
static String |
valueOf(int[] source)
Utility method to take a int[] containing codepoints and return
a string representation with code units.
|
public static String LINE_SEPARATOR
public static final boolean arrayEquals(Object[] source, Object target)
public static final boolean arrayEquals(int[] source,
Object target)
public static final boolean arrayEquals(double[] source,
Object target)
public static final boolean arrayEquals(byte[] source,
Object target)
public static final boolean arrayEquals(Object source, Object target)
public static final boolean arrayRegionMatches(Object[] source, int sourceStart, Object[] target, int targetStart, int len)
len - the length to compare.
The start indices and start+len must be valid.public static final boolean arrayRegionMatches(char[] source,
int sourceStart,
char[] target,
int targetStart,
int len)
len - the length to compare.
The start indices and start+len must be valid.public static final boolean arrayRegionMatches(int[] source,
int sourceStart,
int[] target,
int targetStart,
int len)
len - the length to compare.
The start indices and start+len must be valid.
Ought to be in Systempublic static final boolean arrayRegionMatches(double[] source,
int sourceStart,
double[] target,
int targetStart,
int len)
len - the length to compare.
The start indices and start+len must be valid.
Ought to be in Systempublic static final boolean arrayRegionMatches(byte[] source,
int sourceStart,
byte[] target,
int targetStart,
int len)
public static final boolean sameObjects(Object a, Object b)
public static <T extends Comparable<T>> int checkCompare(T a, T b)
public static int checkHash(Object a)
public static final String arrayToRLEString(int[] a)
public static final String arrayToRLEString(short[] a)
public static final String arrayToRLEString(char[] a)
public static final String arrayToRLEString(byte[] a)
public static final int[] RLEStringToIntArray(String s)
public static final short[] RLEStringToShortArray(String s)
public static final char[] RLEStringToCharArray(String s)
public static final byte[] RLEStringToByteArray(String s)
public static final String formatForSource(String s)
public static final String format1ForSource(String s)
public static final String escape(String s)
public static int unescapeAndLengthAt(CharSequence s, int offset)
offset - the offset to the character after the backslash.public static int cpFromCodePointAndLength(int cpAndLength)
public static int lengthFromCodePointAndLength(int cpAndLength)
public static String unescape(CharSequence s)
IllegalArgumentException - if an invalid escape is
seen.public static String unescapeLeniently(CharSequence s)
public static String hex(long ch)
public static String hex(long i, int places)
public static String hex(CharSequence s)
public static <S extends CharSequence,U extends CharSequence,T extends Appendable> T hex(S s, int width, U separator, boolean useCodePoints, T result)
public static <S extends CharSequence> String hex(S s, int width, S separator)
public static void split(String s, char divider, String[] output)
s - the string to splitdivider - the character on which to split. Occurrences of
this character are not included in the outputoutput - an array to receive the substrings between
instances of divider. It must be large enough on entry to
accommodate all output. Adjacent instances of the divider
character will place empty strings into output. Before
returning, output is padded out with empty strings.public static String[] split(String s, char divider)
s - the string to splitdivider - the character on which to split. Occurrences of
this character are not included in the outputpublic static int lookup(String source, String[] target)
source - the string to search fortarget - the array of zero or more strings in which to
look for sourcepublic static boolean parseChar(String id, int[] pos, char ch)
id - the string to be parsedpos - INPUT-OUTPUT parameter. On input, pos[0] is the
offset of the first character to be parsed. On output, pos[0]
is the index after the last parsed character. If the parse
fails, pos[0] will be unchanged.ch - the non-whitespace character to be parsed.public static int parsePattern(String rule, int pos, int limit, String pattern, int[] parsedInts)
pattern - must only contain lowercase characters, which
will match their uppercase equivalents as well. A space
character matches one or more required spaces. A '~' character
matches zero or more optional spaces. A '#' character matches
an integer and stores it in parsedInts, which the caller must
ensure has enough capacity.parsedInts - array to receive parsed integers. Caller
must ensure that parsedInts.length is >= the number of '#'
signs in 'pattern'.public static int parsePattern(String pat, Replaceable text, int index, int limit)
pat - pattern that controls parsingtext - text to be parsed, starting at indexindex - offset to first character to parselimit - offset after last character to parsepublic static int parseInteger(String rule, int[] pos, int limit)
pos - INPUT-OUTPUT parameter. On input, the first
character to parse. On output, the character after the last
parsed character.public static String parseUnicodeIdentifier(String str, int[] pos)
str - the string to parsepos - INPUT-OUTPUT parameter. On INPUT, pos[0] is the
first character to examine. It must be less than str.length(),
and it must not point to a whitespace character. That is, must
have pos[0] < str.length(). On
OUTPUT, the position after the last parsed character.public static <T extends Appendable> T appendNumber(T result, int n, int radix, int minDigits)
result - the digits of the number are appended heren - the number to be converted to digits; may be negative.
If negative, a '-' is prepended to the digits.radix - a radix from 2 to 36 inclusive.minDigits - the minimum number of digits, not including
any '-', to produce. Values less than 2 have no effect. One
digit is always emitted regardless of this parameter.public static int parseNumber(String text, int[] pos, int radix)
text - the text to be parsedpos - INPUT-OUTPUT parameter. On entry, pos[0] is the
offset within text at which to start parsing; it should point
to a valid digit. On exit, pos[0] is the offset after the last
parsed character. If the parse failed, it will be unchanged on
exit. Must be >= 0 on entry.radix - the radix in which to parse; must be >= 2 and <=
36.public static boolean isUnprintable(int c)
public static boolean shouldAlwaysBeEscaped(int c)
public static <T extends Appendable> boolean escapeUnprintable(T result, int c)
public static <T extends Appendable> T escape(T result, int c)
public static int quotedIndexOf(String text, int start, int limit, String setOfChars)
setOfChars.text - text to be searchedstart - the beginning index, inclusive; 0 <= start
<= limit.limit - the ending index, exclusive; start <= limit
<= text.length().setOfChars - string with one or more distinct characterssetOfChars
found, or -1 if not found.String.indexOf(int)public static void appendToRule(StringBuffer rule, int c, boolean isLiteral, boolean escapeUnprintable, StringBuffer quoteBuf)
rule - the string to append the character toc - the character to append, or (int)-1 if none.isLiteral - if true, then the given character should not be
quoted or escaped. Usually this means it is a syntactic element
such as > or $escapeUnprintable - if true, then unprintable characters
should be escaped using escapeUnprintable(). These escapes will
appear outside of quotes.quoteBuf - a buffer which is used to build up quoted
substrings. The caller should initially supply an empty buffer,
and thereafter should not modify the buffer. The buffer should be
cleared out by, at the end, calling this method with a literal
character (which may be -1).public static void appendToRule(StringBuffer rule, String text, boolean isLiteral, boolean escapeUnprintable, StringBuffer quoteBuf)
public static void appendToRule(StringBuffer rule, UnicodeMatcher matcher, boolean escapeUnprintable, StringBuffer quoteBuf)
public static final int compareUnsigned(int source,
int target)
source - 32 bit unsigned integertarget - 32 bit unsigned integerpublic static final byte highBit(int n)
n - is the integern is not positivepublic static String valueOf(int[] source)
public static String repeat(String s, int count)
s - String to be duplicated.count - Number of times to duplicate a string.public static String[] splitWhitespace(String src)
public static String fromHex(String string, int minLength, String separator)
string - String of hex numbers.minLength - Minimal length.separator - Separator.public static String fromHex(String string, int minLength, Pattern separator)
string - String of hex numbers.minLength - Minimal length.separator - Separator.public static int addExact(int x,
int y)
x - the first valuey - the second valuepublic static boolean charSequenceEquals(CharSequence a, CharSequence b)
public static int charSequenceHashCode(CharSequence value)
public static <A extends Appendable> A appendTo(CharSequence string, A appendable)
public static String joinStrings(CharSequence delimiter, Iterable<? extends CharSequence> elements)
delimiter - the delimiter that separates each elementelements - the elements to join together.NullPointerException - If delimiter or elements is null