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:dollar.internal.runtime.script.parser.DollarLexer.java

@SuppressWarnings("AssignmentToForLoopParameter")
@NotNull//from   w  w w.j av a 2s. c  o  m
private static String tokenizeBackTick(@NotNull CharSequence text) {
    int end = text.length() - 1;
    StringBuilder buf = new StringBuilder();
    for (int i = 1; i < end; i++) {
        char c = text.charAt(i);
        if (c == '`') {
            buf.append('`');
            i++;
        } else {
            buf.append(c);
        }
    }
    return buf.toString();
}

From source file:com.prasanna.android.stacknetwork.utils.MarkdownFormatter.java

public static String escapeHtml(CharSequence text) {
    if (text == null)
        return null;

    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < text.length(); i++) {
        char c = text.charAt(i);

        if (c == '<')
            builder.append("&lt;");
        else if (c == '>')
            builder.append("&gt;");
        else if (c == '&')
            builder.append("&amp;");
        else//from  w  w  w  . ja  va  2s  . c o  m
            builder.append(c);

    }
    return builder.toString();
}

From source file:Main.java

/**
 * Green implementation of regionMatches.
 *
 * @param cs//  ww w. j a v a2s  .  co m
 *            the <code>CharSequence</code> to be processed
 * @param ignoreCase
 *            whether or not to be case insensitive
 * @param thisStart
 *            the index to start on the <code>cs</code> CharSequence
 * @param substring
 *            the <code>CharSequence</code> to be looked for
 * @param start
 *            the index to start on the <code>substring</code> CharSequence
 * @param length
 *            character length of the region
 * @return whether the region matched
 */
static boolean regionMatches(final CharSequence cs, final boolean ignoreCase, final int thisStart,
        final CharSequence substring, final int start, final int length) {
    if (cs instanceof String && substring instanceof String) {
        return ((String) cs).regionMatches(ignoreCase, thisStart, (String) substring, start, length);
    }
    int index1 = thisStart;
    int index2 = start;
    int tmpLen = length;

    while (tmpLen-- > 0) {
        char c1 = cs.charAt(index1++);
        char c2 = substring.charAt(index2++);

        if (c1 == c2) {
            continue;
        }

        if (!ignoreCase) {
            return false;
        }

        // The same check as in String.regionMatches():
        if (Character.toUpperCase(c1) != Character.toUpperCase(c2)
                && Character.toLowerCase(c1) != Character.toLowerCase(c2)) {
            return false;
        }
    }

    return true;
}

From source file:Main.java

/**
 * Green implementation of regionMatches.
 *
 * @param cs         the {@code CharSequence} to be processed
 * @param ignoreCase whether or not to be case insensitive
 * @param thisStart  the index to start on the {@code cs} CharSequence
 * @param substring  the {@code CharSequence} to be looked for
 * @param start      the index to start on the {@code substring} CharSequence
 * @param length     character length of the region
 * @return whether the region matched//  w w  w  .  j av  a2  s.  co m
 */
static boolean regionMatches(final CharSequence cs, final boolean ignoreCase, final int thisStart,
        final CharSequence substring, final int start, final int length) {
    if (cs instanceof String && substring instanceof String) {
        return ((String) cs).regionMatches(ignoreCase, thisStart, (String) substring, start, length);
    }
    int index1 = thisStart;
    int index2 = start;
    int tmpLen = length;

    while (tmpLen-- > 0) {
        final char c1 = cs.charAt(index1++);
        final char c2 = substring.charAt(index2++);

        if (c1 == c2) {
            continue;
        }

        if (!ignoreCase) {
            return false;
        }

        // The same check as in String.regionMatches():
        if (Character.toUpperCase(c1) != Character.toUpperCase(c2)
                && Character.toLowerCase(c1) != Character.toLowerCase(c2)) {
            return false;
        }
    }

    return true;
}

From source file:org.apache.bval.jsr.util.PathNavigation.java

private static void parse(CharSequence path, PathPosition pos) throws Exception {
    int len = path.length();
    boolean sep = true;
    while (pos.getIndex() < len) {
        int here = pos.getIndex();
        char c = path.charAt(here);
        switch (c) {
        case ']':
            throw new IllegalStateException(String.format("Position %s: unexpected '%s'", here, c));
        case '[':
            handleIndex(path, pos.next());
            break;
        case '.':
            if (sep) {
                throw new IllegalStateException(
                        String.format("Position %s: expected property, index/key, or end of expression", here));
            }/*  w  w w . j a va  2  s  . co m*/
            sep = true;
            pos.next();
            // fall through:
        default:
            if (!sep) {
                throw new IllegalStateException(String.format(
                        "Position %s: expected property path separator, index/key, or end of expression",
                        here));
            }
            pos.handleProperty(parseProperty(path, pos));
        }
        sep = false;
    }
}

From source file:org.jcurl.core.helpers.XmlSimpleWriter.java

public static void writeEncoded(final CharSequence src, final Writer target) throws SAXException {
    final int len = src.length();
    for (int i = 0; i < len; i++)
        writeEncoded(src.charAt(i), target);
}

From source file:dollar.internal.runtime.script.parser.DollarLexer.java

private static Parser<?> builtin() {
    //noinspection OverlyComplexAnonymousInnerClass
    return new Pattern() {
        @Override/*from www . j  av  a2  s. c o m*/
        public int match(@NotNull CharSequence src, int begin, int end) {
            int i = begin;
            //noinspection StatementWithEmptyBody
            while ((i < end) && isAlphabetic(src.charAt(i)))
                i++;
            final String name = src.subSequence(begin, i).toString();
            return exists(name) ? (i - begin) : Pattern.MISMATCH;
        }
    }.toScanner("builtin").source().map(new Function<String, Tokens.Fragment>() {
        @Override
        @NotNull
        public Tokens.Fragment apply(@NotNull String text) {
            return fragment(text, "builtin");
        }

        @NotNull
        @Override
        public String toString() {
            return "builtin";
        }
    });
}

From source file:it.unimi.di.big.mg4j.document.SimpleCompressedDocumentCollectionBuilder.java

public static int writeSelfDelimitedUtf8String(final OutputBitStream obs, final CharSequence s)
        throws IOException {
    final int len = s.length();
    int bits = 0;
    bits += obs.writeDelta(len);/*from  w  w  w  .  j  a  v  a2s  .  co m*/
    for (int i = 0; i < len; i++)
        bits += obs.writeZeta(s.charAt(i), 7);
    return bits;
}

From source file:Main.java

/**
 * Green implementation of regionMatches.
 *
 * @param cs the {@code CharSequence} to be processed
 * @param ignoreCase whether or not to be case insensitive
 * @param thisStart the index to start on the {@code cs} CharSequence
 * @param substring the {@code CharSequence} to be looked for
 * @param start the index to start on the {@code substring} CharSequence
 * @param length character length of the region
 * @return whether the region matched//from  w  w w  .  ja v a2 s  .c o m
 */
static boolean regionMatches(final CharSequence cs, final boolean ignoreCase, final int thisStart,
        final CharSequence substring, final int start, final int length) {
    if (cs instanceof String && substring instanceof String) {
        return ((String) cs).regionMatches(ignoreCase, thisStart, (String) substring, start, length);
    } else {
        int index1 = thisStart;
        int index2 = start;
        int tmpLen = length;

        while (tmpLen-- > 0) {
            char c1 = cs.charAt(index1++);
            char c2 = substring.charAt(index2++);

            if (c1 == c2) {
                continue;
            }

            if (!ignoreCase) {
                return false;
            }

            // The same check as in String.regionMatches():
            if (Character.toUpperCase(c1) != Character.toUpperCase(c2)
                    && Character.toLowerCase(c1) != Character.toLowerCase(c2)) {
                return false;
            }
        }

        return true;
    }
}

From source file:Main.java

public static int computeLevenshteinDistance(CharSequence str1, CharSequence str2) {
    int[][] distance = new int[str1.length() + 1][str2.length() + 1];
    for (int i = 0; i <= str1.length(); i++) {
        distance[i][0] = i;//from  w w w. j  a v  a2s  . co  m
    }
    for (int j = 0; j <= str2.length(); j++) {
        distance[0][j] = j;
    }
    for (int i = 1; i <= str1.length(); i++) {
        for (int j = 1; j <= str2.length(); j++) {
            distance[i][j] = minimum(distance[i - 1][j] + 1, distance[i][j - 1] + 1,
                    distance[i - 1][j - 1] + ((str1.charAt(i - 1) == str2.charAt(j - 1)) ? 0 : 1));
        }
    }
    return distance[str1.length()][str2.length()];
}