breakOn package:jsaddle

O(n+m) Find the first instance of needle (which must be non-null) in haystack. The first element of the returned tuple is the prefix of haystack before needle is matched. The second is the remainder of haystack, starting with the match. Examples:
breakOn "::" "a::b::c" ==> ("a", "::b::c")
breakOn "/" "foobar"   ==> ("foobar", "")
Laws:
append prefix match == haystack
where (prefix, match) = breakOn needle haystack
If you need to break a string by a substring repeatedly (e.g. you want to break on every instance of a substring), use breakOnAll instead, as it has lower startup overhead. In (unlikely) bad cases, this function's time complexity degrades towards O(n*m).
O(n+m) Find all non-overlapping instances of needle in haystack. Each element of the returned list consists of a pair:
  • The entire string prior to the kth match (i.e. the prefix)
  • The kth match, followed by the remainder of the string
Examples:
breakOnAll "::" ""
==> []
breakOnAll "/" "a/b/c/"
==> [("a", "/b/c/"), ("a/b", "/c/"), ("a/b/c", "/")]
In (unlikely) bad cases, this function's time complexity degrades towards O(n*m). The needle parameter may not be empty.
O(n+m) Similar to breakOn, but searches from the end of the string. The first element of the returned tuple is the prefix of haystack up to and including the last match of needle. The second is the remainder of haystack, following the match.
breakOnEnd "::" "a::b::c" ==> ("a::b::", "c")