001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: ******************************************************************************/package org.eclipse.ui.internal.quickaccess;
011:
012: import java.util.ArrayList;
013: import java.util.List;
014:
015: /**
016: * Utility methods for camel case style matching.
017: *
018: * @since 3.3
019: *
020: */
021: public class CamelUtil {
022:
023: /**
024: * Returns a lowercase string consisting of all initials of the words in the
025: * given String. Words are separated by whitespace and other special
026: * characters, or by uppercase letters in a word like CamelCase.
027: *
028: * @param s
029: * the string
030: * @return a lowercase string containing the first character of every wordin
031: * the given string.
032: */
033: public static String getCamelCase(String s) {
034: StringBuffer result = new StringBuffer();
035: if (s.length() > 0) {
036: int index = 0;
037: while (index != -1) {
038: result.append(s.charAt(index));
039: index = getNextCamelIndex(s, index + 1);
040: }
041: }
042: return result.toString().toLowerCase();
043: }
044:
045: /**
046: * Return an array with start/end indices for the characters used for camel
047: * case matching, ignoring the first (start) many camel case characters.
048: * For example, getCamelCaseIndices("some CamelCase", 1, 2) will return
049: * {{5,5},{10,10}}.
050: *
051: * @param s the source string
052: * @param start how many characters of getCamelCase(s) should be ignored
053: * @param length for how many characters should indices be returned
054: * @return an array of length start
055: */
056: public static int[][] getCamelCaseIndices(String s, int start,
057: int length) {
058: List result = new ArrayList();
059: int index = 0;
060: while (start > 0) {
061: index = getNextCamelIndex(s, index + 1);
062: start--;
063: }
064: while (length > 0) {
065: result.add(new int[] { index, index });
066: index = getNextCamelIndex(s, index + 1);
067: length--;
068: }
069: return (int[][]) result.toArray(new int[result.size()][]);
070: }
071:
072: /**
073: * Returns the next index to be used for camel case matching.
074: *
075: * @param s the string
076: * @param index the index
077: * @return the next index, or -1 if not found
078: */
079: public static int getNextCamelIndex(String s, int index) {
080: char c;
081: while (index < s.length()
082: && !(isSeparatorForCamelCase(c = s.charAt(index)))
083: && Character.isLowerCase(c)) {
084: index++;
085: }
086: while (index < s.length()
087: && isSeparatorForCamelCase(c = s.charAt(index))) {
088: index++;
089: }
090: if (index >= s.length()) {
091: index = -1;
092: }
093: return index;
094: }
095:
096: /**
097: * Returns true if the given character is to be considered a separator
098: * for camel case matching purposes.
099: *
100: * @param c the character
101: * @return true if the character is a separator
102: */
103: public static boolean isSeparatorForCamelCase(char c) {
104: return !Character.isLetterOrDigit(c);
105: }
106:
107: }
|