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

public abstract Class ArrayBasedCharEscaper

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

A CharEscaper that uses an array to quickly look up replacement characters for a given char value. An additional safe range is provided that determines whether char values 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 Java source code escaping where the replacement array contains information about special ASCII characters such as \\t and \\n while escapeUnsafe is overridden to handle general escaping of the form \\uxxxx.

The size of the data structure used by ArrayBasedCharEscaper is proportional to the highest valued character 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.

Authors
Sven Mawson, David Beaumont
Since
15.0

Field Summary

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

Constructor Summary

AccessConstructor and Description
protected
ArrayBasedCharEscaper(Map<Character, String>
a map of characters to their escaped representations
replacementMap
,
char
the lowest character value in the safe range
safeMin
,
char
the highest character value in the safe range
safeMax
)

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

protected
ArrayBasedCharEscaper(ArrayBasedEscaperMap
the mapping of characters to be escaped
escaperMap
,
char
the lowest character value in the safe range
safeMin
,
char
the highest character value in the safe range
safeMax
)

Creates a new ArrayBasedCharEscaper 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.CharEscaper.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
(char
the character to escape if necessary
c
)

Implements abstract com.google.common.escape.CharEscaper.escape.

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

Returns:

the replacement characters, or null if no escaping was required
escapeUnsafe
(char
the character to escape
c
)

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

Inherited from com.google.common.escape.CharEscaper:
escapeSlow

Field Detail

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

Constructor Detail

ArrayBasedCharEscaperback to summary
protected ArrayBasedCharEscaper(Map<Character, String> replacementMap, char safeMin, char safeMax)

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

If a character 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:char

the lowest character value in the safe range

safeMax:char

the highest character value in the safe range

ArrayBasedCharEscaperback to summary
protected ArrayBasedCharEscaper(ArrayBasedEscaperMap escaperMap, char safeMin, char safeMax)

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

If a character 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 mapping of characters to be escaped

safeMin:char

the lowest character value in the safe range

safeMax:char

the highest character value in the safe range

Method Detail

escapeback to summary
public final String escape(String s)

Overrides com.google.common.escape.CharEscaper.escape.

Doc from com.google.common.escape.CharEscaper.escape.

Returns the escaped form of a given literal string.

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(char c)

Implements abstract com.google.common.escape.CharEscaper.escape.

Escapes a single character 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
c:char

Doc from com.google.common.escape.CharEscaper.escape.

the character 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(char c)

Escapes a char value 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 characters.

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
c:char

the character to escape

Returns:char[]

the replacement characters, or null if no escaping was required

Annotations
@CheckForNull