Example usage for java.text StringCharacterIterator getIndex

List of usage examples for java.text StringCharacterIterator getIndex

Introduction

In this page you can find the example usage for java.text StringCharacterIterator getIndex.

Prototype

public int getIndex() 

Source Link

Document

Implements CharacterIterator.getIndex() for String.

Usage

From source file:com.alfaariss.oa.util.web.RemoteAddrFilter.java

private static boolean matchWildcard(String s, String sMask) {
    //check empty string
    if (s.length() == 0) {
        if (sMask.length() == 0 || sMask.equals("*") || sMask.equals("?"))
            return true;
        return false;
    }/* ww w .  ja va2 s  . c o  m*/

    char ch;
    int i = 0;
    StringCharacterIterator iter = new StringCharacterIterator(sMask);

    for (ch = iter.first(); ch != StringCharacterIterator.DONE && i < s.length(); ch = iter.next()) {
        if (ch == '?')
            i++;
        else if (ch == '*') {
            int j = iter.getIndex() + 1;
            if (j >= sMask.length())
                return true;
            String sSubFilter = sMask.substring(j);
            while (i < s.length()) {
                if (matchWildcard(s.substring(i), sSubFilter))
                    return true;
                i++;
            }
            return false;
        } else if (ch == s.charAt(i)) {
            i++;
        } else
            return false;
    }
    return (i == s.length());
}

From source file:ar.com.tadp.xml.rinzo.core.model.XMLNode.java

/**
 * Devuelve el String sobre el que est posicionado el cursor
 *//*  w  ww .java2 s. c  o m*/
public String getStringAt(int offset) {
    int relativeOffset = offset - this.offset;
    int start = 0, end = 0;
    String content = this.getContent();
    StringCharacterIterator iter = new StringCharacterIterator(content);
    char c;

    for (c = iter.setIndex(relativeOffset); c != CharacterIterator.DONE
            && this.isFullIdentifierPart(c); c = iter.previous()) {
    }
    start = this.isFullIdentifierPart(iter.current()) ? iter.getIndex() : iter.getIndex() + 1;

    for (c = iter.setIndex(relativeOffset); c != CharacterIterator.DONE
            && this.isFullIdentifierPart(c); c = iter.next()) {
    }
    end = iter.getIndex();

    return (start <= end) ? content.substring(start, end) : "";
}

From source file:Unsigned.java

/**
 * Parse a binary number into a Number object. If up to 8 bits are parsed,
 * returns a Byte. If more than 8 and up to 16 bits are parsed, return a
 * Short. If more than 16 and up to 32 bits are parsed, return an Integer.
 * If more than 32 and up to 64 bits are parsed, return a Long.
 * // w w w  .j av a 2 s. co m
 * @param text
 *            a binary number
 * @param parsePosition
 *            position to start parsing from
 * @return return an integer form of Number object if parse is successful;
 *         <CODE>null</CODE> otherwise
 * 
 * @since 1.0
 */
public Number parse(String text, ParsePosition parsePosition) {
    boolean skipWhitespace = true;
    int startIndex, bits;

    // remove whitespace
    StringCharacterIterator iter = new StringCharacterIterator(text, parsePosition.getIndex());
    for (char c = iter.current(); c != CharacterIterator.DONE; c = iter.next()) {
        if (skipWhitespace && Character.isWhitespace(c)) {
            // skip whitespace
            continue;
        }
    }
    parsePosition.setIndex(iter.getIndex());

    startIndex = parsePosition.getIndex();
    Number result = (Number) parseObject(text, parsePosition);

    if (result == null) {
        return (result);
    }

    bits = parsePosition.getIndex() - startIndex;
    if (bits <= 8) {
        result = new Byte(result.byteValue());
    } else if (bits <= 16) {
        result = new Short(result.shortValue());
    } else if (bits <= 32) {
        result = new Integer(result.intValue());
    } else if (bits <= 64) {
        result = new Long(result.longValue());
    }
    return (result);
}

From source file:HexFormat.java

/**
 * Parse a hex number into a Number object. Hexadecimal numbers may be
 * indicated with a leading character designation of '0x'. If up to 1 byte
 * is parsed, returns a Byte. If more than 1 and up to 2 bytes are parsed,
 * return a Short. If more than 2 and up to 4 bytes are parsed, return an
 * Integer. If more than 4 and up to 8 bytes are parsed, return a Long.
 * /*ww w . j av a 2 s. c o  m*/
 * @param text
 *            a hexadecimal number
 * @param parsePosition
 *            position to start parsing from
 * @return return an integer form of Number object if parse is successful;
 *         <CODE>null</CODE> otherwise
 * 
 * @since 1.0
 */
public Number parse(String text, ParsePosition parsePosition) {
    boolean skipWhitespace = true;
    int startIndex, nibbles;

    // remove whitespace
    StringCharacterIterator iter = new StringCharacterIterator(text, parsePosition.getIndex());
    for (char c = iter.current(); c != CharacterIterator.DONE; c = iter.next()) {
        if (skipWhitespace && Character.isWhitespace(c)) {
            // skip whitespace
            continue;
        }
        break;
    }

    // skip a leading hex designation of the characters '0x'
    if (text.regionMatches(iter.getIndex(), "0x", 0, 2)) {
        parsePosition.setIndex(iter.getIndex() + 2);
    } else {
        parsePosition.setIndex(iter.getIndex());
    }

    startIndex = parsePosition.getIndex();
    Number result = (Number) parseObject(text, parsePosition);

    if (result == null) {
        return (result);
    }

    nibbles = parsePosition.getIndex() - startIndex;
    if (nibbles <= 2) {
        result = new Byte(result.byteValue());
    } else if (nibbles <= 4) {
        result = new Short(result.shortValue());
    } else if (nibbles <= 8) {
        result = new Integer(result.intValue());
    } else if (nibbles <= 16) {
        result = new Long(result.longValue());
    }
    return (result);
}

From source file:Unsigned.java

/**
 * Parse a binary number, skipping leading whitespace. Does not throw an
 * exception; if no object can be parsed, index is unchanged!
 * //from  www .  j  a  v a  2s . c o  m
 * @param source
 *            the string to parse
 * @param status
 *            the string index to start at
 * @return The binary number as a Long object.
 * 
 * @since 1.0
 */
public Object parseObject(String source, ParsePosition status) {
    int start = status.getIndex();
    boolean success = false;
    boolean skipWhitespace = true;
    StringBuffer buffer = new StringBuffer();

    StringCharacterIterator iter = new StringCharacterIterator(source, start);

    for (char c = iter.current(); c != CharacterIterator.DONE; c = iter.next()) {
        if (skipWhitespace && Character.isWhitespace(c)) {
            // skip whitespace
            continue;
        }
        skipWhitespace = false;

        if ((c == '1') || (c == '0')) {
            success = true;
            buffer.append(c);
        } else {
            break;
        }
    }

    if (!success) {
        return (null);
    }

    // convert binary to long
    if (buffer.length() > 64) {
        // larger than a long, error
        return (null);
    }

    long result = 0;
    buffer.reverse();
    int length = buffer.length();
    for (int i = 0; i < length; i++) {
        result += (buffer.charAt(i) == '1') ? 1 << i : 0;
    }
    status.setIndex(iter.getIndex());
    return (new Long(result));
}

From source file:HexFormat.java

/**
 * Parse a hexadecimal number, skipping leading whitespace. Does not throw
 * an exception; if no object can be parsed, index is unchanged! Hexadecimal
 * numbers may be indicated with a leading character designation of '0x'.
 * //  w ww  .j av  a  2  s  . c o  m
 * @param source
 *            the string to parse
 * @param status
 *            the string index to start at
 * @return The hexadecimal number as a Long object.
 * 
 * @since 1.0
 */
public Object parseObject(String source, ParsePosition status) {
    int start = status.getIndex();
    boolean success = false;
    StringBuffer buffer = new StringBuffer();
    char c, c2;
    long result;

    StringCharacterIterator iter = new StringCharacterIterator(source, start);

    for (c = iter.current(); c != CharacterIterator.DONE; c = iter.next()) {
        if (Character.isWhitespace(c)) {
            // skip whitespace
            continue;
        }
        break;
    }

    if (c == CharacterIterator.DONE) {
        return (null);
    }

    if (c == '0') {
        c2 = iter.next();

        if (c2 == CharacterIterator.DONE) {
            return (null);
        }

        if (c2 == 'x') {
            // has a leading '0x' designation, so skip over it
        } else {
            // replace the two characters
            iter.previous();
            iter.previous();
        }
    } else {
        // skip back one character
        iter.previous();
    }

    // gather valid hex digits
    for (c = iter.next(); c != CharacterIterator.DONE; c = iter.next()) {
        if (hexDigits.indexOf(c) != -1) {
            success = true;
            buffer.append(c);
        } else {
            break;
        }
    }

    if (!success) {
        // no valid hex digits
        return (null);
    }

    // convert hex to long
    if (buffer.length() > 16) {
        // larger than a long, error
        // with a buffer full of nibbles, the maximum nibbles in a
        // 64 bit number is 16 nibbles
        return (null);
    }

    // parse number
    try {
        result = Long.parseLong(buffer.toString(), 16);
    } catch (NumberFormatException e) {
        // unable to parse number
        return (null);
    }

    status.setIndex(iter.getIndex());
    return (new Long(result));
}