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.apache.rat.report.xml.writer.impl.base.XmlWriter.java

private void rawWrite(final CharSequence sequence) throws IOException {
    for (int i = 0; i < sequence.length(); i++) {
        final char charAt = sequence.charAt(i);
        writer.write(charAt);/*from w  ww.j  a  v a2 s.com*/
    }
}

From source file:org.cosmo.common.util.Util.java

public static byte[] UTF8(CharSequence str) {

    if (str.length() == 0) {
        return Constants.EmptyByteMarker;
    }//from   w  w w.  j a  va 2 s .  c o  m

    int strlen = str.length();
    int utflen = 0;
    int c, count = 0;

    /* use charAt instead of copying String to char array */
    for (int i = 0; i < strlen; i++) {
        c = str.charAt(i);
        if ((c >= 0x0001) && (c <= 0x007F)) {
            utflen++;
        } else if (c > 0x07FF) {
            utflen += 3;
        } else {
            utflen += 2;
        }
    }

    byte[] bytearr = new byte[utflen];

    int i = 0;
    for (i = 0; i < strlen; i++) {
        c = str.charAt(i);
        if (!((c >= 0x0001) && (c <= 0x007F)))
            break;
        bytearr[count++] = (byte) c;
    }

    for (; i < strlen; i++) {
        c = str.charAt(i);
        if ((c >= 0x0001) && (c <= 0x007F)) {
            bytearr[count++] = (byte) c;

        } else if (c > 0x07FF) {
            bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F));
            bytearr[count++] = (byte) (0x80 | ((c >> 6) & 0x3F));
            bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
        } else {
            bytearr[count++] = (byte) (0xC0 | ((c >> 6) & 0x1F));
            bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
        }
    }
    return bytearr;
}

From source file:fabrice.CSVPrinter.java

private void printAndEscape(final CharSequence value, final int offset, final int len) throws IOException {
    int start = offset;
    int pos = offset;
    final int end = offset + len;

    final char delim = format.getDelimiter();
    final char escape = format.getEscapeCharacter().charValue();

    while (pos < end) {
        char c = value.charAt(pos);
        if (c == CR || c == LF || c == delim || c == escape) {
            // write out segment up until this char
            if (pos > start) {
                out.append(value, start, pos);
            }/*  w  ww.  j  av  a  2s  .  c  o m*/
            if (c == LF) {
                c = 'n';
            } else if (c == CR) {
                c = 'r';
            }

            out.append(escape);
            out.append(c);

            start = pos + 1; // start on the current char after this one
        }

        pos++;
    }

    // write last segment
    if (pos > start) {
        out.append(value, start, pos);
    }
}

From source file:com.dwdesign.tweetings.adapter.UserAutoCompleteAdapter.java

@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
    if (mCursorClosed)
        return null;
    final FilterQueryProvider filter = getFilterQueryProvider();
    if (filter != null)
        return filter.runQuery(constraint);
    final StringBuilder where = new StringBuilder();
    constraint = constraint != null ? constraint.toString().replaceAll("_", "^_") : null;
    constraint = constraint != null ? constraint.toString().replaceAll("'", "") : null;
    isHash = false;/* ww  w .j a v a  2 s  . c o m*/
    if (constraint != null) {
        if (constraint.length() > 1 && constraint.charAt(0) == '@') {
            isHash = false;

            mLookupText = null;
            final String lookup = constraint.subSequence(1, constraint.length() - 1).toString();
            where.append(CachedUsers.SCREEN_NAME + " LIKE '" + lookup + "%' ESCAPE '^'");
            where.append(" OR ");
            where.append(CachedUsers.NAME + " LIKE '" + lookup + "%' ESCAPE '^'");
            return mResolver.query(CachedUsers.CONTENT_URI, CachedUsers.COLUMNS,
                    lookup != null ? where.toString() : null, null, null);
        } else if (constraint.length() > 0 && constraint.charAt(0) == '@') {
            isHash = false;
            mLookupText = null;

            return mResolver.query(CachedUsers.CONTENT_URI, CachedUsers.COLUMNS, null, null, null);
        } else if (constraint.length() > 1 && constraint.charAt(0) == '#') {
            isHash = true;
            mLookupText = constraint.toString();
            where.append(Statuses.TEXT_PLAIN + " LIKE '%" + constraint + "%' ESCAPE '^'");
            return mResolver.query(Statuses.CONTENT_URI, Statuses.COLUMNS,
                    constraint != null ? where.toString() : null, null, null);
        } else if (constraint.length() > 0 && constraint.charAt(0) == '#') {
            isHash = true;
            mLookupText = null;

            return mResolver.query(Statuses.CONTENT_URI, Statuses.COLUMNS, null, null, null);
        }
    }
    return null;
}

From source file:br.msf.commons.util.CharSequenceUtils.java

public static Map<Character, Integer> getCharCount(final int fromIndex, final CharSequence sequence,
        final Boolean caseSensitive) {
    ArgumentUtils.rejectIfOutOfBounds(fromIndex, 0, length(sequence) - 1);
    if (isEmptyOrNull(sequence)) {
        return Collections.EMPTY_MAP;
    }/* w w w .  j av  a2s. c o m*/
    final Map<Character, Integer> chars = new TreeMap<Character, Integer>();
    for (int i = fromIndex; i < length(sequence); i++) {
        Character ch = caseSensitive ? sequence.charAt(i) : Character.toLowerCase(sequence.charAt(i));
        Integer count = chars.get(ch);
        chars.put(ch, (count == null) ? 1 : count + 1);
    }
    return chars;
}

From source file:com.android.screenspeak.eventprocessor.ProcessorPhoneticLetters.java

/**
 * Handle an event that indicates a text is being traversed at character
 * granularity./* w ww  .j a  va2s .  c om*/
 */
private void processTraversalEvent(AccessibilityEvent event) {
    final CharSequence text = AccessibilityEventUtils.getEventTextOrDescription(event);
    if (TextUtils.isEmpty(text)) {
        return;
    }

    String letter;
    if ((event.getAction() == AccessibilityNodeInfoCompat.ACTION_NEXT_AT_MOVEMENT_GRANULARITY
            || event.getAction() == AccessibilityNodeInfoCompat.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY)
            && event.getFromIndex() >= 0 && event.getFromIndex() < text.length()) {
        letter = String.valueOf(text.charAt(event.getFromIndex()));
    } else {
        return;
    }

    String phoneticLetter = getPhoneticLetter(Locale.getDefault().toString(), letter);
    if (phoneticLetter != null) {
        postPhoneticLetterRunnable(phoneticLetter);
    }
}

From source file:it.unimi.dsi.util.ImmutableExternalPrefixMap.java

private boolean isEncodable(final CharSequence s) {
    for (int i = s.length(); i-- != 0;)
        if (!char2symbol.containsKey(s.charAt(i)))
            return false;
    return true;/*  w  w w.  j  a va  2 s  .  c  om*/
}

From source file:org.apache.rat.report.xml.writer.impl.base.XmlWriter.java

private boolean isValidName(final CharSequence sequence) {
    boolean result = true;
    final int length = sequence.length();
    for (int i = 0; i < length; i++) {
        char character = sequence.charAt(i);
        if (i == 0) {
            if (!isValidNameStart(character)) {
                result = false;/*w w  w  .  j  a va 2 s .  com*/
                break;
            }
        } else {
            if (!isValidNameBody(character)) {
                result = false;
                break;
            }
        }
    }
    return result;
}

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

/**
 * Map sequence CS, comprising a sequence of UTF-16 encoded Unicode Code Points, to
 * an output character sequence GS, comprising a sequence of Glyph Indices. N.B. Unlike
 * mapChar(), this method does not make use of embedded subset encodings.
 * @param cs a CharSequence containing UTF-16 encoded Unicode characters
 * @returns a CharSequence containing glyph indices
 *//*from w w  w  . ja va  2 s .  com*/
private GlyphSequence mapCharsToGlyphs(CharSequence cs) {
    IntBuffer cb = IntBuffer.allocate(cs.length());
    IntBuffer gb = IntBuffer.allocate(cs.length());
    int gi;
    int giMissing = findGlyphIndex(Typeface.NOT_FOUND);
    for (int i = 0, n = cs.length(); i < n; i++) {
        int cc = cs.charAt(i);
        if ((cc >= 0xD800) && (cc < 0xDC00)) {
            if ((i + 1) < n) {
                int sh = cc;
                int sl = cs.charAt(++i);
                if ((sl >= 0xDC00) && (sl < 0xE000)) {
                    cc = 0x10000 + ((sh - 0xD800) << 10) + ((sl - 0xDC00) << 0);
                } else {
                    throw new IllegalArgumentException(
                            "ill-formed UTF-16 sequence, " + "contains isolated high surrogate at index " + i);
                }
            } else {
                throw new IllegalArgumentException(
                        "ill-formed UTF-16 sequence, " + "contains isolated high surrogate at end of sequence");
            }
        } else if ((cc >= 0xDC00) && (cc < 0xE000)) {
            throw new IllegalArgumentException(
                    "ill-formed UTF-16 sequence, " + "contains isolated low surrogate at index " + i);
        }
        notifyMapOperation();
        gi = findGlyphIndex(cc);
        if (gi == SingleByteEncoding.NOT_FOUND_CODE_POINT) {
            warnMissingGlyph((char) cc);
            gi = giMissing;
        }
        cb.put(cc);
        gb.put(gi);
    }
    cb.flip();
    gb.flip();
    return new GlyphSequence(cb, gb, null);
}

From source file:org.apache.rat.report.xml.writer.impl.base.XmlWriter.java

private void writeEscaped(final CharSequence content, boolean isAttributeContent) throws IOException {
    final int length = content.length();
    for (int i = 0; i < length; i++) {
        char character = content.charAt(i);
        if (character == '&') {
            writer.write("&amp;");
        } else if (character == '<') {
            writer.write("&lt;");
        } else if (character == '>') {
            writer.write("&gt;");
        } else if (isAttributeContent && character == '\'') {
            writer.write("&apos;");
        } else if (isAttributeContent && character == '\"') {
            writer.write("&quot;");
        } else if (isOutOfRange(character)) {
            writer.write('?');
        } else {//from ww  w  . j  av  a2 s .  c  o  m
            writer.write(character);
        }
    }
}