Skip to content

Commit

Permalink
Remove helper methods that are unused externally from the external co…
Browse files Browse the repository at this point in the history
…debase.

Fixes #6445

RELNOTES=n/a
PiperOrigin-RevId: 645455070
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Jun 21, 2024
1 parent 263712a commit cd11e83
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 216 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,36 +78,6 @@ public void testBuilderCreatesIndependentEscapers() {
assertEquals("Xhe-Xuick-Xrown-Xox$", second.escape("The Quick Brown Fox!"));
}

public void testAsUnicodeEscaper() throws IOException {
CharEscaper charEscaper =
createSimpleCharEscaper(
ImmutableMap.<Character, char[]>builder()
.put('x', "<hello>".toCharArray())
.put('\uD800', "<hi>".toCharArray())
.put('\uDC00', "<lo>".toCharArray())
.buildOrThrow());
UnicodeEscaper unicodeEscaper = Escapers.asUnicodeEscaper(charEscaper);
EscaperAsserts.assertBasic(unicodeEscaper);
assertEquals("<hello><hi><lo>", charEscaper.escape("x\uD800\uDC00"));
assertEquals("<hello><hi><lo>", unicodeEscaper.escape("x\uD800\uDC00"));

// Test that wrapped escapers acquire good Unicode semantics.
assertEquals("<hi><hello><lo>", charEscaper.escape("\uD800x\uDC00"));
try {
unicodeEscaper.escape("\uD800x\uDC00");
fail("should have failed for bad Unicode input");
} catch (IllegalArgumentException e) {
// pass
}
assertEquals("<lo><hi>", charEscaper.escape("\uDC00\uD800"));
try {
unicodeEscaper.escape("\uDC00\uD800");
fail("should have failed for bad Unicode input");
} catch (IllegalArgumentException e) {
// pass
}
}

// A trivial non-optimized escaper for testing.
static CharEscaper createSimpleCharEscaper(final ImmutableMap<Character, char[]> replacementMap) {
return new CharEscaper() {
Expand Down
78 changes: 0 additions & 78 deletions android/guava/src/com/google/common/escape/Escapers.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,33 +164,6 @@ protected char[] escapeUnsafe(char c) {
}
}

/**
* Returns a {@link UnicodeEscaper} equivalent to the given escaper instance. If the escaper is
* already a UnicodeEscaper then it is simply returned, otherwise it is wrapped in a
* UnicodeEscaper.
*
* <p>When a {@link CharEscaper} escaper is wrapped by this method it acquires extra behavior with
* respect to the well-formedness of Unicode character sequences and will throw {@link
* IllegalArgumentException} when given bad input.
*
* @param escaper the instance to be wrapped
* @return a UnicodeEscaper with the same behavior as the given instance
* @throws NullPointerException if escaper is null
* @throws IllegalArgumentException if escaper is not a UnicodeEscaper or a CharEscaper
*/
static UnicodeEscaper asUnicodeEscaper(Escaper escaper) {
checkNotNull(escaper);
if (escaper instanceof UnicodeEscaper) {
return (UnicodeEscaper) escaper;
} else if (escaper instanceof CharEscaper) {
return wrap((CharEscaper) escaper);
}
// In practice this shouldn't happen because it would be very odd not to
// extend either CharEscaper or UnicodeEscaper for non-trivial cases.
throw new IllegalArgumentException(
"Cannot create a UnicodeEscaper from: " + escaper.getClass().getName());
}

/**
* Returns a string that would replace the given character in the specified escaper, or {@code
* null} if no replacement should be made. This method is intended for use in tests through the
Expand Down Expand Up @@ -223,55 +196,4 @@ public static String computeReplacement(UnicodeEscaper escaper, int cp) {
private static String stringOrNull(@CheckForNull char[] in) {
return (in == null) ? null : new String(in);
}

/** Private helper to wrap a CharEscaper as a UnicodeEscaper. */
private static UnicodeEscaper wrap(CharEscaper escaper) {
return new UnicodeEscaper() {
@Override
@CheckForNull
protected char[] escape(int cp) {
// If a code point maps to a single character, just escape that.
if (cp < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
return escaper.escape((char) cp);
}
// Convert the code point to a surrogate pair and escape them both.
// Note: This code path is horribly slow and typically allocates 4 new
// char[] each time it is invoked. However this avoids any
// synchronization issues and makes the escaper thread safe.
char[] surrogateChars = new char[2];
Character.toChars(cp, surrogateChars, 0);
char[] hiChars = escaper.escape(surrogateChars[0]);
char[] loChars = escaper.escape(surrogateChars[1]);

// If either hiChars or lowChars are non-null, the CharEscaper is trying
// to escape the characters of a surrogate pair separately. This is
// uncommon and applies only to escapers that assume UCS-2 rather than
// UTF-16. See: http://en.wikipedia.org/wiki/UTF-16/UCS-2
if (hiChars == null && loChars == null) {
// We expect this to be the common code path for most escapers.
return null;
}
// Combine the characters and/or escaped sequences into a single array.
int hiCount = hiChars != null ? hiChars.length : 1;
int loCount = loChars != null ? loChars.length : 1;
char[] output = new char[hiCount + loCount];
if (hiChars != null) {
// TODO: Is this faster than System.arraycopy() for small arrays?
for (int n = 0; n < hiChars.length; ++n) {
output[n] = hiChars[n];
}
} else {
output[0] = surrogateChars[0];
}
if (loChars != null) {
for (int n = 0; n < loChars.length; ++n) {
output[hiCount + n] = loChars[n];
}
} else {
output[hiCount] = surrogateChars[1];
}
return output;
}
};
}
}
30 changes: 0 additions & 30 deletions guava-tests/test/com/google/common/escape/EscapersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,36 +78,6 @@ public void testBuilderCreatesIndependentEscapers() {
assertEquals("Xhe-Xuick-Xrown-Xox$", second.escape("The Quick Brown Fox!"));
}

public void testAsUnicodeEscaper() throws IOException {
CharEscaper charEscaper =
createSimpleCharEscaper(
ImmutableMap.<Character, char[]>builder()
.put('x', "<hello>".toCharArray())
.put('\uD800', "<hi>".toCharArray())
.put('\uDC00', "<lo>".toCharArray())
.buildOrThrow());
UnicodeEscaper unicodeEscaper = Escapers.asUnicodeEscaper(charEscaper);
EscaperAsserts.assertBasic(unicodeEscaper);
assertEquals("<hello><hi><lo>", charEscaper.escape("x\uD800\uDC00"));
assertEquals("<hello><hi><lo>", unicodeEscaper.escape("x\uD800\uDC00"));

// Test that wrapped escapers acquire good Unicode semantics.
assertEquals("<hi><hello><lo>", charEscaper.escape("\uD800x\uDC00"));
try {
unicodeEscaper.escape("\uD800x\uDC00");
fail("should have failed for bad Unicode input");
} catch (IllegalArgumentException e) {
// pass
}
assertEquals("<lo><hi>", charEscaper.escape("\uDC00\uD800"));
try {
unicodeEscaper.escape("\uDC00\uD800");
fail("should have failed for bad Unicode input");
} catch (IllegalArgumentException e) {
// pass
}
}

// A trivial non-optimized escaper for testing.
static CharEscaper createSimpleCharEscaper(final ImmutableMap<Character, char[]> replacementMap) {
return new CharEscaper() {
Expand Down
78 changes: 0 additions & 78 deletions guava/src/com/google/common/escape/Escapers.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,33 +164,6 @@ protected char[] escapeUnsafe(char c) {
}
}

/**
* Returns a {@link UnicodeEscaper} equivalent to the given escaper instance. If the escaper is
* already a UnicodeEscaper then it is simply returned, otherwise it is wrapped in a
* UnicodeEscaper.
*
* <p>When a {@link CharEscaper} escaper is wrapped by this method it acquires extra behavior with
* respect to the well-formedness of Unicode character sequences and will throw {@link
* IllegalArgumentException} when given bad input.
*
* @param escaper the instance to be wrapped
* @return a UnicodeEscaper with the same behavior as the given instance
* @throws NullPointerException if escaper is null
* @throws IllegalArgumentException if escaper is not a UnicodeEscaper or a CharEscaper
*/
static UnicodeEscaper asUnicodeEscaper(Escaper escaper) {
checkNotNull(escaper);
if (escaper instanceof UnicodeEscaper) {
return (UnicodeEscaper) escaper;
} else if (escaper instanceof CharEscaper) {
return wrap((CharEscaper) escaper);
}
// In practice this shouldn't happen because it would be very odd not to
// extend either CharEscaper or UnicodeEscaper for non-trivial cases.
throw new IllegalArgumentException(
"Cannot create a UnicodeEscaper from: " + escaper.getClass().getName());
}

/**
* Returns a string that would replace the given character in the specified escaper, or {@code
* null} if no replacement should be made. This method is intended for use in tests through the
Expand Down Expand Up @@ -223,55 +196,4 @@ public static String computeReplacement(UnicodeEscaper escaper, int cp) {
private static String stringOrNull(@CheckForNull char[] in) {
return (in == null) ? null : new String(in);
}

/** Private helper to wrap a CharEscaper as a UnicodeEscaper. */
private static UnicodeEscaper wrap(CharEscaper escaper) {
return new UnicodeEscaper() {
@Override
@CheckForNull
protected char[] escape(int cp) {
// If a code point maps to a single character, just escape that.
if (cp < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
return escaper.escape((char) cp);
}
// Convert the code point to a surrogate pair and escape them both.
// Note: This code path is horribly slow and typically allocates 4 new
// char[] each time it is invoked. However this avoids any
// synchronization issues and makes the escaper thread safe.
char[] surrogateChars = new char[2];
Character.toChars(cp, surrogateChars, 0);
char[] hiChars = escaper.escape(surrogateChars[0]);
char[] loChars = escaper.escape(surrogateChars[1]);

// If either hiChars or lowChars are non-null, the CharEscaper is trying
// to escape the characters of a surrogate pair separately. This is
// uncommon and applies only to escapers that assume UCS-2 rather than
// UTF-16. See: http://en.wikipedia.org/wiki/UTF-16/UCS-2
if (hiChars == null && loChars == null) {
// We expect this to be the common code path for most escapers.
return null;
}
// Combine the characters and/or escaped sequences into a single array.
int hiCount = hiChars != null ? hiChars.length : 1;
int loCount = loChars != null ? loChars.length : 1;
char[] output = new char[hiCount + loCount];
if (hiChars != null) {
// TODO: Is this faster than System.arraycopy() for small arrays?
for (int n = 0; n < hiChars.length; ++n) {
output[n] = hiChars[n];
}
} else {
output[0] = surrogateChars[0];
}
if (loChars != null) {
for (int n = 0; n < loChars.length; ++n) {
output[hiCount + n] = loChars[n];
}
} else {
output[hiCount] = surrogateChars[1];
}
return output;
}
};
}
}

0 comments on commit cd11e83

Please sign in to comment.