Example usage for java.lang CharSequence subSequence

List of usage examples for java.lang CharSequence subSequence

Introduction

In this page you can find the example usage for java.lang CharSequence subSequence.

Prototype

CharSequence subSequence(int start, int end);

Source Link

Document

Returns a CharSequence that is a subsequence of this sequence.

Usage

From source file:org.archive.modules.extractor.ExtractorJS.java

public long considerStrings(Extractor ext, CrawlURI curi, CharSequence cs, boolean handlingJSFile) {
    long foundLinks = 0;

    Matcher strings = TextUtils.getMatcher(JAVASCRIPT_STRING_EXTRACTOR, cs);

    int startIndex = 0;
    while (strings.find(startIndex)) {
        CharSequence subsequence = cs.subSequence(strings.start(2), strings.end(2));

        if (UriUtils.isPossibleUri(subsequence)) {
            if (considerString(ext, curi, handlingJSFile, subsequence.toString())) {
                foundLinks++;// w w  w . j  ava  2  s.c o  m
            }
        }

        startIndex = strings.end(1);
    }
    TextUtils.recycleMatcher(strings);
    return foundLinks;
}

From source file:org.archive.modules.forms.ExtractorHTMLForms.java

protected List<CharSequence> findGroups(String pattern, int groupNumber, CharSequence cs) {
    ArrayList<CharSequence> groups = new ArrayList<CharSequence>();
    Matcher m = TextUtils.getMatcher(pattern, cs);
    try {//from   w  ww.ja v  a  2  s  .c o m
        while (m.find()) {
            if (m.group(groupNumber) != null) {
                groups.add(cs.subSequence(m.start(groupNumber), m.end(groupNumber)));
            } else {
                // group not found: end find condition
                break;
            }
        }
        return groups;
    } finally {
        TextUtils.recycleMatcher(m);
    }
}

From source file:com.github.nevo.decorators.reply.wechat.WeChatDecorator.java

@Override
public void apply(final StatusBarNotificationEvo evolving) throws RemoteException {
    final INotification n = evolving.notification();
    final IBundle extras = n.extras();

    // WeChat use dynamic counter as notification ID, which unfortunately will be reset upon evolving (removal, to be exact) by us,
    // causing all messages combined into one notification. So we split them by re-coding the notification ID by title.
    final CharSequence title = extras.getCharSequence(NotificationCompat.EXTRA_TITLE);
    if (title == null || title.length() == 0)
        return;/*from w  w w  . ja  v  a2s. c  o  m*/
    evolving.setId(calcSplitId(title)); // Split into separate slots

    // Chat history in big content view
    final Collection<StatusBarNotificationEvo> history = getArchivedNotifications(evolving.getKey(), 20);
    if (history.isEmpty())
        return;
    final List<CharSequence> lines = new ArrayList<>(KMaxNumLines);
    CharSequence text = null;
    int count = 0;
    final String redundant_prefix = title.toString() + ": ";
    for (final StatusBarNotificationEvo each : history) {
        final IBundle its_extras = each.notification().extras();
        final CharSequence its_title = its_extras.getCharSequence(NotificationCompat.EXTRA_TITLE);
        if (!title.equals(its_title))
            continue; // Skip other conversations sharing the same key.
        final CharSequence its_text = its_extras.getCharSequence(NotificationCompat.EXTRA_TEXT);
        if (its_text == null)
            continue;
        final int result = trimAndExtractLeadingCounter(its_text);
        if (result >= 0) {
            count = result & 0xFFFF;
            CharSequence trimmed_text = its_text.subSequence(result >> 16, its_text.length());
            if (trimmed_text.toString().startsWith(redundant_prefix)) // Remove redundant prefix
                trimmed_text = trimmed_text.subSequence(redundant_prefix.length(), trimmed_text.length());
            lines.add(text = trimmed_text);
        } else
            lines.add(text = its_text);
    }
    if (lines.isEmpty())
        return;
    Collections.reverse(lines); // Latest first, since earliest lines will be trimmed by InboxStyle.

    extras.putCharSequence(NotificationCompat.EXTRA_TEXT, text);
    extras.putCharSequence(NotificationCompat.EXTRA_TITLE_BIG, title);
    extras.putCharSequenceArray(NotificationCompat.EXTRA_TEXT_LINES, lines);
    extras.putString(EXTRA_REBUILD_STYLE, STYLE_INBOX);

    n.setColor(0xFF33B332);
    if (count != 0)
        n.setNumber(count);
}

From source file:com.awt.supark.EditCar.java

public void updateLicensePlate(CharSequence charSequence) {
    if (radioNewSrb.isChecked()) {
        if (charSequence.length() > 1) {
            txtCity.setText(charSequence.subSequence(0, 2));

            if (charSequence.length() > 5) {
                txtNum.setText(charSequence.subSequence(2, charSequence.length() - 2) + "-"
                        + charSequence.subSequence(charSequence.length() - 2, charSequence.length()));
            } else if (charSequence.length() > 2) {
                txtNum.setText(charSequence.subSequence(2, charSequence.length()));
            } else {
                txtNum.setText("");
            }/*w  w  w .  j a va 2s .c o m*/
        } else {
            txtCity.setText("");
            txtNum.setText("");
        }

        if (charSequence.length() == 8) {
            txtNum.setTextScaleX(0.9f);
        } else {
            txtNum.setTextScaleX(1);
        }
    } else {
        txtNum.setText(charSequence);

        if (charSequence.length() > 10) {
            txtNum.setTextScaleX(0.85f);
        } else {
            txtNum.setTextScaleX(1);
        }
    }
}

From source file:com.edgenius.wiki.render.RenderUtil.java

/**
 * @param text//  w  ww . j a v a 2 s .c  om
 * @param key
 * @param regions
 */
public static CharSequence createRegionBorder(CharSequence text, String key, Collection<Region> regions,
        String newlineKey) {
    if (regions == null || regions.size() == 0)
        return text;

    //sort border separators from end to start, so that it can easily insert into whole text from end to start,
    //and need not care about string change

    //the key is border point value, value is Set as it may have duplicated start or end in same point.
    Map<Integer, Set<RegionBorderPoint>> borders = new TreeMap<Integer, Set<RegionBorderPoint>>(
            new CompareToComparator(
                    CompareToComparator.TYPE_OVERWRITE_SAME_VALUE | CompareToComparator.DESCEND));

    int index = 1;
    for (Region region : regions) {
        int start = region.getStart();
        int end = region.getEnd();
        String newlineFix = new StringBuilder(text.subSequence(start, end)).indexOf("\n") == -1 ? ""
                : newlineKey;
        String regionKey = key + index + newlineFix;
        region.setKey(index + newlineFix);
        region.setKeyIndex(index);
        //~~~~~~~~~ start
        Set<RegionBorderPoint> list = borders.get(start);
        if (list == null) {
            list = new TreeSet<RegionBorderPoint>(new RegionBorderPointComparator());
            list.add(new RegionBorderPoint(start, true, end, regionKey, index));
            borders.put(start, list);
        } else {
            list.add(new RegionBorderPoint(start, true, end, regionKey, index));
        }
        //~~~~~~~~~ end
        list = borders.get(end);
        if (list == null) {
            list = new TreeSet<RegionBorderPoint>(new RegionBorderPointComparator());
            list.add(new RegionBorderPoint(end, false, start, regionKey, index));
            borders.put(end, list);
        } else {
            list.add(new RegionBorderPoint(end, false, start, regionKey, index));
        }
        index++;
    }

    //ok, here will do insert border key into original string 
    StringBuilder sb = new StringBuilder(text);
    //on behalf of above sort result, insert is simple, just looping borderPoint one by one as new insert won't impact 
    //the others un-inserted point location.
    for (Set<RegionBorderPoint> set : borders.values()) {
        for (RegionBorderPoint point : set) {
            String regionKey = point.getRegionKey() + (point.isStart() ? "S" : "E");
            sb.insert(point.getPoint(), regionKey);
        }
    }
    return sb;

}

From source file:com.perl5.lang.perl.lexer.RegexBlock.java

/**
 * Parses guaranteed opened regex block/*from   w  ww . j  a v a  2  s  . c o  m*/
 *
 * @param buffer      Input characters stream
 * @param startOffset Start parsing offset
 * @param bufferEnd   Buffer last offset
 * @param openingChar Opener character
 * @return Parsed regex block or null if failed
 */
public static RegexBlock parseBlock(CharSequence buffer, int startOffset, int bufferEnd, char openingChar,
        boolean isSecondBlock) {
    char closingChar = getQuoteCloseChar(openingChar);

    boolean isEscaped = false;
    boolean isCharGroup = false;
    boolean isQuotesDiffers = closingChar != openingChar;

    int braceLevel = 0;
    int parenLevel = 0;
    int delimiterLevel = 0;

    RegexBlock newBlock = null;
    int currentOffset = startOffset;

    while (true) {
        if (currentOffset >= bufferEnd) {
            break;
        }

        char currentChar = buffer.charAt(currentOffset);

        if (delimiterLevel == 0 && braceLevel == 0 && !isCharGroup && !isEscaped && parenLevel == 0
                && closingChar == currentChar) {
            newBlock = new RegexBlock(buffer, startOffset, currentOffset + 1, openingChar, closingChar);
            break;
        }

        if (!isSecondBlock) {
            if (!isEscaped && !isCharGroup && currentChar == '[') {
                Matcher m = POSIX_CHAR_CLASS_PATTERN.matcher(buffer.subSequence(currentOffset, bufferEnd));
                if (m.lookingAt()) {
                    currentOffset += m.toMatchResult().group(0).length();
                    continue;
                } else {
                    isCharGroup = true;
                }
            } else if (!isEscaped && isCharGroup && currentChar == ']') {
                isCharGroup = false;
            }

            // @todo this is buggy, sometimes bare is allowed. See example from `redo` doc
            //            if (!isEscaped && !isCharGroup && currentChar == '{')
            //               braceLevel++;
            //            else if (!isEscaped && !isCharGroup && braceLevel > 0 && currentChar == '}')
            //               braceLevel--;
            //
            //            if (!isEscaped && !isCharGroup && currentChar == '(')
            //               parenLevel++;
            //            else if (!isEscaped && !isCharGroup && parenLevel > 0 && currentChar == ')')
            //               parenLevel--;
        }

        if (!isEscaped && isQuotesDiffers && !isCharGroup) {
            if (currentChar == openingChar) {
                delimiterLevel++;
            } else if (currentChar == closingChar && delimiterLevel > 0) {
                delimiterLevel--;
            }
        }

        isEscaped = !isEscaped && closingChar != '\\' && currentChar == '\\';

        currentOffset++;
    }
    return newBlock;
}

From source file:fr.landel.utils.commons.StringUtils.java

/**
 * Try to prefix the sequence//from  w  w w  .  j  a v  a2s . c  o  m
 * 
 * @param sequence
 *            the sequence to prefix
 * @param prefix
 *            the prefix
 * @return the prefixed sequence
 * @throws NullPointerException
 *             if {@code sequence} or {@code prefix} are {@code null}
 */
public static String prefixIfNotStartsWith(final CharSequence sequence, final CharSequence prefix) {
    Objects.requireNonNull(sequence, ERROR_SEQUENCE);
    Objects.requireNonNull(prefix, ERROR_PREFIX);

    int lSequence = sequence.length();
    int lPrefix = prefix.length();
    if (lPrefix == 0 || (lSequence >= lPrefix && sequence.subSequence(0, lPrefix).equals(prefix))) {
        return sequence.toString();
    }
    return prefix.toString().concat(sequence.toString());
}

From source file:fr.landel.utils.commons.StringUtils.java

/**
 * Try to suffix the sequence//  w  ww  . j  av a2s  .com
 * 
 * @param sequence
 *            the sequence to suffix
 * @param suffix
 *            the suffix
 * @return the suffixed sequence
 * @throws NullPointerException
 *             if {@code sequence} or {@code suffix} are {@code null}
 */
public static String suffixIfNotEndsWith(final CharSequence sequence, final CharSequence suffix) {
    Objects.requireNonNull(sequence, ERROR_SEQUENCE);
    Objects.requireNonNull(suffix, ERROR_SUFFIX);

    int lSequence = sequence.length();
    int lSuffix = suffix.length();
    if (lSuffix == 0
            || (lSequence >= lSuffix && sequence.subSequence(lSequence - lSuffix, lSequence).equals(suffix))) {
        return sequence.toString();
    }
    return sequence.toString().concat(suffix.toString());
}

From source file:com.github.nevo.decorators.reply.wechat.WeChatDecorator.java

/** @return the extracted count in 0xFF range and start position in 0xFF00 range */
private int trimAndExtractLeadingCounter(final CharSequence text) {
    // Parse and remove the leading "[n]" or [n?//]
    if (text == null || text.length() < 4 || text.charAt(0) != '[')
        return -1;
    int text_start = 3, count_end;
    while (text.charAt(text_start++) != ']')
        if (text_start >= text.length())
            return -1;

    try {/*from  ww  w .jav a  2  s.  c o m*/
        final String num = text.subSequence(1, text_start - 1).toString(); // may contain the suffix "?/"
        for (count_end = 0; count_end < num.length(); count_end++)
            if (!Character.isDigit(num.charAt(count_end)))
                break;
        if (count_end == 0)
            return -1; // Not the expected "unread count"
        final int count = Integer.parseInt(num.substring(0, count_end));
        if (count < 2)
            return -1;

        return count < 0xFFFF ? (count & 0xFFFF) | ((text_start << 16) & 0xFFFF0000)
                : 0xFFFF | ((text_start << 16) & 0xFF00);
    } catch (final NumberFormatException ignored) {
        Log.d(TAG, "Failed to parse: " + text);
        return -1;
    }
}

From source file:me.philio.pinentry.PinEntryView.java

/**
 * Set text to the EditText/*from  w  w  w . j a va2s .c  om*/
 *
 * @param text
 */
public void setText(CharSequence text) {
    if (text.length() > mDigits) {
        text = text.subSequence(0, mDigits);
    }
    mEditText.setText(text);
}