Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
package com.google.common.escape;

public abstract class ArrayBasedUnicodeEscaper {
// The first code point in the safe range.
/*The first code point in the safe range.*/
private int safeMin;
// The last code point in the safe range.
/*The last code point in the safe range.*/
private int safeMax;

private char safeMinChar;
private char safeMaxChar;

protected ArrayBasedUnicodeEscaper() {
// This is a bit of a hack but lets us do quicker per-character checks in
// the fast path code. The safe min/max values are very unlikely to extend
// into the range of surrogate characters, but if they do we must not test
// any values in that range. To see why, consider the case where:
// safeMin <= {hi,lo} <= safeMax
// where {hi,lo} are characters forming a surrogate pair such that:
// codePointOf(hi, lo) > safeMax
// which would result in the surrogate pair being (wrongly) considered safe.
// If we clip the safe range used during the per-character tests so it is
// below the values of characters in surrogate pairs, this cannot occur.
// This approach does mean that we break out of the fast path code in cases
// where we don't strictly need to, but this situation will almost never
// occur in practice.
/*
This is a bit of a hack but lets us do quicker per-character checks in
the fast path code. The safe min/max values are very unlikely to extend
into the range of surrogate characters, but if they do we must not test
any values in that range. To see why, consider the case where:
safeMin <= {hi,lo} <= safeMax
where {hi,lo} are characters forming a surrogate pair such that:
codePointOf(hi, lo) > safeMax
which would result in the surrogate pair being (wrongly) considered safe.
If we clip the safe range used during the per-character tests so it is
below the values of characters in surrogate pairs, this cannot occur.
This approach does mean that we break out of the fast path code in cases
where we don't strictly need to, but this situation will almost never
occur in practice.
*/
if (safeMin >= Character.MIN_HIGH_SURROGATE) {
this.safeMinChar = Character.MAX_VALUE;
this.safeMaxChar = 0;
Expand Down