gnu.iou
Class linebuf

java.lang.Object
  |
  +--gnu.iou.linebuf

public class linebuf
extends java.lang.Object

Line oriented string buffer, or dynamic string array container. Plus string utility methods. For an array of strings this is faster than a Vector which is synchronized and generalized (not String).

This uses character arrays internally because more intensive use of the line buffer transforms Strings to character arrays and back into String objects and then back into character arrays. And in the very common `toString()' case a String will be concatenated as character arrays, too.

Author:
John Pritchard (john@syntelos.org)

Field Summary
protected  char[][] buf
           
static char[] default_line_separator
           
protected  int p
           
static java.lang.String sys_line_separator
           
 
Constructor Summary
linebuf()
          Default growth factor of ten.
linebuf(int growthfactor)
           
linebuf(java.lang.Object obj)
           
linebuf(java.lang.String line)
          Append argument line.
linebuf(java.lang.String[] lineset)
          Append argument lines.
linebuf(java.lang.String[] lineset, java.lang.String line_sep)
          Append argument lines.
linebuf(java.lang.String prefix, java.util.Enumeration lineset, java.lang.String line_sep)
          Append argument lines.
linebuf(java.lang.String prefix, java.lang.Object[] lineset, java.lang.String line_sep)
          Append argument lines.
linebuf(java.lang.String prefix, java.lang.String[] lineset, java.lang.String line_sep)
          Append argument lines.
 
Method Summary
 linebuf append(char[] line)
          Append line, return this.
 linebuf append(char[] line, int ofs, int len)
          Append line, return this.
 linebuf append(java.lang.Object o)
           
 linebuf append(java.lang.Object[] oary)
          Will insert null line elements.
 linebuf append(java.lang.String line)
          Append line, return this.
 linebuf append(java.lang.String[] lines)
          Will insert null line elements.
 linebuf append(java.lang.String s, java.lang.String delimiters)
          Split input string `s' on delimiter characters, and add each resulting token as an element- line.
 void backspace(int rowidx, int colidx)
           
 void delete(int rowidx, int colidx)
           
static char[] detab(char[] scary)
           
static char[] detab(char[] scary, int ofs, int len)
           
static char[] detab(java.lang.String s)
          Convert each tab to five spaces.
static java.lang.String indent(int lvl, java.lang.String text)
          Insert 'lvl' tabs before each line of a multiline text, else return text (use leveltabs for a single line).
 void index_append(int idx, char[] val)
           
 void index_append(int idx, java.lang.String val)
           
 char[] index_cary(int idx)
           
 void index_column_overwrite(int idx, char[] col, int col0, int colw)
          Use the first `colw' characters of `col' to overwrite the "[col0,colw)" segment of the line.
 void index_column_overwrite(int idx, java.lang.String col, int col0, int colw)
          Use the first `colw' characters of `col' to overwrite the "[col0,colw)" segment of the line.
 void index_prepend(int idx, java.lang.String val)
           
 int index()
           
 java.lang.String index(int idx)
           
 void index(int idx, char[] val)
           
 void index(int idx, java.lang.String val)
           
 void insert(char key, int rowidx, int colidx)
           
 linebuf invert()
          Invert (reverse) the order of strings in the linebuffer.
static linebuf invert(java.lang.String s, linebuf lb, java.lang.String delimiters)
           
static java.lang.String[] invert(java.lang.String s, java.lang.String delim)
           
 int length()
          Number of lines.
 void line_append(java.lang.String s)
           
 java.lang.String line_separator()
           
 linebuf line_separator(java.lang.String sep)
          Reset the line separator used by `toString'.
 linebuf lines_append(java.lang.String val)
          Convenience method to append string value to elements currently in the line buffer.
 linebuf lines_prepend(java.lang.String val)
          Convenience method to prepend string value to elements currently in the line buffer.
 void pop()
          Delete the first line
 linebuf println()
          Append null line, return this.
 linebuf reset()
          Discard content, reuse buffer.
static java.lang.String[] space_quoted(java.lang.String line)
          Parse series of space separated, optionally quoted strings.
static char[] sptab(java.lang.String s)
          Convert each tab to a space.
 char[] toCharArray()
          Terminate each line with the line separator string, by default this is the local platform newline (eg, CRLF (Mac/ Win) or LF (Unix)).
 char[][] toCharCharArray()
          Returns lines or null.
 java.lang.String toString()
          Terminate each line with the line separator string, by default this is the local platform newline (eg, CRLF (Mac/ Win) or LF (Unix)).
static java.lang.String toString(java.lang.Object obj)
           
static java.lang.String toString(java.lang.Object[] oary)
           
static java.lang.String[] toStringArray_term(java.lang.String s, java.lang.String term)
          Use the `term' argument literally, not as a set of delimiter characters but as a whole term delimiting substrings in `s'.
 java.lang.String[] toStringArray()
          Returns lines or null.
static java.lang.String[] toStringArray(java.io.DataInputStream din)
           
static java.lang.String[] toStringArray(java.util.Enumeration en)
           
static linebuf toStringArray(java.util.Enumeration en, linebuf lb)
           
static java.lang.String[] toStringArray(java.lang.Object o)
           
static java.lang.String[] toStringArray(java.lang.String s)
          Parses input using default delimiters of carriage return and newline (CR and LF).
static java.lang.String[] toStringArray(java.lang.String s, char sep)
           
static linebuf toStringArray(java.lang.String s, linebuf lb)
          Uses default delimiters of carriage return and newline (CR and LF).
static linebuf toStringArray(java.lang.String s, linebuf lb, java.lang.String delimiters)
           
static java.lang.String[] toStringArray(java.lang.String s, java.lang.String delimiters)
           
static java.lang.String toStringline(java.lang.Object o)
          Convert multiline object descriptor onto a single line.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

default_line_separator

public static final char[] default_line_separator

sys_line_separator

public static java.lang.String sys_line_separator

buf

protected char[][] buf

p

protected int p
Constructor Detail

linebuf

public linebuf(int growthfactor)
Parameters:
growthfactor - Set internal buffer allocation unit length (number of lines).

linebuf

public linebuf()
Default growth factor of ten.

linebuf

public linebuf(java.lang.String line)
Append argument line.
Parameters:
line - One line to append.

linebuf

public linebuf(java.lang.Object obj)

linebuf

public linebuf(java.lang.String[] lineset)
Append argument lines.
Parameters:
lineset - Lines to append.

linebuf

public linebuf(java.lang.String[] lineset,
               java.lang.String line_sep)
Append argument lines.
Parameters:
lineset - Lines to append.
line_sep - Linebuf string element (line) separator.

linebuf

public linebuf(java.lang.String prefix,
               java.lang.Object[] lineset,
               java.lang.String line_sep)
Append argument lines.
Parameters:
prefix - Output prefix.
lineset - Lines to append using Object.toString().
line_sep - Linebuf string element (line) separator.

linebuf

public linebuf(java.lang.String prefix,
               java.lang.String[] lineset,
               java.lang.String line_sep)
Append argument lines.
Parameters:
prefix - Output prefix.
lineset - Lines to append.
line_sep - Linebuf string element (line) separator.

linebuf

public linebuf(java.lang.String prefix,
               java.util.Enumeration lineset,
               java.lang.String line_sep)
Append argument lines.
Parameters:
prefix - Output prefix.
lineset - Lines to append using Object.toString().
line_sep - Linebuf string element (line) separator.
Method Detail

index

public int index()

index

public java.lang.String index(int idx)
Parameters:
idx - Line index in this buffer.

index_cary

public char[] index_cary(int idx)
Parameters:
idx - Line index in this buffer.

index

public void index(int idx,
                  java.lang.String val)
Parameters:
idx - Line index in this buffer.
val - Line text

index

public void index(int idx,
                  char[] val)
Parameters:
idx - Line index in this buffer.
val - Line text

index_prepend

public void index_prepend(int idx,
                          java.lang.String val)
Parameters:
idx - Line index in this buffer.
val - Line text to prepend to any current text in line

index_append

public void index_append(int idx,
                         java.lang.String val)
Parameters:
idx - Line index in this buffer.
val - Line text

index_append

public void index_append(int idx,
                         char[] val)
Parameters:
idx - Line index in this buffer.
val - Line text

line_append

public void line_append(java.lang.String s)

index_column_overwrite

public void index_column_overwrite(int idx,
                                   java.lang.String col,
                                   int col0,
                                   int colw)
Use the first `colw' characters of `col' to overwrite the "[col0,colw)" segment of the line.
Parameters:
idx - Line index in this buffer.
col - Column text
col0 - Column specification first column index
colw - Column specification width

index_column_overwrite

public void index_column_overwrite(int idx,
                                   char[] col,
                                   int col0,
                                   int colw)
Use the first `colw' characters of `col' to overwrite the "[col0,colw)" segment of the line.
Parameters:
idx - Line index in this buffer.
col - Column text
col0 - Column specification first column index
colw - Column specification width

lines_prepend

public linebuf lines_prepend(java.lang.String val)
Convenience method to prepend string value to elements currently in the line buffer. Has no effect on elements subsequently appended to line buffer, or any other future operations.

Makes null elements non- null!

Parameters:
val - String value to prepend to existing elements of the buffer.

lines_append

public linebuf lines_append(java.lang.String val)
Convenience method to append string value to elements currently in the line buffer. Has no effect on elements subsequently appended to line buffer, or any other future operations.

Makes null elements non- null!

Parameters:
val - String value to append to existing elements of the buffer.

println

public linebuf println()
Append null line, return this.

append

public linebuf append(char[] line)
Append line, return this.

Will insert null lines.


append

public linebuf append(char[] line,
                      int ofs,
                      int len)
Append line, return this.

Will insert null lines.


append

public linebuf append(java.lang.String line)
Append line, return this.

Will insert null lines.


append

public linebuf append(java.lang.String[] lines)
Will insert null line elements.

append

public linebuf append(java.lang.Object[] oary)
Will insert null line elements.

append

public linebuf append(java.lang.Object o)

append

public linebuf append(java.lang.String s,
                      java.lang.String delimiters)
Split input string `s' on delimiter characters, and add each resulting token as an element- line.

toStringArray

public java.lang.String[] toStringArray()
Returns lines or null.

toCharCharArray

public char[][] toCharCharArray()
Returns lines or null.

line_separator

public linebuf line_separator(java.lang.String sep)
Reset the line separator used by `toString'. By default it is the local platform line separator, typically CRLF for MacOs or Windows, and LF for Unix, Linux, Be, Inferno, Plan9, NeXt or MacOsX.

line_separator

public java.lang.String line_separator()

invert

public linebuf invert()
Invert (reverse) the order of strings in the linebuffer.

Reminder: Not MT SAFE, although MT robust. Works on a copy of the internal buffer, then replaces the internal buffer with the inverted one. Changes made to the internal buffer by another thread during this operation will be lost.


toString

public java.lang.String toString()
Terminate each line with the line separator string, by default this is the local platform newline (eg, CRLF (Mac/ Win) or LF (Unix)).
Overrides:
toString in class java.lang.Object

toCharArray

public char[] toCharArray()
Terminate each line with the line separator string, by default this is the local platform newline (eg, CRLF (Mac/ Win) or LF (Unix)).

reset

public linebuf reset()
Discard content, reuse buffer.

length

public int length()
Number of lines.

backspace

public void backspace(int rowidx,
                      int colidx)

pop

public void pop()
Delete the first line

delete

public void delete(int rowidx,
                   int colidx)

insert

public void insert(char key,
                   int rowidx,
                   int colidx)

toString

public static final java.lang.String toString(java.lang.Object[] oary)

toString

public static final java.lang.String toString(java.lang.Object obj)

toStringArray

public static final java.lang.String[] toStringArray(java.lang.Object o)

toStringArray

public static final java.lang.String[] toStringArray(java.io.DataInputStream din)
                                              throws java.io.IOException

toStringArray

public static final java.lang.String[] toStringArray(java.util.Enumeration en)

toStringArray

public static final linebuf toStringArray(java.util.Enumeration en,
                                          linebuf lb)

toStringArray

public static final java.lang.String[] toStringArray(java.lang.String s)
Parses input using default delimiters of carriage return and newline (CR and LF).

toStringArray

public static final java.lang.String[] toStringArray(java.lang.String s,
                                                     java.lang.String delimiters)

toStringArray

public static final linebuf toStringArray(java.lang.String s,
                                          linebuf lb)
Uses default delimiters of carriage return and newline (CR and LF).

toStringArray

public static final linebuf toStringArray(java.lang.String s,
                                          linebuf lb,
                                          java.lang.String delimiters)

toStringArray

public static final java.lang.String[] toStringArray(java.lang.String s,
                                                     char sep)

toStringArray_term

public static final java.lang.String[] toStringArray_term(java.lang.String s,
                                                          java.lang.String term)
Use the `term' argument literally, not as a set of delimiter characters but as a whole term delimiting substrings in `s'.
Parameters:
s - String source
term - Substring delimiter

invert

public static final java.lang.String[] invert(java.lang.String s,
                                              java.lang.String delim)

invert

public static final linebuf invert(java.lang.String s,
                                   linebuf lb,
                                   java.lang.String delimiters)

indent

public static final java.lang.String indent(int lvl,
                                            java.lang.String text)
Insert 'lvl' tabs before each line of a multiline text, else return text (use leveltabs for a single line).

space_quoted

public static final java.lang.String[] space_quoted(java.lang.String line)
Parse series of space separated, optionally quoted strings.

toStringline

public static final java.lang.String toStringline(java.lang.Object o)
Convert multiline object descriptor onto a single line.

detab

public static final char[] detab(java.lang.String s)
Convert each tab to five spaces.
Parameters:
s - String to detabify

detab

public static final char[] detab(char[] scary)

detab

public static final char[] detab(char[] scary,
                                 int ofs,
                                 int len)

sptab

public static final char[] sptab(java.lang.String s)
Convert each tab to a space. Used by `index_column_overwrite' which in the context of column formatting with spaces, truncates any tabs for "belt & braces" safety.
Parameters:
s - String to detabify
See Also:
index_column_overwrite(int, java.lang.String, int, int)