x-engine-c v0.2.13
The C engine for x-lang
Loading...
Searching...
No Matches
string.c File Reference

String manipulation primitives. More...

#include "x-prim.h"
#include "x-type/char.h"
#include "x-type/int.h"
#include "x-type/str.h"
#include "x-type/symbol.h"

Functions

static x_obj_tx_prim_string_append (x_obj_t *p_base, x_obj_t *p_args)
 
static x_obj_tx_prim_make_str (x_obj_t *p_base, x_obj_t *p_args)
 
static x_obj_tx_prim_string_to_symbol (x_obj_t *p_base, x_obj_t *p_args)
 
static x_obj_tx_prim_symbol_to_string (x_obj_t *p_base, x_obj_t *p_args)
 
static x_obj_tx_prim_list_to_string (x_obj_t *p_base, x_obj_t *p_args)
 
static x_obj_tx_prim_str_byte_len (x_obj_t *p_base, x_obj_t *p_args)
 
static x_obj_tx_prim_str_byte_ref (x_obj_t *p_base, x_obj_t *p_args)
 
static x_obj_tx_prim_str_byte_sub (x_obj_t *p_base, x_obj_t *p_args)
 
x_obj_tx_prim_string_register (x_obj_t *p_base, x_obj_t *p_args)
 

Detailed Description

String manipulation primitives.

Author
Jon Ruttan (jonru.nosp@m.ttan.nosp@m.@gmai.nosp@m.l.co.nosp@m.m)

Function Documentation

◆ x_prim_list_to_string()

static x_obj_t * x_prim_list_to_string ( x_obj_t p_base,
x_obj_t p_args 
)
static

Build a string from a list of characters, one byte per character. x-lang: (bytes->str list-of-chars)

Parameters
p_baseInterpreter base context.
p_argsUnevaluated argument list: (bytes->str list-of-chars).
Returns
A newly allocated string holding each character's low byte.
Note
Byte-level and protocol-agnostic: each CHARACTER contributes one byte (its value masked to 0-255). This is the dumb byte-packer; assembling a multi-byte UTF-8 string from code points is done in the x-lang layer (str->list / list->str in x/type/string, via the x/codec/utf8 encoder). Walks the list twice: once to count, once to copy. No x-lang dispatch, so it stays safe inside tokenizer callbacks.

◆ x_prim_make_str()

static x_obj_t * x_prim_make_str ( x_obj_t p_base,
x_obj_t p_args 
)
static

Allocate a fresh writable byte region as a string object. x-lang: (make-str n)

Parameters
p_baseInterpreter base context.
p_argsUnevaluated argument list: (make-str n).
Returns
A new OWNED string of exactly n bytes.
Note
SPACE-filled and NUL-terminated so the strlen-based byte accessors see exactly n bytes. Callers fill it in place via (str ->ptr) + the raw-mem stores, File read, or FFI calls. The object OWNS the region – the GC frees it, so there is no free door and no leak on an error path. UNCHECKED like first/rest: n is trusted (the x layer guards).

◆ x_prim_str_byte_len()

static x_obj_t * x_prim_str_byte_len ( x_obj_t p_base,
x_obj_t p_args 
)
static

Raw byte length of a string. x-lang: (str-byte-len s)

Parameters
p_baseInterpreter base context.
p_argsUnevaluated argument list: (str-byte-len s).
Returns
Integer: the number of bytes in s.
Note
Always byte-level, independent of any pushed string-call handler. This is the explicit 8-bit accessor the Str8 protocol bottoms out in; readers/tokenizers that need bytes must use this, not the ambient (s) call (whose meaning depends on the installed protocol).

◆ x_prim_str_byte_ref()

static x_obj_t * x_prim_str_byte_ref ( x_obj_t p_base,
x_obj_t p_args 
)
static

Byte at index i of a string, as a CHARACTER (0-255). x-lang: (str-byte-ref s i)

Parameters
p_baseInterpreter base context.
p_argsUnevaluated argument list: (str-byte-ref s i).
Returns
The i-th byte as a CHARACTER. Negative i counts from the end.
Note
Always byte-level, independent of any pushed string-call handler.

◆ x_prim_str_byte_sub()

static x_obj_t * x_prim_str_byte_sub ( x_obj_t p_base,
x_obj_t p_args 
)
static

Byte substring: len bytes of s starting at byte offset start. x-lang: (str-byte-sub s start len)

Parameters
p_baseInterpreter base context.
p_argsUnevaluated argument list: (str-byte-sub s start len).
Returns
A newly allocated string of len bytes from byte offset start.
Note
Always byte-level, independent of any pushed string-call handler.

◆ x_prim_string_append()

static x_obj_t * x_prim_string_append ( x_obj_t p_base,
x_obj_t p_args 
)
static

Concatenate two strings. x-lang: (str-append a b)

Parameters
p_baseInterpreter base context.
p_argsUnevaluated argument list: (str-append a b).
Returns
A newly allocated string containing a followed by b.
Note
The result is heap-allocated and owned by the returned object.

◆ x_prim_string_register()

x_obj_t * x_prim_string_register ( x_obj_t p_base,
x_obj_t p_args 
)

Register string manipulation primitives.

Binds: str-append, str->symbol, symbol->str, bytes->str, str-byte-len, str-byte-ref, str-byte-sub.

Note
bytes->str is the raw byte-packer (one low byte per char). The code-point-aware list->str is pure x-lang (x/type/str-utf8), UTF-8 encoding each char on top of bytes->str; boot-time callers that want bytes call bytes->str directly.
Parameters
p_baseInterpreter base context.
p_argsUnused.
Returns
p_base.

◆ x_prim_string_to_symbol()

static x_obj_t * x_prim_string_to_symbol ( x_obj_t p_base,
x_obj_t p_args 
)
static

Convert a string to an interned symbol. x-lang: (str->symbol str)

Parameters
p_baseInterpreter base context.
p_argsUnevaluated argument list: (str->symbol str).
Returns
A symbol whose name matches the content of str.

◆ x_prim_symbol_to_string()

static x_obj_t * x_prim_symbol_to_string ( x_obj_t p_base,
x_obj_t p_args 
)
static

Convert a symbol to a string. x-lang: (symbol->str sym)

Parameters
p_baseInterpreter base context.
p_argsUnevaluated argument list: (symbol->str sym).
Returns
A string object containing the symbol's name.