/** * Returns the index of the first character in {@code input} that is {@code delimiter}. Returns * limit if there is no such character. */ public static int delimiterOffset(String input, int pos, int limit, char delimiter) { for (int i = pos; i < limit; i++) { if (input.charAt(i) == delimiter) return i; } return limit; }
/** * Returns the index of the first character in {@code input} that contains a character in {@code * delimiters}. Returns limit if there is no such character. */ public static int delimiterOffset(String input, int pos, int limit, String delimiters) { for (int i = pos; i < limit; i++) { if (delimiters.indexOf(input.charAt(i)) != -1) return i; } return limit; }
/** * Returns the next index in {@code input} at or after {@code pos} that contains a character from * {@code characters}. Returns the input length if none of the requested characters can be found. */ public static int skipUntil(String input, int pos, String characters) { for (; pos < input.length(); pos++) { if (characters.indexOf(input.charAt(pos)) != -1) { break; } } return pos; }
public static String humanReadableByteCount(long bytes, boolean si) { int unit = si ? 1000 : 1024; if (bytes < unit) return bytes + " B"; int exp = (int) (Math.log(bytes) / Math.log(unit)); String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i"); return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre); }
@Override public Character convert(ResponseBody value) throws IOException { String body = value.string(); if (body.length() != 1) { throw new IOException( "Expected body of length 1 for Character conversion but was " + body.length()); } return body.charAt(0); } }
private static String escapeAndQuote(String value) { StringBuilder escaped = new StringBuilder(value.length() + 16).append('"'); for (int i = 0; i < value.length(); i++) { char ch = value.charAt(i); if (ch == '\r' || ch == '\\' || ch == '"') { escaped.append('\\'); } escaped.append(ch); } return escaped.append('"').toString(); } }
static int countLine(String s, int kdx) { int c = 1; for (int i = kdx; i >= 0; i--) { if (s.charAt(i) == '\n') { c++; } } return c; }
static void checkValue(String value, String name) { if (value == null) throw new NullPointerException("value for name " + name + " == null"); for (int i = 0, length = value.length(); i < length; i++) { char c = value.charAt(i); if ((c <= '\u001f' && c != '\t') || c >= '\u007f') { throw new IllegalArgumentException(Util.format( "Unexpected char %#04x at %d in %s value: %s", (int) c, i, name, value)); } } }
private static boolean pathMatch(HttpUrl url, String path) { String urlPath = url.encodedPath(); if (urlPath.equals(path)) { return true; // As in '/foo' matching '/foo'. } if (urlPath.startsWith(path)) { if (path.endsWith("/")) return true; // As in '/' matching '/foo'. if (urlPath.charAt(path.length()) == '/') return true; // As in '/foo' matching '/foo/bar'. } return false; }
private static boolean domainMatch(String urlHost, String domain) { if (urlHost.equals(domain)) { return true; // As in 'example.com' matching 'example.com'. } if (urlHost.endsWith(domain) && urlHost.charAt(urlHost.length() - domain.length() - 1) == '.' && !verifyAsIpAddress(urlHost)) { return true; // As in 'example.com' matching 'www.example.com'. } return false; }
static void checkName(String name) { if (name == null) throw new NullPointerException("name == null"); if (name.isEmpty()) throw new IllegalArgumentException("name is empty"); for (int i = 0, length = name.length(); i < length; i++) { char c = name.charAt(i); if (c <= '\u0020' || c >= '\u007f') { throw new IllegalArgumentException(Util.format( "Unexpected char %#04x at %d in header name: %s", (int) c, i, name)); } } }
@Override public Character apply(String string) { return string.charAt(index); } }
@Override public Character apply(String input) { return input == null ? null : input.charAt(0); } };
/** * Writes a {@code String} as specified by {@link DataOutputStream#writeChars(String)}, except * each character is written using little-endian byte order. * * @throws IOException if an I/O error occurs */ @Override public void writeChars(String s) throws IOException { for (int i = 0; i < s.length(); i++) { writeChar(s.charAt(i)); } }
@Override public String escape(String s) { int slen = s.length(); for (int index = 0; index < slen; index++) { char c = s.charAt(index); if (c < replacements.length && replacements[c] != null) { return escapeSlow(s, index); } } return s; }
private static String firstCharOnlyToUpper(String word) { return word.isEmpty() ? word : Ascii.toUpperCase(word.charAt(0)) + Ascii.toLowerCase(word.substring(1)); } }
@Override public boolean apply(String input) { return asList('a', 'e', 'i', 'o', 'u').contains(input.charAt(0)); } };
public void testSkipFully() throws IOException { String testString = "abcdef"; Reader reader = new StringReader(testString); assertEquals(testString.charAt(0), reader.read()); CharStreams.skipFully(reader, 1); assertEquals(testString.charAt(2), reader.read()); CharStreams.skipFully(reader, 2); assertEquals(testString.charAt(5), reader.read()); assertEquals(-1, reader.read()); }