Skip to content

Commit 4ade5cd

Browse files
committed
Make calcHash() a method of SharedBuffer
This will make it easier to use calcHash() in other SharedBuffer methods.
1 parent c9f84cd commit 4ade5cd

File tree

3 files changed

+25
-25
lines changed

3 files changed

+25
-25
lines changed

internal/buffer/buffer.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,26 @@ func (b *SharedBuffer) remove(start, end Loc) []byte {
140140
return b.LineArray.remove(start, end)
141141
}
142142

143+
// calcHash calculates md5 hash of all lines in the buffer
144+
func (b *SharedBuffer) calcHash(out *[md5.Size]byte) {
145+
h := md5.New()
146+
147+
if len(b.lines) > 0 {
148+
h.Write(b.lines[0].data)
149+
150+
for _, l := range b.lines[1:] {
151+
if b.Endings == FFDos {
152+
h.Write([]byte{'\r', '\n'})
153+
} else {
154+
h.Write([]byte{'\n'})
155+
}
156+
h.Write(l.data)
157+
}
158+
}
159+
160+
h.Sum((*out)[:0])
161+
}
162+
143163
// MarkModified marks the buffer as modified for this frame
144164
// and performs rehighlighting if syntax highlighting is enabled
145165
func (b *SharedBuffer) MarkModified(start, end int) {
@@ -416,7 +436,7 @@ func NewBuffer(r io.Reader, size int64, path string, startcursor Loc, btype BufT
416436
} else if !hasBackup {
417437
// since applying a backup does not save the applied backup to disk, we should
418438
// not calculate the original hash based on the backup data
419-
calcHash(b, &b.origHash)
439+
b.calcHash(&b.origHash)
420440
}
421441
}
422442

@@ -558,7 +578,7 @@ func (b *Buffer) ReOpen() error {
558578
if len(data) > LargeFileThreshold {
559579
b.Settings["fastdirty"] = true
560580
} else {
561-
calcHash(b, &b.origHash)
581+
b.calcHash(&b.origHash)
562582
}
563583
}
564584
b.isModified = false
@@ -643,7 +663,7 @@ func (b *Buffer) Modified() bool {
643663

644664
var buff [md5.Size]byte
645665

646-
calcHash(b, &buff)
666+
b.calcHash(&buff)
647667
return buff != b.origHash
648668
}
649669

@@ -663,26 +683,6 @@ func (b *Buffer) Size() int {
663683
return nb
664684
}
665685

666-
// calcHash calculates md5 hash of all lines in the buffer
667-
func calcHash(b *Buffer, out *[md5.Size]byte) {
668-
h := md5.New()
669-
670-
if len(b.lines) > 0 {
671-
h.Write(b.lines[0].data)
672-
673-
for _, l := range b.lines[1:] {
674-
if b.Endings == FFDos {
675-
h.Write([]byte{'\r', '\n'})
676-
} else {
677-
h.Write([]byte{'\n'})
678-
}
679-
h.Write(l.data)
680-
}
681-
}
682-
683-
h.Sum((*out)[:0])
684-
}
685-
686686
func parseDefFromFile(f config.RuntimeFile, header *highlight.Header) *highlight.Def {
687687
data, err := f.Data()
688688
if err != nil {

internal/buffer/save.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ func (b *Buffer) saveToFile(filename string, withSudo bool, autoSave bool) error
318318
// For large files 'fastdirty' needs to be on
319319
b.Settings["fastdirty"] = true
320320
} else {
321-
calcHash(b, &b.origHash)
321+
b.calcHash(&b.origHash)
322322
}
323323
}
324324

internal/buffer/settings.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func (b *Buffer) DoSetOptionNative(option string, nativeValue interface{}) {
7373
b.Settings["fastdirty"] = true
7474
} else {
7575
if !b.isModified {
76-
calcHash(b, &b.origHash)
76+
b.calcHash(&b.origHash)
7777
} else {
7878
// prevent using an old stale origHash value
7979
b.origHash = [md5.Size]byte{}

0 commit comments

Comments
 (0)