skip CharBuffer Whitespace - Java java.nio

Java examples for java.nio:CharBuffer

Description

skip CharBuffer Whitespace

Demo Code


import java.nio.CharBuffer;

public class Main{
    /**// w  w w .j  a  v  a2 s . com
     *
     * @return the index of the first non-whitespace char
     */
    public static int skipWhitespace(CharBuffer buf, int startInc,
            int limitExc) {
        // NB replacing i<limitEx with i-limitExc<0 (which is equivalent)
        // shaved 50ns per line of csv parsing; I would expect the compilers to
        // make this optimisation.. but as of jdk1.7.0_21 on mac osx, it does not.
        // -- and yet in other similar situations it makes things worse! I need
        //    to grab the assembler at some point and see what is happening.

        for (int i = startInc; i - limitExc < 0; i++) {
            if (!CharBufferUtils.isWhitespaceAt(i, buf)) {
                return i;
            }
        }

        return limitExc;
    }
    public static boolean isWhitespaceAt(int pos, CharBuffer buf) {
        char c = buf.get(pos);

        return c == ' ' || c == '\t';
    }
}

Related Tutorials