Class XltCharBuffer

  • All Implemented Interfaces:
    java.lang.CharSequence, java.lang.Comparable<XltCharBuffer>

    public class XltCharBuffer
    extends java.lang.Object
    implements java.lang.CharSequence, java.lang.Comparable<XltCharBuffer>
    This class does not implement the CharBuffer of the JDK, but uses the idea of a shared character array with views. This is also a very unsafe implementation with as little as possible boundary checks to achieve the maximum speed possible. To enhance use, we implement CharSequence and hence can also do regex with it now. It also features common String and StringBuilder methods to make it versatile and avoid the typical overhead when doing conversions back and forth.
    Since:
    7.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static XltCharBuffer EMPTY
      An empty static XltCharBuffer
    • Constructor Summary

      Constructors 
      Constructor Description
      XltCharBuffer​(char[] src)
      New buffer from a raw char array
      XltCharBuffer​(char[] src, int from, int length)
      A new buffer from a char array including a view port.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      char charAt​(int pos)
      Return the character at a position.
      int compareTo​(XltCharBuffer other)
      Compares this object with the specified object for order.
      static XltCharBuffer empty()
      Just returns an empty buffer.
      static XltCharBuffer emptyWhenNull​(XltCharBuffer s)
      Returns the empty string if the provided buffer is null the buffer otherwise
      boolean endsWith​(XltCharBuffer s)
      Checks whether or not a buffer ends with the content of another buffer
      boolean equals​(java.lang.Object obj)  
      int hashCode()
      Optimized hashcode calculation for large strings using all execution units of the CPU.
      int indexOf​(char c)
      Find the first occurrence of a char
      int indexOf​(XltCharBuffer s)
      Search for the first occurrence of another buffer in this buffer
      int indexOf​(XltCharBuffer s, int fromIndex)
      Search for the first occurrence of another buffer in this buffer
      int lastIndexOf​(XltCharBuffer s)
      Returns the last occurrence of a buffer in this buffer
      int lastIndexOf​(XltCharBuffer s, int from)
      Returns the last occurrence of a buffer in this starting from a certain offset and searching backwards(!)
      int length()
      Returns the length of this buffer
      char peakAhead​(int pos)
      Looks ahead, otherwise returns 0.
      XltCharBuffer put​(int pos, char c)
      Set a character at this position.
      java.util.List<XltCharBuffer> split​(char splitChar)
      Splits up this sequence into sub-sequences at splitChar markers excluding the marker
      boolean startsWith​(XltCharBuffer s)
      Checks if the start of the buffer matches another buffer
      java.lang.CharSequence subSequence​(int start, int end)  
      XltCharBuffer substring​(int from)
      Creates a new buffer similar to a String.substring call from a position till the end
      XltCharBuffer substring​(int from, int to)
      Creates a new buffer similar to a String.substring call.
      char[] toCharArray()
      Returns a copy of the backing char array for the range of this buffer aka not more than needed
      java.lang.String toDebugString()  
      java.lang.String toString()
      Just return the content of this buffer as string.
      static XltCharBuffer valueOf​(char[] s)
      Create a new char buffer from a char array without copying it.
      static XltCharBuffer valueOf​(XltCharBuffer s1, char c)
      Creates a new char buffer by adding a single char
      static XltCharBuffer valueOf​(XltCharBuffer s1, XltCharBuffer s2)
      Creates a new char buffer by merging XltCharBuffers
      static XltCharBuffer valueOf​(XltCharBuffer s1, XltCharBuffer s2, XltCharBuffer s3)
      Creates a new char buffer by merging strings
      static XltCharBuffer valueOf​(java.lang.String s)
      Create a new char buffer from a string.
      static XltCharBuffer valueOf​(java.lang.String s1, java.lang.String s2)
      Creates a new char buffer by merging strings
      static XltCharBuffer valueOf​(java.lang.String s1, java.lang.String s2, java.lang.String s3)
      Creates a new char buffer by merging strings
      static XltCharBuffer valueOf​(java.lang.String s1, java.lang.String s2, java.lang.String s3, java.lang.String... more)
      Creates a new char buffer by merging strings
      XltCharBuffer viewByLength​(int from, int length)
      Returns a new buffer with a view on the current.
      XltCharBuffer viewFromTo​(int from, int to)
      Returns a new buffer with a view on the current.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.CharSequence

        chars, codePoints
    • Field Detail

      • EMPTY

        public static final XltCharBuffer EMPTY
        An empty static XltCharBuffer
    • Constructor Detail

      • XltCharBuffer

        public XltCharBuffer​(char[] src)
        New buffer from a raw char array
        Parameters:
        src - a char array
      • XltCharBuffer

        public XltCharBuffer​(char[] src,
                             int from,
                             int length)
        A new buffer from a char array including a view port.
        Parameters:
        src - the char array, if is is null, we fix that silently
        from - from where to deliver the buffer
        length - how long should the buffer be
    • Method Detail

      • empty

        public static XltCharBuffer empty()
        Just returns an empty buffer. This is a static object and not a new buffer every time, so apply caution.
        Returns:
        the empty buffer
      • charAt

        public char charAt​(int pos)
        Return the character at a position. This code does not run any checks in regards to pos being correct (≥ 0, < length). This will automatically apply the view on the underlying array hence incorrect pos values might return something unexpected. So know what you do or else...
        Specified by:
        charAt in interface java.lang.CharSequence
        Parameters:
        pos - the position to return
        Returns:
        the character at this position.
      • put

        public XltCharBuffer put​(int pos,
                                 char c)
        Set a character at this position. Similarly to charAt, this does not check for correctness of pos in favor of speed.
        Parameters:
        pos - the pos to write to
        c - the character to set
        Returns:
        this instance so put can be chained
      • split

        public java.util.List<XltCharBuffer> split​(char splitChar)
        Splits up this sequence into sub-sequences at splitChar markers excluding the marker
        Parameters:
        splitChar - the split character
        Returns:
        a list of the sub-sequences
      • peakAhead

        public char peakAhead​(int pos)
        Looks ahead, otherwise returns 0. Only safety bound against ahead misses, not any behind misses
        Parameters:
        pos - the position to look at
        Returns:
        the content of the peaked pos or 0 if this position does not exist
      • viewByLength

        public XltCharBuffer viewByLength​(int from,
                                          int length)
        Returns a new buffer with a view on the current. No copy is made. No runtime checks
        Parameters:
        from - start position
        length - length of the view port
        Returns:
        a new buffer
      • viewFromTo

        public XltCharBuffer viewFromTo​(int from,
                                        int to)
        Returns a new buffer with a view on the current. No copy is made. No runtime checks
        Parameters:
        from - start position
        to - end position
        Returns:
        a new buffer
      • substring

        public XltCharBuffer substring​(int from,
                                       int to)
        Creates a new buffer similar to a String.substring call. There is no copy created, we still look at the same buffer, but have a reduced view.
        Parameters:
        from - first position (inclusive)
        to - last position (exclusive)
        Returns:
      • substring

        public XltCharBuffer substring​(int from)
        Creates a new buffer similar to a String.substring call from a position till the end
        Parameters:
        from - first position
        Returns:
      • valueOf

        public static XltCharBuffer valueOf​(java.lang.String s1,
                                            java.lang.String s2)
        Creates a new char buffer by merging strings
        Parameters:
        s1 - string 1
        s2 - string 2
        Returns:
        the new charbuffer
      • valueOf

        public static XltCharBuffer valueOf​(XltCharBuffer s1,
                                            XltCharBuffer s2)
        Creates a new char buffer by merging XltCharBuffers
        Parameters:
        s1 - buffer 1
        s2 - buffer 2
        Returns:
        the new charbuffer
      • valueOf

        public static XltCharBuffer valueOf​(XltCharBuffer s1,
                                            char c)
        Creates a new char buffer by adding a single char
        Parameters:
        s1 -
        c -
        Returns:
      • valueOf

        public static XltCharBuffer valueOf​(java.lang.String s1,
                                            java.lang.String s2,
                                            java.lang.String s3)
        Creates a new char buffer by merging strings
        Parameters:
        s1 -
        s2 -
        s3 -
        Returns:
      • valueOf

        public static XltCharBuffer valueOf​(java.lang.String s1,
                                            java.lang.String s2,
                                            java.lang.String s3,
                                            java.lang.String... more)
        Creates a new char buffer by merging strings
        Parameters:
        s1 - string 1
        s2 - string 2
        s3 - string 3
        more - more strings
        Returns:
        a new char buffer
      • valueOf

        public static XltCharBuffer valueOf​(char[] s)
        Create a new char buffer from a char array without copying it. It assume that the full array is valid and because we don't copy, we don't have immutability!
        Parameters:
        s - the char array to use
        Returns:
        a new charbuffer instance
      • valueOf

        public static XltCharBuffer valueOf​(java.lang.String s)
        Create a new char buffer from a string. Because a string does provide array access, we use the returned copy by toCharArray to set up the char buffer.
        Parameters:
        s - the string to use
        Returns:
        a new charbuffer instance
      • toString

        public java.lang.String toString()
        Just return the content of this buffer as string. This is of course a copy operation.
        Specified by:
        toString in interface java.lang.CharSequence
        Overrides:
        toString in class java.lang.Object
        Returns:
        a string representation of this buffer
      • toCharArray

        public char[] toCharArray()
        Returns a copy of the backing char array for the range of this buffer aka not more than needed
        Returns:
        a copy of the relevant portion of the backing array
      • indexOf

        public int indexOf​(char c)
        Find the first occurrence of a char
        Parameters:
        c - the char to search
        Returns:
        the position or -1 otherwise
      • indexOf

        public int indexOf​(XltCharBuffer s)
        Search for the first occurrence of another buffer in this buffer
        Parameters:
        s - the buffer to be search for
        Returns:
        the first found position or -1 if not found
      • indexOf

        public int indexOf​(XltCharBuffer s,
                           int fromIndex)
        Search for the first occurrence of another buffer in this buffer
        Parameters:
        s - the buffer to be search for
        Returns:
        the first found position or -1 if not found
      • endsWith

        public boolean endsWith​(XltCharBuffer s)
        Checks whether or not a buffer ends with the content of another buffer
        Parameters:
        s - the buffer that has to be machting the end of this buffer
        Returns:
        true if the end matches, false otherwise
      • startsWith

        public boolean startsWith​(XltCharBuffer s)
        Checks if the start of the buffer matches another buffer
        Parameters:
        s - the buffer to match the start against
        Returns:
        true if the start matches, false otherwise
      • lastIndexOf

        public int lastIndexOf​(XltCharBuffer s)
        Returns the last occurrence of a buffer in this buffer
        Parameters:
        s - the buffer to looks for
        Returns:
        the position of the last occurrence or -1
      • lastIndexOf

        public int lastIndexOf​(XltCharBuffer s,
                               int from)
        Returns the last occurrence of a buffer in this starting from a certain offset and searching backwards(!) from there
        Parameters:
        s - the buffer to looks for
        from - the offset to start from
        Returns:
        the position of the last occurrence or -1
      • length

        public int length()
        Returns the length of this buffer
        Specified by:
        length in interface java.lang.CharSequence
      • hashCode

        public int hashCode()
        Optimized hashcode calculation for large strings using all execution units of the CPU. This is a trade off between cpu and branches. Assumes we are not mutating... if we mutate, we would have to reset the hashCode Taken from JDK 19 - JDK-8282664, Code and Idea by Richard Startin https://twitter.com/richardstartin
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        the hash code
      • emptyWhenNull

        public static XltCharBuffer emptyWhenNull​(XltCharBuffer s)
        Returns the empty string if the provided buffer is null the buffer otherwise
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • subSequence

        public java.lang.CharSequence subSequence​(int start,
                                                  int end)
        Specified by:
        subSequence in interface java.lang.CharSequence
      • compareTo

        public int compareTo​(XltCharBuffer other)
        Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

        The implementor must ensure sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) for all x and y. (This implies that x.compareTo(y) must throw an exception iff y.compareTo(x) throws an exception.)

        Specified by:
        compareTo in interface java.lang.Comparable<XltCharBuffer>
        Parameters:
        other - the buffer to compare to
        Returns:
        -1, if this is smaller than other, 0 if the same, 1 if this is larger
      • toDebugString

        public java.lang.String toDebugString()