CharsView
public struct CharsView { /* private fields */ }A view over the Unicode code points in a String.
Returned by String.chars. Iteration is O(1) per code point but
count() is O(n) because UTF-8 is variable-width. Range subscripts
are O(n) (the segment-walk dominates) but yield a zero-copy
CharsView sub-view — call .toString() to materialize an owned
String. To index in O(1), use BytesView and convert byte offsets
back yourself.
Examples
let s = "héllo";
s.chars.count; // 5 (code points)
s.bytes.count; // 6 (bytes — 'é' is 2 bytes)
s.chars(0..<4).toString(); // "héll"
s.chars.substring(0..<4); // "héll"
Representation
A (ptr, length) pair, plus the on-demand UTF-8 decoder.
Memory Model
Borrows the source string's buffer. Invalidated by any mutation that reallocates the storage.
Properties
public var count: Int64 { get }
public var count: Int64 { get }public var endIndex: CharIndex { get }
public var endIndex: CharIndex { get }Char index at the end (one past the last byte).
public var first: Char? { get }
public var first: Char? { get }The first code point, or None if the view is empty.
public var isEmpty: Bool { get }
public var isEmpty: Bool { get }true when the view spans zero bytes (no code points).
O(1) — checks byteCount, not count.
public var last: Char? { get }
public var last: Char? { get }The last code point, or None if the view is empty.
public var reversed: ReversedCharsView { get }
public var reversed: ReversedCharsView { get }A reversed view that iterates code points back-to-front.
public var startIndex: CharIndex { get }
public var startIndex: CharIndex { get }Char index at byte 0 (the first code point).
Initializers
public init(slice: StringSlice)
public init(slice: StringSlice)Constructs a chars view backed by the given string slice. The view retains shared ownership of the underlying bytes.
Methods
public func firstIndex(of: Char) -> CharIndex?
public func firstIndex(of: Char) -> CharIndex?Returns the index of the first occurrence of c, or .None.
public func firstIndex(where: (Char) -> Bool) -> CharIndex?
public func firstIndex(where: (Char) -> Bool) -> CharIndex?Returns the index of the first code point matching predicate, or .None.
public func index(at: Int64) -> CharIndex?
public func index(at: Int64) -> CharIndex?Resolves the n-th code point to its byte offset. O(n).
public func indexedIter() -> IndexedCharsIterator
public func indexedIter() -> IndexedCharsIteratorReturns an iterator yielding (CharIndex, Char) pairs.
public func lastIndex(of: Char) -> CharIndex?
public func lastIndex(of: Char) -> CharIndex?Returns the index of the last occurrence of c, or .None.
public func lastIndex(where: (Char) -> Bool) -> CharIndex?
public func lastIndex(where: (Char) -> Bool) -> CharIndex?Returns the index of the last code point matching predicate, or .None.
public func slice(from: CharIndex, to: CharIndex) -> StringSlice
public func slice(from: CharIndex, to: CharIndex) -> StringSliceReturns a StringSlice covering [start, end) by byte offset.
public func substring[__opaque_0](__opaque_0) -> String where __opaque_0: CharsSubstringIndex
public func substring[__opaque_0](__opaque_0) -> String where __opaque_0: CharsSubstringIndexConvenience: dispatches to a CharsSubstringIndex to produce
an owned String covering the requested code-point range.
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. O(n) — copies bytes.
Subscripts
public subscript[I](checked: I) -> I.CharsYield? { get }
public subscript[I](checked: I) -> I.CharsYield? { get }Reads at index, returning .None on out-of-bounds.
public subscript[I](clamped: I) -> I.CharsClampedYield { get }
public subscript[I](clamped: I) -> I.CharsClampedYield { get }Reads at index with bounds saturated to [0, count). Single-
char indexes yield Char? (None on empty view); range indexes
yield CharsView (always valid, possibly empty).
public subscript[I](I) -> I.CharsYield { get }
public subscript[I](I) -> I.CharsYield { get }Reads a single code point (Char) for Int64 indexes, or a
zero-copy CharsView sub-view for Range[Int64] /
ClosedRange[Int64]. All access is O(n) because UTF-8 is
variable-width. Panics on out-of-bounds.
public subscript[I](wrapped: I) -> I.CharsWrappedYield { get }
public subscript[I](wrapped: I) -> I.CharsWrappedYield { get }Reads at index with modulo wrap-around. Negative indices wrap
from the end: view.chars(wrapped: -1) reads the last char.
Returns None on an empty view.
ImplementsIterable
Associated Types
type Item = Char
type Item = CharThe element type yielded by iteration — always Char.
type TargetIterator = CharsIterator
type TargetIterator = CharsIteratorThe iterator type returned by iter().
Methods
public func iter() -> CharsIterator
public func iter() -> CharsIteratorReturns a CharsIterator positioned at byte 0.
Each call returns a fresh iterator; the view itself is reusable.
ImplementsCloneable
Methods
public func clone() -> CharsView
public func clone() -> CharsViewDefined in lang/std/text/views.ks