List of usage examples for com.google.gwt.regexp.shared RegExp setLastIndex
public final void setLastIndex(int lastIndex)
From source file:com.ciplogic.web.codeeditor.render.html.StringFormat.java
License:Open Source License
public static String format(String str, String... replaceValues) { for (int i = 0; i < replaceValues.length; i++) { RegExp regexp = RegExp.compile("(\\{" + i + "\\})", "g"); MatchResult match;/*from w w w.j a v a 2 s .com*/ while ((match = regexp.exec(str)) != null) { str = str.substring(0, match.getIndex()) + replaceValues[i] + str.substring(match.getIndex() + match.getGroup(1).length()); regexp.setLastIndex(match.getIndex() + replaceValues[i].length()); } } return str; }
From source file:com.codenvy.ide.texteditor.linedimensions.LineDimensionsCalculator.java
License:Open Source License
/** * Builds the cache for a line up to a particular column. Should not be called * if the line has already been {@link ColumnOffsetCache#FULLY_MEASURED}. * <p/>/*from w w w .j av a2 s.c om*/ * <p/> * You should only rely on either endColumn or endX, one or the other should * be the max value for its data type. * * @param endColumn * inclusive end column (we will end on or after end) * @param endX * inclusive end x pixel width (we will end on or after endX) * @see #measureLineStoppingAtColumn(ColumnOffsetCache, Line, int) * @see #measureLineStoppingAtX(ColumnOffsetCache, Line, double) */ private void measureLine(ColumnOffsetCache cache, Line line, int endColumn, double endX) { /* * Starting at cache.measuredColumn we will use the regex to scan forward to * see if we hit an interesting character other than prefixed tab. if we do * we'll measure that to that point and append a {@link ColumnOffset} if it * is a special size. Rinse and repeat. */ RegExp regexp = UnicodeUtils.regexpNonAsciiTabOrCarriageReturn; regexp.setLastIndex(cache.measuredOffset.column); MatchResult result = regexp.exec(line.getText()); if (result != null) { double x = 0; int index = 0; do { // Calculate any x offset up to this point in the line ColumnOffset offset = cache.getLastColumnOffsetInCache(); double baseXOffset = smartColumnToX(offset, result.getIndex()); /* * TODO: we can be smarter here, if i > 1, then this character * is a mark. We could separate out the RegExp into non-spacing, * enclosing-marks v. spacing-marks and already know which are supposed * to be zero-width based on which groups are null. */ String match = result.getGroup(0); for (int i = 0; i < match.length(); i++) { x = addOffsetForResult(cache, match.charAt(i), result.getIndex() + i, line, baseXOffset); baseXOffset = x; } result = regexp.exec(line.getText()); // we have to ensure we measure through the last zero-width character. index = result == null ? 0 : result.getIndex(); } while (result != null && result.getIndex() < endColumn && x < endX); } cache.measuredOffset = ColumnOffsetCache.FULLY_MEASURED; if (result == null) { return; } }
From source file:com.google.collide.client.document.linedimensions.LineDimensionsCalculator.java
License:Open Source License
/** * Builds the cache for a line up to a particular column. Should not be called * if the line has already been {@link ColumnOffsetCache#FULLY_MEASURED}. * * <p>/*from w w w. j av a 2 s . com*/ * You should only rely on either endColumn or endX, one or the other should * be the max value for its data type. * * @see #measureLineStoppingAtColumn(ColumnOffsetCache, Line, int) * @see #measureLineStoppingAtX(ColumnOffsetCache, Line, double) * * @param endColumn inclusive end column (we will end on or after end) * @param endX inclusive end x pixel width (we will end on or after endX) */ private void measureLine(ColumnOffsetCache cache, Line line, int endColumn, double endX) { /* * Starting at cache.measuredColumn we will use the regex to scan forward to * see if we hit an interesting character other than prefixed tab. if we do * we'll measure that to that point and append a {@link ColumnOffset} if it * is a special size. Rinse and repeat. */ LineDimensionsUtils.markTimeline(getClass(), "Beginning measure line"); RegExp regexp = UnicodeUtils.regexpNonAsciiTabOrCarriageReturn; regexp.setLastIndex(cache.measuredOffset.column); MatchResult result = regexp.exec(line.getText()); if (result != null) { double x = 0; do { // Calculate any x offset up to this point in the line ColumnOffset offset = cache.getLastColumnOffsetInCache(); double baseXOffset = smartColumnToX(offset, result.getIndex()); /* * TODO: we can be smarter here, if i > 1, then this character * is a mark. We could separate out the RegExp into non-spacing, * enclosing-marks v. spacing-marks and already know which are supposed * to be zero-width based on which groups are null. */ String match = result.getGroup(0); for (int i = 0; i < match.length(); i++) { x = addOffsetForResult(cache, match.charAt(i), result.getIndex() + i, line, baseXOffset); baseXOffset = x; } result = regexp.exec(line.getText()); // we have to ensure we measure through the last zero-width character. } while (result != null && result.getIndex() < endColumn && x < endX); } if (result == null) { cache.measuredOffset = ColumnOffsetCache.FULLY_MEASURED; return; } LineDimensionsUtils.markTimeline(getClass(), "Ending measure line"); }
From source file:com.google.collide.client.editor.search.SearchMatchRenderer.java
License:Open Source License
@Override public boolean resetToBeginningOfLine(Line line, int lineNumber) { assert model.getQuery() != null; assert model.getSearchPattern() != null; edges.clear();/*from ww w . j a v a 2 s . c o m*/ String text = line.getText(); RegExp regex = model.getSearchPattern(); /* * We must not forget to clear the lastIndex since it is a global regex, if * we don't it can lead to a false negative for matches. */ regex.setLastIndex(0); MatchResult match = regex.exec(text); if (match == null || match.getGroup(0).isEmpty()) { return false; } do { int start = regex.getLastIndex() - match.getGroup(0).length(); edges.add(start); edges.add(regex.getLastIndex()); match = regex.exec(text); } while (match != null && !match.getGroup(0).isEmpty()); // Handles the edge cases of matching at beginning or end of a line inMatch = true; if (edges.get(0) != 0) { inMatch = false; edges.splice(0, 0, 0); } if (edges.peek() != text.length()) { edges.add(text.length()); } return true; }
From source file:com.google.collide.shared.util.RegExpUtils.java
License:Open Source License
/** * Creates a regular expression which will match the given wildcard pattern * * Backslashes can be used to escape a wildcard character and make it a * literal; likewise, backslashes before wildcard characters can be escaped. */// w w w.j a v a2s.c o m private static String createRegExpStringForWildcardPattern(String wildcardPattern) { String escaped = regexpWildcardEscape.replace(wildcardPattern, "\\$&"); /** * We have already run the pattern through the naive regex escape which * escapes all characters except the * and ?. This leads to double escaped \ * characters that we have to inspect to determine if the user escaped the * wildcard or if we should replace it with it's regex equivalent. * * NOTE: * is replaced with \S+ (matches all non-whitespace characters) and * ? is replaced with a single \S to match any non-whitespace */ RegExp mimicLookbehind = RegExp.compile("([\\\\]*)([?*])", "g"); StringBuilder wildcardStr = new StringBuilder(escaped); for (MatchResult match = mimicLookbehind.exec( wildcardStr.toString()); match != null; match = mimicLookbehind.exec(wildcardStr.toString())) { // in some browsers an optional group is null, in others its empty string if (match.getGroup(1) != null && !match.getGroup(1).isEmpty()) { // We undo double-escaping of backslashes performed by the naive escape int offset = match.getGroup(1).length() / 2; wildcardStr.delete(match.getIndex(), match.getIndex() + offset); /* * An even number of slashes means the wildcard was not escaped so we * must replace it with its regex equivalent. */ if (offset % 2 == 0) { if (match.getGroup(2).equals("?")) { wildcardStr.replace(match.getIndex() + offset, match.getIndex() + offset + 1, "\\S"); // we added 1 more character, so we remove 1 less from the index offset -= 1; } else { wildcardStr.replace(match.getIndex() + offset, match.getIndex() + offset + 1, "\\S+"); // we added 2 characters, so we need to remove 2 less from the index offset -= 2; } } mimicLookbehind.setLastIndex(mimicLookbehind.getLastIndex() - offset); } else if (match.getGroup(2).equals("?")) { wildcardStr.replace(match.getIndex(), match.getIndex() + 1, "\\S"); mimicLookbehind.setLastIndex(mimicLookbehind.getLastIndex() + 1); } else { wildcardStr.replace(match.getIndex(), match.getIndex() + 1, "\\S+"); mimicLookbehind.setLastIndex(mimicLookbehind.getLastIndex() + 2); } } return wildcardStr.toString(); }
From source file:com.google.collide.shared.util.RegExpUtils.java
License:Open Source License
public static MatchResult findMatchBeforeIndex(RegExp regexp, String text, int exclusiveEndIndex) { regexp.setLastIndex(0); // Find the last match without going over our startIndex MatchResult lastMatch = null; for (MatchResult result = regexp.exec(text); result != null && result.getIndex() < exclusiveEndIndex; result = regexp.exec(text)) { lastMatch = result;//from w w w . ja va2 s . co m } return lastMatch; }
From source file:com.google.collide.shared.util.RegExpUtils.java
License:Open Source License
/** * Find the next match after exclusiveStartIndex. *///www. j a v a 2s.c o m public static MatchResult findMatchAfterIndex(RegExp regexp, String text, int exclusiveStartIndex) { regexp.setLastIndex(exclusiveStartIndex + 1); return regexp.exec(text); }
From source file:com.google.collide.shared.util.RegExpUtils.java
License:Open Source License
/** * Resets the RegExp lastIndex to 0 and returns the number of matches found in * a string by a regexp. If the regexp is not a global regexp this will return * a maximum of 1. This does not setLastIndex(0) automatically, you must do it * manually.//w ww . java2s. co m * * @returns number of matches */ public static int resetAndGetNumberOfMatches(RegExp regexp, String input) { regexp.setLastIndex(0); return getNumberOfMatches(regexp, input); }
From source file:com.google.collide.shared.util.RegExpUtils.java
License:Open Source License
/** * Resets the RegExp lastIndex to 0 before testing. This is only useful for * global RegExps.// w ww . j a va2 s .co m */ public static boolean resetAndTest(RegExp regexp, String input) { regexp.setLastIndex(0); return regexp.test(input); }
From source file:elemental.json.impl.JsonUtil.java
License:Apache License
/** * Execute a regular expression and invoke a callback for each match * occurance. The return value of the callback is substituted for the match. * * @param expression a compiled regular expression * @param text a String on which to perform replacement * @param replacer a callback that maps matched strings into new values *//*ww w . ja va 2 s.c o m*/ private static String replace(RegExp expression, String text, RegExpReplacer replacer) { expression.setLastIndex(0); MatchResult mresult = expression.exec(text); StringBuilder toReturn = new StringBuilder(); int lastIndex = 0; while (mresult != null) { toReturn.append(text.substring(lastIndex, mresult.getIndex())); toReturn.append(replacer.replace(mresult.getGroup(0))); lastIndex = mresult.getIndex() + 1; mresult = expression.exec(text); } toReturn.append(text.substring(lastIndex)); return toReturn.toString(); }