List of usage examples for java.lang CharSequence charAt
char charAt(int index);
From source file:org.orekit.propagation.analytical.tle.TLE.java
/** Compute the checksum of the first 68 characters of a line. * @param line line to check//from w w w. j a va 2 s . c o m * @return checksum */ private static int checksum(final CharSequence line) { int sum = 0; for (int j = 0; j < 68; j++) { final char c = line.charAt(j); if (Character.isDigit(c)) { sum += Character.digit(c, 10); } else if (c == '-') { ++sum; } } return sum % 10; }
From source file:org.cleverbus.common.Strings.java
/** * @param input the input/*from w w w . j a v a 2s .c om*/ * @param c the searched character * @return the index or -1 * @since 3.8.2 */ public static int indexOf(CharSequence input, char c) { if (input instanceof String) { return ((String) input).indexOf(c); } for (int i = 0; i < input.length(); i++) { if (c == input.charAt(i)) { return i; } } return -1; }
From source file:cn.knet.showcase.demos.servletproxy.ProxyServlet.java
/** * 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./*from ww w .j a va 2 s. c o m*/ * * @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:io.github.hidroh.materialistic.AppUtils.java
private static CharSequence trim(CharSequence charSequence) { if (TextUtils.isEmpty(charSequence)) { return charSequence; }/*from w w w.j a v a2 s.co m*/ int end = charSequence.length() - 1; while (Character.isWhitespace(charSequence.charAt(end))) { end--; } return charSequence.subSequence(0, end + 1); }
From source file:net.darkmist.alib.res.PkgRes.java
/** * Gets a resource path using cls's package name as the prefix. * @param name The name of the resource. * @param cls The class to get the package prefix from. * @return Path of a resource prefixed by the class package name. * @throws NullPointerException if name or cls are null. */// w ww . ja v a 2 s.c om public static String getResourcePathFor(CharSequence name, Class<?> cls) { int nameLen; StringBuilder sb; if (name == null) throw new NullPointerException("name is null"); if (cls == null) throw new NullPointerException("cls is null"); nameLen = name.length(); sb = new StringBuilder(cls.getName().length() + nameLen + 2); appendResourcePathPrefixFor(sb, cls); cls = null; if (name.charAt(0) != '/') sb.append(name, 1, nameLen); else sb.append(name); name = null; return sb.toString(); }
From source file:org.apache.james.jdkim.tagvalue.SignatureRecordImpl.java
public static String dkimQuotedPrintableDecode(CharSequence input) throws IllegalArgumentException { StringBuilder sb = new StringBuilder(input.length()); // TODO should we fail on WSP that is not part of FWS? // the specification in 2.6 DKIM-Quoted-Printable is not // clear//from www .ja v a 2 s . co m int state = 0; int start = 0; int d = 0; boolean lastWasNL = false; for (int i = 0; i < input.length(); i++) { if (lastWasNL && input.charAt(i) != ' ' && input.charAt(i) != '\t') { throw new IllegalArgumentException("Unexpected LF not part of an FWS"); } lastWasNL = false; switch (state) { case 0: switch (input.charAt(i)) { case ' ': case '\t': case '\r': case '\n': if ('\n' == input.charAt(i)) lastWasNL = true; sb.append(input.subSequence(start, i)); start = i + 1; // ignoring whitespace by now. break; case '=': sb.append(input.subSequence(start, i)); state = 1; break; } break; case 1: case 2: if (input.charAt(i) >= '0' && input.charAt(i) <= '9' || input.charAt(i) >= 'A' && input.charAt(i) <= 'F') { int v = Arrays.binarySearch("0123456789ABCDEF".getBytes(), (byte) input.charAt(i)); if (state == 1) { state = 2; d = v; } else { d = d * 16 + v; sb.append((char) d); state = 0; start = i + 1; } } else { throw new IllegalArgumentException("Invalid input sequence at " + i); } } } if (state != 0) { throw new IllegalArgumentException("Invalid quoted printable termination"); } sb.append(input.subSequence(start, input.length())); return sb.toString(); }
From source file:org.ocpsoft.rewrite.servlet.config.proxy.ProxyServlet.java
/** * Encodes characters in the query or fragment part of the URI. * //w ww .ja v a 2 s . co m * <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 */ 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. TODO: * replace/compare to with Rewrite Encoding */ 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(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.apache.poi.util.StringUtil.java
/** * Count number of occurrences of needle in haystack * Has same signature as org.apache.commons.lang3.StringUtils#countMatches * * @param haystack the CharSequence to check, may be null * @param needle the character to count the quantity of * @return the number of occurrences, 0 if the CharSequence is null *//* ww w. ja va2 s . c om*/ public static int countMatches(CharSequence haystack, char needle) { if (haystack == null) return 0; int count = 0; final int length = haystack.length(); for (int i = 0; i < length; i++) { if (haystack.charAt(i) == needle) { count++; } } return count; }
From source file:com.gmail.walles.johan.batterylogger.BatteryPlotFragment.java
private static CharSequence trimTrailingWhitespace(CharSequence source) { int i = source.length(); // loop back to the first non-whitespace character while (--i >= 0 && Character.isWhitespace(source.charAt(i))) { // This block intentionally left blank }/*from w ww. j a v a2 s. c o m*/ return source.subSequence(0, i + 1); }
From source file:hrytsenko.csv.IO.java
static CsvSchema.Builder getSchema(Map<String, ?> args) { CharSequence separator = (CharSequence) args.get("separator"); if (separator == null) { separator = ","; }/*from w w w . j a v a 2 s . c o m*/ if (separator.length() != 1) { throw new IllegalArgumentException("Use single character as separator."); } CharSequence qualifier = (CharSequence) args.get("qualifier"); if (qualifier == null) { qualifier = "\""; } if (qualifier.length() != 1) { throw new IllegalArgumentException("Use single character as qualifier."); } CsvSchema.Builder schema = CsvSchema.builder(); schema.setColumnSeparator(separator.charAt(0)); schema.setQuoteChar(qualifier.charAt(0)); return schema; }