-
Notifications
You must be signed in to change notification settings - Fork 3
/
encoding.go
69 lines (53 loc) · 1.17 KB
/
encoding.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
62
63
64
65
66
67
68
69
package gohl7
import (
"errors"
)
var (
ErrBadEncoding = errors.New("Invalid Encoding")
ErrRepeatedEncoding = errors.New("Invalid Encoding, repeated chars")
)
type Encoding struct {
Field byte
Component byte
Repeated byte
Escaping byte
Subcomponent byte
}
func ParseEncoding(buffer []byte) (*Encoding, error) {
l := len(buffer)
if l != 5 {
return nil, ErrBadEncoding
}
e := &Encoding{
Field: buffer[0],
Component: buffer[1],
Repeated: buffer[2],
Escaping: buffer[3],
Subcomponent: buffer[4],
}
//checking for duplicates
bag := make(map[byte]bool)
for _, v := range buffer {
_, ok := bag[v]
if ok {
return nil, ErrRepeatedEncoding
}
bag[v] = true
}
return e, nil
}
// Function Clean removes all unescape/clean the given input
// the process is done in-place no buffer is made
func (enc *Encoding) Clean(input []byte) []byte {
l, j := len(input), 0
for i := 0; i < l; j++ {
if input[i] == enc.Escaping {
i++
//not checking for out of bounds because this will imply a wrongly formatted
//field, which should have be cought be the parser
}
input[j] = input[i]
i++
}
return input[:j]
}