Repository navigation
6 2 strings and i o
Source: https://deepwiki.com/PonvasanthPon/TransCompilator/6-2-strings-and-i-o
Relevant source files
The following files were used as context for generating this wiki page:
This page covers the implementation of text management and input/output operations within the TransCompilator English standard library, primarily defined in std/string.eng and std/io.eng. TransCompilator (tc_r1) provides a growable byte sequence structure (TcString) equipped with zero-termination invariants for low-level kernel interaction, along with robust formatting, string searching, and low-level Linux syscall wrappers for console I/O.
The standard library defines text as a heap-allocated, growable sequence of bytes guaranteed to end with a final zero byte (0x00) std/string.eng:1-3. This zero-termination design ensures that string buffers can be passed directly to underlying operating system kernel calls without reallocating or copying.
The core data structure is declared as a public record named TcString comprising three fields std/string.eng:8:
-
data: A pointer to byte pointing to the heap memory buffer. -
len: An unsigned number representing the current string length in bytes. -
cap: An unsigned number representing the total allocated capacity of the buffer.
-
str_new: Creates an empty text instance with zeroed metadata std/string.eng:10-12. -
str_from_bytes: Allocates capacity equal ton + 1bytes viaallocate capacity bytes, copiesnbytes from the input pointer, and appends the trailing null terminator (0) std/string.eng:14-21. -
str_free: Releases the underlying heap memory if non-empty and resets the pointer, length, and capacity to zero std/string.eng:66-68.
Sources: std/string.eng:1-68
String mutation and analysis routines are implemented natively in Structured English. These functions handle dynamic buffer resizing, slicing, and pattern matching.
When appending bytes to an existing TcString, the method checks whether the required capacity (len + n + 1) exceeds the current capacity (cap) std/string.eng:23-26. If an expansion is necessary:
- The new capacity is set to double the old capacity (
s's cap times 2) std/string.eng:27. - If the doubled capacity is insufficient, it adjusts to fit the required amount std/string.eng:28.
- A minimum capacity floor of 16 bytes is enforced std/string.eng:29.
-
resize memoryis invoked to grow the buffer, followed by copying the new bytes and appending a null byte std/string.eng:31-36.
-
str_eq: Compares twoTcStringbuffers byte-by-byte after verifying length parity std/string.eng:43-51; mapped from the natural language syntax"text <a> matches text <b>"std/string.eng:44. -
str_contains: Performs a sliding window search overslooking for aneedlesubsequence std/string.eng:104-119. -
str_trim: Scans from both ends of the string to strip ASCII whitespace characters (space32, tab9, line feed10, carriage return13) and returns a sub-slice std/string.eng:121-134.
The following diagram illustrates how natural language English phrases in source files map to underlying TcString functions in the standard library.
graph TD
NL_Match["Natural Language: text a matches text b"] --> CE_Eq["TcString::str_eq"]
NL_Clone["Natural Language: copy text s"] --> CE_Clone["TcString::str_clone"]
NL_Trim["Natural Language: trim text s"] --> CE_Trim["TcString::str_trim"]
CE_Eq --> Src_Eq["std/string.eng:43"]
CE_Clone --> Src_Clone["std/string.eng:56"]
CE_Trim --> Src_Trim["std/string.eng:121"]
Sources: std/string.eng:23-134
The std/io.eng module implements numeric-to-string formatting routines directly into raw byte buffers without relying on external libc formatting functions like sprintf.
Converts an unsigned 64-bit integer into a decimal ASCII representation std/string.eng:7-23:
- Uses a local 20-byte stack block (
digits) std/string.eng:8. - Extracts digits iteratively via modulo and division by 10, storing them in reverse order std/string.eng:12-16.
- Reverses the digits into the destination buffer
bufand returns the final byte length std/string.eng:17-23.
-
format_i64_into: Handles optional negative signs ('-', ASCII45) for signed whole numbers before delegating to the unsigned formatter std/io.eng:25-31. -
format_f64_into: Scales decimal floating-point numbers by10000.0to preserve four decimal places of precision, separates the integer and fractional components, and formats them with a decimal point ('.') std/io.eng:32-53.
Sources: std/io.eng:7-53
Console input and output in TransCompilator interact directly with Linux kernel system interfaces via the Ask Linux kernel syntax.
-
Write Operations (
write_bytes): Invokes Linux kernel system call1(sys_write) with file descriptor1(stdout), a pointer to the buffer, and the byte length std/io.eng:57-58. -
Read Operations (
read_i64_stdin): Invokes Linux kernel system call0(sys_read) with file descriptor0(stdin) to read byte streams character by character std/io.eng:143-152. It parses optional negative signs ('-', ASCII45) and numeric digit ranges (48through57) into whole numbers std/io.eng:154-157.
The following diagram maps high-level English I/O statements to their standard library implementations and kernel system calls.
graph TD
IO_Say["Natural Language: say n (a whole number)"] --> IO_Print["std/io.eng:print_i64"]
IO_Read["Natural Language: read a whole number from input"] --> IO_Scan["std/io.eng:read_i64_stdin"]
IO_Print --> SysWrite["LinuxKernelSyscall1_sys_write"]
IO_Scan --> SysRead["LinuxKernelSyscall0_sys_read"]
SysWrite --> CodeOut["std/io.eng:81"]
SysRead --> CodeIn["std/io.eng:143"]
Sources: std/io.eng:57-178
The TransCompilator Project