Class: C0::Body

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/c0/body.rb

Overview

Zero-copy navigator for a document body: the bytes between a heading and EOT, or the inside of a nested level. Everything it returns is a shared byteslice of the original buffer.

Parse is validate: Body.new(bytes) applies the whole grammar to the bytes and raises a C0::SyntaxError on malformed input. The navigators then report the result model: a dead element (␇ before it) is dropped with everything it holds. SUB makes the element after it a child of the element before, at that code's grain (DESIGN.md "Subordinates"): ␚␝s is a subsection one deeper than the group before it, and ␞a␟b␚␟c is two units with c a sub-unit of b, not a unit of the record; a record's sub-units are reached through Record#unit.

Records before the first GS of a file form an unnamed group; records and groups before the first FS form the unnamed leading file. Enumerates the groups of the first file.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buf, start = 0, stop = nil, validate: true) ⇒ Body

Parse buf (or its [start, stop) range) as a body: no SYN, heading or EOT. Pass validate: false only for a range already checked as part of a larger parse (the inside of a level, a document's body).



28
29
30
31
32
33
# File 'lib/c0/body.rb', line 28

def initialize(buf, start = 0, stop = nil, validate: true)
  @buf = C0.to_bin(buf)
  @start = start
  @stop = stop || @buf.bytesize
  Ext.body_read(@buf, @start, @stop) if validate
end

Instance Attribute Details

#buf ⇒ Object (readonly)

Returns the value of attribute buf.



23
24
25
# File 'lib/c0/body.rb', line 23

def buf
  @buf
end

#start ⇒ Object (readonly)

Returns the value of attribute start.



23
24
25
# File 'lib/c0/body.rb', line 23

def start
  @start
end

#stop ⇒ Object (readonly)

Returns the value of attribute stop.



23
24
25
# File 'lib/c0/body.rb', line 23

def stop
  @stop
end

Class Method Details

.view(buf, start, stop) ⇒ Object

A body over an already-validated range.



36
# File 'lib/c0/body.rb', line 36

def self.view(buf, start, stop) = new(buf, start, stop, validate: false)

Instance Method Details

#bare? ⇒ Boolean

Whether the body is just records: one unnamed file, at most the unnamed group.

Returns:

  • (Boolean)


80
# File 'lib/c0/body.rb', line 80

def bare? = Ext.body_bare(@buf, @start, @stop)

#each_group ⇒ Object Also known as: each



60
# File 'lib/c0/body.rb', line 60

def each_group(&) = first_file.groups.each(&)

#each_record ⇒ Object



77
# File 'lib/c0/body.rb', line 77

def each_record(&) = records.each(&)

#files ⇒ Object

The files, in order. The unnamed leading file is reported when it has content or no FS follows it.



40
41
42
# File 'lib/c0/body.rb', line 40

def files
  @files ||= Ext.files(@buf, @start, @stop).map { |ns, ne, s, e, leading| FileView.new(@buf, ns, ne, s, e, leading) }
end

#group(index_or_name) ⇒ Object Also known as: []

Group by index or by name (first file). Raises C0::KeyError for a name with no group.



54
55
56
# File 'lib/c0/body.rb', line 54

def group(index_or_name)
  index_or_name.is_a?(Integer) ? first_file.groups[index_or_name] : first_file.group(index_or_name)
end

#group_count ⇒ Object

Groups of the first file.



50
# File 'lib/c0/body.rb', line 50

def group_count = first_file.groups.size

#group_names ⇒ Object



64
# File 'lib/c0/body.rb', line 64

def group_names = first_file.groups.map(&:name)

#groups ⇒ Object

All groups of all files, in order.



45
46
47
# File 'lib/c0/body.rb', line 45

def groups
  @groups ||= Ext.groups(@buf, @start, @stop).map { |d, ns, ne, s, e| Group.new(@buf, d, ns, ne, s, e) }
end

#name ⇒ Object

Name of the first file (empty when the body has no FS).



67
# File 'lib/c0/body.rb', line 67

def name = first_file.name

#raw ⇒ Object

The body's bytes.



83
# File 'lib/c0/body.rb', line 83

def raw = @buf.byteslice(@start, @stop - @start)

#record(index) ⇒ Object



75
# File 'lib/c0/body.rb', line 75

def record(index) = records[index]

#record_count ⇒ Object



73
# File 'lib/c0/body.rb', line 73

def record_count = records.size

#records ⇒ Object

Records of the body when it is a bare record stream (no FS, no GS): the unnamed group's records.



71
# File 'lib/c0/body.rb', line 71

def records = first_file.groups.first&.records || []