Class: C0::Record

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

Overview

A record: zero-copy field access over the underlying buffer.

Instance Method Summary collapse

Constructor Details

#initialize(buf, start, finish) ⇒ Record

Returns a new instance of Record.



89
90
91
92
93
# File 'lib/c0.rb', line 89

def initialize(buf, start, finish)
  @buf = buf
  @start = start
  @end = finish
end

Instance Method Details

#each ⇒ Object



118
119
120
121
122
# File 'lib/c0.rb', line 118

def each
  return enum_for(:each) unless block_given?

  spans.each { |s, e| yield @buf.byteslice(s, e - s) }
end

#field(index) ⇒ Object



99
100
101
102
# File 'lib/c0.rb', line 99

def field(index)
  s, e = spans[index]
  @buf.byteslice(s, e - s)
end

#fields ⇒ Object



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

def fields = spans.map { |s, e| @buf.byteslice(s, e - s) }

#list(index) ⇒ Object

Field at index read as a list (STX items-separated-by-US ETX), each item unescaped. A plain field yields one item; an empty scope yields [].



111
112
113
114
# File 'lib/c0.rb', line 111

def list(index)
  s, e = spans[index]
  Ext.field_items(@buf, s, e).map { |is, ie| C0.unescape(@buf.byteslice(is, ie - is)) }
end

#raw ⇒ Object



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

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

#size ⇒ Object



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

def size = spans.size

#value(index) ⇒ Object

Field at index with DLE escapes decoded.



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

def value(index) = C0.unescape(field(index))

#values ⇒ Object



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

def values = spans.map { |s, e| C0.unescape(@buf.byteslice(s, e - s)) }