List of usage examples for java.lang CharSequence charAt
char charAt(int index);
From source file:com.liferay.events.global.mobile.Utils.java
/** * Gets a set of matching characters between two strings. * <p/>/*from w ww. java 2 s. co m*/ * <p><Two characters from the first string and the second string are considered matching if the character's * respective positions are no farther than the limit value.</p> * * @param first The first string. * @param second The second string. * @param limit The maximum distance to consider. * @return A string contain the set of common characters. */ private static String getSetOfMatchingCharacterWithin(final CharSequence first, final CharSequence second, final int limit) { final StringBuilder common = new StringBuilder(); final StringBuilder copy = new StringBuilder(second); for (int i = 0; i < first.length(); i++) { final char ch = first.charAt(i); boolean found = false; // See if the character is within the limit positions away from the original position of that character. for (int j = Math.max(0, i - limit); !found && j < Math.min(i + limit, second.length()); j++) { if (copy.charAt(j) == ch) { found = true; common.append(ch); copy.setCharAt(j, '*'); } } } return common.toString(); }
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. j ava 2 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); } int index1 = thisStart; int index2 = start; int tmpLen = length; // Extract these first so we detect NPEs the same as the java.lang.String version final int srcLen = cs.length() - thisStart; final int otherLen = substring.length() - start; // Check for invalid parameters if (thisStart < 0 || start < 0 || length < 0) { return false; } // Check that the regions are long enough if (srcLen < length || otherLen < length) { return false; } 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:com.google.gwt.jolokia.server.servlet.ProxyServlet.java
/** * Encodes characters in the query or fragment part of the URI. * * <p>/*from w w w .jav a2s . c o m*/ * Unfortunately, an incoming URI sometimes has characters disallowed by the * spec. HttpClient insists that the outgoing proxied request has a valid * URI because it uses Java's {@link URI}. To be more forgiving, we must * escape the problematic characters. See the URI class for the spec. * * @param in * example: name=value&foo=bar#fragment */ protected static CharSequence encodeUriQuery(CharSequence in) { // Note that I can't simply use URI.java to encode because it will // escape pre-existing escaped things. StringBuilder outBuf = null; Formatter formatter = null; for (int i = 0; i < in.length(); i++) { char c = in.charAt(i); boolean escape = true; if (c < 128) { if (asciiQueryChars.get((int) c)) { escape = false; } } else if (!Character.isISOControl(c) && !Character.isSpaceChar(c)) {// not-ascii escape = false; } if (!escape) { if (outBuf != null) outBuf.append(c); } else { // escape if (outBuf == null) { outBuf = new StringBuilder(in.length() + 5 * 3); outBuf.append(in, 0, i); formatter = new Formatter(outBuf); } // leading %, 0 padded, width 2, capital hex formatter.format("%%%02X", (int) c);// TODO } } return outBuf != null ? outBuf : in; }
From source file:org.opengeoportal.proxy.controllers.DynamicOgcController.java
/** * <p>Encodes characters in the query or fragment part of the URI. * * <p>Unfortunately, an incoming URI sometimes has characters disallowed by the spec. HttpClient * insists that the outgoing proxied request has a valid URI because it uses Java's {@link URI}. To be more * forgiving, we must escape the problematic characters. See the URI class for the spec. * * @param in example: name=value&foo=bar#fragment *//*from ww w.j a va 2 s. co m*/ static CharSequence encodeUriQuery(CharSequence in) { //Note that I can't simply use URI.java to encode because it will escape pre-existing escaped things. StringBuilder outBuf = null; Formatter formatter = null; for (int i = 0; i < in.length(); i++) { char c = in.charAt(i); boolean escape = true; if (c < 128) { if (asciiQueryChars.get((int) c)) { escape = false; } } else if (!Character.isISOControl(c) && !Character.isSpaceChar(c)) {//not-ascii escape = false; } if (!escape) { if (outBuf != null) outBuf.append(c); } else { //escape if (outBuf == null) { outBuf = new StringBuilder(in.length() + 5 * 3); outBuf.append(in, 0, i); formatter = new Formatter(outBuf); } //leading %, 0 padded, width 2, capital hex formatter.format("%%%02X", (int) c);//TODO formatter.close(); } } return outBuf != null ? outBuf : in; }
From source file:org.renjin.parser.NumericLiterals.java
/** * Parses a string to a double./*www. j a v a 2 s. c om*/ * * @param s the string to parse * @param dec the decimal point character to use. Generally '.' or ',' * @param startIndex the index, inclusive at which to start parsing * @param endIndex the index, exculsive, at which to stop parsing * @return the number as a {@code double} value, or {@code DoubleVector.NA} if {@code s} is not a number or * malformatted. */ public static double parseDouble(CharSequence s, int startIndex, int endIndex, char dec, boolean NA) { int sign = 1; int p = startIndex; /* Trim optional whitespace */ while (p < endIndex && Character.isWhitespace(s.charAt(p))) { p++; } while (endIndex > p && Character.isWhitespace(s.charAt(endIndex - 1))) { endIndex--; } /* Check for the input 'NA' */ if (NA && (p + 2 < endIndex) && s.charAt(p) == 'N' && s.charAt(p + 1) == 'A') { return DoubleVector.NA; } /* Empty input? Return NA */ if (p == endIndex) { return DoubleVector.NA; } /* Sign is optional */ switch (s.charAt(p)) { case '-': sign = -1; p++; break; case '+': p++; break; } if (equalsIgnoringCase(s, p, endIndex, "NAN")) { return Double.NaN; } if (equalsIgnoringCase(s, p, endIndex, "INF") || equalsIgnoringCase(s, p, endIndex, "INFINITY")) { return sign * Double.POSITIVE_INFINITY; } if (((endIndex - p) > 2) && s.charAt(p) == '0' && (s.charAt(p + 1) == 'x' || s.charAt(p + 2) == 'X')) { return parseDoubleHex(s, sign, p, endIndex, dec); } else { return parseDoubleDecimal(s, sign, p, endIndex, dec); } }
From source file:jetx.ext.common.StringMethods.java
/** * <p>Checks if the CharSequence contains only ASCII printable characters.</p> * * <p>{@code null} will return {@code false}. * An empty CharSequence (length()=0) will return {@code true}.</p> * * <pre>/*w w w .j a va 2s . c o m*/ * StringMethods.isAsciiPrintable(null) = false * StringMethods.isAsciiPrintable("") = true * StringMethods.isAsciiPrintable(" ") = true * StringMethods.isAsciiPrintable("Ceki") = true * StringMethods.isAsciiPrintable("ab2c") = true * StringMethods.isAsciiPrintable("!ab-c~") = true * StringMethods.isAsciiPrintable("\u0020") = true * StringMethods.isAsciiPrintable("\u0021") = true * StringMethods.isAsciiPrintable("\u007e") = true * StringMethods.isAsciiPrintable("\u007f") = false * StringMethods.isAsciiPrintable("Ceki G\u00fclc\u00fc") = false * </pre> * * @param cs the CharSequence to check, may be null * @return {@code true} if every character is in the range * 32 thru 126 */ public static boolean isAsciiPrintable(CharSequence cs) { if (cs == null) { return false; } int sz = cs.length(); for (int i = 0; i < sz; i++) { if ((cs.charAt(i) >= 32 && cs.charAt(i) < 127) == false) { return false; } } return true; }
From source file:org.renjin.parser.NumericLiterals.java
private static boolean equalsIgnoringCase(CharSequence s, int start, int endIndex, String word) { int lenRemaining = endIndex - start; int wordLen = word.length(); if (lenRemaining != wordLen) { return false; }/*from w ww . java 2 s .c o m*/ for (int i = 0; i < wordLen; ++i) { if (Character.toUpperCase(s.charAt(start + i)) != word.charAt(i)) { return false; } } return true; }
From source file:net.gkovalechyn.minereset.Util.java
public static int LevenshteinDistance(CharSequence lhs, CharSequence rhs) { int len0 = lhs.length() + 1; int len1 = rhs.length() + 1; // the array of distances int[] cost = new int[len0]; int[] newcost = new int[len0]; // initial cost of skipping prefix in String s0 for (int i = 0; i < len0; i++) { cost[i] = i;//from w w w . j a v a2s .c o m } // dynamically computing the array of distances // transformation cost for each letter in s1 for (int j = 1; j < len1; j++) { // initial cost of skipping prefix in String s1 newcost[0] = j; // transformation cost for each letter in s0 for (int i = 1; i < len0; i++) { // matching current letters in both strings int match = (lhs.charAt(i - 1) == rhs.charAt(j - 1)) ? 0 : 1; // computing cost for each transformation int cost_replace = cost[i - 1] + match; int cost_insert = cost[i] + 1; int cost_delete = newcost[i - 1] + 1; // keep minimum cost newcost[i] = Math.min(Math.min(cost_insert, cost_delete), cost_replace); } // swap cost/newcost arrays int[] swap = cost; cost = newcost; newcost = swap; } // the distance is the cost for transforming all letters in both strings return cost[len0 - 1]; }
From source file:edu.cornell.med.icb.goby.modes.FastaToCompactMode.java
public static byte[] convertQualityScores(final QualityEncoding qualityEncoding, final CharSequence quality, final boolean verboseQualityScores, final boolean apiMode) { // Only Solexa, Sanger and Illumina encoding are supported at this time final int size = quality.length(); final byte[] qualityScoreBuffer = new byte[size]; if (verboseQualityScores) { System.out.println(quality); }// ww w . j a va 2 s. co m for (int position = 0; position < size; position++) { qualityScoreBuffer[position] = qualityEncoding .asciiEncodingToPhredQualityScore(quality.charAt(position)); if (!qualityEncoding.isWithinValidRange(qualityScoreBuffer[position])) { final String message = "Phred quality scores must be within specific ranges for specfic encodings. " + "The value decoded was " + qualityScoreBuffer[position] + " and outside of the valid range for " + qualityEncoding + " You may have selected an incorrect encoding."; if (apiMode) { throw new IllegalArgumentException(message); } else { System.err.println(message); System.exit(10); } } if (verboseQualityScores) { System.out.print(qualityScoreBuffer[position]); System.out.print(" "); } } if (verboseQualityScores) { System.out.println(); } return qualityScoreBuffer; }
From source file:com.gamesalutes.utils.WebUtils.java
private static boolean encodeReserved(CharSequence s, int index, boolean isLast) { // FIXME: this does not work properly for & int len = s.length(); // reserved chars // & encode within a given query parameter // / don't encode unless in query // : after the first / following the hostname // ; always?//from www.j a v a 2 s . c o m // = always outside a query name-value separation // ? after the first appearance in a path - denoting the start of the query string // @ always? if (index < 0 || index >= len) return false; char c = s.charAt(index); switch (c) { case '&': { if (isLast) return false; int qi = StringUtils.lastIndexOf(s, '?', index - 1); if (qi == -1) return false; // find prev one int start = StringUtils.lastIndexOf(s, c, index - 1); if (start == -1) start = qi + 1; if (start >= index - 1) return true; // check to see if we name=value return StringUtils.indexOf(false, s, start, '=', index - start) == -1; } case '?': { return StringUtils.lastIndexOf(s, c, index - 1) != -1; } case '/': { return StringUtils.lastIndexOf(s, '?', index - 1) != -1; } case ':': { return StringUtils.indexOf(s, "//", index - 1) != -1 && StringUtils.indexOf(s, c, index - 1) != -1; } case ';': case '@': { return true; } case '=': { if (isLast) return false; int qi = StringUtils.lastIndexOf(s, '?', index - 1); if (qi == -1) return false; // find current token int start = StringUtils.lastIndexOf(s, '&', index - 1); if (start == -1) start = qi + 1; if (start >= index - 1) return true; // if previous character was first a & and not a = then we are good // check to see if we name=value return StringUtils.indexOf(false, s, start, '=', index - start) != -1; } default: return false; } }