-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfields.go
45 lines (40 loc) · 1.13 KB
/
fields.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package hl7
// Fields is a slice of fields
type Fields []Field
// GetField is used to get the field at the given index.
func (f Fields) GetField(idx int) (Field, bool) {
if idx >= len(f) {
return nil, false
}
return f[idx], true
}
// GetComponent is used to get the component at the given index.
func (f Fields) GetComponent(fieldIdx, compIdx int) (Component, bool) {
if field, ok := f.GetField(fieldIdx); ok {
return field.GetComponent(compIdx)
}
return nil, false
}
// GetSubComponent is used to get the component at the given index.
func (f Fields) GetSubComponent(fieldIdx, compIdx, subCompIdx int) (SubComponent, bool) {
if field, ok := f.GetField(fieldIdx); ok {
return field.GetSubComponent(compIdx, subCompIdx)
}
return nil, false
}
func newFields(compSep, subCompSep, repeat, escape byte, data []byte) Fields {
var (
fields Fields
start int
)
for i := range data {
if data[i] == repeat {
fields = append(fields, newField(compSep, subCompSep, escape, data[start:i]))
start = i + 1
}
if i == len(data)-1 {
fields = append(fields, newField(compSep, subCompSep, escape, data[start:]))
}
}
return fields
}