aboutsummaryrefslogtreecommitdiff
path: root/guava/src/com/google/common/escape/CharEscaper.java
diff options
context:
space:
mode:
Diffstat (limited to 'guava/src/com/google/common/escape/CharEscaper.java')
-rw-r--r--guava/src/com/google/common/escape/CharEscaper.java71
1 files changed, 35 insertions, 36 deletions
diff --git a/guava/src/com/google/common/escape/CharEscaper.java b/guava/src/com/google/common/escape/CharEscaper.java
index b8ffee3a9..4be21a6c1 100644
--- a/guava/src/com/google/common/escape/CharEscaper.java
+++ b/guava/src/com/google/common/escape/CharEscaper.java
@@ -1,15 +1,17 @@
/*
* Copyright (C) 2006 The Guava Authors
*
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
- * in compliance with the License. You may obtain a copy of the License at
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
- * Unless required by applicable law or agreed to in writing, software distributed under the License
- * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
- * or implied. See the License for the specific language governing permissions and limitations under
- * the License.
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
*/
package com.google.common.escape;
@@ -32,9 +34,10 @@ import com.google.common.annotations.GwtCompatible;
* <p>A {@code CharEscaper} instance is required to be stateless, and safe when used concurrently by
* multiple threads.
*
- * <p>Popular escapers are defined as constants in classes like {@link
- * com.google.common.html.HtmlEscapers} and {@link com.google.common.xml.XmlEscapers}. To create
- * your own escapers extend this class and implement the {@link #escape(char)} method.
+ * <p>Several popular escapers are defined as constants in classes like {@link
+ * com.google.common.html.HtmlEscapers}, {@link com.google.common.xml.XmlEscapers}, and {@link
+ * SourceCodeEscapers}. To create your own escapers extend this class and implement the {@link
+ * #escape(char)} method.
*
* @author Sven Mawson
* @since 15.0
@@ -52,9 +55,8 @@ public abstract class CharEscaper extends Escaper {
* @return the escaped form of {@code string}
* @throws NullPointerException if {@code string} is null
*/
- @Override
- public String escape(String string) {
- checkNotNull(string); // GWT specific check (do not optimize)
+ @Override public String escape(String string) {
+ checkNotNull(string); // GWT specific check (do not optimize)
// Inlineable fast-path loop which hands off to escapeSlow() only if needed
int length = string.length();
for (int index = 0; index < length; index++) {
@@ -66,23 +68,6 @@ public abstract class CharEscaper extends Escaper {
}
/**
- * Returns the escaped form of the given character, or {@code null} if this character does not
- * need to be escaped. If an empty array is returned, this effectively strips the input character
- * from the resulting text.
- *
- * <p>If the character does not need to be escaped, this method should return {@code null}, rather
- * than a one-character array containing the character itself. This enables the escaping algorithm
- * to perform more efficiently.
- *
- * <p>An escaper is expected to be able to deal with any {@code char} value, so this method should
- * not throw any exceptions.
- *
- * @param c the character to escape if necessary
- * @return the replacement characters, or {@code null} if no escaping was needed
- */
- protected abstract char[] escape(char c);
-
- /**
* Returns the escaped form of a given literal string, starting at the given index. This method is
* called by the {@link #escape(String)} method when it discovers that escaping is required. It is
* protected to allow subclasses to override the fastpath escaping function to inline their
@@ -110,9 +95,7 @@ public abstract class CharEscaper extends Escaper {
char[] r = escape(s.charAt(index));
// If no replacement is needed, just continue.
- if (r == null) {
- continue;
- }
+ if (r == null) continue;
int rlen = r.length;
int charsSkipped = index - lastEscape;
@@ -156,13 +139,27 @@ public abstract class CharEscaper extends Escaper {
}
/**
+ * Returns the escaped form of the given character, or {@code null} if this character does not
+ * need to be escaped. If an empty array is returned, this effectively strips the input character
+ * from the resulting text.
+ *
+ * <p>If the character does not need to be escaped, this method should return {@code null}, rather
+ * than a one-character array containing the character itself. This enables the escaping algorithm
+ * to perform more efficiently.
+ *
+ * <p>An escaper is expected to be able to deal with any {@code char} value, so this method should
+ * not throw any exceptions.
+ *
+ * @param c the character to escape if necessary
+ * @return the replacement characters, or {@code null} if no escaping was needed
+ */
+ protected abstract char[] escape(char c);
+
+ /**
* Helper method to grow the character buffer as needed, this only happens once in a while so it's
* ok if it's in a method call. If the index passed in is 0 then no copying will be done.
*/
private static char[] growBuffer(char[] dest, int index, int size) {
- if (size < 0) { // overflow - should be OutOfMemoryError but GWT/j2cl don't support it
- throw new AssertionError("Cannot increase internal buffer any further");
- }
char[] copy = new char[size];
if (index > 0) {
System.arraycopy(dest, 0, copy, 0, index);
@@ -170,6 +167,8 @@ public abstract class CharEscaper extends Escaper {
return copy;
}
- /** The multiplier for padding to use when growing the escape buffer. */
+ /**
+ * The multiplier for padding to use when growing the escape buffer.
+ */
private static final int DEST_PAD_MULTIPLIER = 2;
}