forked from balacode/go-delta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelta_internal.go
57 lines (54 loc) · 1.55 KB
/
delta_internal.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
// -----------------------------------------------------------------------------
// github.com/balacode/go-delta go-delta/[delta_internal.go]
// (c) [email protected] License: MIT
// -----------------------------------------------------------------------------
package delta
// write appends binary difference data
func (ob *Delta) write(sourceLoc, size int, data []byte) {
if DebugTiming {
tmr.Start("write")
defer tmr.Stop("write")
}
if DebugInfo && DebugWriteArgs {
PL("write",
"sourceLoc:", sourceLoc,
"size:", size,
"data:", data, string(data))
}
// argument validations
switch {
case sourceLoc < -1:
mod.Error("sourceLoc:", sourceLoc, " < -1")
return
case sourceLoc == -1 && len(data) == 0:
mod.Error("sourceLoc == -1 && len(data) == 0")
return
case sourceLoc != -1 && len(data) != 0:
mod.Error("sourceLoc != -1 && len(data):", len(data), "!= 0")
return
case size < 1:
mod.Error("size:", size, " < 1")
return
}
// if the previous part was embedded directly, append to that part's data
if sourceLoc == -1 {
n := len(ob.parts)
if n > 0 {
last := &ob.parts[n-1]
if last.sourceLoc == -1 {
last.size += len(data)
last.data = append(last.data, data...)
return
}
}
}
// append a new part
var ar []byte
if sourceLoc == -1 {
ar = make([]byte, len(data))
copy(ar, data)
}
ob.parts = append(ob.parts,
deltaPart{sourceLoc: sourceLoc, size: size, data: ar})
} // write
// end