-
Notifications
You must be signed in to change notification settings - Fork 8
/
input_spliter.go
63 lines (56 loc) · 972 Bytes
/
input_spliter.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
package gtree
import (
"bufio"
"context"
"fmt"
"io"
md "github.com/ddddddO/gtree/markdown"
)
func split(ctx context.Context, r io.Reader) (<-chan string, <-chan error) {
sc := bufio.NewScanner(r)
blockc := make(chan string)
errc := make(chan error)
go func() {
defer func() {
close(blockc)
close(errc)
}()
block := ""
for sc.Scan() {
select {
case <-ctx.Done():
return
default:
l := sc.Text()
if isRootBlockBeginning(l) {
if len(block) != 0 {
select {
case <-ctx.Done():
return
case blockc <- block:
}
}
block = ""
}
block += fmt.Sprintln(l)
}
}
if err := sc.Err(); err != nil {
errc <- err
return
}
select {
case <-ctx.Done():
return
case blockc <- block: // 最後のRootブロック送出
return
}
}()
return blockc, errc
}
func isRootBlockBeginning(l string) bool {
if len(l) == 0 {
return false
}
return md.IsSymbol(l[0:1])
}