-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit.go
More file actions
85 lines (74 loc) · 1.6 KB
/
split.go
File metadata and controls
85 lines (74 loc) · 1.6 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"io"
"os"
"github.com/finkf/lev"
"github.com/spf13/cobra"
)
var (
splitCmd = cobra.Command{
Use: "split",
Short: `Split blocks into tokens`,
RunE: runSplit,
Args: cobra.ExactArgs(0),
Long: `Split blocks of aligned lines at a set of characters
and return according sub blocks. Can be used
to split e.g. blocks of aligned lines into blocks
of aligned words.`,
}
splitCharSet string
)
const (
defaultSplitCharSet = "\t "
)
func init() {
splitCmd.Flags().StringVarP(&splitCharSet, "chars", "c",
defaultSplitCharSet, "set the character set used to split blocks")
}
func runSplit(cmd *cobra.Command, args []string) error {
return split(os.Stdin, os.Stdout)
}
func split(stdin io.Reader, stdout io.Writer) error {
return readBlocks(stdin, func(b block) error {
return splitBlocks(b, stdout)
})
}
func splitBlocks(b block, stdout io.Writer) error {
i := 0
for j := indexAny(b.a.S1[i:], splitCharSet); j > 0; {
if err := splitBlock(b, i, j).write(stdout); err != nil {
return err
}
i, j = nextSplitBlock(b, splitCharSet, j)
}
return splitBlock(b, i, len(b.a.S1)).write(stdout)
}
func indexAny(rs []rune, set string) int {
for i, r := range rs {
for _, c := range set {
if r == c {
return i
}
}
}
return -1
}
func splitBlock(b block, i, j int) block {
return block{
p1: b.p1,
p2: b.p2,
a: lev.Alignment{
S1: b.a.S1[i:j],
S2: b.a.S2[i:j],
Trace: b.a.Trace[i:j],
},
}
}
func nextSplitBlock(b block, set string, j int) (int, int) {
i := j + 1
j = indexAny(b.a.S1[i:], splitCharSet)
if j == -1 {
return i, j
}
return i, j + i
}