Skip to content

Commit

Permalink
PDFBOX-5660: refactor usage of isEndOfName() + use switch + use stati…
Browse files Browse the repository at this point in the history
…c where possible, by Axel Howind

git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1914661 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
THausherr committed Dec 14, 2023
1 parent 2ca2b86 commit 1968e01
Showing 2 changed files with 38 additions and 14 deletions.
50 changes: 37 additions & 13 deletions pdfbox/src/main/java/org/apache/pdfbox/pdfparser/BaseParser.java
Original file line number Diff line number Diff line change
@@ -758,9 +758,27 @@ protected COSArray parseCOSArray() throws IOException
*/
protected boolean isEndOfName(int ch)
{
return ch == ASCII_SPACE || ch == ASCII_CR || ch == ASCII_LF || ch == 9 || ch == '>' ||
ch == '<' || ch == '[' || ch =='/' || ch ==']' || ch ==')' || ch =='(' ||
ch == 0 || ch == '\f' || ch == '%';
switch (ch)
{
case ASCII_SPACE:
case ASCII_CR:
case ASCII_LF:
case 9:
case '>':
case '<':
case '[':
case '/':
case ']':
case ')':
case '(':
case 0:
case '\f':
case '%':
case -1:
return true;
default:
return false;
}
}

/**
@@ -774,9 +792,9 @@ protected COSName parseCOSName() throws IOException
readExpectedChar('/');
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int c = source.read();
while (c != -1)
while (!isEndOfName(c))
{
int ch = c;
final int ch = c;
if (ch == '#')
{
int ch1 = source.read();
@@ -814,10 +832,6 @@ protected COSName parseCOSName() throws IOException
buffer.write(ch);
}
}
else if (isEndOfName(ch))
{
break;
}
else
{
buffer.write(ch);
@@ -967,7 +981,7 @@ protected String readString() throws IOException
skipSpaces();
StringBuilder buffer = new StringBuilder();
int c = source.read();
while( !isEndOfName((char)c) && c != -1 )
while (!isEndOfName(c))
{
buffer.append( (char)c );
c = source.read();
@@ -1174,10 +1188,20 @@ protected boolean isWhitespace() throws IOException
* @param c The character to check against whitespace
* @return true if the character is a whitespace character.
*/
protected boolean isWhitespace( int c )
protected static boolean isWhitespace( int c )
{
return c == 0 || c == 9 || c == 12 || c == ASCII_LF
|| c == ASCII_CR || c == ASCII_SPACE;
switch (c)
{
case 0:
case 9:
case 12:
case ASCII_LF:
case ASCII_CR:
case ASCII_SPACE:
return true;
default:
return false;
}
}

/**
Original file line number Diff line number Diff line change
@@ -1757,7 +1757,7 @@ protected boolean parseXrefTable(long startByteOffset) throws IOException
skipSpaces();
for(int i = 0; i < count; i++)
{
if(source.isEOF() || isEndOfName((char)source.peek()))
if (source.isEOF() || isEndOfName(source.peek()))
{
break;
}

0 comments on commit 1968e01

Please sign in to comment.