struct
Pf::StringSeln
- Pf::StringSeln
- Struct
- Value
- Object
Overview
A string selection type whose indivisible unit is the character (Char).
See also: GraphemeSeln.
StringSeln is most useful for lexical analysis/parsing and similar. Do not
use it as a general-purpose replacement for strings: string selections do not
play well with the GC since they keep the whole string in memory even if all
you have at that point is its tiny (or even empty) fragment.
Defined in:
permafrost/string_seln.cr:1permafrost/string_seln.cr:141
Constructors
- .between(a : StringSeln, b : StringSeln) : StringSeln
-
.chain(a : StringSeln, b : StringSeln) : StringSeln
Joins two contiguous selections a, b.
- .extend(a : StringSeln, b : StringSeln) : StringSeln
-
.new(string : String) : StringSeln
Constructs a string selection from the given string.
-
.new_scrub(string : String, replacement : Char = Char::REPLACEMENT) : StringSeln
Constructs a string selection from the given string, replacing invalid bytes (according to UTF-8) with replacement.
- .span(a : StringSeln, b : StringSeln) : StringSeln
Class Method Summary
- .contiguous?(a : StringSeln, b : StringSeln) : Bool
- .new?(string : String) : StringSeln | Nil
-
.siblings?(seln ref : StringSeln, *selns : StringSeln) : Bool
Returns
trueif seln, selns are all of common descent, pointing to the same trunk. - .unsafe_gte?(bytes : Bytes, size : Int32) : Bool
Instance Method Summary
- #&+(other : StringSeln) : StringSeln
- #<=>(other : StringSeln) : Int32
- #==(other : StringSeln) : Bool
- #==(other : String) : Bool
- #==(other : Char) : Bool
-
#after_end : StringSeln
Returns an empty selection pointing after the end of this one.
-
#all_after_end : StringSeln
Selects all characters in the trunk string after the end of this selection.
-
#all_before_begin : StringSeln
Selects all characters in the trunk string before the start of this selection.
-
#before_begin : StringSeln
Returns an empty selection pointing before the beginning of this one.
-
#blank? : Bool
Returns
trueif this selection is empty or contains exclusively whitespace (seeChar#whitespace?). -
#byte_end : UInt32
Returns the ending byte of this selection (exclusive).
- #byte_range : Range(UInt32, UInt32)
-
#byte_start : UInt32
Returns the starting byte of this selection (inclusive).
-
#bytesize : UInt32
Returns the number of selected bytes.
-
#char_end : UInt32
Returns the index of the last selected character.
-
#char_start : UInt32
Returns the index of the first selected character.
- #chomp : StringSeln
- #chop?(l, r) : StringSeln | Nil
- #clone : StringSeln
-
#count(pattern : Char) : Int32
A hand-optimized alternative to
ee.count. - #covers_fully? : Bool
- #each_before_and_after(& : StringSeln, StringSeln -> ) : Nil
-
#each_char(& : Char -> ) : Nil
Yields selected characters.
- #each_char_seln(& : StringSeln -> ) : Nil
-
#each_char_with_abs_byte_index(& : Char, Int32 -> ) : Nil
Yields selected characters along with their absolute byte index.
- #each_char_with_index(& : Char, Int32 -> ) : Nil
- #each_line(& : StringSeln -> )
- #each_line_with_index(& : StringSeln, Int32 -> )
- #each_partition(& : StringSeln, StringSeln, StringSeln -> ) : Nil
- #each_split(& : StringSeln, StringSeln, StringSeln -> ) : Nil
-
#each_word(& : StringSeln -> ) : Nil
Yields each word in this selection.
-
#each_word_with_index(& : StringSeln, Int32 -> ) : Nil
Yields each word in this selection along with its index.
- #ee : Enumerable(Char)
-
#empty? : Bool
Returns
trueif this selection contains zero characters. - #ends_with?(suffix : Char) : Bool
- #ends_with?(suffix : String) : Bool
-
#expand : StringSeln
Expands this selection to enclose the entire trunk string.
- #extend(*, exclusive = true, & : Char -> Bool) : StringSeln
- #find(pattern) : StringSeln
-
#first : StringSeln
Selects the first character if there are one or more characters.
-
#first_and_rest : Tuple(StringSeln, StringSeln)
Splits this selection into two: the first selection contains the first character, and the second one contains the rest of characters.
-
#first_char : Char
Returns the first character in this selection.
-
#first_char? : Char | Nil
Returns the first character in this selection.
-
#hash(hasher)
See
Object#hash(hasher) - #highlight(io, marker : String = "⏏")
- #includes?(object : Char) : Bool
- #inspect(io)
-
#last : StringSeln
Selects the last character if there is one or more characters.
-
#last_char : Char
Returns the last character in this selection.
-
#last_char? : Char | Nil
Returns the last character in this selection.
-
#lchop(chr : Char) : StringSeln
Skips one leading character chr from this selection, if present.
-
#lchop : StringSeln
Returns a selection that excludes the first selected character.
-
#lchop?(pattern : Char) : StringSeln | Nil
Skips one leading character chr from this selection, if present.
- #lchop?(pattern : String) : StringSeln | Nil
-
#lskip(nchars : Int) : StringSeln
Skips a number of characters from the left.
- #lstrip(charset : String = "\n") : StringSeln
- #ltake?(pattern) : StringSeln | Nil
-
#nonempty? : Bool
Returns
trueif this selection contains one or more characters. - #partition(& : Char -> Bool) : Tuple(StringSeln, StringSeln, StringSeln)
- #partition(pattern : Char) : Tuple(StringSeln, StringSeln, StringSeln)
- #partition(index needle : Int) : Tuple(StringSeln, StringSeln, StringSeln)
- #postfixed_by?(r : Char | String) : Bool
- #prefixed_by?(l : Char | String) : Bool
-
#prior : StringSeln
Selects characters before the last character in this selection.
-
#prior_and_last : Tuple(StringSeln, StringSeln)
Splits this selection into two: the first selection contains characters before the last character, and the second selection contains the last character.
-
#rchop(chr : Char) : StringSeln
Skips one trailing character chr from this selection, if present.
-
#rchop : StringSeln
Returns a selection that excludes the last selected character.
- #rchop?(chr : Char) : StringSeln | Nil
- #rchop?(pattern : String) : StringSeln | Nil
-
#rest : StringSeln
Selects characters after the first character in this selection.
- #reverse_each_before_and_after(& : StringSeln, StringSeln -> ) : Nil
-
#reverse_each_char(& : Char -> ) : Nil
Yields selected characters in reverse.
- #reverse_each_char_seln(& : StringSeln -> ) : Nil
- #reverse_each_partition(& : StringSeln, StringSeln, StringSeln -> ) : Nil
- #reverse_each_split(& : StringSeln, StringSeln, StringSeln -> ) : Nil
- #reverse_extend(*, exclusive = true, & : Char -> Bool) : StringSeln
- #rpartition(& : Char -> Bool) : Tuple(StringSeln, StringSeln, StringSeln)
- #rpartition(pattern : Char) : Tuple(StringSeln, StringSeln, StringSeln)
- #rskip_thru(charset : String) : Tuple(StringSeln, StringSeln)
- #rskip_to(charset : String) : Tuple(StringSeln, StringSeln)
- #rstrip(charset : String = "\n") : StringSeln
-
#size : Int32
Returns the number of selected characters.
- #skip_thru(charset : String) : Tuple(StringSeln, StringSeln)
- #skip_thru_seq(*, limit : UInt32 = UInt32::MAX, & : Char -> T | Nil) : Tuple(StringSeln, Array(T), StringSeln) forall T
- #skip_to(charset : String) : Tuple(StringSeln, StringSeln)
- #span(other : StringSeln) : StringSeln
- #split(sep : Char, **kwargs) : Array(StringSeln)
- #split(sep : Char, **kwargs, &) : Nil
- #split_and_rest(sep : Char, *, allow_empty : Bool = true, & : StringSeln, StringSeln -> ) : Nil
- #starts_with?(prefix : Char) : Bool
- #starts_with?(prefix : String) : Bool
- #strip(charset : String = "\n") : StringSeln
- #surrounded_by?(l : Char | String, r : Char | String) : Bool
- #to_s(io)
-
#to_s : String
Returns a nicely readable and concise string representation of this object, typically intended for users.
-
#to_slice : Bytes
Returns the underlying bytes.
- #upto(other : StringSeln) : StringSeln
Constructor Detail
Joins two contiguous selections a, b. a must end exactly where b begins.
Constructs a string selection from the given string.
This forces an encoding check on string. This method requires string to be a valid UTF-8 string.
If you want to scrub, use .new_scrub instead to skip the check.
Constructs a string selection from the given string, replacing invalid bytes (according to UTF-8) with replacement.
See also: String#scrub.
Prefer this method over .new(string.scrub) to avoid the overhead of validating
the encoding.
Class Method Detail
Returns true if seln, selns are all of common descent, pointing
to the same trunk.
Instance Method Detail
Returns an empty selection pointing after the end of this one. Imagine this this as placing an "I-beam" after the last character in this selection (if any).
Selects all characters in the trunk string after the end of this selection.
Selects all characters in the trunk string before the start of this selection.
Returns an empty selection pointing before the beginning of this one. Imagine this as placing an "I-beam" before the first character in this selection (if any).
Returns true if this selection is empty or contains exclusively
whitespace (see Char#whitespace?).
A hand-optimized alternative to ee.count. This is about a hundred times faster
than ee.count on my machine for basic tasks such as counting newlines, due to
vectorization-friendliness in the single-byte fast path.
Yields selected characters along with their absolute byte index.
Yields each word in this selection.
This method never consumes any characters. All trailing and leading whitespaces are kept (if any) -- attached either to the left- or the right-hand side word.
Yields each word in this selection along with its index.
See also: #each_word.
Selects the first character if there are one or more characters. If there are zero characters, the returned selection is empty.
Splits this selection into two: the first selection contains the first character, and the second one contains the rest of characters. If this selection is empty, returns two empty selections.
Returns the first character in this selection. Raises IndexError if
this selection is empty.
Returns the first character in this selection. Returns nil if
this selection is empty.
Selects the last character if there is one or more characters. If there are zero characters, the returned selection is empty.
Returns the last character in this selection. Raises IndexError if
this selection is empty.
Returns the last character in this selection. Returns nil if
this selection is empty.
Skips one leading character chr from this selection, if present.
Skips one leading character chr from this selection, if present.
Returns nil if chr is absent.
Selects characters before the last character in this selection. If there are zero characters, the returned selection is empty.
Splits this selection into two: the first selection contains characters before the last character, and the second selection contains the last character.
Skips one trailing character chr from this selection, if present.
Selects characters after the first character in this selection. If there are zero characters, the returned selection is empty.
Returns the number of selected characters.
NOTE Unlike String, this method does not cache the size -- since
StringSeln is located entirely on the stack (minus the trunk string),
there's nowhere for the size to go.
However, I tried to optimize this method well. In fact, due to the invariants
of StringSeln, it runs about 10x faster than uncached String#size on
my machine (the example I used was a 20MiB string; the results were: Crystal ~11ms,
StringSeln ~1.25ms).
The design is that you aren't expected to call this method very often. If you do end up doing that, we try really hard to remain fast, but ultimately, this is still worst-case O(N).
Returns a nicely readable and concise string representation of this object, typically intended for users.
This method should usually not be overridden. It delegates to
#to_s(IO) which can be overridden for custom implementations.
Also see #inspect.
Returns the underlying bytes. The returned slice is read-only. It is guaranteed to contain valid UTF-8 encoded bytes. It is guaranteed to start and end at a UTF-8 character boundary.