-
Notifications
You must be signed in to change notification settings - Fork 5
/
module.go
106 lines (85 loc) · 3.09 KB
/
module.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// -----------------------------------------------------------------------------
// github.com/balacode/go-delta go-delta/[module.go]
// (c) balarabe@protonmail.com License: MIT
// -----------------------------------------------------------------------------
package delta
import (
"bytes"
"errors"
"fmt"
"github.com/balacode/zr"
)
// -----------------------------------------------------------------------------
// # Module Constants / Variables
const (
// MatchLimit specifies the maximum number of positions tracked
// for each unique key in the map of source data. See makeMap().
MatchLimit = 50
// MatchSize specifies the size of unique
// chunks being searched for, in bytes.
MatchSize = 9
)
var (
// PL is fmt.Println() but is used only for debugging.
PL = fmt.Println
// TempBufferSize sets the size of memory buffers for reading files and other
// streams. This memory is not fixed but allocated/released transiently.
TempBufferSize = 32 * 1024 * 1024 // 32 MB
// tmr is used for timing all methods/functions during tuning.
tmr zr.Timer
)
// -----------------------------------------------------------------------------
// # Debugging Flags
var (
// DebugInfo when set, causes printing of messages helpful for debugging.
DebugInfo = false
// DebugTiming controls timing (benchmarking) of time spent in each function.
DebugTiming = true
// DebugWriteArgs when set, prints the arguments passed to write()
DebugWriteArgs = false
)
// -----------------------------------------------------------------------------
// # Error Handler
// SetErrorFunc changes the error-handling function, so that
// all errors in this package will be sent to this handler,
// which is useful for custom logging and mocking during unit tests.
// To restore the default error handler use SetErrorFunc(nil).
func SetErrorFunc(fn func(args ...interface{}) error) {
if fn == nil {
mod.Error = defaultErrorFunc
return
}
mod.Error = fn
} // SetErrorFunc
// defaultErrorFunc is the default error
// handling function assigned to mod.Error
func defaultErrorFunc(args ...interface{}) error {
//
// write all args to a message string (add spaces between args)
var buf bytes.Buffer
for i, arg := range args {
if i > 0 {
buf.WriteString(" ")
}
buf.WriteString(fmt.Sprint(arg))
}
msg := buf.String()
//
// if DebugInfo is on, print the message to the console
if DebugInfo {
fmt.Println("ERROR:\n", msg)
}
// return error based on message
return errors.New(msg)
} // defaultErrorFunc
// -----------------------------------------------------------------------------
// # Module Global
// mod variable though wich mockable functions are called
var mod = thisMod{Error: defaultErrorFunc}
// thisMod specifies mockable functions
type thisMod struct {
Error func(args ...interface{}) error
}
// ModReset restores all mocked functions to the original standard functions.
func (ob *thisMod) Reset() { ob.Error = defaultErrorFunc }
// end