copy fixes made to original in r1616430

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1616532 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastian Bazley 2014-08-07 16:43:37 +00:00
parent 262fec5203
commit d5a4d66890
1 changed files with 32 additions and 24 deletions

View File

@ -37,11 +37,13 @@ import org.apache.http.util.CharArrayBuffer;
* to produce near zero intermediate garbage and make no intermediate copies of input data. * to produce near zero intermediate garbage and make no intermediate copies of input data.
* <p> * <p>
* This class is immutable and thread safe. * This class is immutable and thread safe.
*
* Temporary package-private copy of org.apache.http.message.TokenParser
*/ */
class TokenParser { class TokenParser {
public static BitSet INIT_BITSET(final int ... b) { public static BitSet INIT_BITSET(final int ... b) {
final BitSet bitset = new BitSet(b.length); final BitSet bitset = new BitSet();
for (final int aB : b) { for (final int aB : b) {
bitset.set(aB); bitset.set(aB);
} }
@ -49,16 +51,22 @@ class TokenParser {
} }
/** US-ASCII CR, carriage return (13) */ /** US-ASCII CR, carriage return (13) */
public static final int CR = '\r'; public static final char CR = '\r';
/** US-ASCII LF, line feed (10) */ /** US-ASCII LF, line feed (10) */
public static final int LF = '\n'; public static final char LF = '\n';
/** US-ASCII SP, space (32) */ /** US-ASCII SP, space (32) */
public static final int SP = ' '; public static final char SP = ' ';
/** US-ASCII HT, horizontal-tab (9) */ /** US-ASCII HT, horizontal-tab (9) */
public static final int HT = '\t'; public static final char HT = '\t';
/** Double quote */
public static final char DQUOTE = '\"';
/** Backward slash / escape character */
public static final char ESCAPE = '\\';
public static boolean isWhitespace(final char ch) { public static boolean isWhitespace(final char ch) {
return ch == SP || ch == HT || ch == CR || ch == LF; return ch == SP || ch == HT || ch == CR || ch == LF;
@ -67,10 +75,10 @@ class TokenParser {
public static final TokenParser INSTANCE = new TokenParser(); public static final TokenParser INSTANCE = new TokenParser();
/** /**
* Extracts from the sequence of bytes a token terminated with any of the given delimiters * Extracts from the sequence of chars a token terminated with any of the given delimiters
* discarding semantically insignificant whitespace characters. * discarding semantically insignificant whitespace characters.
* *
* @param buf buffer with the sequence of bytes to be parsed * @param buf buffer with the sequence of chars to be parsed
* @param cursor defines the bounds and current position of the buffer * @param cursor defines the bounds and current position of the buffer
* @param delimiters set of delimiting characters. Can be <code>null</code> if the token * @param delimiters set of delimiting characters. Can be <code>null</code> if the token
* is not delimited by any character. * is not delimited by any character.
@ -86,7 +94,7 @@ class TokenParser {
skipWhiteSpace(buf, cursor); skipWhiteSpace(buf, cursor);
whitespace = true; whitespace = true;
} else { } else {
if (dst.length() > 0 && whitespace) { if (whitespace && dst.length() > 0) {
dst.append(' '); dst.append(' ');
} }
copyContent(buf, cursor, delimiters, dst); copyContent(buf, cursor, delimiters, dst);
@ -97,11 +105,11 @@ class TokenParser {
} }
/** /**
* Extracts from the sequence of bytes a value which can be enclosed in quote marks and * Extracts from the sequence of chars a value which can be enclosed in quote marks and
* terminated with any of the given delimiters discarding semantically insignificant * terminated with any of the given delimiters discarding semantically insignificant
* whitespace characters. * whitespace characters.
* *
* @param buf buffer with the sequence of bytes to be parsed * @param buf buffer with the sequence of chars to be parsed
* @param cursor defines the bounds and current position of the buffer * @param cursor defines the bounds and current position of the buffer
* @param delimiters set of delimiting characters. Can be <code>null</code> if the value * @param delimiters set of delimiting characters. Can be <code>null</code> if the value
* is not delimited by any character. * is not delimited by any character.
@ -116,14 +124,14 @@ class TokenParser {
} else if (isWhitespace(current)) { } else if (isWhitespace(current)) {
skipWhiteSpace(buf, cursor); skipWhiteSpace(buf, cursor);
whitespace = true; whitespace = true;
} else if (current == '\"') { } else if (current == DQUOTE) {
if (dst.length() > 0 && whitespace) { if (whitespace && dst.length() > 0) {
dst.append(' '); dst.append(' ');
} }
copyQuotedContent(buf, cursor, dst); copyQuotedContent(buf, cursor, dst);
whitespace = false; whitespace = false;
} else { } else {
if (dst.length() > 0 && whitespace) { if (whitespace && dst.length() > 0) {
dst.append(' '); dst.append(' ');
} }
copyUnquotedContent(buf, cursor, delimiters, dst); copyUnquotedContent(buf, cursor, delimiters, dst);
@ -137,7 +145,7 @@ class TokenParser {
* Skips semantically insignificant whitespace characters and moves the cursor to the closest * Skips semantically insignificant whitespace characters and moves the cursor to the closest
* non-whitespace character. * non-whitespace character.
* *
* @param buf buffer with the sequence of bytes to be parsed * @param buf buffer with the sequence of chars to be parsed
* @param cursor defines the bounds and current position of the buffer * @param cursor defines the bounds and current position of the buffer
*/ */
public void skipWhiteSpace(final CharArrayBuffer buf, final ParserCursor cursor) { public void skipWhiteSpace(final CharArrayBuffer buf, final ParserCursor cursor) {
@ -159,7 +167,7 @@ class TokenParser {
* Transfers content into the destination buffer until a whitespace character or any of * Transfers content into the destination buffer until a whitespace character or any of
* the given delimiters is encountered. * the given delimiters is encountered.
* *
* @param buf buffer with the sequence of bytes to be parsed * @param buf buffer with the sequence of chars to be parsed
* @param cursor defines the bounds and current position of the buffer * @param cursor defines the bounds and current position of the buffer
* @param delimiters set of delimiting characters. Can be <code>null</code> if the value * @param delimiters set of delimiting characters. Can be <code>null</code> if the value
* is delimited by a whitespace only. * is delimited by a whitespace only.
@ -186,7 +194,7 @@ class TokenParser {
* Transfers content into the destination buffer until a whitespace character, a quote, * Transfers content into the destination buffer until a whitespace character, a quote,
* or any of the given delimiters is encountered. * or any of the given delimiters is encountered.
* *
* @param buf buffer with the sequence of bytes to be parsed * @param buf buffer with the sequence of chars to be parsed
* @param cursor defines the bounds and current position of the buffer * @param cursor defines the bounds and current position of the buffer
* @param delimiters set of delimiting characters. Can be <code>null</code> if the value * @param delimiters set of delimiting characters. Can be <code>null</code> if the value
* is delimited by a whitespace or a quote only. * is delimited by a whitespace or a quote only.
@ -200,7 +208,7 @@ class TokenParser {
for (int i = indexFrom; i < indexTo; i++) { for (int i = indexFrom; i < indexTo; i++) {
final char current = buf.charAt(i); final char current = buf.charAt(i);
if ((delimiters != null && delimiters.get(current)) if ((delimiters != null && delimiters.get(current))
|| isWhitespace(current) || current == '\"') { || isWhitespace(current) || current == DQUOTE) {
break; break;
} else { } else {
pos++; pos++;
@ -213,7 +221,7 @@ class TokenParser {
/** /**
* Transfers content enclosed with quote marks into the destination buffer. * Transfers content enclosed with quote marks into the destination buffer.
* *
* @param buf buffer with the sequence of bytes to be parsed * @param buf buffer with the sequence of chars to be parsed
* @param cursor defines the bounds and current position of the buffer * @param cursor defines the bounds and current position of the buffer
* @param dst destination buffer * @param dst destination buffer
*/ */
@ -226,7 +234,7 @@ class TokenParser {
int indexFrom = cursor.getPos(); int indexFrom = cursor.getPos();
final int indexTo = cursor.getUpperBound(); final int indexTo = cursor.getUpperBound();
char current = buf.charAt(pos); char current = buf.charAt(pos);
if (current != '\"') { if (current != DQUOTE) {
return; return;
} }
pos++; pos++;
@ -235,19 +243,19 @@ class TokenParser {
for (int i = indexFrom; i < indexTo; i++, pos++) { for (int i = indexFrom; i < indexTo; i++, pos++) {
current = buf.charAt(i); current = buf.charAt(i);
if (escaped) { if (escaped) {
if (current != '\"' && current != '\\') { if (current != DQUOTE && current != ESCAPE) {
dst.append('\\'); dst.append(ESCAPE);
} }
dst.append(current); dst.append(current);
escaped = false; escaped = false;
} else { } else {
if (current == '\"') { if (current == DQUOTE) {
pos++; pos++;
break; break;
} }
if (current == '\\') { if (current == ESCAPE) {
escaped = true; escaped = true;
} else if (current != '\r' && current != '\n') { } else if (current != CR && current != LF) {
dst.append(current); dst.append(current);
} }
} }