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 5408 2010-11-16 06:52:07Z mgricken $

Field Summary
private static String blank0
          Defines blank[k] (k = 0,..,16) as a string consisting of k blanks
private static String blank1
           
private static String blank10
           
private static String blank11
           
private static String blank12
           
private static String blank13
           
private static String blank14
           
private static String blank15
           
private static String blank16
           
private static String blank2
           
private static String blank3
           
private static String blank4
           
private static String blank5
           
private static String blank6
           
private static String blank7
           
private static String blank8
           
private static String blank9
           
static String EOL
           
static char newline
           
static String NEWLINE
           
static char SEPARATOR
           
 
Constructor Summary
StringOps()
           
 
Method Summary
private static void _ensureColInRow(String fullString, int col, int rowStartIndex)
          Verifies that the given column position is within the row at rowStartIndex in the given String.
private static void _ensureStartBeforeEnd(int startRow, int startCol, int endRow, int endCol)
          Verifies that (startRow, startCol) occurs before (endRow, endCol).
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.
private static String makeBlankString(int n)
          Constructs a new string containng n blanks.
static String memSizeToString(long l)
          Return a string representing the approximate amount of memory specified in bytes.
static String removeHTML(String s)
          Remove HTML tags from the string.
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 toStringHexDump(String s)
          Return a string containing a hexdump of the input string.
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

blank0

private static final String blank0
Defines blank[k] (k = 0,..,16) as a string consisting of k blanks

See Also:
Constant Field Values

blank1

private static final String blank1

blank2

private static final String blank2

blank3

private static final String blank3

blank4

private static final String blank4

blank5

private static final String blank5

blank6

private static final String blank6

blank7

private static final String blank7

blank8

private static final String blank8

blank9

private static final String blank9

blank10

private static final String blank10

blank11

private static final String blank11

blank12

private static final String blank12

blank13

private static final String blank13

blank14

private static final String blank14

blank15

private static final String blank15

blank16

private static final String blank16
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.


makeBlankString

private static String makeBlankString(int n)
Constructs a new string containng n blanks. Intended for small values of n (typically < 50).


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

_ensureStartBeforeEnd

private static void _ensureStartBeforeEnd(int startRow,
                                          int startCol,
                                          int endRow,
                                          int endCol)
Verifies that (startRow, startCol) occurs before (endRow, endCol).

Throws:
IllegalArgumentException - if end is before start

_ensureColInRow

private static void _ensureColInRow(String fullString,
                                    int col,
                                    int rowStartIndex)
Verifies that the given column position is within the row at rowStartIndex in the given String.

Parameters:
fullString - the string in which to check the column
col - the column index that should be within the row
rowStartIndex - the first index of the row within fullString that col should be in
Throws:
IllegalArgumentException - if col is after the end of the given row

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

toStringHexDump

public static String toStringHexDump(String s)
Return a string containing a hexdump of the input string. The string will be formatted in the canonical hexdump format: xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx | aaaaaaaaaaaaaaaa

Parameters:
s - string to dump
Returns:
hexdump string

removeHTML

public static String removeHTML(String s)
Remove HTML tags from the string. Based on http://stackoverflow.com/questions/240546/removing-html-from-a-java-string

Parameters:
s - string with HTML tags
Returns:
string without HTML tags.