Class MemString

java.lang.Object
eu.bandm.tools.d2d2.infra.MemString
All Implemented Interfaces:
Cloneable
Direct Known Subclasses:
MemScanner

public class MemString extends Object implements Cloneable
Any memString is either one pointer (i.e. a position) or two pointers (i.e. a range) into a range of memory filled with character data, i.e. a MemString.CharMem. It supports sequential access and maintains line and column counters as follows:
Line >= 0, Column == 0 is the first visible character in the line.
Line == 0,Column ==-1 means pointing to a non-existing char preceding ALL data.
Line > 0, Column == -1 means pointing to the LF-char ending the preceding line.

Currently this class is used as a base class for MemScanner.

CHAR_EOTEXT is reserved for "end of data" indication. It must not be contained in any method's arguments! Most methods on this level do NOT test for "end of data", but rely on lower-level access violation ("array index out of range", etc.) in case of overrun. The responsibility to avoid these is by the caller.

MemStrings normally behave immutable, but for some methods there are desctructive variants, the names of which end with an "X"

The character "0x0A" is used "hard" for linefeed recognition. For use with windows and Mac systems a LineNumberReader is inserted which does normalization to this character.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    The data storage underlying every MemString.
    class 
    Evident
    class 
    Used for aMemString.new Reader() to use the memString as sequential character data source, e.g.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final char
    Used to mark the end of valid character data.
    static final char
    Used to mark the end of a line / start of a new line.
    protected int
     
    The underlying data storage.
    protected int
     
    protected int
     
    protected int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a copy of the MemString argument.
    Creates a MemString pointing to the first character in the whole character data, as given by the charmMem.
    MemString(String id, String data)
    Creates a new MemString.CharMem store and creates a new MemString pointing to the first character in this data.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Destructive variant: after advancement guaranteed not to CROSS a linefeed, (by any method which adds directly to pos and colnum) the fact of pointing TO a linefeed will be reflected in colnum/linenum.
    void
    Destructive variant: advance position and column number by the argument.
    void
    Destructive variant: modifies this to point one(1) character position behind its current position, and adjusts line and column number Does not test for overrun but maybe causes a lower-level error message.
    int
    Append the character contents to the given StringBuilder.
    Return a String wich contains the character contents of this MemString.
    void
    Destructive variant: modifies this to point one(1) character position before its current position and adjusts line and column number
    void
    backwardsX(int num)
    Destructive variant: modifies this to point given characters position before its current position and adjusts line and column number
    protected void
    Count distance to newline character backwards in data.
    Make shallow copy.
    boolean
     
    boolean
     
    boolean
    Returns whether the lengths of this and the argument are the same, plus startsWith(String).
    int
    Return the current column number of the character contents, relative to the last preceding line-feed character in the underlying character memory data.
    Return the underlying character memory data.
    int
    Return the current length of the character contents.
    int
    Return the current line number of the character contents, relative to the underlying character memory data.
    int
    Return the current start index of the character contents, relative to the underlying character memory data.
    char
    Get the first character of the contents.
    char
    getChar(int i)
    Get the character at position i of the contents.
    Returns a location of the position directly after this MemString.
    Returns a location with the field value MemString.CharMem.originalAddress and the line and column number.
    Returns a string containing blanks and one single "up-arrow" to indicate the current column optically.
    int
     
    void
    Re-sets all values of "this" to the values of the argument.
    boolean
    Returns what it says.
    Returns a diagnosis text which shows a representation of the underlying MemString.CharMem, the position, length, line and column number, and the first 10 characters.
    Returns a new MemString describing the range starting with this and ending before the argument.
    void
    Limit the length of the character contents as destructive variant.

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • CHAR_EOTEXT

      public static final char CHAR_EOTEXT
      Used to mark the end of valid character data.
      See Also:
    • CHAR_NEWLINE

      public static final char CHAR_NEWLINE
      Used to mark the end of a line / start of a new line.
      See Also:
    • data

      protected MemString.CharMem data
      The underlying data storage. The contents of the MemString is a sub-range of the data of that storage object.
    • start

      protected int start
    • len

      protected int len
    • linenum

      protected int linenum
    • colnum

      protected int colnum
  • Constructor Details

    • MemString

      public MemString(MemString.CharMem mem)
      Creates a MemString pointing to the first character in the whole character data, as given by the charmMem.
    • MemString

      public MemString(String id, String data)
      Creates a new MemString.CharMem store and creates a new MemString pointing to the first character in this data.
      Parameters:
      id - the name of the text, as used for diagnosis messages, etc.
      data - the character contents copied into the memory.
    • MemString

      public MemString(MemString ms)
      Creates a copy of the MemString argument.
  • Method Details

    • asString

      public String asString()
      Return a String wich contains the character contents of this MemString.
    • appendAll

      public int appendAll(StringBuilder sb)
      Append the character contents to the given StringBuilder.
    • getChar

      public char getChar()
      Get the first character of the contents. Is sensible only if len > 0. But may work without exception for other values, depnding on the state of the underlying data.
    • getChar

      public char getChar(int i)
      Get the character at position i of the contents. Is sensible only if 0 <= i < len. But may work without exception for other values, depnding on the state of the underlying data.
    • initFrom

      public void initFrom(MemString ms)
      Re-sets all values of "this" to the values of the argument. This is often a copy of a former state of "this", made by clone()
    • toString

      public String toString()
      Returns a diagnosis text which shows a representation of the underlying MemString.CharMem, the position, length, line and column number, and the first 10 characters.
      Overrides:
      toString in class Object
    • getLocation

      public Location<String> getLocation()
      Returns a location with the field value MemString.CharMem.originalAddress and the line and column number.

      Attention: these numbers are 1-(one)-based. They are one(1) bigger than the numbers stored internally and described above.

    • getFollowLocation

      public Location<String> getFollowLocation()
      Returns a location of the position directly after this MemString. See getLocation() for caveats.
    • clone

      public MemString clone()
      Make shallow copy. Delegates to Object.
      Overrides:
      clone in class Object
    • upTo

      public MemString upTo(MemString n)
      Returns a new MemString describing the range starting with this and ending before the argument.
      Throws:
      IllegalArgumentException - when the underlying CharMem is not identical, or the argument is "left of" this.
    • upToX

      public void upToX(MemString n)
      Limit the length of the character contents as destructive variant. Alters this to a region which ends directly before the argument.
      Throws:
      IllegalArgumentException - when the underlying CharMem is not identical, or the argument is "left of" this.
    • equals

      public boolean equals(String n)
      Returns whether the lengths of this and the argument are the same, plus startsWith(String).
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • equals

      public boolean equals(MemString o)
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • startsWith

      public boolean startsWith(String n)
      Returns what it says.
    • advanceByX_sameLine

      public void advanceByX_sameLine(int i)
      Destructive variant: advance position and column number by the argument. Assume that no linefeed is crossed by this advancement. The new position may be a linefeed, which will lead to an adjustment. Currently only needed by MemScanner. The caller is responsible for avoiding overrun, i.e. may call it only when the next "count" characters have been identified as != CHAR_EOTEXT.
    • adjustLineNumberX

      public void adjustLineNumberX()
      Destructive variant: after advancement guaranteed not to CROSS a linefeed, (by any method which adds directly to pos and colnum) the fact of pointing TO a linefeed will be reflected in colnum/linenum.
    • advanceX

      public void advanceX()
      Destructive variant: modifies this to point one(1) character position behind its current position, and adjusts line and column number Does not test for overrun but maybe causes a lower-level error message. The caller is responsible for avoiding this, i.e. call it only when the current character has been identified as != CHAR_EOTEXT.
    • calccolnum

      protected void calccolnum()
      Count distance to newline character backwards in data. Only needed by backwardsX()
    • backwardsX

      public void backwardsX()
      Destructive variant: modifies this to point one(1) character position before its current position and adjusts line and column number
    • backwardsX

      public void backwardsX(int num)
      Destructive variant: modifies this to point given characters position before its current position and adjusts line and column number
    • get_len

      public int get_len()
      Return the current length of the character contents.
    • get_start

      public int get_start()
      Return the current start index of the character contents, relative to the underlying character memory data.
    • get_linenum

      public int get_linenum()
      Return the current line number of the character contents, relative to the underlying character memory data.
    • get_colnum

      public int get_colnum()
      Return the current column number of the character contents, relative to the last preceding line-feed character in the underlying character memory data.
    • get_data

      public MemString.CharMem get_data()
      Return the underlying character memory data.
    • getPositionIndication

      public String getPositionIndication()
      Returns a string containing blanks and one single "up-arrow" to indicate the current column optically.