Class ArrayBasedUnicodeEscaper
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(int) 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.
- Since:
- 15.0
- Author:
- David Beaumont
- 
Constructor SummaryConstructorsModifierConstructorDescriptionprotectedArrayBasedUnicodeEscaper(ArrayBasedEscaperMap escaperMap, int safeMin, int safeMax, @Nullable String unsafeReplacement) Creates a new ArrayBasedUnicodeEscaper instance with the given replacement map and specified safe range.protectedArrayBasedUnicodeEscaper(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.
- 
Method SummaryModifier and TypeMethodDescriptionprotected final char @Nullable []escape(int cp) Escapes a single Unicode code point using the replacement array and safe range values.final StringReturns the escaped form of a given literal string.protected abstract char @Nullable []escapeUnsafe(int cp) Escapes a code point that has no direct explicit value in the replacement array and lies outside the stated safe range.protected final intnextEscapeIndex(CharSequence csq, int index, int end) Scans a sub-sequence of characters from a givenCharSequence, returning the index of the next character that requires escaping.Methods inherited from class com.google.common.escape.UnicodeEscapercodePointAt, escapeSlowMethods inherited from class com.google.common.escape.EscaperasFunction
- 
Constructor Details- 
ArrayBasedUnicodeEscaperprotected 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. IfsafeMax < safeMinthen 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(int)is called, otherwise no escaping is performed.- Parameters:
- replacementMap- a map of characters to their escaped representations
- safeMin- the lowest character value in the safe range
- safeMax- the highest character value in the safe range
- unsafeReplacement- the default replacement for unsafe characters or null if no default replacement is required
 
- 
ArrayBasedUnicodeEscaperprotected ArrayBasedUnicodeEscaper(ArrayBasedEscaperMap escaperMap, int safeMin, int safeMax, @Nullable String unsafeReplacement) Creates a new ArrayBasedUnicodeEscaper instance with the given replacement map and specified safe range. IfsafeMax < safeMinthen 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(int)is called, otherwise no escaping is performed.- Parameters:
- escaperMap- the map of replacements
- safeMin- the lowest character value in the safe range
- safeMax- the highest character value in the safe range
- unsafeReplacement- the default replacement for unsafe characters or null if no default replacement is required
 
 
- 
- 
Method Details- 
escapeDescription copied from class:UnicodeEscaperReturns 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 UnicodeEscaper.nextEscapeIndex(CharSequence, int, int)directly. Doing this forPercentEscapermore than doubled the performance for unescaped strings (as measured byCharEscapersBenchmark).- Overrides:
- escapein class- UnicodeEscaper
- Parameters:
- s- the literal string to be escaped
- Returns:
- the escaped form of string
 
- 
escapeEscapes 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 thenescapeUnsafe(int)is called.- Specified by:
- escapein class- UnicodeEscaper
- Parameters:
- cp- the Unicode code point to escape if necessary
- Returns:
- the replacement characters, or nullif no escaping was required
 
- 
nextEscapeIndexDescription copied from class:UnicodeEscaperScans a sub-sequence of characters from a givenCharSequence, 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 UnicodeEscaper.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 usingCharSequence.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 PercentEscaperfor an example.- Overrides:
- nextEscapeIndexin class- UnicodeEscaper
- Parameters:
- csq- a sequence of characters
- index- the index of the first character to be scanned
- end- the index immediately after the last character to be scanned
 
- 
escapeUnsafeEscapes 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- the Unicode code point to escape
- Returns:
- the replacement characters, or nullif no escaping was required
 
 
-