[Advanced-java] Coding String.indexOf() with ignore quoted
Carlo
carlo.ravagli at extropia.com
Thu Apr 3 02:24:27 2003
----- Original Message -----
From: "Nikolaos Giannopoulos" <nikolaos@solmar.ca>
To: <advanced-java@lists.xcf.berkeley.edu>
Sent: Thursday, April 03, 2003 7:18 AM
Subject: [Advanced-java] Coding String.indexOf() with ignore quoted
> I coded a method similar to String.indexOf(char) except that it skips over
> regions that are bound by matching quotes. The code is below.
>
> However, I'm not too fond of the fact that this implementation contains
> method calls to get each char. I imagine that I could further improve the
> current version by grabbing a chars array of the string and operate on it
> directly.
Here is Sun J2SE 1.4.1 code for getting a char from a string:
public char charAt(int index) {
if ((index < 0) || (index >= count)) {
throw new StringIndexOutOfBoundsException(index);
}
return value[index + offset];
}
The hotspot/JIT compliers will probably perform some optimizations
similar to the C/C++ complier "inline" feature to remove the actual
method call from inside your loop. Therefore, there is not much more to
optimize except removing the index out of bounds check.
If you were to get the String's underlying char array (using
getCharArray()), a new buffer would have to allocated and then the
contents copied over. This may be more expensive (depending on the
length of your string) than just doing what you are already doing.
Anyway, not sure what your entire program is doing (or if you have already
done this) but if I were you I would not spend time speculating over the
performance of this one function but rather look at the entire execution of
your program using a profiling tool. This should give you a better idea of
were your program needs optimization. OptimizeIt is one such tool I have
used in the past and it seemed to do the job OK.
>
> The alternate implementation that I considered was to use indexOf to
> determine positions of quote chars but since I want to handle both single
> and double quotes this seemed kind of wasteful (i.e. using indexOf twice
at
> each position) and more complex.
>
> Assuming that the data is evenly distributed - Which approach is better in
> terms of general performance?
>
> Is there another better approach (that I am not considering)?
>
> public static final int indexOfIgnoreQuoted(String s, char ch, int
> fromIndex) {
> int i, j = s.length();
> char c;
>
> if (fromIndex < 0) {
> fromIndex = 0;
> }
> if (fromIndex < j) {
> i = fromIndex;
> while (i < j) {
> c = s.charAt(i);
> switch (c) {
> case '"':
> case '\'':
> i++;
> while (i < j) {
> if (s.charAt(i++) == c)
> break;
> }
> break;
> default:
> if (c == ch)
> return (i);
> i++;
> }
> }
> }
> return (-1);
> }
>
> --Nikolaos
>
>
>
> _______________________________________________
> Advanced-java mailing list
> Advanced-java@lists.xcf.berkeley.edu
> http://lists.xcf.berkeley.edu/mailman/listinfo/advanced-java
>
>