BytesView

public struct BytesView { /* private fields */ }

A read-only view over the raw UTF-8 bytes of a String.

Returned by String.bytes. Provides O(1) byte indexing and iteration; the bytes are returned as UInt8 exactly as they sit in memory. The most common reason to reach for BytesView is to perform byte-level operations (substring searches, hashing) without paying the cost of UTF-8 decoding. For code-point or grapheme iteration, see CharsView / GraphemesView.

Examples

let s = "hi"; s.bytes.count; // 2 s.bytes(checked: 0); // Some(104) s.bytes(checked: 5); // None (out of bounds)

Representation

A (ptr, length) pair pointing at the source string's UTF-8 buffer.

Memory Model

Borrows the source string's storage; the view is invalidated by any mutation that reallocates that buffer. Copy out to a new String (e.g. via substring) if you need an independent value.

Properties

public var count: Int64 { get }

Number of bytes in the view.

O(1). This is byte count, not character count — see CharsView.count for the latter (which is O(n)).

public var endIndex: ByteIndex { get }

Byte index one past the last byte.

public var isEmpty: Bool { get }

true if the view spans zero bytes.

public var startIndex: ByteIndex { get }

Byte index of the first byte.

Initializers

public init(slice: StringSlice)

Constructs a bytes view backed by the given string slice. The view retains shared ownership of the underlying bytes.

Methods

public func asRaw() -> lang.ptr[lang.i8]

Returns the raw pointer to the underlying byte buffer.

Intended for FFI bridges; the pointer is only valid as long as the source string remains live and unmutated.

public func firstIndex(of: UInt8) -> ByteIndex?

Returns the index of the first occurrence of byte, or .None.

public func lastIndex(of: UInt8) -> ByteIndex?

Returns the index of the last occurrence of byte, or .None.

public func slice(from: ByteIndex, to: ByteIndex) -> StringSlice

Returns a StringSlice covering the byte range [start, end).

public func substring[__opaque_0](__opaque_0) -> String where __opaque_0: BytesSubstringIndex

Convenience: dispatches to a BytesSubstringIndex to produce an owned String covering the requested byte range. Equivalent to self(range).toString() for both Range[Int64] and ClosedRange[Int64].

public func toString() -> String

Materializes the view as an owned String. Copies all bytes into a fresh buffer; the result is independent of the source. Bytes are copied verbatim — no UTF-8 validation is performed.

Subscripts

public subscript[I](checked: I) -> I.BytesYield? { get }

Reads at index, returning .None on out-of-bounds.

public subscript[I](clamped: I) -> I.BytesClampedYield { get }

Reads at index with bounds saturated to [0, count). Single- byte indexes yield UInt8? (None on empty view); range indexes yield BytesView (always valid, possibly empty).

public subscript[I](I) -> I.BytesYield { get }

Reads a single byte (UInt8) for Int64 indexes, or a zero-copy sub-view (BytesView) for Range[Int64] / ClosedRange[Int64]. Panics on out-of-bounds. Range slicing does not validate UTF-8 boundaries — call .toString() on the sub-view if you need an owned String (which validates).

public subscript[I](unchecked: I) -> I.BytesYield { get }

Reads at index with no bounds check.

Safety

Caller must guarantee 0 <= index < count. For ranges, the endpoints must be in 0..=count; otherwise the resulting sub-view aliases out-of-bounds memory.

public subscript[I](wrapped: I) -> I.BytesWrappedYield { get }

Reads at index with modulo wrap-around. Negative indices wrap from the end: view.bytes(wrapped: -1) reads the last byte. Returns None on an empty view.

ImplementsIterable

Associated Types

type Item = UInt8

The element type yielded by iteration — always UInt8.

type TargetIterator = BytesIterator

The iterator type returned by iter().

Methods

public func iter() -> BytesIterator

Returns a BytesIterator positioned at byte 0.

Required by Iterable. Each call produces a fresh iterator — the view is reusable.

ImplementsCloneable

Methods

public func clone() -> BytesView

Defined in lang/std/text/views.ks