Example usage for java.lang CharSequence charAt

List of usage examples for java.lang CharSequence charAt

Introduction

In this page you can find the example usage for java.lang CharSequence charAt.

Prototype

char charAt(int index);

Source Link

Document

Returns the char value at the specified index.

Usage

From source file:org.xerela.net.sim.telnet.TelnetTest.java

private boolean compare(CharSequence one, CharSequence two) {
    int len = one.length();
    if (len != two.length()) {
        return false;
    }//w ww  . j av a 2  s  . c o  m

    for (int i = 0; i < len; i++) {
        if (one.charAt(i) != two.charAt(i)) {
            return false;
        }
    }
    return true;
}

From source file:org.apache.fop.fonts.GlyphMapping.java

private static GlyphMapping processWordMapping(TextFragment text, int startIndex, int endIndex, final Font font,
        final char breakOpportunityChar, final boolean endsWithHyphen, int level,
        boolean dontOptimizeForIdentityMapping, boolean retainAssociations, boolean retainControls) {
    int e = endIndex; // end index of word in FOText character buffer
    int nLS = 0; // # of letter spaces
    String script = text.getScript();
    String language = text.getLanguage();

    if (LOG.isDebugEnabled()) {
        LOG.debug("PW: [" + startIndex + "," + endIndex + "]: {" + " +M" + ", level = " + level + " }");
    }//from   w w  w. j  a v  a2s.c  om

    // 1. extract unmapped character sequence.
    CharSequence ics = text.subSequence(startIndex, e);

    // 2. if script is not specified (by FO property) or it is specified as 'auto',
    // then compute dominant script.
    if ((script == null) || "auto".equals(script)) {
        script = CharScript.scriptTagFromCode(CharScript.dominantScript(ics));
    }
    if ((language == null) || "none".equals(language)) {
        language = "dflt";
    }

    // 3. perform mapping of chars to glyphs ... to glyphs ... to chars, retaining
    // associations if requested.
    List associations = retainAssociations ? new java.util.ArrayList() : null;
    CharSequence mcs = font.performSubstitution(ics, script, language, associations, retainControls);

    // 4. compute glyph position adjustments on (substituted) characters.
    int[][] gpa = null;
    if (font.performsPositioning()) {
        // handle GPOS adjustments
        gpa = font.performPositioning(mcs, script, language);
    }
    if (useKerningAdjustments(font, script, language)) {
        // handle standard (non-GPOS) kerning adjustments
        gpa = getKerningAdjustments(mcs, font, gpa);
    }

    // 5. reorder combining marks so that they precede (within the mapped char sequence) the
    // base to which they are applied; N.B. position adjustments (gpa) are reordered in place.
    mcs = font.reorderCombiningMarks(mcs, gpa, script, language, associations);

    // 6. compute word ipd based on final position adjustments.
    MinOptMax ipd = MinOptMax.ZERO;
    for (int i = 0, n = mcs.length(); i < n; i++) {
        int c = mcs.charAt(i);
        // TODO !BMP
        int w = font.getCharWidth(c);
        if (w < 0) {
            w = 0;
        }
        if (gpa != null) {
            w += gpa[i][GlyphPositioningTable.Value.IDX_X_ADVANCE];
        }
        ipd = ipd.plus(w);
    }

    // [TBD] - handle letter spacing

    return new GlyphMapping(startIndex, e, 0, nLS, ipd, endsWithHyphen, false, breakOpportunityChar != 0, font,
            level, gpa,
            !dontOptimizeForIdentityMapping && CharUtilities.isSameSequence(mcs, ics) ? null : mcs.toString(),
            associations);
}

From source file:org.mariotaku.commons.emojione.AbsShortnameToUnicodeTranslator.java

/**
 * {@inheritDoc}//from   ww w .  j  av a  2  s.c om
 */
@Override
public int translate(final CharSequence input, final int index, final Writer out) throws IOException {
    final int inputLength = input.length();
    if (index + 1 >= inputLength)
        return 0;
    // check if translation exists for the input at position index
    if (':' == input.charAt(index) && prefixSet.contains(input.charAt(index + 1))) {
        int max = longest;
        if (index + longest > inputLength) {
            max = inputLength - index;
        }
        // implement greedy algorithm by trying maximum match first
        for (int i = max; i >= shortest; i--) {
            final CharSequence subSeq = input.subSequence(index, index + i);
            final String result = lookupMap.get(subSeq);

            if (result != null) {
                out.write(result);
                return i;
            }
        }
    }
    return 0;
}

From source file:org.renjin.parser.NumericLiterals.java

/**
 * Parses a real-valued number in decimal format.
 *
 * <p>This implementation is based on OpenJDK's {@code com.sun.misc.FloatingDecimal.readJavaFormatString}, but
 * adapted to allow for different decimal points, and to match R's allowed numeric formats. The original code
 * is copyright 1996, 2013, Oracle and/or its affiliates and licensed under the GPL v2.</p>
 *
 * @param in the input string/* w ww.j a va  2  s  .  com*/
 * @param sign the sign, -1 or +1, parsed above in {@link #parseDouble(CharSequence, int, int, char, boolean)}
 * @param startIndex the index at which to start parsing
 * @param endIndex the index, exclusive, at which to stop parsing
 * @param decimalPoint the decimal point character to use. Generally either '.' or ','
 * @return the number as a {@code double}, or {@code NA} if the string is malformatted.
 */
private static double parseDoubleDecimal(CharSequence in, int sign, int startIndex, int endIndex,
        char decimalPoint) {

    char[] digits = new char[endIndex - startIndex];
    int nDigits = 0;
    boolean decSeen = false;
    int decPt = 0;
    int nLeadZero = 0;
    int nTrailZero = 0;
    int i = startIndex;

    char c;

    skipLeadingZerosLoop: while (i < endIndex) {
        c = in.charAt(i);
        if (c == '0') {
            nLeadZero++;
        } else if (c == decimalPoint) {
            if (decSeen) {
                // already saw one ., this is the 2nd.
                return DoubleVector.NA;
            }
            decPt = i - startIndex;
            decSeen = true;
        } else {
            break skipLeadingZerosLoop;
        }
        i++;
    }

    digitLoop: while (i < endIndex) {
        c = in.charAt(i);
        if (c >= '1' && c <= '9') {
            digits[nDigits++] = c;
            nTrailZero = 0;
        } else if (c == '0') {
            digits[nDigits++] = c;
            nTrailZero++;
        } else if (c == decimalPoint) {
            if (decSeen) {
                // already saw one ., this is the 2nd.
                return DoubleVector.NA;
            }
            decPt = i - startIndex;
            decSeen = true;
        } else {
            break digitLoop;
        }
        i++;
    }
    nDigits -= nTrailZero;
    //
    // At this point, we've scanned all the digits and decimal
    // point we're going to see. Trim off leading and trailing
    // zeros, which will just confuse us later, and adjust
    // our initial decimal exponent accordingly.
    // To review:
    // we have seen i total characters.
    // nLeadZero of them were zeros before any other digits.
    // nTrailZero of them were zeros after any other digits.
    // if ( decSeen ), then a . was seen after decPt characters
    // ( including leading zeros which have been discarded )
    // nDigits characters were neither lead nor trailing
    // zeros, nor point
    //
    //
    // special hack: if we saw no non-zero digits, then the
    // answer is zero!
    // Unfortunately, we feel honor-bound to keep parsing!
    //
    boolean isZero = (nDigits == 0);
    if (isZero && nLeadZero == 0) {
        // we saw NO DIGITS AT ALL,
        // not even a crummy 0!
        // this is not allowed.
        return DoubleVector.NA;
    }
    //
    // Our initial exponent is decPt, adjusted by the number of
    // discarded zeros. Or, if there was no decPt,
    // then its just nDigits adjusted by discarded trailing zeros.
    //
    int decExp;
    if (decSeen) {
        decExp = decPt - nLeadZero;
    } else {
        decExp = nDigits + nTrailZero;
    }

    //
    // Look for 'e' or 'E' and an optionally signed integer.
    //
    if ((i < endIndex) && (((c = in.charAt(i)) == 'e') || (c == 'E'))) {
        int expSign = 1;
        int expVal = 0;
        int reallyBig = Integer.MAX_VALUE / 10;
        boolean expOverflow = false;
        switch (in.charAt(++i)) {
        case '-':
            expSign = -1;
            //FALLTHROUGH
        case '+':
            i++;
        }
        int expAt = i;
        expLoop: while (i < endIndex) {
            if (expVal >= reallyBig) {
                // the next character will cause integer
                // overflow.
                expOverflow = true;
            }
            c = in.charAt(i++);
            if (c >= '0' && c <= '9') {
                expVal = expVal * 10 + ((int) c - (int) '0');
            } else {
                i--; // back up.
                break expLoop; // stop parsing exponent.
            }
        }
        int expLimit = BIG_DECIMAL_EXPONENT + nDigits + nTrailZero;
        if (expOverflow || (expVal > expLimit)) {
            //
            // The intent here is to end up with
            // infinity or zero, as appropriate.
            // The reason for yielding such a small decExponent,
            // rather than something intuitive such as
            // expSign*Integer.MAX_VALUE, is that this value
            // is subject to further manipulation in
            // doubleValue() and floatValue(), and I don't want
            // it to be able to cause overflow there!
            // (The only way we can get into trouble here is for
            // really outrageous nDigits+nTrailZero, such as 2 billion. )
            //
            decExp = expSign * expLimit;
        } else {
            // this should not overflow, since we tested
            // for expVal > (MAX+N), where N >= abs(decExp)
            decExp = decExp + expSign * expVal;
        }

        // if we saw something not a digit ( or end of string )
        // after the [Ee][+-], without seeing any digits at all
        // this is certainly an error. If we saw some digits,
        // but then some trailing garbage, that might be ok.
        // so we just fall through in that case.
        // HUMBUG
        if (i == expAt) {
            return DoubleVector.NA; // certainly bad
        }
    }
    //
    // We parsed everything we could.
    // If there are leftovers, then this is not good input!
    //
    if (i < endIndex) {
        // Not a number...
        return DoubleVector.NA;
    }
    if (isZero) {
        return sign * 0d;
    }
    return doubleValue((sign < 0), decExp, digits, nDigits);
}

From source file:CSVParser.java

/**
 * precondition: sb.length() > 0/*from  w w w. j ava2  s  .c o m*/
 * @param sb A sequence of characters to examine
 * @return true if every character in the sequence is whitespace
 */
protected boolean isAllWhiteSpace(CharSequence sb) {
    boolean result = true;
    for (int i = 0; i < sb.length(); i++) {
        char c = sb.charAt(i);

        if (!Character.isWhitespace(c)) {
            return false;
        }
    }
    return result;
}

From source file:org.miaowo.miaowo.util.Html.java

private static void withinStyle(StringBuilder out, CharSequence text, int start, int end) {
    for (int i = start; i < end; i++) {
        char c = text.charAt(i);

        if (c == '<') {
            out.append("&lt;");
        } else if (c == '>') {
            out.append("&gt;");
        } else if (c == '&') {
            out.append("&amp;");
        } else if (c >= 0xD800 && c <= 0xDFFF) {
            if (c < 0xDC00 && i + 1 < end) {
                char d = text.charAt(i + 1);
                if (d >= 0xDC00 && d <= 0xDFFF) {
                    i++;/*from w  ww .ja  v  a2  s.  co m*/
                    int codepoint = 0x010000 | (int) c - 0xD800 << 10 | (int) d - 0xDC00;
                    out.append("&#").append(codepoint).append(";");
                }
            }
        } else if (c > 0x7E || c < ' ') {
            out.append("&#").append((int) c).append(";");
        } else if (c == ' ') {
            while (i + 1 < end && text.charAt(i + 1) == ' ') {
                out.append("&nbsp;");
                i++;
            }

            out.append(' ');
        } else {
            out.append(c);
        }
    }
}

From source file:fr.landel.utils.commons.StringUtils.java

/**
 * Converts the char sequence in char array. For {@link String}, you should
 * use {@link String#toCharArray()}.//from  w  w  w  . j av a 2s. c  o  m
 * 
 * @param sequence
 *            the input sequence
 * @return the array
 */
public static char[] toChars(final CharSequence sequence) {
    Objects.requireNonNull(sequence, ERROR_SEQUENCE);

    final int length = sequence.length();
    char[] chars = new char[length];
    for (int i = 0; i < length; i++) {
        chars[i] = sequence.charAt(i);
    }
    return chars;
}

From source file:org.sipfoundry.sipxconfig.bulk.csv.CsvParserImpl.java

protected String[] parseLine(CharSequence line) {
    ArrayList row = new ArrayList(DEFAULT_FIELD_COUNT);

    boolean inQuotedField = false;
    int fieldStart = 0;

    final int len = line.length();
    for (int i = 0; i < len; i++) {
        char c = line.charAt(i);
        if (c == FIELD_SEPARATOR) {
            if (!inQuotedField) {
                // ignore we are quoting
                addField(row, line, fieldStart, i);
                fieldStart = i + 1;//  ww  w.j  a v a 2  s.  c o m
            }
        } else if (c == FIELD_QUOTE) {
            if (inQuotedField) {
                // we are already quoting - peek to see if this is the end of the field
                if (i + 1 == len || line.charAt(i + 1) == FIELD_SEPARATOR) {
                    addField(row, line, fieldStart, i);
                    fieldStart = i + 2;
                    // and skip the comma
                    i++;
                    inQuotedField = false;
                }
            } else if (fieldStart == i) {
                // this is a beginning of a quote
                inQuotedField = true;
                // move field start
                fieldStart++;
            }
        }
    }
    // add last field - but only if string was not empty
    if (len > 0 && fieldStart <= len) {
        addField(row, line, fieldStart, len);
    }

    return (String[]) row.toArray(new String[row.size()]);
}

From source file:edu.chalmers.dat076.moviefinder.service.TitleParser.java

public int checkForYear(CharSequence cs) {
    int year = 0;
    if (cs.length() == 4) {

        for (int i = 0; i < 4; i++) {
            if (Character.isDigit(cs.charAt(i))) {
                year = year * 10;/* ww w  . j av  a 2s.c o m*/
                year = year + Character.getNumericValue(cs.charAt(i));
            } else {
                return -1;
            }
        }
    }
    return year;
}

From source file:org.batoo.common.util.StringUtils.java

/**
 * Test whether the given string matches the given substring at the given index.
 * /*from   ww w.j  ava  2s. c o m*/
 * @param str
 *            the original string (or StringBuilder)
 * @param index
 *            the index in the original string to start matching against
 * @param substring
 *            the substring to match at the given index
 * @return <code>true</code> if the given string matches the given substring at the given index, <code>false</code> otherwise
 */
public static boolean substringMatch(CharSequence str, int index, CharSequence substring) {
    for (int j = 0; j < substring.length(); j++) {
        final int i = index + j;
        if ((i >= str.length()) || (str.charAt(i) != substring.charAt(j))) {
            return false;
        }
    }
    return true;
}