Standard library, strings, StringView, and regex

April 21, 2026 · View on GitHub

This document ties together three related layers: Casprix .cpx modules under lib/, the C runtime used by generated code and embedders, and compiler internals for regex and linear string views.


1. Casprix standard modules (lib/)

Shipped Casprix sources are rooted at lib/. Typical entry points:

AreaPathNotes
Core collections / genericslib/stdlib/Collections.cpx, Generics.cpx, Collections_Simple.cpxLists, maps, patterns (evolving)
Interfaces & lambdaslib/stdlib/Interfaces.cpx, Lambdas.cpxShared protocols and helpers
Concurrencylib/stdlib/Thread.cpx, AsyncThread.cpxMaps to runtime thread / async APIs
Networkinglib/stdlib/Network.cpx, AsyncNetwork.cpxHTTP / socket-oriented wrappers
GUI (Skia)lib/stdlib/GUI.cpx, ModernGUI.cpxOptional; needs ENABLE_SKIA_GUI build
LLMlib/llm/Tokenizer and related .cpx

Imports use quoted paths, e.g. import "lib/stdlib/Collections.cpx" (exact style depends on your project layout).

The stdlib/ directory holds the bootstrap package index and small modules used before the full tree is available; see docs/PROJECT_STRUCTURE.md.


2. Runtime string API (C)

User-facing string operations for generated code and tests are implemented in C and declared in include/casprix/string_ops.h. The implementation lives in runtime/stdlib/string_ops.c.

Symbols use the historical nuwan_string_* prefix. Capabilities include:

  • Concatenation, substring, length
  • Case conversion (hot paths may use SIMD-style batching where available)
  • Search: index_of, last_index_of, contains, starts_with, ends_with
  • Trim variants, split, replace / replace_all
  • FNV-1a hash (nuwan_string_hash) and helpers used by the runtime

Unit coverage: tests/runtime/test_stdlib.c.


3. Linear StringView (compiler semantic layer)

The compiler models a non-owning view into string storage as a linear type: a StringView has move-only semantics and must not outlive its parent string (enforced in semantic analysis, escape analysis, and the drop planner).

Authoritative design notes: src/compiler/sema/linear_view.h.

Practical implications:

  • Use-after-move of a StringView is rejected at compile time where tracked.
  • Returning or storing a StringView beyond the parent’s scope is rejected when detected.
  • Regression / exploratory samples: tests/corpus/stringview_linear.cpx.

4. Regex → MIR compiler (inside the toolchain)

End-user regex syntax in .cpx is not the focus here; the repository includes a compiler subsystem that compiles a regex pattern into a MIR function that scans a byte buffer in one left-to-right pass (DFA-shaped control flow).

This is primarily for optimization / codegen experiments and corpus verification; wiring every regex surface feature in the language front end is separate work.