package org.eclipse.jface.text; | |
/* | |
* (c) Copyright IBM Corp. 2000, 2001. | |
* All Rights Reserved. | |
*/ | |
/** | |
* Collection of text functions. | |
*/ | |
class TextUtilities { | |
public final static String[] fgDelimiters= new String[] { "\n", "\r", "\r\n" }; //$NON-NLS-3$ //$NON-NLS-2$ //$NON-NLS-1$ | |
/** | |
* Determines which one of fgDelimiters appears first in the list. If none of them the | |
* hint is returned. | |
*/ | |
public static String determineLineDelimiter(String text, String hint) { | |
try { | |
int[] info= indexOf(fgDelimiters, text, 0); | |
return fgDelimiters[info[1]]; | |
} catch (ArrayIndexOutOfBoundsException x) { | |
} | |
return hint; | |
} | |
/** | |
* Returns the position in the string greater than offset | |
* of the longest matching search string. | |
*/ | |
public static int[] indexOf(String[] searchStrings, String text, int offset) { | |
int[] result= { -1, -1 }; | |
int zeroIndex= -1; | |
for (int i= 0; i < searchStrings.length; i++) { | |
int length= searchStrings[i].length(); | |
if (length == 0) { | |
zeroIndex= i; | |
continue; | |
} | |
int index= text.indexOf(searchStrings[i], offset); | |
if (index >= 0) { | |
if (result[0] == -1) { | |
result[0]= index; | |
result[1]= i; | |
} else if (index < result[0]) { | |
result[0]= index; | |
result[1]= i; | |
} else if (index == result[0] && length > searchStrings[result[1]].length()) { | |
result[0]= index; | |
result[1]= i; | |
} | |
} | |
} | |
if (zeroIndex > -1 && result[0] == -1) { | |
result[0]= 0; | |
result[1]= zeroIndex; | |
} | |
return result; | |
} | |
/** | |
* Returns the longest search string with which the given text ends. | |
*/ | |
public static int endsWith(String[] searchStrings, String text) { | |
int index= -1; | |
for (int i= 0; i < searchStrings.length; i++) { | |
if (text.endsWith(searchStrings[i])) { | |
if (index == -1 || searchStrings[i].length() > searchStrings[index].length()) | |
index= i; | |
} | |
} | |
return index; | |
} | |
/** | |
* Returns the longest search string with which the given text starts. | |
*/ | |
public static int startsWith(String[] searchStrings, String text) { | |
int index= -1; | |
for (int i= 0; i < searchStrings.length; i++) { | |
if (text.startsWith(searchStrings[i])) { | |
if (index == -1 || searchStrings[i].length() > searchStrings[index].length()) | |
index= i; | |
} | |
} | |
return index; | |
} | |
/** | |
* Returns whether the text equals one of the given compare strings. | |
*/ | |
public static int equals(String[] compareStrings, String text) { | |
for (int i= 0; i < compareStrings.length; i++) { | |
if (text.equals(compareStrings[i])) | |
return i; | |
} | |
return -1; | |
} | |
} |