Java Code Examples for java.lang.CharSequence#length()

The following examples show how to use java.lang.CharSequence#length() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UnitFormat.java    From microMathematics with GNU General Public License v3.0 6 votes vote down vote up
private long readLong (CharSequence csq, ParsePosition pos) {
    final int length = csq.length();
    int result = 0;
    boolean isNegative = false;
    while (pos.getIndex() < length) {
        char c = csq.charAt(pos.getIndex());
        if (c == '-') {
            isNegative = true;
        } else if ((c >= '0') && (c <= '9')) {
            result = result * 10 + (c - '0');
        } else {
            break;
        }
        pos.setIndex(pos.getIndex()+1);
    }
    return isNegative ? -result : result;
}
 
Example 2
Source File: UnitFormat.java    From microMathematics with GNU General Public License v3.0 5 votes vote down vote up
private int nextToken(CharSequence csq, ParsePosition pos) {
    final int length = csq.length();
    while (pos.getIndex() < length) {
        char c = csq.charAt(pos.getIndex());
        if (isUnitIdentifierPart(c)) {
            return IDENTIFIER;
        } else if (c == '(') {
            return OPEN_PAREN;
        } else if (c == ')') {
            return CLOSE_PAREN;
        } else if ((c == '^') || (c == '¹') || (c == '²') || (c == '³')) {
            return EXPONENT;
        } else if (c == '*') {
            char c2 = csq.charAt(pos.getIndex() + 1);
            if (c2 == '*') {
                return EXPONENT;
            } else {
                return MULTIPLY;
            }
        } else if (c == '·') {
            return MULTIPLY;
        } else if (c == '/') {
            return DIVIDE;
        } else if (c == '+') {
            return PLUS;
        } else if ((c == '-') || Character.isDigit(c)) {
            int index = pos.getIndex()+1;
            while ((index < length) && 
                   (Character.isDigit(c) || (c == '-') || (c == '.') || (c == 'E'))) {
                c = csq.charAt(index++);
                if (c == '.') {
                    return FLOAT;
                }
            }
            return INTEGER;
        }
        pos.setIndex(pos.getIndex() + 1);
    }
    return EOF;
}
 
Example 3
Source File: UnitFormat.java    From microMathematics with GNU General Public License v3.0 5 votes vote down vote up
private double readDouble (CharSequence csq, ParsePosition pos) {
    final int length = csq.length();
    int start = pos.getIndex();
    int end = start+1;
    while (end < length) {
        if ("012356789+-.E".indexOf(csq.charAt(end)) < 0) {
            break;
        }
        end += 1;
    }
    pos.setIndex(end+1);
    return Double.parseDouble(csq.subSequence(start,end).toString());
}
 
Example 4
Source File: UnitFormat.java    From microMathematics with GNU General Public License v3.0 5 votes vote down vote up
private String readIdentifier(CharSequence csq, ParsePosition pos) {
    final int length = csq.length();
    int start = pos.getIndex();
    int i = start;
    while ((++i < length) && isUnitIdentifierPart(csq.charAt(i))) { }
    pos.setIndex(i);
    return csq.subSequence(start, i).toString();
}
 
Example 5
Source File: NixParserUtil.java    From nix-idea with Apache License 2.0 5 votes vote down vote up
public static int indentationLevel(CharSequence c) {
    int i = 0;
    while(i < c.length() && c.charAt(i) == ' ') {
        i++;
    }
    return i;
}
 
Example 6
Source File: UnitFormat.java    From microMathematics with GNU General Public License v3.0 4 votes vote down vote up
private Exponent readExponent (CharSequence csq, ParsePosition pos) {
    char c = csq.charAt(pos.getIndex());
    if (c == '^') {
        pos.setIndex(pos.getIndex()+1);
    } else if (c == '*') {
        pos.setIndex(pos.getIndex()+2);
    }
    final int length = csq.length();
    int pow = 0;
    boolean isPowNegative = false;
    int root = 0;
    boolean isRootNegative = false;
    boolean isRoot = false;
    while (pos.getIndex() < length) {
        c = csq.charAt(pos.getIndex());
        if (c == '¹') {
            if (isRoot) {
                root = root * 10 + 1;
            } else {
                pow = pow * 10 + 1;
            }
        } else if (c == '²') {
            if (isRoot) {
                root = root * 10 + 2;
            } else {
                pow = pow * 10 + 2;
            }
        } else if (c == '³') {
            if (isRoot) {
                root = root * 10 + 3;
            } else {
                pow = pow * 10 + 3;
            }
        } else if (c == '-') {
            if (isRoot) {
                isRootNegative = true;
            } else {
                isPowNegative = true;
            }
        } else if ((c >= '0') && (c <= '9')) {
            if (isRoot) {
                root = root * 10 + (c - '0');
            } else {
                pow = pow * 10 + (c - '0');
            }
        } else if (c == ':') {
            isRoot = true;
        } else {
            break;
        }
        pos.setIndex(pos.getIndex()+1);
    }
    if (pow == 0) pow = 1;
    if (root == 0) root = 1;
    return new Exponent(isPowNegative ? -pow : pow, 
                      isRootNegative ? -root : root);
}