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 |
package doc
import (
"crypto/sha256"
"encoding/hex"
"strings"
"golang.org/x/text/unicode/norm"
)
// Normalize returns the text Patchwork stores: no BOM, LF line endings, NFC.
// Editors and upload tools change these freely, and none of them is a
// revision.
func Normalize(raw []byte) string {
s := string(raw)
s = strings.TrimPrefix(s, "\xef\xbb\xbf")
s = strings.ReplaceAll(s, "\r\n", "\n")
s = strings.ReplaceAll(s, "\r", "\n")
return norm.NFC.String(s)
}
// hashText hashes text with trailing whitespace removed from every line and
// trailing blank lines dropped, so re-saving a file never looks like an edit.
func hashText(s string) string {
lines := strings.Split(s, "\n")
for i, l := range lines {
lines[i] = strings.TrimRight(l, " \t")
}
sum := sha256.Sum256([]byte(strings.TrimRight(strings.Join(lines, "\n"), "\n")))
return hex.EncodeToString(sum[:])
}
|