Class StringSegment

  • All Implemented Interfaces:
    java.lang.CharSequence

    public class StringSegment
    extends java.lang.Object
    implements java.lang.CharSequence
    A mutable String wrapper with a variable offset and length and support for case folding. The charAt, length, and subSequence methods all operate relative to the fixed offset into the String. Intended to be useful for parsing. CAUTION: Since this class is mutable, it must not be used anywhere that an immutable object is required, like in a cache or as the key of a hash map.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private int end  
      private boolean foldCase  
      private int start  
      private java.lang.String str  
    • Constructor Summary

      Constructors 
      Constructor Description
      StringSegment​(java.lang.String str, boolean foldCase)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void adjustOffset​(int delta)
      Equivalent to setOffset(getOffset()+delta).
      void adjustOffsetByCodePoint()
      Adjusts the offset by the width of the current lead code point, either 1 or 2 chars.
      java.lang.String asString()
      Returns a String that is equivalent to the CharSequence representation.
      char charAt​(int index)  
      int codePointAt​(int index)
      Returns the code point at the given index relative to the current offset.
      private static boolean codePointsEqual​(int cp1, int cp2, boolean foldCase)  
      boolean contentEquals​(java.lang.CharSequence other)
      Returns true if this segment contains the same characters as the other CharSequence.
      int getCaseSensitivePrefixLength​(java.lang.CharSequence other)
      Like getCommonPrefixLength(java.lang.CharSequence), but never performs case folding, even if case folding was enabled in the constructor.
      int getCodePoint()
      Returns the first code point in the string segment.
      int getCommonPrefixLength​(java.lang.CharSequence other)
      Returns the length of the prefix shared by this StringSegment and the given CharSequence.
      int getOffset()  
      private int getPrefixLengthInternal​(java.lang.CharSequence other, boolean foldCase)  
      int length()  
      void resetLength()  
      void setLength​(int length)  
      void setOffset​(int start)  
      boolean startsWith​(int otherCp)
      Returns true if the first code point of this StringSegment equals the given code point.
      boolean startsWith​(UnicodeSet uniset)
      Returns true if the first code point of this StringSegment is in the given UnicodeSet.
      boolean startsWith​(java.lang.CharSequence other)
      Returns true if there is at least one code point of overlap between this StringSegment and the given CharSequence.
      java.lang.CharSequence subSequence​(int start, int end)  
      java.lang.String toString()
      Returns a string representation useful for debugging.
      • Methods inherited from class java.lang.Object

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

        chars, codePoints
    • Field Detail

      • str

        private final java.lang.String str
      • start

        private int start
      • end

        private int end
      • foldCase

        private boolean foldCase
    • Constructor Detail

      • StringSegment

        public StringSegment​(java.lang.String str,
                             boolean foldCase)
    • Method Detail

      • getOffset

        public int getOffset()
      • setOffset

        public void setOffset​(int start)
      • adjustOffset

        public void adjustOffset​(int delta)
        Equivalent to setOffset(getOffset()+delta).

        Number parsing note: This method is usually called by a Matcher to register that a char was consumed. If the char is strong (it usually is, except for things like whitespace), follow this with a call to ParsedNumber#setCharsConsumed(). For more information on strong chars, see that method.

      • adjustOffsetByCodePoint

        public void adjustOffsetByCodePoint()
        Adjusts the offset by the width of the current lead code point, either 1 or 2 chars.
      • setLength

        public void setLength​(int length)
      • resetLength

        public void resetLength()
      • length

        public int length()
        Specified by:
        length in interface java.lang.CharSequence
      • charAt

        public char charAt​(int index)
        Specified by:
        charAt in interface java.lang.CharSequence
      • subSequence

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

        public int getCodePoint()
        Returns the first code point in the string segment.

        Important: Most of the time, you should use startsWith(int), which handles case folding logic, instead of this method.

      • codePointAt

        public int codePointAt​(int index)
        Returns the code point at the given index relative to the current offset.
      • startsWith

        public boolean startsWith​(int otherCp)
        Returns true if the first code point of this StringSegment equals the given code point.

        This method will perform case folding if case folding is enabled for the parser.

      • startsWith

        public boolean startsWith​(UnicodeSet uniset)
        Returns true if the first code point of this StringSegment is in the given UnicodeSet.
      • startsWith

        public boolean startsWith​(java.lang.CharSequence other)
        Returns true if there is at least one code point of overlap between this StringSegment and the given CharSequence. Null-safe.
      • getCommonPrefixLength

        public int getCommonPrefixLength​(java.lang.CharSequence other)
        Returns the length of the prefix shared by this StringSegment and the given CharSequence. For example, if this string segment is "aab", and the char sequence is "aac", this method returns 2, since the first 2 characters are the same.

        This method only returns offsets along code point boundaries.

        This method will perform case folding if case folding was enabled in the constructor.

        IMPORTANT: The given CharSequence must not be empty! It is the caller's responsibility to check.

      • getCaseSensitivePrefixLength

        public int getCaseSensitivePrefixLength​(java.lang.CharSequence other)
        Like getCommonPrefixLength(java.lang.CharSequence), but never performs case folding, even if case folding was enabled in the constructor.
      • getPrefixLengthInternal

        private int getPrefixLengthInternal​(java.lang.CharSequence other,
                                            boolean foldCase)
      • codePointsEqual

        private static final boolean codePointsEqual​(int cp1,
                                                     int cp2,
                                                     boolean foldCase)
      • contentEquals

        public boolean contentEquals​(java.lang.CharSequence other)
        Returns true if this segment contains the same characters as the other CharSequence.

        This method does not perform case folding; if you want case-insensitive equality, use getCommonPrefixLength(java.lang.CharSequence).

      • toString

        public java.lang.String toString()
        Returns a string representation useful for debugging.
        Specified by:
        toString in interface java.lang.CharSequence
        Overrides:
        toString in class java.lang.Object
      • asString

        public java.lang.String asString()
        Returns a String that is equivalent to the CharSequence representation.