Class StringSegment

java.lang.Object
com.ibm.icu.impl.StringSegment
All Implemented Interfaces:
CharSequence

public class StringSegment extends Object implements 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 Details

    • str

      private final String str
    • start

      private int start
    • end

      private int end
    • foldCase

      private boolean foldCase
  • Constructor Details

    • StringSegment

      public StringSegment(String str, boolean foldCase)
  • Method Details

    • 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 CharSequence
    • charAt

      public char charAt(int index)
      Specified by:
      charAt in interface CharSequence
    • subSequence

      public CharSequence subSequence(int start, int end)
      Specified by:
      subSequence in interface 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(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(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(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(CharSequence other, boolean foldCase)
    • codePointsEqual

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

      public boolean contentEquals(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 String toString()
      Returns a string representation useful for debugging.
      Specified by:
      toString in interface CharSequence
      Overrides:
      toString in class Object
    • asString

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