下面列出了com.google.common.base.CharMatcher#matches ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@GwtIncompatible // Reader
static Reader ignoringReader(
final Reader delegate, final CharMatcher toIgnore) {
checkNotNull(delegate);
checkNotNull(toIgnore);
return new Reader() {
@Override
public int read() throws IOException {
int readChar;
do {
readChar = delegate.read();
} while (readChar != -1 && toIgnore.matches((char) readChar));
return readChar;
}
@Override
public int read(char[] cbuf, int off, int len) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public void close() throws IOException {
delegate.close();
}
};
}
@GwtIncompatible // Reader
static Reader ignoringReader(
final Reader delegate, final CharMatcher toIgnore) {
checkNotNull(delegate);
checkNotNull(toIgnore);
return new Reader() {
@Override
public int read() throws IOException {
int readChar;
do {
readChar = delegate.read();
} while (readChar != -1 && toIgnore.matches((char) readChar));
return readChar;
}
@Override
public int read(char[] cbuf, int off, int len) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public void close() throws IOException {
delegate.close();
}
};
}
@GwtIncompatible // Reader
static Reader ignoringReader(
final Reader delegate, final CharMatcher toIgnore) {
checkNotNull(delegate);
checkNotNull(toIgnore);
return new Reader() {
@Override
public int read() throws IOException {
int readChar;
do {
readChar = delegate.read();
} while (readChar != -1 && toIgnore.matches((char) readChar));
return readChar;
}
@Override
public int read(char[] cbuf, int off, int len) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public void close() throws IOException {
delegate.close();
}
};
}
@GwtIncompatible // Reader
static Reader ignoringReader(
final Reader delegate, final CharMatcher toIgnore) {
checkNotNull(delegate);
checkNotNull(toIgnore);
return new Reader() {
@Override
public int read() throws IOException {
int readChar;
do {
readChar = delegate.read();
} while (readChar != -1 && toIgnore.matches((char) readChar));
return readChar;
}
@Override
public int read(char[] cbuf, int off, int len) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public void close() throws IOException {
delegate.close();
}
};
}
@GwtIncompatible // Reader
static Reader ignoringReader(final Reader delegate, final CharMatcher toIgnore) {
checkNotNull(delegate);
checkNotNull(toIgnore);
return new Reader() {
@Override
public int read() throws IOException {
int readChar;
do {
readChar = delegate.read();
} while (readChar != -1 && toIgnore.matches((char) readChar));
return readChar;
}
@Override
public int read(char[] cbuf, int off, int len) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public void close() throws IOException {
delegate.close();
}
};
}
/**
* Returns the index of the next unescaped occurrence matching {@code ch} in {@code str} (after {@code fromIndex}). If no such match exists the method must
* return {@code str#length()}.
*
* @param str
* the string, must not be {@code null}
* @param ch
* character match, must not be {@code null}
* @param fromIndex
* index at which to start scanning for matches
* @return index of the next occurrence in the given string, or {@code str#length()} if none can be found
*/
protected int indexOfUnescapedChar(final String str, final CharMatcher ch, final int fromIndex) {
boolean escaped = false;
for (int index = fromIndex; index < str.length(); index++) {
if (escaped) {
escaped = false;
continue;
}
char c = str.charAt(index);
if (ch.matches(c)) {
return index;
} else if (c == ESCAPE_CHARACTER) {
escaped = true;
}
}
return str.length();
}
/**
* Unescapes the given escape characters contained in the given text.
* <p>
* Reserved characters for e.g. segment and list separation cannot be used by custom fragment providers unless escaped, i.e. prefixed with a '\'. Such URI
* segments need to be escaped when forming the URI fragment, and consequently unescaped when reading the URI segments.
* </p>
*
* @param text
* the text to unescape, must not be {@code null}
* @param charactersToEscape
* the characters to escape, must not be {@code null}
* @return the unescaped text, never {@code null}
*/
protected String unescape(final String text, final CharMatcher charactersToEscape) {
StringBuilder result = null;
int lastIndex = 0;
boolean escaped = false;
for (int index = 0; index < text.length(); index++) {
char character = text.charAt(index);
if (escaped) {
escaped = false;
if (charactersToEscape.matches(character)) {
if (result == null) {
result = new StringBuilder(text.length());
}
result.append(text.substring(lastIndex, index - 1)).append(character);
lastIndex = index + 1;
}
} else if (character == ESCAPE_CHARACTER) {
escaped = true;
}
}
if (result == null) {
return text;
}
result.append(text.substring(lastIndex));
return result.toString();
}
private static boolean matches(
String str, CharMatcher matcher, boolean requiresAtLeastOneCasedLetter) {
if (str.isEmpty()) {
return false;
} else if (!requiresAtLeastOneCasedLetter) {
return matcher.matchesAllOf(str);
}
int casedLetters = 0;
for (char current : str.toCharArray()) {
if (!matcher.matches(current)) {
return false;
} else if (requiresAtLeastOneCasedLetter && CASED.matches(current)) {
++casedLetters;
}
}
return casedLetters > 0;
}
private boolean matches(CharMatcher charMatcher, int charOrEof) {
if (charOrEof == CharacterScanner.EOF) {
return false;
}
return charMatcher.matches((char) charOrEof);
}
private static String stringLStrip(String self, String chars) {
CharMatcher matcher = CharMatcher.anyOf(chars);
for (int i = 0; i < self.length(); i++) {
if (!matcher.matches(self.charAt(i))) {
return self.substring(i);
}
}
return ""; // All characters were stripped.
}
private static String stringRStrip(String self, String chars) {
CharMatcher matcher = CharMatcher.anyOf(chars);
for (int i = self.length() - 1; i >= 0; i--) {
if (!matcher.matches(self.charAt(i))) {
return self.substring(0, i + 1);
}
}
return ""; // All characters were stripped.
}
@StarlarkMethod(
name = "istitle",
doc =
"Returns True if the string is in title case and it contains at least one character. "
+ "This means that every uppercase character must follow an uncased one (e.g. "
+ "whitespace) and every lowercase character must follow a cased one (e.g. "
+ "uppercase or lowercase).",
parameters = {@Param(name = "self", type = String.class, doc = "This string.")})
public Boolean isTitle(String self) throws EvalException {
if (self.isEmpty()) {
return false;
}
// From the Python documentation: "uppercase characters may only follow uncased characters
// and lowercase characters only cased ones".
char[] data = self.toCharArray();
CharMatcher matcher = CharMatcher.any();
char leftMostCased = ' ';
for (int pos = data.length - 1; pos >= 0; --pos) {
char current = data[pos];
// 1. Check condition that was determined by the right neighbor.
if (!matcher.matches(current)) {
return false;
}
// 2. Determine condition for the left neighbor.
if (LOWER.matches(current)) {
matcher = CASED;
} else if (UPPER.matches(current)) {
matcher = CASED.negate();
} else {
matcher = CharMatcher.any();
}
// 3. Store character if it is cased.
if (CASED.matches(current)) {
leftMostCased = current;
}
}
// The leftmost cased letter must be uppercase. If leftMostCased is not a cased letter here,
// then the string doesn't have any cased letter, so UPPER.test will return false.
return UPPER.matches(leftMostCased);
}
private int whileMatches(int p, CharMatcher matcher) {
for (;; p++) {
if (!matcher.matches(get(p))) {
return p;
}
}
}
private void nextSequenceEnd(final CharMatcher matcher) {
while (!allCharactersConsumed() && matcher.matches(xpathString.charAt(offset))) {
offset++;
}
}
private void nextSequenceEnd(final CharMatcher matcher) {
while (!allCharactersConsumed() && matcher.matches(data.charAt(offset))) {
offset++;
}
}
/**
* Escapes the escape characters contained in the given text and appends the result to the given {@link StringBuilder}.
* <p>
* Reserved characters for e.g. segment and list separation cannot be used by custom fragment providers unless escaped, i.e. prefixed with a '\'. Such URI
* segments need to be escaped when forming the URI fragment, and consequently unescaped when reading the URI segments.
* </p>
*
* @param text
* the text to escape, must not be {@code null}
* @param builder
* builder to append the escaped text to, must not be {@code null}
* @param charactersToEscape
* the characters to escape, must not be {@code null}
*/
protected void appendEscaped(final String text, final StringBuilder builder, final CharMatcher charactersToEscape) {
int lastIndex = 0;
for (int index = 0; index < text.length(); index++) {
char character = text.charAt(index);
if (charactersToEscape.matches(character)) {
builder.append(text.substring(lastIndex, index)).append(ESCAPE_CHARACTER).append(character);
lastIndex = index + 1;
}
}
builder.append(text.substring(lastIndex));
}