Top Description Fields Constructors Methods
com.google.common.escape

public abstract Class ArrayBasedUnicodeEscaper

extends UnicodeEscaper
Class Inheritance
Annotations
@GwtCompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkNotNull

A UnicodeEscaper that uses an array to quickly look up replacement characters for a given code point. An additional safe range is provided that determines whether code points without specific replacements are to be considered safe and left unescaped or should be escaped in a general way.

A good example of usage of this class is for HTML escaping where the replacement array contains information about the named HTML entities such as & and " while escapeUnsafe is overridden to handle general escaping of the form &#NNNNN;.

The size of the data structure used by ArrayBasedUnicodeEscaper is proportional to the highest valued code point that requires escaping. For example a replacement map containing the single character '\u1000' will require approximately 16K of memory. If you need to create multiple escaper instances that have the same character replacement mapping consider using ArrayBasedEscaperMap.

Author
David Beaumont
Since
15.0

Field Summary

Modifier and TypeField and Description
private final char[][]
private final int
private final int
private final char
private final int
private final char

Constructor Summary

AccessConstructor and Description
protected
ArrayBasedUnicodeEscaper(Map<Character, String>
a map of characters to their escaped representations
replacementMap
,
int
the lowest character value in the safe range
safeMin
,
int
the highest character value in the safe range
safeMax
,
@Nullable String
the default replacement for unsafe characters or null if no default replacement is required
unsafeReplacement
)

Creates a new ArrayBasedUnicodeEscaper instance with the given replacement map and specified safe range.

protected
ArrayBasedUnicodeEscaper(ArrayBasedEscaperMap
the map of replacements
escaperMap
,
int
the lowest character value in the safe range
safeMin
,
int
the highest character value in the safe range
safeMax
,
@Nullable String
the default replacement for unsafe characters or null if no default replacement is required
unsafeReplacement
)

Creates a new ArrayBasedUnicodeEscaper instance with the given replacement map and specified safe range.

Method Summary

Modifier and TypeMethod and Description
public final String
escape(String
the literal string to be escaped
s
)

Overrides com.google.common.escape.UnicodeEscaper.escape.

Returns the escaped form of a given literal string.
protected final char[]

Returns:

the replacement characters, or null if no escaping was required
escape
(int
the Unicode code point to escape if necessary
cp
)

Implements abstract com.google.common.escape.UnicodeEscaper.escape.

Escapes a single Unicode code point using the replacement array and safe range values.
protected abstract char[]

Returns:

the replacement characters, or null if no escaping was required
escapeUnsafe
(int
the Unicode code point to escape
cp
)

Escapes a code point that has no direct explicit value in the replacement array and lies outside the stated safe range.

protected final int
nextEscapeIndex(CharSequence
a sequence of characters
csq
,
int
the index of the first character to be scanned
index
,
int
the index immediately after the last character to be scanned
end
)

Overrides com.google.common.escape.UnicodeEscaper.nextEscapeIndex.

Scans a sub-sequence of characters from a given CharSequence, returning the index of the next character that requires escaping.
Inherited from com.google.common.escape.UnicodeEscaper:
codePointAtescapeSlow

Field Detail

replacementsback to summary
private final char[][] replacements
replacementsLengthback to summary
private final int replacementsLength
safeMaxback to summary
private final int safeMax
safeMaxCharback to summary
private final char safeMaxChar
safeMinback to summary
private final int safeMin
safeMinCharback to summary
private final char safeMinChar

Constructor Detail

ArrayBasedUnicodeEscaperback to summary
protected ArrayBasedUnicodeEscaper(Map<Character, String> replacementMap, int safeMin, int safeMax, @Nullable String unsafeReplacement)

Creates a new ArrayBasedUnicodeEscaper instance with the given replacement map and specified safe range. If safeMax < safeMin then no code points are considered safe.

If a code point has no mapped replacement then it is checked against the safe range. If it lies outside that, then escapeUnsafe is called, otherwise no escaping is performed.

Parameters
replacementMap:Map<Character, String>

a map of characters to their escaped representations

safeMin:int

the lowest character value in the safe range

safeMax:int

the highest character value in the safe range

unsafeReplacement:@Nullable String

the default replacement for unsafe characters or null if no default replacement is required

ArrayBasedUnicodeEscaperback to summary
protected ArrayBasedUnicodeEscaper(ArrayBasedEscaperMap escaperMap, int safeMin, int safeMax, @Nullable String unsafeReplacement)

Creates a new ArrayBasedUnicodeEscaper instance with the given replacement map and specified safe range. If safeMax < safeMin then no code points are considered safe. This initializer is useful when explicit instances of ArrayBasedEscaperMap are used to allow the sharing of large replacement mappings.

If a code point has no mapped replacement then it is checked against the safe range. If it lies outside that, then escapeUnsafe is called, otherwise no escaping is performed.

Parameters
escaperMap:ArrayBasedEscaperMap

the map of replacements

safeMin:int

the lowest character value in the safe range

safeMax:int

the highest character value in the safe range

unsafeReplacement:@Nullable String

the default replacement for unsafe characters or null if no default replacement is required

Method Detail

escapeback to summary
public final String escape(String s)

Overrides com.google.common.escape.UnicodeEscaper.escape.

Doc from com.google.common.escape.UnicodeEscaper.escape.

Returns the escaped form of a given literal string.

If you are escaping input in arbitrary successive chunks, then it is not generally safe to use this method. If an input string ends with an unmatched high surrogate character, then this method will throw IllegalArgumentException. You should ensure your input is valid UTF-16 before calling this method.

Note

When implementing an escaper it is a good idea to override this method for efficiency by inlining the implementation of nextEscapeIndex(CharSequence, int, int) directly. Doing this for com.google.common.net.PercentEscaper more than doubled the performance for unescaped strings (as measured by CharEscapersBenchmark).

Parameters
s:String

the literal string to be escaped

Returns:String

the escaped form of string

Annotations
@Override
escapeback to summary
protected final char[] escape(int cp)

Implements abstract com.google.common.escape.UnicodeEscaper.escape.

Escapes a single Unicode code point using the replacement array and safe range values. If the given character does not have an explicit replacement and lies outside the safe range then escapeUnsafe is called.

Parameters
cp:int

Doc from com.google.common.escape.UnicodeEscaper.escape.

the Unicode code point to escape if necessary

Returns:char[]

the replacement characters, or null if no escaping was required

Annotations
@Override
@CheckForNull
escapeUnsafeback to summary
protected abstract char[] escapeUnsafe(int cp)

Escapes a code point that has no direct explicit value in the replacement array and lies outside the stated safe range. Subclasses should override this method to provide generalized escaping for code points if required.

Note that arrays returned by this method must not be modified once they have been returned. However it is acceptable to return the same array multiple times (even for different input characters).

Parameters
cp:int

the Unicode code point to escape

Returns:char[]

the replacement characters, or null if no escaping was required

Annotations
@CheckForNull
nextEscapeIndexback to summary
protected final int nextEscapeIndex(CharSequence csq, int index, int end)

Overrides com.google.common.escape.UnicodeEscaper.nextEscapeIndex.

Doc from com.google.common.escape.UnicodeEscaper.nextEscapeIndex.

Scans a sub-sequence of characters from a given CharSequence, returning the index of the next character that requires escaping.

Note

When implementing an escaper, it is a good idea to override this method for efficiency. The base class implementation determines successive Unicode code points and invokes escape(int) for each of them. If the semantics of your escaper are such that code points in the supplementary range are either all escaped or all unescaped, this method can be implemented more efficiently using CharSequence#charAt(int).

Note however that if your escaper does not escape characters in the supplementary range, you should either continue to validate the correctness of any surrogate characters encountered or provide a clear warning to users that your escaper does not validate its input.

See com.google.common.net.PercentEscaper for an example.

Parameters
csq:CharSequence

a sequence of characters

index:int

the index of the first character to be scanned

end:int

the index immediately after the last character to be scanned

Annotations
@Override