LinesView
public struct LinesView { /* private fields */ }A view over the lines of a String, split on \n, \r\n, or \r.
Returned by String.lines. The yielded strings (from iteration or
single-line subscripting) do not include the terminator; a trailing
line without a terminator is still emitted. Range subscripts
(lines(0..<n)) yield a zero-copy LinesView sub-view whose
underlying byte range still includes the original terminators —
iterating the sub-view round-trips the same line strings, and
.toString() reconstructs the original substring exactly.
Examples
var lines = Array[String]();
for line in "a\nb\nc".lines {
lines.append(line);
}
lines.count; // 3
// Range subscript preserves terminators in the underlying bytes:
"a\r\nb\nc".lines(0..<2).toString(); // "a\r\nb\n"
Representation
A (ptr, length) pair pointing into the source string.
Properties
public var count: Int64 { get }
public var count: Int64 { get }Number of lines in the view. O(n) — walks the buffer scanning for terminators. Cache the result if you need it more than once.
public var endIndex: LineIndex { get }
public var endIndex: LineIndex { get }Line index at the end (one past the last byte).
public var first: String? { get }
public var first: String? { get }The first line (without terminator), or None if the view is empty.
O(first line length) — scans for the first terminator.
public var isEmpty: Bool { get }
public var isEmpty: Bool { get }true when the view spans zero bytes (no lines).
O(1) — checks byteCount, not count.
public var startIndex: LineIndex { get }
public var startIndex: LineIndex { get }Line index at byte 0.
Initializers
public init(slice: StringSlice)
public init(slice: StringSlice)Constructs a lines view backed by the given string slice. The view retains shared ownership of the underlying bytes.
Methods
public func index(at: Int64) -> LineIndex?
public func index(at: Int64) -> LineIndex?Resolves the n-th line to its byte offset. O(n) — scans for line terminators from the start.
public func indexedIter() -> IndexedLinesIterator
public func indexedIter() -> IndexedLinesIteratorReturns an iterator yielding (LineIndex, String) pairs.
public func slice(from: LineIndex, to: LineIndex) -> StringSlice
public func slice(from: LineIndex, to: LineIndex) -> StringSliceReturns a StringSlice covering [start, end) by byte offset.
public func substring[__opaque_0](__opaque_0) -> String where __opaque_0: LinesSubstringIndex
public func substring[__opaque_0](__opaque_0) -> String where __opaque_0: LinesSubstringIndexConvenience: dispatches to a LinesSubstringIndex to produce
an owned String covering the requested line range, with their
original terminators preserved. Equivalent to
self(range).toString() for both Range[Int64] and
ClosedRange[Int64].
public func toString() -> String
public func toString() -> StringMaterializes the view as an owned String covering the entire
underlying buffer (terminators included). O(n) — copies bytes.
Subscripts
public subscript[I](checked: I) -> I.LinesYield? { get }
public subscript[I](checked: I) -> I.LinesYield? { get }Reads at index, returning .None on out-of-bounds.
public subscript[I](clamped: I) -> I.LinesClampedYield { get }
public subscript[I](clamped: I) -> I.LinesClampedYield { get }Reads at index saturated to [0, count). Single-line indexes
yield String? (.None only when the view holds no lines);
range indexes yield LinesView (always valid, possibly empty).
public subscript[I](I) -> I.LinesYield { get }
public subscript[I](I) -> I.LinesYield { get }Int64 reads a single line as a String (without terminator).
Range[Int64] / ClosedRange[Int64] yield a zero-copy
LinesView sub-view covering those lines (terminators preserved
in the underlying bytes). O(n) — walks the buffer from the
start. Panics on out-of-bounds.
public subscript[I](wrapped: I) -> I.LinesWrappedYield { get }
public subscript[I](wrapped: I) -> I.LinesWrappedYield { get }Reads at index with modulo wrap-around. Negative indices wrap
from the end: view.lines(wrapped: -1) reads the last line.
Returns None on an empty view.
ImplementsIterable
Associated Types
type Item = String
type Item = StringThe element type yielded by iteration — always String.
type TargetIterator = LinesIterator
type TargetIterator = LinesIteratorThe iterator type returned by iter().
Methods
public func iter() -> LinesIterator
public func iter() -> LinesIteratorReturns a LinesIterator positioned at byte 0.
ImplementsCloneable
Methods
public func clone() -> LinesView
public func clone() -> LinesViewDefined in lang/std/text/views.ks