Documentation
¶
Overview ¶
Package c0 implements C0DATA — structured data using ASCII C0 control codes.
Values are plain UTF-8 text; structure is expressed through single-byte control codes. The read path is zero-copy: accessors return sub-slices of the input buffer (Go slices are views into the backing array). The hot loop is a single comparison, byte < 0x20.
Index ¶
- Constants
- Variables
- func Build(fn func(*Builder)) ([]byte, error)
- func Canonical(buf []byte) bool
- func Format(buf []byte) string
- func FormatWith(buf []byte, indent string) string
- func Glyph(b byte) rune
- func IsAssigned(b byte) bool
- func Parse(s string) []byte
- func ReadLog(path string) ([]byte, error)
- func Unescape(buf []byte) []byte
- type Builder
- func (b *Builder) Block(text string) *Builder
- func (b *Builder) Bytes() []byte
- func (b *Builder) EOT() *Builder
- func (b *Builder) ETB() *Builder
- func (b *Builder) ETBPayload(payload string) *Builder
- func (b *Builder) Err() error
- func (b *Builder) Field(value string) *Builder
- func (b *Builder) File(name string) *Builder
- func (b *Builder) Group(name string, headers []string) *Builder
- func (b *Builder) Header(names []string) *Builder
- func (b *Builder) Item(text string) *Builder
- func (b *Builder) ListField(items ...string) *Builder
- func (b *Builder) Nested(fn func(*Builder)) *Builder
- func (b *Builder) Record(fields ...string) *Builder
- func (b *Builder) Ref(name string) *Builder
- func (b *Builder) RefPath(path ...string) *Builder
- func (b *Builder) Section(name string, depth int) *Builder
- type Document
- type FileLog
- type Group
- type Record
- type StreamReader
- func (s *StreamReader) Block(i int) []byte
- func (s *StreamReader) BlockCount() int
- func (s *StreamReader) Blocks() [][]byte
- func (s *StreamReader) Committed() []byte
- func (s *StreamReader) CommittedEnd() int
- func (s *StreamReader) Table() *Table
- func (s *StreamReader) Tail() []byte
- func (s *StreamReader) Torn() bool
- type StreamWriter
- type Table
- type Token
- type TokenType
- type Tokenizer
- type UnassignedCodeError
Constants ¶
const ( SOH byte = 0x01 // Header (field name declarations) STX byte = 0x02 // Open nested sub-structure / reference scope ETX byte = 0x03 // Close nested sub-structure / reference scope EOT byte = 0x04 // End of document / message ENQ byte = 0x05 // Reference (enquiry — look up named data) DLE byte = 0x10 // Escape (next byte is literal) ETB byte = 0x17 // Commit marker (stream mode block terminator) SUB byte = 0x1a // Substitution (old → new, C0-DIFF) FS byte = 0x1c // File / Database separator GS byte = 0x1d // Group / Table / Section separator RS byte = 0x1e // Record / Row separator US byte = 0x1f // Unit / Field separator )
Assigned C0 control codes.
Variables ¶
var ErrUnexpectedEnd = errors.New("c0: unexpected end of input after DLE escape")
ErrUnexpectedEnd is returned when input ends immediately after a DLE escape.
Functions ¶
func Canonical ¶
Canonical reports whether bytes are a canonical document unit for content addressing: well-formed, minimally escaped (DLE appears only before bytes < 0x20), and free of framing bytes (ETB, EOT). Stream logs validate per block, not with this.
func Format ¶
Format renders compact bytes as a human-readable Unicode string with two-space indentation (compact layout).
func FormatWith ¶
FormatWith renders with a custom indent string.
func IsAssigned ¶
IsAssigned reports whether b is an assigned C0 control code.
func Parse ¶
Parse parses pretty-form text back to compact bytes. Control Pictures (U+2400–U+241F) become C0 bytes; LF/CR are ignored; whitespace adjacent to control codes is trimmed; inside STX/ETX everything is preserved verbatim.
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder builds C0DATA documents in compact form. Methods chain. Names (file/group/header) reject control bytes — the first such error is recorded and reported by Err; record field values are byte-transparent and DLE-escaped automatically.
func (*Builder) Block ¶ added in v0.2.0
Block writes a document-mode content block (RS + escaped text).
func (*Builder) ETBPayload ¶ added in v0.2.0
ETBPayload writes a stream-mode commit marker followed by an integrity payload. The payload may not contain control bytes (it is terminated by the next control code on read); the first such error is recorded and reported by Err, and the payload is not written.
func (*Builder) Field ¶ added in v0.2.0
Field writes a single field value (US + escaped value), for building a record's fields individually.
func (*Builder) Group ¶
Group writes a group/table scope (GS + name) with optional SOH headers (pass nil for none).
func (*Builder) ListField ¶ added in v0.2.0
ListField writes a field whose value is a flat list (spec: "arrays are US-separated values inside STX/ETX"): US, STX, the items separated by US (each DLE-escaped), ETX. Read back with Record.List.
func (*Builder) Nested ¶ added in v0.2.0
Nested writes a nested sub-structure: STX, whatever fn writes, ETX.
func (*Builder) Record ¶
Record writes a record with positional fields. A Go string may carry any bytes, so binary fields are fine.
func (*Builder) Ref ¶ added in v0.2.0
Ref writes a reference to a named group (ENQ + name). Reference targets are names, so a control byte is recorded via Err like any other name.
type Document ¶
type Document struct {
// contains filtered or unexported fields
}
Document is a zero-copy navigator for a full C0DATA document.
func NewDocument ¶
NewDocument indexes a document buffer (FS/GS/RS/US structure).
func (*Document) GroupByName ¶
GroupByName returns the group with the given name, or nil if none.
func (*Document) GroupCount ¶
GroupCount returns the number of top-level groups.
func (*Document) GroupNames ¶
GroupNames returns all top-level group names.
type FileLog ¶
type FileLog struct {
// contains filtered or unexported fields
}
FileLog is an append-only log file with ETB commits. Each commit is flushed and, when sync is set, fsync'd.
func OpenLog ¶
OpenLog opens an append-only log file, repairing any torn tail first (truncating to the last commit). Each commit is fsync'd.
func OpenLogSync ¶
OpenLogSync is OpenLog with explicit control over per-commit fsync.
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
Group is a group within a document; read it as a Table.
func (*Group) RecordCount ¶
RecordCount returns the number of records.
type Record ¶
type Record struct {
// contains filtered or unexported fields
}
Record is a zero-copy accessor for a single record within a table.
func (*Record) Field ¶
Field returns field n. Respects DLE escaping and STX/ETX nesting; the field is raw (use Value to decode escapes).
func (*Record) FieldCount ¶
FieldCount returns the number of fields (N separators yield N+1 fields).
func (*Record) List ¶ added in v0.2.0
List returns field n as a flat list (see Builder.ListField): the items of its STX/ETX scope, split on top-level US, with escapes decoded. A field that is not a list comes back as a single item; an empty list scope yields an empty slice.
type StreamReader ¶
type StreamReader struct {
// contains filtered or unexported fields
}
StreamReader scans an append-only log for ETB commit markers and exposes only the committed region. Zero-copy: accessors return sub-slices of the buffer.
func NewStreamReader ¶
func NewStreamReader(buf []byte) *StreamReader
NewStreamReader scans buf for ETB commits.
func (*StreamReader) Block ¶
func (s *StreamReader) Block(i int) []byte
Block returns committed block i (marker and payload excluded).
func (*StreamReader) BlockCount ¶
func (s *StreamReader) BlockCount() int
BlockCount returns the number of committed blocks.
func (*StreamReader) Blocks ¶
func (s *StreamReader) Blocks() [][]byte
Blocks returns all committed blocks.
func (*StreamReader) Committed ¶
func (s *StreamReader) Committed() []byte
Committed returns the committed region.
func (*StreamReader) CommittedEnd ¶
func (s *StreamReader) CommittedEnd() int
CommittedEnd returns the offset just past the last commit marker and payload.
func (*StreamReader) Table ¶
func (s *StreamReader) Table() *Table
Table reads the committed region as a Table.
func (*StreamReader) Tail ¶
func (s *StreamReader) Tail() []byte
Tail returns the uncommitted trailing bytes.
func (*StreamReader) Torn ¶
func (s *StreamReader) Torn() bool
Torn reports whether uncommitted bytes trail the last commit marker.
type StreamWriter ¶
type StreamWriter struct {
// contains filtered or unexported fields
}
StreamWriter appends ETB-committed blocks to any io.Writer. Each block and its ETB are written as one unit. For files, prefer OpenLog (torn-tail repair + per-commit fsync).
func NewStreamWriter ¶
func NewStreamWriter(w io.Writer) *StreamWriter
NewStreamWriter returns a StreamWriter over w.
func (*StreamWriter) Batch ¶
func (sw *StreamWriter) Batch(fn func(*Builder)) error
Batch appends several records under a single commit (an atomic batch).
func (*StreamWriter) Header ¶
func (sw *StreamWriter) Header(names []string) error
Header appends an SOH header as a committed block.
func (*StreamWriter) Record ¶
func (sw *StreamWriter) Record(fields ...string) error
Record appends one record as a committed block.
type Table ¶
type Table struct {
// contains filtered or unexported fields
}
Table is a zero-copy accessor for a tabular C0DATA group.
func NewTableAt ¶
NewTableAt indexes a tabular group starting at offset (e.g. a group's GS).
func (*Table) HeaderCount ¶
HeaderCount returns the number of header fields.
func (*Table) RecordCount ¶
RecordCount returns the number of records.
type Token ¶
type Token struct {
Type TokenType
// Start and End are byte offsets into the buffer; End is exclusive.
Start, End int
}
Token is a span of the source buffer.
type Tokenizer ¶
type Tokenizer struct {
// contains filtered or unexported fields
}
Tokenizer scans a buffer for control codes, yielding tokens as offsets.
func NewTokenizer ¶
NewTokenizer returns a Tokenizer over buf.
type UnassignedCodeError ¶
UnassignedCodeError reports a control byte (< 0x20) that is not assigned.
func (*UnassignedCodeError) Error ¶
func (e *UnassignedCodeError) Error() string