Skip to content

6 2 strings and i o

TransCompilator edited this page Nov 26, 2026 · 1 revision

6.2 Strings and I/O

Source: https://deepwiki.com/PonvasanthPon/TransCompilator/6-2-strings-and-i-o

Strings and I/O

Relevant source files

The following files were used as context for generating this wiki page:

Purpose and Scope

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.


1. The TcString Record and Memory Model

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.

Initialization and Destruction

  • str_new: Creates an empty text instance with zeroed metadata std/string.eng:10-12.
  • str_from_bytes: Allocates capacity equal to n + 1 bytes via allocate capacity bytes, copies n bytes 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


2. String Manipulation and Searching Algorithms

String mutation and analysis routines are implemented natively in Structured English. These functions handle dynamic buffer resizing, slicing, and pattern matching.

Resizing and Appending (str_push_bytes)

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:

  1. The new capacity is set to double the old capacity (s's cap times 2) std/string.eng:27.
  2. If the doubled capacity is insufficient, it adjusts to fit the required amount std/string.eng:28.
  3. A minimum capacity floor of 16 bytes is enforced std/string.eng:29.
  4. resize memory is invoked to grow the buffer, followed by copying the new bytes and appending a null byte std/string.eng:31-36.

Searching and Slicing

  • str_eq: Compares two TcString buffers 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 over s looking for a needle subsequence std/string.eng:104-119.
  • str_trim: Scans from both ends of the string to strip ASCII whitespace characters (space 32, tab 9, line feed 10, carriage return 13) and returns a sub-slice std/string.eng:121-134.

Natural Language to Code Mapping: String Operations

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"]
Loading

Sources: std/string.eng:23-134


3. Formatting Routines (std/io.eng)

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.

Unsigned Integer Formatting (format_u64_into)

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 buf and returns the final byte length std/string.eng:17-23.

Signed Integer and Decimal Formatting

  • format_i64_into: Handles optional negative signs ('-', ASCII 45) for signed whole numbers before delegating to the unsigned formatter std/io.eng:25-31.
  • format_f64_into: Scales decimal floating-point numbers by 10000.0 to 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


4. Standard I/O and System Call Integration

Console input and output in TransCompilator interact directly with Linux kernel system interfaces via the Ask Linux kernel syntax.

Syscall Interface

  • Write Operations (write_bytes): Invokes Linux kernel system call 1 (sys_write) with file descriptor 1 (stdout), a pointer to the buffer, and the byte length std/io.eng:57-58.
  • Read Operations (read_i64_stdin): Invokes Linux kernel system call 0 (sys_read) with file descriptor 0 (stdin) to read byte streams character by character std/io.eng:143-152. It parses optional negative signs ('-', ASCII 45) and numeric digit ranges (48 through 57) into whole numbers std/io.eng:154-157.

Natural Language to Code Mapping: I/O Routines

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"]
Loading

Sources: std/io.eng:57-178


Clone this wiki locally