Example usage for org.apache.commons.lang3.text StrMatcher isMatch

List of usage examples for org.apache.commons.lang3.text StrMatcher isMatch

Introduction

In this page you can find the example usage for org.apache.commons.lang3.text StrMatcher isMatch.

Prototype

public abstract int isMatch(char[] buffer, int pos, int bufferStart, int bufferEnd);

Source Link

Document

Returns the number of matching characters, zero for no match.

Usage

From source file:it.d4nguard.rgrpg.util.StringCompiler.java

/**
 * Searches the string builder using the matcher to find the first
 * match searching from the given index.
 * <p>/*ww w .ja v a2  s . c  o  m*/
 * Matchers can be used to perform advanced searching behaviour. For example
 * you could write a matcher to find the character 'a' followed by a number.
 * 
 * @param matcher
 *            the matcher to use, null returns -1
 * @param startIndex
 *            the index to start at, invalid index rounded to edge
 * @return the first index matched, or -1 if not found
 */
public int indexOf(StrMatcher matcher, int startIndex) {
    startIndex = (startIndex < 0 ? 0 : startIndex);
    if (matcher == null || startIndex >= size) {
        return -1;
    }
    int len = size;
    char[] buf = buffer;
    for (int i = startIndex; i < len; i++) {
        if (matcher.isMatch(buf, i, startIndex, len) > 0) {
            return i;
        }
    }
    return -1;
}

From source file:it.d4nguard.rgrpg.util.StringCompiler.java

/**
 * Searches the string builder using the matcher to find the last
 * match searching from the given index.
 * <p>// w w w .ja va  2 s  .c o m
 * Matchers can be used to perform advanced searching behaviour. For example
 * you could write a matcher to find the character 'a' followed by a number.
 * 
 * @param matcher
 *            the matcher to use, null returns -1
 * @param startIndex
 *            the index to start at, invalid index rounded to edge
 * @return the last index matched, or -1 if not found
 */
public int lastIndexOf(StrMatcher matcher, int startIndex) {
    startIndex = (startIndex >= size ? size - 1 : startIndex);
    if (matcher == null || startIndex < 0) {
        return -1;
    }
    char[] buf = buffer;
    int endIndex = startIndex + 1;
    for (int i = startIndex; i >= 0; i--) {
        if (matcher.isMatch(buf, i, 0, endIndex) > 0) {
            return i;
        }
    }
    return -1;
}

From source file:it.d4nguard.rgrpg.util.StringCompiler.java

/**
 * Replaces within the builder using a matcher.
 * <p>/*w  w  w.j a  v  a2 s  . c o m*/
 * Matchers can be used to perform advanced behaviour. For example you could
 * write a matcher to delete all occurances where the character 'a' is
 * followed by a number.
 * 
 * @param matcher
 *            the matcher to use to find the deletion, null causes no action
 * @param replaceStr
 *            the string to replace the match with, null is a delete
 * @param from
 *            the start index, must be valid
 * @param to
 *            the end index (exclusive), must be valid
 * @param replaceCount
 *            the number of times to replace, -1 for replace all
 * @return this, to enable chaining
 * @throws IndexOutOfBoundsException
 *             if any index is invalid
 */
private StringCompiler replaceImpl(StrMatcher matcher, String replaceStr, int from, int to, int replaceCount) {
    if (matcher == null || size == 0) {
        return this;
    }
    int replaceLen = (replaceStr == null ? 0 : replaceStr.length());
    char[] buf = buffer;
    for (int i = from; i < to && replaceCount != 0; i++) {
        int removeLen = matcher.isMatch(buf, i, from, to);
        if (removeLen > 0) {
            replaceImpl(i, i + removeLen, removeLen, replaceStr, replaceLen);
            to = to - removeLen + replaceLen;
            i = i + replaceLen - 1;
            if (replaceCount > 0) {
                replaceCount--;
            }
        }
    }
    return this;
}