Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Substring

Source: lib/substring.x

Guide: Iteration

Borrowed immutable views of String bytes.

Include it with #include "substring.x".

Primary API

FunctionSummary
String.substringReturns a borrowed immutable view of the requested byte range.
Substring.compareCompares contents in bytewise lexical order.
Substring.equalCompares contents without materializing either view.
Substring.hashHashes the referenced bytes, independent of their source or offset.
Substring.lenReturns the byte length without allocating.
Substring.newBorrows length bytes at offset.
Substring.strMaterializes a canonical String in the active pool.
Substring.stringMaterializes the same canonical String as str().
Substring.try_nextYields the next word view.

String

String.substring

Substring String.substring(String source, int offset, int length)

Returns a borrowed immutable view of the requested byte range.

Source: lib/substring.x

Substring

Substring.compare

inline int Substring.compare(Substring left, Substring right)

Compares contents in bytewise lexical order.

Source: lib/substring.x

Substring.equal

inline int Substring.equal(Substring left, Substring right)

Compares contents without materializing either view.

Source: lib/substring.x

Substring.hash

inline unsigned Substring.hash(Substring view)

Hashes the referenced bytes, independent of their source or offset.

Source: lib/substring.x

Substring.len

inline int Substring.len(Substring view)

Returns the byte length without allocating.

Source: lib/substring.x

Substring.new

Substring Substring.new(String source, int offset, int length)

Borrows length bytes at offset. An invalid range raises bad-arg.

Source: lib/substring.x

Substring.str

String Substring.str(Substring view)

Materializes a canonical String in the active pool. Repeated conversion reuses the canonical value. The result follows its actual owning pool.

Source: lib/substring.x

Substring.string

String Substring.string(Substring view)

Materializes the same canonical String as str().

Source: lib/substring.x

Substring.try_next

int Substring.try_next(String source, int &cursor, Substring &out)

Yields the next word view. Start the caller-owned cursor at zero. The source must stay live; exhaustion leaves cursor and out unchanged. foreach (Substr word in source) uses this allocation-free cursor.

Source: lib/substring.x

Public types

TypeKindSummary
SubstraliasShort name for the same borrowed view representation as Substring.
SubstringstructA borrowed immutable range of a String.

Substr

typedef Substring Substr

Short name for the same borrowed view representation as Substring.

Source: lib/substring.x

Substring

typedef struct Substring { String source; int offset, length; } Substring

A borrowed immutable range of a String. Assignment copies the view and allocates nothing. Keep the source alive and unchanged through every use; use str() or string() to obtain a canonical String in the active pool. Construct views through Substring.new or String.substring.

Source: lib/substring.x

Examples