Package fj.data

Class LazyString

java.lang.Object
fj.data.LazyString
All Implemented Interfaces:
CharSequence

public final class LazyString extends Object implements CharSequence
A lazy (non-evaluated) immutable character string.
  • Field Details

  • Constructor Details

  • Method Details

    • str

      public static LazyString str(String s)
      Constructs a lazy string from a String.
      Parameters:
      s - A string from which to construct a lazy string.
      Returns:
      A lazy string with the characters from the given string.
    • fromStream

      public static LazyString fromStream(Stream<Character> s)
      Constructs a lazy string from a stream of characters.
      Parameters:
      s - A stream of characters.
      Returns:
      A lazy string with the characters from the given stream.
    • toStream

      public Stream<Character> toStream()
      Gives a stream representation of this lazy string.
      Returns:
      A stream representation of this lazy string.
    • length

      public int length()
      The length of the lazy string. Note that this operation is O(n).
      Specified by:
      length in interface CharSequence
      Returns:
      The length of this lazy string.
    • charAt

      public char charAt(int index)
      Returns the caracter at the specified index.
      Specified by:
      charAt in interface CharSequence
      Parameters:
      index - The index for the character to be returned.
      Returns:
      The character at the specified index.
    • subSequence

      public CharSequence subSequence(int start, int end)
      Gets the specified subsequence of this lazy string. This operation does not fail for indexes that are out of bounds. If the start index is past the end of this lazy string, then the resulting character sequence will be empty. If the end index is past the end of this lazy string, then the resulting character sequence will be truncated.
      Specified by:
      subSequence in interface CharSequence
      Parameters:
      start - The character index of this lazy string at which to start the subsequence.
      end - The character index of this lazy string at which to end the subsequence.
      Returns:
      A character sequence containing the specified character subsequence.
    • toStringEager

      public String toStringEager()
      Returns the String representation of this lazy string.
      Returns:
      The String representation of this lazy string.
    • toStringLazy

      public String toStringLazy()
    • toString

      public String toString()
      Specified by:
      toString in interface CharSequence
      Overrides:
      toString in class Object
    • eval

      public String eval()
    • append

      public LazyString append(LazyString cs)
      Appends the given lazy string to the end of this lazy string.
      Parameters:
      cs - A lazy string to append to this one.
      Returns:
      A new lazy string that is the concatenation of this string and the given string.
    • append

      public LazyString append(String s)
      Appends the given String to the end of this lazy string.
      Parameters:
      s - A String to append to this lazy string.
      Returns:
      A new lazy string that is the concatenation of this lazy string and the given string.
    • contains

      public boolean contains(LazyString cs)
      Returns true if the given lazy string is a substring of this lazy string.
      Parameters:
      cs - A substring to find in this lazy string.
      Returns:
      True if the given string is a substring of this string, otherwise False.
    • endsWith

      public boolean endsWith(LazyString cs)
      Returns true if the given lazy string is a suffix of this lazy string.
      Parameters:
      cs - A string to find at the end of this lazy string.
      Returns:
      True if the given string is a suffix of this lazy string, otherwise False.
    • startsWith

      public boolean startsWith(LazyString cs)
      Returns true if the given lazy string is a prefix of this lazy string.
      Parameters:
      cs - A string to find at the start of this lazy string.
      Returns:
      True if the given string is a prefix of this lazy string, otherwise False.
    • startsWith

      public static F<LazyString,F<LazyString,Boolean>> startsWith()
      First-class prefix check.
      Returns:
      A function that yields true if the first argument is a prefix of the second.
    • head

      public char head()
      Returns the first character of this string.
      Returns:
      The first character of this string, or error if the string is empty.
    • tail

      public LazyString tail()
      Returns all but the first character of this string.
      Returns:
      All but the first character of this string, or error if the string is empty.
    • isEmpty

      public boolean isEmpty()
      Checks if this string is empty.
      Specified by:
      isEmpty in interface CharSequence
      Returns:
      True if there are no characters in this string, otherwise False.
    • reverse

      public LazyString reverse()
      Returns the reverse of this string.
      Returns:
      the reverse of this string.
    • indexOf

      public Option<Integer> indexOf(char c)
      Returns the first index of the given character in this lazy string, if present.
      Parameters:
      c - A character to find in this lazy string.
      Returns:
      The first index of the given character in this lazy string, or None if the character is not present.
    • indexOf

      public Option<Integer> indexOf(LazyString cs)
      Returns the first index of the given substring in this lazy string, if present.
      Parameters:
      cs - A substring to find in this lazy string.
      Returns:
      The first index of the given substring in this lazy string, or None if there is no such substring.
    • matches

      public boolean matches(String regex)
      Regular expression pattern matching.
      Parameters:
      regex - A regular expression to match this lazy string.
      Returns:
      True if this string mathches the given regular expression, otherwise False.
    • split

      public Stream<LazyString> split(F<Character,Boolean> p)
      Splits this lazy string by characters matching the given predicate.
      Parameters:
      p - A predicate that matches characters to be considered delimiters.
      Returns:
      A stream of the substrings in this lazy string, when separated by the given predicate.
    • map

      public LazyString map(F<Character,Character> f)
    • bind

      public LazyString bind(F<Character,LazyString> f)
    • split

      public Stream<LazyString> split(char c)
      Splits this lazy string by the given delimiter character.
      Parameters:
      c - A delimiter character at which to split.
      Returns:
      A stream of substrings of this lazy string, when separated by the given delimiter.
    • words

      public Stream<LazyString> words()
      Splits this lazy string into words by spaces.
      Returns:
      A stream of the words in this lazy string, when split by spaces.
    • lines

      public Stream<LazyString> lines()
      Splits this lazy string into lines.
      Returns:
      A stream of the lines in this lazy string, when split by newlines.
    • lines_

      public static F<LazyString,Stream<LazyString>> lines_()
    • unlines

      public static LazyString unlines(Stream<LazyString> str)
      Joins the given stream of lazy strings into one, separated by newlines.
      Parameters:
      str - A stream of lazy strings to join by newlines.
      Returns:
      A new lazy string, consisting of the given strings separated by newlines.
    • unlines_

      public static F<Stream<LazyString>,LazyString> unlines_()
    • unwords

      public static LazyString unwords(Stream<LazyString> str)
      Joins the given stream of lazy strings into one, separated by spaces.
      Parameters:
      str - A stream of lazy strings to join by spaces.
      Returns:
      A new lazy string, consisting of the given strings with spaces in between.