-
Notifications
You must be signed in to change notification settings - Fork 1
/
split.go
53 lines (44 loc) · 1.11 KB
/
split.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
package stl
import (
"bytes"
"fmt"
"math"
)
func splitTrianglesASCII(data []byte, atEOF bool) (advance int, token []byte, err error) {
// End of input
if atEOF && len(data) == 0 {
return 0, nil, nil
}
// Find the data before the 7th newline
for n := 0; n < 7; n++ {
idx := bytes.IndexByte(data[advance+1:], '\n')
if idx < 0 {
// Invalid input
if atEOF && (len(data) < 8 || string(data[:8]) != "endsolid") {
return 0, nil, fmt.Errorf("invalid input data")
}
// Request more data
return 0, nil, nil
}
advance += idx + 1
}
// Made it to the end of a token
return advance + 1, data[:advance], nil
}
func splitTrianglesBinary(data []byte, atEOF bool) (advance int, token []byte, err error) {
const chunkSize = 50000
// Return the next chunk, or ask for more data
if len(data) >= chunkSize {
return chunkSize, data[:chunkSize], nil
}
// Invalid data
if atEOF && math.Mod(float64(len(data)), 50) != 0 {
return 0, nil, fmt.Errorf("invalid input data")
}
// Last chunk of data
if atEOF && len(data) > 0 {
return len(data), data, nil
}
// Request more data
return 0, nil, nil
}