Skip to content

Standard library

The xcc standard library is a set of .xc classes that ship with the compiler. Each class can be imported by name with #import. Most methods are static, so most calls look like Stdio.print("hi\n") or Math.rand() with no instance needed.

support/
generic/lib/ ← portable classes: work on every target
Foundation.xc ← umbrella: Object + Number + String + Data + Array + Map + Set
+ Bag + Range + BinaryHeap + Cache + Null
Object.xc ← the runtime's root class
Number.xc String.xc Data.xc Array.xc Map.xc Set.xc CharacterSet.xc
Bag.xc Range.xc BinaryHeap.xc Cache.xc Null.xc
JSON.xc ← JSON text to Foundation objects and back
CSV.xc ← comma-separated values to rows and back
Expression.xc ← arithmetic expressions evaluated against variables
NumberFormatter.xc ← numbers to display text and back
NotificationCenter.xc ← a publish/subscribe bus
UndoManager.xc ← undo and redo
Progress.xc ← how far work has got
StateMachine.xc ← named states driven by events
SearchIndex.xc ← a small full-text index
IndexSet.xc ← a set of indexes kept as ranges
AttributedString.xc ← text with attributes over ranges
Regex.xc ← regular expressions
Predicate.xc ← conditions over records
Validator.xc ← rules a field's text must pass
SortDescriptor.xc ← sort records by keys
Socket.xc ← a TCP connection
Comparable.xc Hashable.xc Enumerable.xc Copying.xc Codable.xc Error.xc ← protocols
Coder.xc ← keyed archiving to JSON, with gzip
Thread.xc Mutex.xc Cond.xc Sem.xc Atomic.xc ThreadLocal.xc Pool.xc
Assert.xc Sort.xc
Platform.xc ← auto-included prelude
Settings.xc Bundle.xc ← persistent settings, and a program's own files
Http.xc HttpTls.xc ← HTTP/1.1 client and url.fetch transport; https
AsyncFiles.xc ← Files operations on a background thread, in order
RunLoop.xc ← hand work to one thread, and timers
arm64/lib/ ← macOS / Linux on 64-bit ARM
Stdio.xc Math.xc Time.xc Heap.xc FILE.xc Files.xc Process.xc
Gfx*.xc GfxFactory.xc Platform.xc
x86_64/lib/ ← Linux (musl)
Stdio.xc Math.xc Time.xc Heap.xc FILE.xc Platform.xc
win64/lib/ ← Windows; the rest comes from x86_64/ and generic/
Platform.xc
arm9/lib/ ← AArch32 / XTOS, plus the GEM app framework
Stdio.xc Math.xc Time.xc Heap.xc FILE.xc Files.xc Process.xc Runtime.xc
GApplication.xc GEvent.xc XTGem.xc Gfx*.xc Platform.xc
xt6502/lib/ ← the banked 6502
Stdio.xc Math.xc Time.xc Heap.xc System.xc Vbi.xc FILE.xc Memory.xc
Array.xc Map.xc Set.xc String.xc Data.xc Number.xc ← 6502 Foundation build
Bag.xc ← 6502 Bag (u16 indexes)
Enumerable.xc Hashable.xc ← 6502-width protocols
Gfx*.xc GfxFactory.xc mapData.xc symbols.xc Platform.xc
xt6502/asm/ ← 6502 assembly runtime (mul/div, heap, float) — not .xc classes
xt6502/layouts/ ← .lnk memory maps
<target>/runtime/ ← the small C host runtime linked into native builds

In an installed toolchain this tree is lib/xc/ under the install root (xc\ on Windows); see Install. The paths above show a source checkout. Both layouts resolve.

The compiler’s #import machinery searches the active target’s directory first, then generic/lib/, so a class with the same name in both wins on the active platform. This is how Stdio.xc gets per-platform implementations, and how Foundation ships two builds behind one API: a 32-bit one in generic/lib/ and a 6502-tuned one in xt6502/lib/. Files with no integer width in them (Object, Comparable, Error, Assert, Sort, the Foundation umbrella) exist once and are shared by both. The width-bearing ones (the containers, plus Hashable and Enumerable) are duplicated. Every target except xt6502 uses the generic/lib/ Foundation directly. The threading classes (Thread, Mutex, Cond, Sem, Atomic, ThreadLocal, Pool) also live in generic/lib/, but are a hard #error on xt6502 and m68k rather than a stub; see Threading. Coder and Codable are a hard #error on xt6502 only.

Platform.xc is different from the rest: the compiler emits an implicit #import "Platform.xc" before every compilation. It is where a target’s system bindings live, so the user’s source stays platform-agnostic. Every shipped copy is currently an empty placeholder.

Most library methods are static. You can call them three ways:

#import <Stdio.xc>
void main(void) {
Stdio.print("explicit\n"); // class.method()
}
#import <Stdio.xc>
use Stdio; // language-level promotion
void main(void) {
print("bare-call\n"); // resolves to Stdio.print
}
#use Stdio // preprocessor sugar:
// #import + use in one line
void main(void) {
print("shortest form\n");
}

Bare-call promotion (use Stdio; and the #use shorthand) is documented under Classes → Bare-call promotion and Preprocessor → #use. These pages use the explicit Klass.method(...) form because it is unambiguous. In your own code, use whichever form you prefer.

The reference is grouped the same way as the sidebar. Each class page is a complete method reference: an overview, the protocols the class conforms to, and every method grouped by task with a jump-list at the top.

Foundation: the object library, one page per class.

ClassRole
Objectthe runtime’s root class — equals, hash, description
Numbera boxed scalar (any int width, float, double) for containers
Stringheap-owned UTF-8 string, byte- and character-indexed
Dataa growable byte buffer, plus the String ⇄ bytes encoding bridge
Arrayan ordered, growable list with map / filter / reduce and sort
Mapan insertion-ordered hash map
Seta hash set with union / intersection / difference
Baga counted set (from 0.72)
Rangea half-open index range (from 0.72)
BinaryHeapa priority queue (from 0.72)
Cachea bounded LRU cache (from 0.72)
Nullthe shared “nothing here” object (from 0.72)
JSONJSON text to Foundation objects and back (from 0.72)
Expressionarithmetic expressions evaluated against variables (from 0.72)
NumberFormatternumbers to display text and back (from 0.72)
NotificationCentera publish/subscribe bus (from 0.72)
UndoManagerundo and redo (from 0.72)
Progresshow far work has got (from 0.72)
StateMachinenamed states driven by events (from 0.72)
SearchIndexa small full-text index (from 0.72)
IndexSeta set of indexes kept as ranges (from 0.72)
AttributedStringtext with attributes over ranges (from 0.72)
Regexregular expressions (from 0.72)
Predicateconditions over records (from 0.72)
Validatorrules a field’s text must pass (from 0.72)
SortDescriptorsort records by keys (from 0.72)
Socketa TCP connection (from 0.72)
CSVcomma-separated values to rows and back (from 0.72)
Coderkeyed archiving of an object graph to JSON, optionally gzipped

Protocols: Comparable, Hashable, Enumerable, Copying, Codable and Error, the small interfaces the classes conform to.

System utilities (cross-platform):

ClassRole
Stdioformatted output (printf), screen/cursor helpers
Mathrandom numbers, sqrt, trig, log/exp/pow, constants
Sortin-place quicksort with a user-supplied comparator
Memorybulk memset / memclr / memcpy / memmove (xt6502)
Asserttest-fixture assertions; no-ops under -DNDEBUG / -DRELEASE
Settingsa persistent key/value store: in memory always, in a text file where there is a filesystem
Bundlewhere a program’s own files live, and the resources inside
HttpHTTP/1.1 requests, blocking or on their own thread, and the transport behind url.fetch; https through the optional TLS library
Fileswhole-file reads and writes, appends, directories and existence checks
AsyncFilesthe Files operations off the calling thread, run in order, with a completion block
RunLoopa queue of blocks run on one thread, posted to from any thread, and timers

6502 (8-bit): the utilities the 8-bit target provides in place of an OS: Time, Heap, Vbi, System. On the native targets these are thin wrappers over the host. On the 6502 they are target-specific implementations.

Also documented: the 6502’s graphics classes (Gfx, GfxFactory), FILE (a stdio-shaped file layer), CharacterSet, string-xt6502, and the symbols / mapData helpers.

A note on overload resolution by return type

Section titled “A note on overload resolution by return type”

xcc supports overloading by return type for zero-arg static methods, and the standard library uses this for Math.rand() and the math constants. auto x = Math.rand(); is ambiguous, because the compiler needs to know which type you want:

u8 a = Math.rand(); // resolves to the u8 overload
u16 b = Math.rand(); // resolves to the u16 overload
float c = Math.rand(); // resolves to the float overload
double d = Math.rand(); // resolves to the double overload

The same applies to Math.PI(), Math.E() and the other constants: each has a float-returning and a double-returning overload, picked by the receiving variable’s type.