edu.rice.cs.util
Class StringOps

java.lang.Object
  extended by edu.rice.cs.util.StringOps

public abstract class StringOps
extends Object

A class to provide some convenient String operations as static methods. It's abstract to prevent (useless) instantiation, though it can be subclassed to provide convenient namespace importation of its methods.

Version:
$Id: StringOps.java 4691 2008-12-02 23:33:27Z dlsmith $

Field Summary
static String EOL
           
static char newline
           
static String NEWLINE
           
static char SEPARATOR
           
 
Constructor Summary
StringOps()
           
 
Method Summary
static List<List<List<String>>> commandLineToLists(String cmdline)
          Convert a command line into a list of list of lists of individual arguments.
static String compress(String s)
           
static String convertToLiteral(String s)
          Converts the given string to a valid Java string literal.
static String encodeHTML(String s)
          Encode &, <, > and newlines as HTML entities.
static String escapeFileName(String s)
          Escapes spaces ' ' with the sequence " ", and a single '' with a double.
static String flatten(String s)
          Converts newline chars to SEPARATOR char (a solid black upright rectangle).
static String getBlankString(int n)
          Gets a string consisting of n blanks.
static Pair<Integer,Integer> getOffsetAndLength(String fullString, int startRow, int startCol, int endRow, int endCol)
          Gets the offset and length equivalent to the given pairs start and end row-col.
static String getSimpleName(Class<?> c)
          Returns the simple class name.
static String getStackTrace()
          Gets the stack trace of the current code.
static String getStackTrace(Throwable t)
          Gets the stack trace of the given Throwable as a String.
static boolean isAnonymousClass(Class<?> c)
          Returns true if the class is an anonymous inner class.
static boolean isAsciiDigit(char c)
          Character.isDigit answers true to some non-ascii digits.
static boolean isMemberClass(Class<?> c)
          Returns true if the class is a member class.
static String memSizeToString(long l)
          Return a string representing the approximate amount of memory specified in bytes.
static String replace(String fullString, String toReplace, String replacement)
          Takes theString fullString and replaces all instances of toReplace with replacement.
static String replaceVariables(String str, PropertyMaps props, Lambda2<DrJavaProperty,PropertyMaps,String> getter)
          Replace variables of the form "${variable}" with the value associated with the string "variable" in the provided hash table.
static String splitStringAtWordBoundaries(String s, int widthInChars, String lineBreak, String wordSepChars)
          Split a string into lines at a certain width, at word boundaries.
static String toString(boolean[] a)
          This works just like java.util.Arrays.toString in Java 5.0 but is not version-specific.
static String toString(byte[] a)
          This works just like java.util.Arrays.toString in Java 5.0 but is not version-specific.
static String toString(char[] a)
          This works just like java.util.Arrays.toString in Java 5.0 but is not version-specific.
static String toString(double[] a)
          This works just like java.util.Arrays.toString in Java 5.0 but is not version-specific.
static String toString(float[] a)
          This works just like java.util.Arrays.toString in Java 5.0 but is not version-specific.
static String toString(int[] a)
          This works just like java.util.Arrays.toString in Java 5.0 but is not version-specific.
static String toString(long[] a)
          This works just like java.util.Arrays.toString in Java 5.0 but is not version-specific.
static String toString(Object[] a)
          This works just like java.util.Arrays.toString in Java 5.0 but is not version-specific.
static String toString(short[] a)
          This works just like java.util.Arrays.toString in Java 5.0 but is not version-specific.
static String unescapeFileName(String s)
          Unescapes spaces the sequence " " to a space ' ', and a double '' to a single.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EOL

public static final String EOL

NEWLINE

public static final String NEWLINE
See Also:
Constant Field Values

newline

public static final char newline
See Also:
Constant Field Values

SEPARATOR

public static final char SEPARATOR
See Also:
Constant Field Values
Constructor Detail

StringOps

public StringOps()
Method Detail

getBlankString

public static String getBlankString(int n)
Gets a string consisting of n blanks. The values for n <= 16 are stored in a switch table.


replace

public static String replace(String fullString,
                             String toReplace,
                             String replacement)
Takes theString fullString and replaces all instances of toReplace with replacement. TODO: deprecate and used corresponding String method added in Java 5.0.


convertToLiteral

public static String convertToLiteral(String s)
Converts the given string to a valid Java string literal. All back slashes, quotes, new-lines, and tabs are converted to their escap character form, and the sourounding quotes are added.

Parameters:
s - the normal string to turn into a string literal
Returns:
the valid Java string literal

getOffsetAndLength

public static Pair<Integer,Integer> getOffsetAndLength(String fullString,
                                                       int startRow,
                                                       int startCol,
                                                       int endRow,
                                                       int endCol)
Gets the offset and length equivalent to the given pairs start and end row-col.

Parameters:
fullString - the string in which to compute the offset/length
startRow - the row on which the error starts, starting at one for the first row
startCol - the col on which the error starts, starting at one for the first column
endRow - the row on which the error ends. Equals the startRow for one-line errors
endCol - the character position on which the error ends. Equals the startCol for one-character errors.
Returns:
a Pair of which the first is the offset, the second is the length

getStackTrace

public static String getStackTrace(Throwable t)
Gets the stack trace of the given Throwable as a String.

Parameters:
t - the throwable object for which to get the stack trace
Returns:
the stack trace of the given Throwable

getStackTrace

public static String getStackTrace()
Gets the stack trace of the current code. Does not include this method.

Returns:
the stack trace for the current code

isAsciiDigit

public static boolean isAsciiDigit(char c)
Character.isDigit answers true to some non-ascii digits. This one does not.


isAnonymousClass

public static boolean isAnonymousClass(Class<?> c)
Returns true if the class is an anonymous inner class. This works just like Class.isAnonymousClass() in Java 5.0 but is not version-specific.

Parameters:
c - class to check
Returns:
true if anonymous inner class

isMemberClass

public static boolean isMemberClass(Class<?> c)
Returns true if the class is a member class. This works just like Class.isMemberClass() in Java 5.0 but is not version-specific.

Parameters:
c - class to check
Returns:
true if member class

getSimpleName

public static String getSimpleName(Class<?> c)
Returns the simple class name. This works just like Class.getSimpleName() in Java 5.0 but is not version-specific.

Parameters:
c - class for which to get the simple name
Returns:
simple name

toString

public static String toString(long[] a)
This works just like java.util.Arrays.toString in Java 5.0 but is not version-specific.


toString

public static String toString(int[] a)
This works just like java.util.Arrays.toString in Java 5.0 but is not version-specific.


toString

public static String toString(short[] a)
This works just like java.util.Arrays.toString in Java 5.0 but is not version-specific.


toString

public static String toString(char[] a)
This works just like java.util.Arrays.toString in Java 5.0 but is not version-specific.


toString

public static String toString(byte[] a)
This works just like java.util.Arrays.toString in Java 5.0 but is not version-specific.


toString

public static String toString(boolean[] a)
This works just like java.util.Arrays.toString in Java 5.0 but is not version-specific.


toString

public static String toString(float[] a)
This works just like java.util.Arrays.toString in Java 5.0 but is not version-specific.


toString

public static String toString(double[] a)
This works just like java.util.Arrays.toString in Java 5.0 but is not version-specific.


toString

public static String toString(Object[] a)
This works just like java.util.Arrays.toString in Java 5.0 but is not version-specific.


encodeHTML

public static String encodeHTML(String s)
Encode &, <, > and newlines as HTML entities.

Parameters:
s - string to encode
Returns:
encoded string

compress

public static String compress(String s)

flatten

public static String flatten(String s)
Converts newline chars to SEPARATOR char (a solid black upright rectangle).


memSizeToString

public static String memSizeToString(long l)
Return a string representing the approximate amount of memory specified in bytes.

Parameters:
l - memory in bytes
Returns:
string approximating the amount of memory

escapeFileName

public static String escapeFileName(String s)
Escapes spaces ' ' with the sequence " ", and a single '' with a double. It treats File.pathSeparatorChar (';' or ':'), ProcessChain.PROCESS_SEPARATOR_CHAR ('#'), ProcessChain.PIPE_SEPARATOR_CHAR, and ':' (for Windows drive letters) the same way. '' was picked because its ASCII meaning is 'escape', and it should be platform-independent. This method keeps file names with spaces, pound, colons and semicolons together and prevents them from being split apart.

Parameters:
s - string to encode
Returns:
encoded string

unescapeFileName

public static String unescapeFileName(String s)
Unescapes spaces the sequence " " to a space ' ', and a double '' to a single. '' was picked because its ASCII meaning is 'escape', and it should be platform-independent.

Parameters:
s - string to encode
Returns:
encoded string

commandLineToLists

public static List<List<List<String>>> commandLineToLists(String cmdline)
Convert a command line into a list of list of lists of individual arguments. The outermost list is a list of list of lists of arguments for processes separated by ProcessChain.PROCESS_SEPARATOR (either ';' or ':', depending on which is NOT File.pathSeparatorChar). The lists contained in the outermost list are lists of lists of arguments for processes in the same piping chain, i.e. processes separated by '|'. The innermost lists are lists of arguments for the individual processes. This method keeps quoted parts together using ", ' and `. It also keeps treats a '' followed by a space as non-breaking space. And a double '' becomes a single ''. It does not allow escaping of the quote characters.


replaceVariables

public static String replaceVariables(String str,
                                      PropertyMaps props,
                                      Lambda2<DrJavaProperty,PropertyMaps,String> getter)
Replace variables of the form "${variable}" with the value associated with the string "variable" in the provided hash table. To give the "$" character its literal meaning, it needs to be escaped as "\$" (backslash dollar). To make the "\" character not escaping, escape it as "\\"(double backslash).

Parameters:
str - input string
props - map with maps of variable-value pairs
getter - lambda from a DrJavaProperty to String
Returns:
string with variables replaced by values

splitStringAtWordBoundaries

public static String splitStringAtWordBoundaries(String s,
                                                 int widthInChars,
                                                 String lineBreak,
                                                 String wordSepChars)
Split a string into lines at a certain width, at word boundaries.

Parameters:
s - string to split
widthInChars - approximate width of the new lines
lineBreak - string to be inserted at line breaks
wordSepChars - string of characters that can serve as word separators