#1 Optimization: No toCharArray() for whitespace-testing

open
nobody
None
5
2004-04-13
2004-04-13
No

There are some more unnecessary toCharArray() calls,
which should be substituted. E.x:

public void whitespace(Writer out, String s)
throws NullPointerException, InvalidXMLException,
IOException {

char[] ch = s.toCharArray();
int length = ch.length;
whitespace(out, ch, 0, length);
}

Why is'nt there a whitespace(out, STRING, 0, length)??
This method is calles a LOT of time.

This optimization is simple:

public void whitespace(Writer out, String s, int start,
int length)
throws NullPointerException,
IndexOutOfBoundsException, InvalidXMLException,
IOException {

// Check the string
XMLChecker.checkS(s, start, length);

// Write the complete character string at once
out.write(s, start, length);
}

OK. There is a out.write( s, start, len), but no
XMLChecker.checkS(string,start,len) now, but

public static final void checkS(String s)
throws NullPointerException {
checkS(s.toCharArray(), 0, s.length());
}

Another toCharArray! So I suggest

public static final void checkS(String s, int start, int
length)
throws NullPointerException,
IndexOutOfBoundsException, InvalidXMLException {

// Loop through the array and check each character
for (int i = start; i < s.length(); i++) {
int c = (int) ch.charAt(i);

if (c != 0x20 && c != 0x9 && c != 0xD && c !=
0xA) {
throw new InvalidXMLException("The character
0x" + Integer.toHexString(c) + " is not valid for the 'S'
production (white space).");
}
}
}

(Source not tested)

Bye,

Christian Ullenboom

Discussion


Log in to post a comment.