List of usage examples for com.google.gwt.regexp.shared MatchResult getIndex
public final int getIndex()
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; 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()); }/*from w ww . j av a 2s. c o m*/ } return str; }
From source file:com.codenvy.ide.ext.java.jdt.text.FindReplaceDocumentAdapter.java
License:Open Source License
/** * Stateful findReplace executes a FIND, REPLACE, REPLACE_FIND or FIND_FIRST operation. In case of REPLACE and REPLACE_FIND it * sends a <code>DocumentEvent</code> to all registered <code>IDocumentListener</code>. * * @param startOffset/* w ww . j a v a 2s .c om*/ * document offset at which search starts this value is only used in the FIND_FIRST operation and otherwise * ignored * @param findString * the string to find this value is only used in the FIND_FIRST operation and otherwise ignored * @param replaceText * the string to replace the current match this value is only used in the REPLACE and REPLACE_FIND * operations and otherwise ignored * @param forwardSearch * the search direction * @param caseSensitive * indicates whether lower and upper case should be distinguished * @param wholeWord * indicates whether the findString should be limited by white spaces as defined by Character.isWhiteSpace. * Must not be used in combination with <code>regExSearch</code>. * @param regExSearch * if <code>true</code> this operation represents a regular expression Must not be used in combination with * <code>wholeWord</code>. * @param operationCode * specifies what kind of operation is executed * @return the find or replace region or <code>null</code> if there was no match * @throws com.codenvy.ide.api.text.BadLocationException * if startOffset is an invalid document offset * @throws IllegalStateException * if a REPLACE or REPLACE_FIND operation is not preceded by a successful FIND operation * @throws PatternSyntaxException * if a regular expression has invalid syntax */ private Region findReplace(final FindReplaceOperationCode operationCode, int startOffset, String findString, String replaceText, boolean forwardSearch, boolean caseSensitive, boolean wholeWord) throws BadLocationException { // Validate state if ((operationCode == REPLACE || operationCode == REPLACE_FIND_NEXT) && (fFindReplaceState != FIND_FIRST && fFindReplaceState != FIND_NEXT)) throw new IllegalStateException("illegal findReplace state: cannot replace without preceding find"); //$NON-NLS-1$ if (operationCode == FIND_FIRST) { // Reset if (findString == null || findString.length() == 0) return null; // Validate start offset if (startOffset < 0 || startOffset >= length()) throw new BadLocationException(); String patternFlags = "g"; if (caseSensitive) patternFlags += "i"; if (wholeWord) findString = "\\b" + findString + "\\b"; //$NON-NLS-1$ //$NON-NLS-2$ if (!wholeWord) findString = asRegPattern(findString); fFindReplaceMatchOffset = startOffset; regExp = RegExp.compile(findString, patternFlags); regExp.setLastIndex(fFindReplaceMatchOffset); } // Set state fFindReplaceState = operationCode; if (operationCode != REPLACE) { if (forwardSearch) { MatchResult matchResult = regExp.exec(String.valueOf(this)); if (matchResult != null && matchResult.getGroupCount() > 0 && !matchResult.getGroup(0).isEmpty()) return new RegionImpl(matchResult.getIndex(), matchResult.getGroup(0).length()); return null; } // backward search regExp.setLastIndex(0); MatchResult matchResult = regExp.exec(String.valueOf(this)); boolean found = matchResult != null; int index = -1; int length = -1; while (found && matchResult.getIndex() + matchResult.getGroup(0).length() <= fFindReplaceMatchOffset + 1) { index = matchResult.getIndex(); length = matchResult.getGroup(0).length(); regExp.setLastIndex(index + 1); matchResult = regExp.exec(String.valueOf(this)); found = matchResult != null; } fFindReplaceMatchOffset = index; if (index > -1) { // must set matcher to correct position regExp.setLastIndex(index); matchResult = regExp.exec(String.valueOf(this)); return new RegionImpl(index, length); } return null; } return null; }
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/>// w ww. j ava2 s. c o m * <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.gafactory.core.client.utils.StringUtils.java
License:Open Source License
public static String markFound(final String str, final RegExp pattern) { Preconditions.checkNotNull(str);// ww w . j ava 2 s . c o m final StringBuilder sb = new StringBuilder(); String substring = str; MatchResult exec = pattern.exec(str); while (exec != null) { final int start = exec.getIndex(); sb.append(substring.substring(0, start)).append("<b>"); final String occurance = exec.getGroup(0); sb.append(occurance).append("</b>"); substring = substring.substring(exec.getIndex() + occurance.length()); exec = pattern.exec(substring); } sb.append(substring); return sb.toString(); }
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>/*w w w. j a v a 2 s.c o m*/ * 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.SearchMatchManager.java
License:Open Source License
/** * Selects the next match using the search pattern given line and startIndex. * * @param startIndex The boundary to find the next match after. * @param endIndex The boundary to find the next match before. * * @returns true if match is found/*from w ww . j a v a2s . c o m*/ */ private boolean selectNextMatchOnLine(LineInfo line, int startIndex, int endIndex) { searchPattern.setLastIndex(startIndex); MatchResult result = searchPattern.exec(line.line().getText()); if (result == null || result.getIndex() >= endIndex) { return false; } moveAndSelectMatch(line, result.getIndex(), result.getGroup(0).length()); return true; }
From source file:com.google.collide.client.editor.search.SearchMatchManager.java
License:Open Source License
/** * Selects the previous match using the search pattern given line and * startIndex./* w w w . ja v a 2 s.c om*/ * * @param startIndex The boundary to find a previous match after. * @param endIndex The boundary to find a previous match before. * * @returns true if a match is found */ private boolean selectPreviousMatchOnLine(LineInfo line, int startIndex, int endIndex) { searchPattern.setLastIndex(0); // Find the last match without going over our startIndex MatchResult lastMatch = null; for (MatchResult result = searchPattern.exec(line.line().getText()); result != null && result.getIndex() < endIndex && result.getIndex() >= startIndex; result = searchPattern.exec(line.line().getText())) { lastMatch = result; } if (lastMatch == null) { return false; } moveAndSelectMatch(line, lastMatch.getIndex(), lastMatch.getGroup(0).length()); return true; }
From source file:com.google.collide.clientlibs.invalidation.InvalidationLogger.java
License:Open Source License
/** * GWT does not emulate string formatting so we just do a simple stupid one which looks for %s or * %d and replaces it with an arg. If there are less args than %markers we put in [MISSING ARG]. * If there are more args than %markers we just append them at the end within brackets. *//*from www. j av a 2 s. c o m*/ private static String format(String template, Object... args) { StringBuilder builder = new StringBuilder(); ArgumentFormatHelper helper = new ArgumentFormatHelper(args); RegExp formatMatcher = RegExp.compile("(%s)|(%d)", "ig"); int lastIndex = 0; MatchResult result = formatMatcher.exec(template); while (result != null) { String fragment = template.substring(lastIndex, result.getIndex() - 1); builder.append(fragment); builder.append(helper.next()); lastIndex = result.getIndex() + result.getGroup(0).length(); } String lastFragment = template.substring(lastIndex, template.length()); builder.append(lastFragment); builder.append(helper.rest()); return builder.toString(); }
From source file:com.google.collide.shared.util.TextUtils.java
License:Open Source License
/** * Finds the next character which is not a mark or other character. Will * return column if the end of the line is reached or column is a non-mark or * other character./*from w w w. java 2 s .c o m*/ */ public static int findNextCharacterInclusive(String text, int column) { MatchResult result = RegExpUtils .findMatchAfterIndex(UnicodeUtils.regexpNotMarkOrOtherExcludingTabAndNewline, text, column - 1); // if result is null, then it's likely we're at the \n (I think). return result == null ? column : result.getIndex(); }
From source file:com.google.collide.shared.util.TextUtils.java
License:Open Source License
/** * Finds the next character which is not a combining character. *//*w ww .j a v a2 s . c o m*/ public static int findNonMarkNorOtherCharacter(String text, int column) { /* * If moving forward: if next character is combining mark, skip to next * non-combining mark character, else go forward one character. */ if (column + 1 >= text.length()) { return text.length() + 1; } MatchResult match = RegExpUtils.findMatchAfterIndex(UnicodeUtils.regexpNotMarkOrOtherExcludingTabAndNewline, text, column); if (match == null) { return text.length() + 1; } else { return match.getIndex(); } }