String & Slice Proposal #339
Closed
tahadostifam
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
String and Slice Proposal
Overview
Cyrus provides two fundamental string representations:
char*char[]ptr + len)String literals may be created as either regular slices or null-terminated strings.
String Literals
Null-Terminated String
The
zprefix guarantees a trailing'\0'.Memory layout:
Slice String
Creates a slice whose length is known at compile time.
Memory layout:
Slice from a Null-Terminated Literal
Creates a slice view over a null-terminated string.
The slice length excludes the trailing
'\0'.Creating Slices from Pointers
Pointers may be explicitly converted into slices using
@slice.Equivalent conceptual representation:
The conversion is explicit because the compiler cannot infer the length of a raw pointer.
Accessing Slice Components
Slices expose their pointer and length directly.
No conversion builtin is required.
Converting a Slice to a Null-Terminated String
A slice may be explicitly converted to a null-terminated string pointer using
@as_zstr.Requirements
@as_zstrrequires that the slice references a buffer whose element immediately following the slice is'\0'.Valid:
Potentially invalid:
The implementation may emit a compile-time error, runtime check, or require an
unsafecontext if null termination cannot be proven.Summary
Builtins
Beta Was this translation helpful? Give feedback.
All reactions