-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
67 lines (59 loc) · 1.6 KB
/
process.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
package tomlseq
import (
"bytes"
"fmt"
"strconv"
)
// Process iterates over each line starting with "[[" (the beginning of a toml table) and writes the current iteration
// index to the next line starting with configured identifier. This method also attempts to ignore comments and multiline
// strings, focusing on tables only.
func Process(identifier string, data []byte) []byte {
return process(identifier, data)
}
func process(id string, bs []byte) []byte {
bs = bytes.Replace(bs, []byte("\r\n"), []byte("\n"), -1)
var rb []byte
var tableCount int
var mlsTracker bool
var commentTracker bool
for i, b := range bs {
if i < 4 {
rb = append(rb, b)
continue
}
// if starting or ending a multiline string, set mlsTracker
if b == '"' {
if bs[i-1] == '"' && bs[i-2] == '"' {
mlsTracker = !mlsTracker
}
rb = append(rb, b)
continue
}
// if within a multiline string or a line comment, continue without logic
if mlsTracker || (commentTracker && b != '\n') {
rb = append(rb, b)
continue
}
// flip commentTracker to false if line comment is ending
if b == '\n' && commentTracker {
commentTracker = false
rb = append(rb, b)
continue
}
// flip commentTracker to true if a line comment is starting
if b == '#' && bs[i-1] == '\n' {
commentTracker = true
rb = append(rb, b)
continue
}
// Logic for building sequence ids
if bs[i-1] == '\n' && bs[i-2] == ']' && bs[i-3] == ']' {
rb = append(rb, []byte(fmt.Sprintf("%s = "+strconv.Itoa(tableCount)+"\n", id))...)
rb = append(rb, b)
tableCount++
continue
}
rb = append(rb, b)
}
return rb
}