-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
segment.go
61 lines (54 loc) · 1.64 KB
/
segment.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package hl7
// Segment is a slice of fields.
type Segment []Fields
// Type is used to return the type of segment this is.
func (s Segment) Type() string {
if subComp, ok := s.GetSubComponent(0, 0, 0, 0); ok {
return subComp.String()
}
return ""
}
// GetFields is used to get the fields at the given index
func (s Segment) GetFields(idx int) (Fields, bool) {
if idx >= len(s) {
return nil, false
}
return s[idx], true
}
// GetField is used to get the field at the given index
func (s Segment) GetField(fieldsIdx, fieldIdx int) (Field, bool) {
if fields, ok := s.GetFields(fieldsIdx); ok {
return fields.GetField(fieldIdx)
}
return nil, false
}
// GetComponent is used to get the component at the given index
func (s Segment) GetComponent(fieldsIdx, fieldIdx, compIdx int) (Component, bool) {
if fields, ok := s.GetFields(fieldsIdx); ok {
return fields.GetComponent(fieldIdx, compIdx)
}
return nil, false
}
// GetSubComponent is used to get the sub-component at the given index
func (s Segment) GetSubComponent(fieldsIdx, fieldIdx, compIdx, subCompIdx int) (SubComponent, bool) {
if fields, ok := s.GetFields(fieldsIdx); ok {
return fields.GetSubComponent(fieldIdx, compIdx, subCompIdx)
}
return nil, false
}
func newSegment(fieldSep, compSep, subCompSep, repeat, escape byte, data []byte) Segment {
var (
segment Segment
start int
)
for i := range data {
if data[i] == fieldSep {
segment = append(segment, newFields(compSep, subCompSep, repeat, escape, data[start:i]))
start = i + 1
}
if i == len(data)-1 {
segment = append(segment, newFields(compSep, subCompSep, repeat, escape, data[start:]))
}
}
return segment
}