com.taco.text
Class StringUtilities

java.lang.Object
  extended by com.taco.text.StringUtilities
All Implemented Interfaces:
ICommonRegexConstants

public class StringUtilities
extends java.lang.Object
implements ICommonRegexConstants

Miscellaneous utilities functions for strings.


Field Summary
static java.lang.String CHARACTERS_TO_ESCAPE_IN_DOUBLE_QUOTES
          Characters that should be escaped with a backslash before output assuming that the character appears inside a double quoted expression in Java.
static int NOT_FOUND
          An error code returned by findStart() and findEnd() that indicates that the substring searched is only whitespace, so the string that was searched for was never found.
static int UNEXPECTED_CHARACTER
          An error code returned by findStart() and findEnd() that indicates that non-whitespace characters were found before the string that was searched for.
 
Fields inherited from interface com.taco.text.ICommonRegexConstants
BOOLEAN_REGEX_STRING, CHAR_REGEX_STRING, FLOAT_REGEX_STRING, HEX_NUMBER_REGEX_STRING, INTEGER_REGEX_STRING, JAVA_CLASS_NAME_REGEX_STRING, LONG_HEX_REGEX_STRING, LONG_INTEGER_REGEX_STRING, NON_NEGATIVE_FLOAT_REGEX_STRING, PROPERTY_PREFIX_PATTERN, PROPERTY_PREFIX_REGEX_STRING, QUOTED_STRING_REGEX_STRING, WHITESPACE_PATTERN
 
Constructor Summary
protected StringUtilities()
          Since this class is a static utility class, its sole constructor is protected.
 
Method Summary
static java.lang.StringBuffer appendEscapedChar(java.lang.StringBuffer sb, char c, java.lang.String charsToEscape)
          Append the escaped version of the character to the argument string buffer.
static java.lang.String escape(java.lang.CharSequence cs)
          Return escape(cs, CHARACTERS_TO_ESCAPE_IN_DOUBLE_QUOTES), which is suitable for a double-quoted expression in Java.
static java.lang.String escape(java.lang.CharSequence cs, java.lang.String charsToEscape)
          Return the escaped version of the characters in cs.
static int findEnd(java.lang.CharSequence s1, java.lang.CharSequence s2, int startIndex)
          Call findEnd(s1, s2, startIndex, true) (compare case-insensitively).
static int findEnd(java.lang.CharSequence s1, java.lang.CharSequence s2, int startIndex, boolean ignoreCase)
          Scan s1 for the first occurance of s2 at or after the start index.
static MatchResult findRawChar(java.lang.CharSequence cs, int startIndex, char c)
          Find the next raw occurance of c in cs starting from startIndex.
static int findStart(java.lang.CharSequence s1, java.lang.CharSequence s2, int startIndex)
          Call findStart(s1, s2, startIndex, true) (compare case-insensitively).
static int findStart(java.lang.CharSequence s1, java.lang.CharSequence s2, int startIndex, boolean ignoreCase)
          Scan s1 for the first occurance of s2 at or after the start index.
static int findWhitespace(java.lang.CharSequence cs, int startIndex)
          Starting from startIndex, return the index of first whitespace character in the input sequence.
static int indexOf(java.lang.CharSequence cs, char c, int startIndex, int endIndex)
          Find the first index of cs greater than or equal to startIndex and less than endIndex that contains the argument character.
static boolean startsWith(java.lang.CharSequence cs, java.lang.CharSequence prefix, int toffset, boolean ignoreCase)
          Like String.startsWith(String, int), but take instances of CharSequence, and allow the comparison to be done case-insensitively.
static java.lang.String unbrace(java.lang.CharSequence cs, char braceChar)
          Given a character sequence that starts with a opening C punctuation character and ends with the corresponding C punctutation character, return the contents of the sequence inside the punctuation characters, stripped of whitespace if the brace character is not a quote character.
static java.lang.String unescape(java.lang.CharSequence s)
          Convert all escaped characters back to the original ones.
static java.lang.String unescapeQuoted(java.lang.CharSequence cs, boolean stripQuotes)
          Given a non-null string which either starts and ends in double quotes, or has appeared in a double-quoted context, convert all escaped characters back to the original ones.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NOT_FOUND

public static final int NOT_FOUND
An error code returned by findStart() and findEnd() that indicates that the substring searched is only whitespace, so the string that was searched for was never found. This is less than 0 to avoid confusion with valid indices.

See Also:
Constant Field Values

UNEXPECTED_CHARACTER

public static final int UNEXPECTED_CHARACTER
An error code returned by findStart() and findEnd() that indicates that non-whitespace characters were found before the string that was searched for. This is less than 0 to avoid confusion with valid indices.

See Also:
Constant Field Values

CHARACTERS_TO_ESCAPE_IN_DOUBLE_QUOTES

public static final java.lang.String CHARACTERS_TO_ESCAPE_IN_DOUBLE_QUOTES
Characters that should be escaped with a backslash before output assuming that the character appears inside a double quoted expression in Java.

See Also:
Constant Field Values
Constructor Detail

StringUtilities

protected StringUtilities()
Since this class is a static utility class, its sole constructor is protected.

Method Detail

unescape

public static final java.lang.String unescape(java.lang.CharSequence s)
                                       throws java.text.ParseException
Convert all escaped characters back to the original ones.

Throws:
java.text.ParseException

unescapeQuoted

public static final java.lang.String unescapeQuoted(java.lang.CharSequence cs,
                                                    boolean stripQuotes)
                                             throws java.text.ParseException
Given a non-null string which either starts and ends in double quotes, or has appeared in a double-quoted context, convert all escaped characters back to the original ones. If stripQuotes is true, make sure the first and last character are double quotes and remove them.

Throws:
java.text.ParseException

escape

public static final java.lang.String escape(java.lang.CharSequence cs)
Return escape(cs, CHARACTERS_TO_ESCAPE_IN_DOUBLE_QUOTES), which is suitable for a double-quoted expression in Java.


escape

public static final java.lang.String escape(java.lang.CharSequence cs,
                                            java.lang.String charsToEscape)
Return the escaped version of the characters in cs. Any character in charsToEscape will be escaped with a backslash before being output.


appendEscapedChar

public static final java.lang.StringBuffer appendEscapedChar(java.lang.StringBuffer sb,
                                                             char c,
                                                             java.lang.String charsToEscape)
Append the escaped version of the character to the argument string buffer. Any character in charsToEscape will be escaped with a backslash before being output. Return sb.


indexOf

public static final int indexOf(java.lang.CharSequence cs,
                                char c,
                                int startIndex,
                                int endIndex)
Find the first index of cs greater than or equal to startIndex and less than endIndex that contains the argument character. If the character cannot be found, return -1.


findStart

public static final int findStart(java.lang.CharSequence s1,
                                  java.lang.CharSequence s2,
                                  int startIndex)
Call findStart(s1, s2, startIndex, true) (compare case-insensitively).


findStart

public static final int findStart(java.lang.CharSequence s1,
                                  java.lang.CharSequence s2,
                                  int startIndex,
                                  boolean ignoreCase)
Scan s1 for the first occurance of s2 at or after the start index. Skipping only whitespace, return the index of the character at which s2 appears. If any non-whitespace character appears before s2 appears, return NOT_FOUND. If s2 never appears, return UNEXPECTED_CHARACTER. Initial whitespace will be skipped regardless of what s2 is, so findStart(s1, "", 0) can be used to located the end of the initial block of whitespace in s1. If s1 is all whitespace starting from startIndex, and s2 is "", return the length of s1. If ignoreCase is true, string comparisons will be made case-insensitively.


findEnd

public static final int findEnd(java.lang.CharSequence s1,
                                java.lang.CharSequence s2,
                                int startIndex)
Call findEnd(s1, s2, startIndex, true) (compare case-insensitively).


findEnd

public static final int findEnd(java.lang.CharSequence s1,
                                java.lang.CharSequence s2,
                                int startIndex,
                                boolean ignoreCase)
Scan s1 for the first occurance of s2 at or after the start index. Return the index of the character just after s2 appears. If any non-whitespace character appears before s2 appears, return NOT_FOUND. If s2 never appears, return UNEXPECTED_CHARACTER. Note that string comparisons are made case-sensitively.


startsWith

public static final boolean startsWith(java.lang.CharSequence cs,
                                       java.lang.CharSequence prefix,
                                       int toffset,
                                       boolean ignoreCase)
Like String.startsWith(String, int), but take instances of CharSequence, and allow the comparison to be done case-insensitively. If prefix is "" and toffset is within cs, return true.


findWhitespace

public static final int findWhitespace(java.lang.CharSequence cs,
                                       int startIndex)
Starting from startIndex, return the index of first whitespace character in the input sequence. If no whitespace is found, return NOT_FOUND.


unbrace

public static final java.lang.String unbrace(java.lang.CharSequence cs,
                                             char braceChar)
                                      throws java.text.ParseException
Given a character sequence that starts with a opening C punctuation character and ends with the corresponding C punctutation character, return the contents of the sequence inside the punctuation characters, stripped of whitespace if the brace character is not a quote character. The contents inside < and > can also be unbraced with this method.

Throws:
java.text.ParseException

findRawChar

public static final MatchResult findRawChar(java.lang.CharSequence cs,
                                            int startIndex,
                                            char c)
                                     throws java.text.ParseException
Find the next raw occurance of c in cs starting from startIndex. Occurances of c that are escaped will be ignored. Copy startIndex to the startIndex field of the returned result. Set the endIndex field of the returned result to the location of the raw occurance. Set the s field of the returned result to the portion of cs between startIndex and endIndex, except that escaped occurances of c are unescaped. If no raw occurance of c occurs before the end of cs, treat cs as if it were terminated by a raw occurance of c. If startIndex is the length of cs or cs.charAt(startIndex) == '#', return a MatchResult with its s field set to the empty string, and its startIndex and endIndex fields set to startIndex.

Throws:
java.lang.NullPointerException - if cs is null
java.lang.IndexOutOfBoundsException - if startIndex is greater than the length of cs.
java.text.ParseException