Java Code Examples for com.google.common.base.CharMatcher#matches()

The following examples show how to use com.google.common.base.CharMatcher#matches() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseEncoding.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@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();
    }
  };
}
 
Example 2
Source File: BaseEncoding.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@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();
    }
  };
}
 
Example 3
Source File: BaseEncoding.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@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();
    }
  };
}
 
Example 4
Source File: BaseEncoding.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@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();
    }
  };
}
 
Example 5
Source File: BaseEncoding.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@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();
    }
  };
}
 
Example 6
Source File: AbstractFragmentProvider.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 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();
}
 
Example 7
Source File: AbstractFragmentProvider.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 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();
}
 
Example 8
Source File: StringModule.java    From bazel with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 9
Source File: WhitespaceTokenEvaluator.java    From putnami-web-toolkit with GNU Lesser General Public License v3.0 5 votes vote down vote up
private boolean matches(CharMatcher charMatcher, int charOrEof) {
	if (charOrEof == CharacterScanner.EOF) {
		return false;
	}

	return charMatcher.matches((char) charOrEof);
}
 
Example 10
Source File: StringModule.java    From bazel with Apache License 2.0 5 votes vote down vote up
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.
}
 
Example 11
Source File: StringModule.java    From bazel with Apache License 2.0 5 votes vote down vote up
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.
}
 
Example 12
Source File: StringModule.java    From bazel with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 13
Source File: Code.java    From immutables with Apache License 2.0 5 votes vote down vote up
private int whileMatches(int p, CharMatcher matcher) {
  for (;; p++) {
    if (!matcher.matches(get(p))) {
      return p;
    }
  }
}
 
Example 14
Source File: LeafrefXPathStringParsingPathArgumentBuilder.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
private void nextSequenceEnd(final CharMatcher matcher) {
    while (!allCharactersConsumed() && matcher.matches(xpathString.charAt(offset))) {
        offset++;
    }
}
 
Example 15
Source File: XpathStringParsingPathArgumentBuilder.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
private void nextSequenceEnd(final CharMatcher matcher) {
    while (!allCharactersConsumed() && matcher.matches(data.charAt(offset))) {
        offset++;
    }
}
 
Example 16
Source File: AbstractFragmentProvider.java    From dsl-devkit with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * 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));
}