refac: atomic merge, lost-update race, RunPurge cost, duplicated walks
This commit is contained in:
1 parent
0fc3511e2b
commit
074944c99a
45 files changed
+2204
-267
No files matched your search
@@ -246,3 +246,52 @@ type Image struct {
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
func isStoredNameByte(char byte) bool {
|
||||
return char == '-' || char == '_' || char == '.' || char >= '0' && char <= '9' || char >= 'a' && char <= 'z' || char >= 'A' && char <= 'Z'
|
||||
}
|
||||
|
||||
func continuesName(body string, end int) bool {
|
||||
if end >= len(body) || !isStoredNameByte(body[end]) {
|
||||
return false
|
||||
}
|
||||
return body[end] != '.' || end+1 < len(body) && isStoredNameByte(body[end+1])
|
||||
}
|
||||
|
||||
func storedNameMatches(body string, name string) []int {
|
||||
var starts []int
|
||||
if name == "" {
|
||||
return starts
|
||||
}
|
||||
for from := 0; from < len(body); {
|
||||
found := strings.Index(body[from:], name)
|
||||
if found < 0 {
|
||||
break
|
||||
}
|
||||
start := from + found
|
||||
end := start + len(name)
|
||||
from = start + 1
|
||||
if start > 0 && isStoredNameByte(body[start-1]) || continuesName(body, end) {
|
||||
continue
|
||||
}
|
||||
starts = append(starts, start)
|
||||
from = end
|
||||
}
|
||||
return starts
|
||||
}
|
||||
|
||||
func ContainsStoredName(body string, name string) bool {
|
||||
return len(storedNameMatches(body, name)) > 0
|
||||
}
|
||||
|
||||
func ReplaceStoredName(body string, from string, to string) string {
|
||||
var rewritten strings.Builder
|
||||
last := 0
|
||||
for _, start := range storedNameMatches(body, from) {
|
||||
rewritten.WriteString(body[last:start])
|
||||
rewritten.WriteString(to)
|
||||
last = start + len(from)
|
||||
}
|
||||
rewritten.WriteString(body[last:])
|
||||
return rewritten.String()
|
||||
}
|
||||
Reference in new issue
Block a user