Java Bit Shift shiftKeyword(StringBuffer buffer, int posBegin, int posEnd, String keyword, boolean ignoreCase, boolean wholeWord)

Here you can find the source of shiftKeyword(StringBuffer buffer, int posBegin, int posEnd, String keyword, boolean ignoreCase, boolean wholeWord)

Description

Reads eventually the following text if the given keyword matches.

License

Open Source License

Parameter

Parameter Description
buffer is the buffer to parse
posBegin is the beginning index
posEnd is the ending index
keyword is the keyword to read in the text at the current index
ignoreCase indicates if the search mode is 'ignore case'
wholeWord indicates if the search mode is 'whole word'

Return

the ending index of the keyword, or the beginning index if it doesn't exist

Declaration

public static int shiftKeyword(StringBuffer buffer, int posBegin, int posEnd, String keyword,
        boolean ignoreCase, boolean wholeWord) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2008, 2012 Obeo./*  ww w  .j av a  2s .  co m*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Obeo - initial API and implementation
 *******************************************************************************/

public class Main {
    /**
     * Reads eventually the following text if the given keyword matches. Spaces are ignored.
     * 
     * @param buffer
     *            is the buffer to parse
     * @param posBegin
     *            is the beginning index
     * @param posEnd
     *            is the ending index
     * @param keyword
     *            is the keyword to read in the text at the current index
     * @param wholeWord
     *            indicates if the search mode is 'whole word'
     * @return the ending index of the keyword, or the beginning index if it doesn't exist
     */
    public static int shiftKeyword(StringBuffer buffer, int posBegin, int posEnd, String keyword,
            boolean wholeWord) {
        return shiftKeyword(buffer, posBegin, posEnd, keyword, false, wholeWord);
    }

    /**
     * Reads eventually the following text if the given keyword matches. Spaces are ignored.
     * 
     * @param buffer
     *            is the buffer to parse
     * @param posBegin
     *            is the beginning index
     * @param posEnd
     *            is the ending index
     * @param keyword
     *            is the keyword to read in the text at the current index
     * @param ignoreCase
     *            indicates if the search mode is 'ignore case'
     * @param wholeWord
     *            indicates if the search mode is 'whole word'
     * @return the ending index of the keyword, or the beginning index if it doesn't exist
     */
    public static int shiftKeyword(StringBuffer buffer, int posBegin, int posEnd, String keyword,
            boolean ignoreCase, boolean wholeWord) {
        if (keyword == null || keyword.length() == 0) {
            return posBegin;
        }
        int b = posBegin;
        while (b < posEnd && Character.isWhitespace(buffer.charAt(b))) {
            b++;
        }
        int result;
        if (b + keyword.length() <= posEnd) {
            int e = strictlyReadKeyword(buffer, b, keyword, ignoreCase);
            if ((b != e) && (e == posEnd || !wholeWord || !Character.isJavaIdentifierStart(buffer.charAt(e)))) {
                result = e;
            } else {
                result = posBegin;
            }
        } else {
            result = posBegin;
        }
        return result;
    }

    /**
     * Reads strictly the following text if the given keyword matches.
     * 
     * @param buffer
     *            is the buffer to parse
     * @param posBegin
     *            is the beginning index
     * @param keyword
     *            is the keyword to read in the text at the current index
     * @param ignoreCase
     *            indicates if the search mode is 'ignore case'
     * @return the ending index of the keyword, or the beginning index if it doesn't exist
     */
    private static int strictlyReadKeyword(StringBuffer buffer, int posBegin, String keyword, boolean ignoreCase) {
        int e = posBegin;
        for (int i = 0; i < keyword.length(); i++) {
            char keywordChar = keyword.charAt(i);
            char bufferChar = buffer.charAt(e);
            if (!similarCharacters(keywordChar, bufferChar, ignoreCase)) {
                return posBegin;
            }
            e++;
        }
        return e;
    }

    /**
     * Indicates if the characters are similar.
     * 
     * @param c1
     *            is the first character
     * @param c2
     *            is the second character
     * @param ignoreCase
     *            indicates if the case is ignored
     * @return true if the characters are similar
     */
    private static boolean similarCharacters(char c1, char c2, boolean ignoreCase) {
        if (ignoreCase) {
            return Character.toUpperCase(c1) == Character.toUpperCase(c2);
        }
        return c1 == c2;
    }
}

Related

  1. shiftByte(byte n, short shift)
  2. shiftCharacter(char c, int offset)
  3. shiftDouble(Object o, double shift, String suffix)
  4. shiftHorizontally(int inkX, int inkXWidth, int textWidth)
  5. shiftIdentifier(StringBuffer buffer, int posBegin, int posEnd)
  6. shiftLastAlphabets(String id)
  7. shiftLeft(char ch)
  8. shiftLeft(final Character orig, final int shiftDistance)
  9. shiftPath(String uri)