-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit.go
More file actions
100 lines (90 loc) · 2.77 KB
/
Copy pathedit.go
File metadata and controls
100 lines (90 loc) · 2.77 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Package editpro provides byte-exact text editing primitives and safe file helpers.
package editpro
import (
"bytes"
"errors"
"fmt"
)
var (
ErrEmptyOld = errors.New("old text must not be empty")
ErrNoMatch = errors.New("old text was not found")
ErrInvalidLimit = errors.New("replacement limit must not be negative")
ErrInvalidExpect = errors.New("expected match count must not be negative")
ErrResultTooLarge = errors.New("replacement result is too large")
)
// ReplaceOptions controls a byte-exact, non-overlapping replacement.
// Limit == 0 replaces every match. ExpectedMatches == 0 disables the check.
type ReplaceOptions struct {
Limit int
ExpectedMatches int
RequireMatch bool
}
// ReplaceResult describes a replacement without performing file I/O.
type ReplaceResult struct {
Data []byte
Matches int
Replaced int
Changed bool
}
// Replace replaces old with replacement using byte-exact, non-overlapping matches.
// When no change is needed, Data aliases src.
func Replace(src, old, replacement []byte, opts ReplaceOptions) (ReplaceResult, error) {
if len(old) == 0 {
return ReplaceResult{}, ErrEmptyOld
}
if opts.Limit < 0 {
return ReplaceResult{}, ErrInvalidLimit
}
if opts.ExpectedMatches < 0 {
return ReplaceResult{}, ErrInvalidExpect
}
matches := bytes.Count(src, old)
if opts.ExpectedMatches > 0 && matches != opts.ExpectedMatches {
return ReplaceResult{}, fmt.Errorf("expected %d matches, found %d", opts.ExpectedMatches, matches)
}
if opts.RequireMatch && matches == 0 {
return ReplaceResult{}, ErrNoMatch
}
replaced := matches
if opts.Limit > 0 && replaced > opts.Limit {
replaced = opts.Limit
}
if replaced == 0 || bytes.Equal(old, replacement) {
return ReplaceResult{Data: src, Matches: matches, Replaced: replaced}, nil
}
data, err := replaceKnownCount(src, old, replacement, replaced)
if err != nil {
return ReplaceResult{}, err
}
return ReplaceResult{
Data: data,
Matches: matches,
Replaced: replaced,
Changed: true,
}, nil
}
// replaceKnownCount avoids the second Count performed by bytes.Replace. The
// caller has already validated that count non-overlapping matches exist.
func replaceKnownCount(src, old, replacement []byte, count int) ([]byte, error) {
delta := len(replacement) - len(old)
resultLen := len(src)
if delta > 0 {
maxInt := int(^uint(0) >> 1)
if count > (maxInt-resultLen)/delta {
return nil, ErrResultTooLarge
}
resultLen += count * delta
} else {
resultLen += count * delta
}
result := make([]byte, 0, resultLen)
offset := 0
for range count {
i := bytes.Index(src[offset:], old)
result = append(result, src[offset:offset+i]...)
result = append(result, replacement...)
offset += i + len(old)
}
result = append(result, src[offset:]...)
return result, nil
}