Example usage for com.google.common.base CharMatcher indexIn

List of usage examples for com.google.common.base CharMatcher indexIn

Introduction

In this page you can find the example usage for com.google.common.base CharMatcher indexIn.

Prototype

public int indexIn(CharSequence sequence, int start) 

Source Link

Document

Returns the index of the first matching character in a character sequence, starting from a given position, or -1 if no character matches after that position.

Usage

From source file:de.hpi.bp2013n1.anonymizer.util.SafeStringSplitter.java

/**
 * Splits a string at each occurrence of a split character but not within
 * quotes. Example: SafeStringSplitter.splitSafely("'a.b'.c'", '.') 
 * returns a List ["'a.b'", "c"].//from ww w.  j av  a2  s. co  m
 * @param input
 * @param splitChar
 * @return
 */
public static List<String> splitSafely(String input, char splitChar) {
    List<String> splitParts = Lists.newArrayList(input.split(Character.toString(splitChar)));
    CharMatcher quoteMatcher = CharMatcher.anyOf("'\"");
    for (int i = 0; i < splitParts.size(); i++) {
        StringBuilder currentPart = new StringBuilder(splitParts.get(i));
        int start = 0;
        boolean sq = false, dq = false;
        while (true) {
            int position = quoteMatcher.indexIn(currentPart, start);
            if (position == -1) {
                if (sq || dq) {
                    currentPart.append(splitChar).append(splitParts.remove(i + 1));
                    // TODO: catch IndexOutOfBoundsException => unbalanced
                    continue;
                } else {
                    splitParts.set(i, currentPart.toString());
                    break;
                }
            }
            switch (currentPart.charAt(position)) {
            case '\'':
                if (!dq)
                    sq = !sq;
                break;
            case '"':
                if (!sq)
                    dq = !dq;
                break;
            }
            start = position + 1;
        }
    }
    return splitParts;
}

From source file:com.google.android.mail.common.base.Splitter.java

/**
 * Returns a splitter that considers any single character matched by the
 * given {@code CharMatcher} to be a separator. For example, {@code
 * Splitter.on(CharMatcher.anyOf(";,")).split("foo,;bar,quux")} returns an
 * iterable containing {@code ["foo", "", "bar", "quux"]}.
 *
 * @param separatorMatcher a {@link CharMatcher} that determines whether a
 *     character is a separator//from   www  .  j  av  a2  s. c o m
 * @return a splitter, with default settings, that uses this matcher
 */
public static Splitter on(final CharMatcher separatorMatcher) {
    checkNotNull(separatorMatcher);

    return new Splitter(new Strategy() {
        /*@Override*/ public SplittingIterator iterator(Splitter splitter, final CharSequence toSplit) {
            return new SplittingIterator(splitter, toSplit) {
                @Override
                int separatorStart(int start) {
                    return separatorMatcher.indexIn(toSplit, start);
                }

                @Override
                int separatorEnd(int separatorPosition) {
                    return separatorPosition + 1;
                }
            };
        }
    });
}

From source file:com.indeema.mail.lib.base.Splitter.java

/**
 * Returns a splitter that considers any single character matched by the
 * given {@code CharMatcher} to be a separator. For example, {@code
 * Splitter.on(CharMatcher.anyOf(";,")).split("foo,;bar,quux")} returns an
 * iterable containing {@code ["foo", "", "bar", "quux"]}.
 *
 * @param separatorMatcher a {@link CharMatcher} that determines whether a
 *     character is a separator/*from  w  w  w. ja  v a2 s . c om*/
 * @return a splitter, with default settings, that uses this matcher
 */
public static Splitter on(final CharMatcher separatorMatcher) {
    Preconditions.checkNotNull(separatorMatcher);

    return new Splitter(new Strategy() {
        /*@Override*/ public SplittingIterator iterator(Splitter splitter, final CharSequence toSplit) {
            return new SplittingIterator(splitter, toSplit) {
                @Override
                int separatorStart(int start) {
                    return separatorMatcher.indexIn(toSplit, start);
                }

                @Override
                int separatorEnd(int separatorPosition) {
                    return separatorPosition + 1;
                }
            };
        }
    });
}