Module: C0

Defined in:
lib/c0.rb,
lib/c0/body.rb,
lib/c0/diff.rb,
lib/c0/pretty.rb,
lib/c0/stream.rb,
lib/c0/builder.rb,
lib/c0/document.rb

Defined Under Namespace

Modules: Diff, Pretty, Stream, UnitType Classes: Body, Builder, Container, DiffBuilder, Document, FileView, Group, Heading, PatternBuilder, Record, Reference, Unit

Constant Summary collapse

SOH =

Assigned C0 control codes (DESIGN.md "Assigned Control Codes").

0x01
STX =

Heading: a key for the value after it, or SOH RS a header row

0x02
ETX =

Start of text (a literal value)

0x03
EOT =

End of text

0x04
ENQ =

End of document

0x05
BEL =

Reference: a group name, or a pattern (ENQ DLE … CAN)

0x07
DLE =

Comment: a note, or marks the next element dead

0x10
SYN =

Recurse: opens a nested level (outside text); escape (inside text)

0x16
ETB =

Start of document, then the heading

0x17
CAN =

Commit marker (stream mode)

0x18
EM =

Close a nested level

0x19
SUB =

End of medium: a raw blob follows

0x1a
FS =

Subordinate: the element after is a child of the element before (GS: subsection, US: sub-unit)

0x1c
GS =

File separator

0x1d
RS =

Group separator

0x1e
US =

Record separator

0x1f
HT =

Layout codes outside text.

0x09
LF =
0x0a
CR =
0x0d
SP =
0x20
Table =
Group

Class Method Summary collapse

Class Method Details

.assigned?(byte) ⇒ Boolean

Whether a byte is one of the sixteen assigned codes.

Returns:

  • (Boolean)


76
# File 'lib/c0.rb', line 76

def assigned?(byte) = Ext.is_assigned(byte)

.build ⇒ Object

Build a buffer by driving a fresh Builder, returning its bytes.



116
# File 'lib/c0.rb', line 116

def build(&) = Builder.build(&)

.canonical?(buf) ⇒ Boolean

Whether bytes are a canonical span: well-formed, no layout, one spelling per value, unique keys in each keyed record, no framing, no notes or dead elements. A SUB prefix is structure and is kept.

Returns:

  • (Boolean)


103
# File 'lib/c0.rb', line 103

def canonical?(buf) = Ext.canonical(to_bin(buf))

.control?(byte) ⇒ Boolean

Whether a byte is a control code (below 0x20).

Returns:

  • (Boolean)


82
# File 'lib/c0.rb', line 82

def control?(byte) = byte < 0x20

.decode_text(inside) ⇒ Object

Decode the inside of a text (the bytes between STX and ETX).



113
# File 'lib/c0.rb', line 113

def decode_text(inside) = Ext.decode_text(to_bin(inside))

.layout?(byte) ⇒ Boolean

Whether a byte is layout outside text (SP, HT, LF, CR).

Returns:

  • (Boolean)


79
# File 'lib/c0.rb', line 79

def layout?(byte) = Ext.is_layout(byte)

.name?(value) ⇒ Boolean

Whether bytes are a legal name (label, key, header name): the same test as plain.

Returns:

  • (Boolean)


88
# File 'lib/c0.rb', line 88

def name?(value) = Ext.is_plain(to_bin(value))

.plain?(value) ⇒ Boolean

Whether a value can be written plain: no byte below 0x20, no edge whitespace.

Returns:

  • (Boolean)


85
# File 'lib/c0.rb', line 85

def plain?(value) = Ext.is_plain(to_bin(value))

.pretty_format(buf, indent: " ", mode: :compact) ⇒ Object

Pretty form (Unicode Control Pictures) of compact bytes.



119
# File 'lib/c0.rb', line 119

def pretty_format(buf, indent: "  ", mode: :compact) = Pretty.format(buf, indent: indent, mode: mode)

.pretty_parse(text) ⇒ Object

Compact bytes from pretty form.



122
# File 'lib/c0.rb', line 122

def pretty_parse(text) = Pretty.parse(text)

.to_bin(str) ⇒ Object

Coerce to a binary (ASCII-8BIT) String without copying when already binary.



70
71
72
73
# File 'lib/c0.rb', line 70

def to_bin(str)
  str = str.to_s unless str.is_a?(String)
  str.encoding == Encoding::BINARY ? str : str.b
end

.trim(raw) ⇒ Object

Layout trimmed from both ends of a raw slice.



91
# File 'lib/c0.rb', line 91

def trim(raw) = Ext.trim(to_bin(raw))

.validate(buf) ⇒ Object

Check a whole buffer (one or more documents, an optional EM tail) against the grammar. Raises a C0::SyntaxError (or a subclass) on the first error; returns nil when well-formed.



96
97
98
# File 'lib/c0.rb', line 96

def validate(buf)
  Ext.validate(to_bin(buf))
end

.value(raw) ⇒ Object

The logical value of a raw unit: a plain value trimmed, or the inside of a text with its escapes decoded. Raises C0::Error on a level or reference.



107
108
109
110
# File 'lib/c0.rb', line 107

def value(raw)
  b = to_bin(raw)
  Ext.value(b, 0, b.bytesize)
end