Class ArrayBasedCharEscaper
- java.lang.Object
-
- com.google.common.escape.Escaper
-
- com.google.common.escape.CharEscaper
-
- com.google.common.escape.ArrayBasedCharEscaper
-
@GwtCompatible public abstract class ArrayBasedCharEscaper extends CharEscaper
ACharEscaper
that uses an array to quickly look up replacement characters for a givenchar
value. An additional safe range is provided that determines whetherchar
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
whileescapeUnsafe(char)
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 usingArrayBasedEscaperMap
.- Since:
- 15.0
- Author:
- Sven Mawson, David Beaumont
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
ArrayBasedCharEscaper(ArrayBasedEscaperMap escaperMap, char safeMin, char safeMax)
Creates a new ArrayBasedCharEscaper instance with the given replacement map and specified safe range.protected
ArrayBasedCharEscaper(Map<Character,String> replacementMap, char safeMin, char safeMax)
Creates a new ArrayBasedCharEscaper instance with the given replacement map and specified safe range.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected char[]
escape(char c)
Escapes a single character using the replacement array and safe range values.String
escape(String s)
Returns the escaped form of a given literal string.protected abstract char[]
escapeUnsafe(char c)
Escapes achar
value that has no direct explicit value in the replacement array and lies outside the stated safe range.-
Methods inherited from class com.google.common.escape.CharEscaper
escapeSlow
-
Methods inherited from class com.google.common.escape.Escaper
asFunction
-
-
-
-
Constructor Detail
-
ArrayBasedCharEscaper
protected ArrayBasedCharEscaper(Map<Character,String> replacementMap, char safeMin, char safeMax)
Creates a new ArrayBasedCharEscaper instance with the given replacement map and specified safe range. IfsafeMax < 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(char)
is called, otherwise no escaping is performed.- Parameters:
replacementMap
- a map of characters to their escaped representationssafeMin
- the lowest character value in the safe rangesafeMax
- the highest character value in the safe range
-
ArrayBasedCharEscaper
protected ArrayBasedCharEscaper(ArrayBasedEscaperMap escaperMap, char safeMin, char safeMax)
Creates a new ArrayBasedCharEscaper instance with the given replacement map and specified safe range. IfsafeMax < 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(char)
is called, otherwise no escaping is performed.- Parameters:
escaperMap
- the mapping of characters to be escapedsafeMin
- the lowest character value in the safe rangesafeMax
- the highest character value in the safe range
-
-
Method Detail
-
escape
public final String escape(String s)
Description copied from class:CharEscaper
Returns the escaped form of a given literal string.- Overrides:
escape
in classCharEscaper
- Parameters:
s
- the literal string to be escaped- Returns:
- the escaped form of
string
-
escape
@CheckForNull protected final char[] escape(char c)
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 thenescapeUnsafe(char)
is called.- Specified by:
escape
in classCharEscaper
- Parameters:
c
- the character to escape if necessary- Returns:
- the replacement characters, or
null
if no escaping was required
-
escapeUnsafe
@CheckForNull protected abstract char[] escapeUnsafe(char c)
Escapes achar
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
- the character to escape- Returns:
- the replacement characters, or
null
if no escaping was required
-
-