summaryrefslogtreecommitdiff
path: root/internal/keepass/format.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-04-18 16:26:33 +0300
committerPaul Buetow <paul@buetow.org>2026-04-18 16:26:33 +0300
commit7292a5db4e96ffeb30697ce3308d47777bf7c2f5 (patch)
tree82df398206ab89487c0d7608068a31c5cbf97b89 /internal/keepass/format.go
parent667bf24a8ac2c2b4c4f9befd7f77b22f0a156a27 (diff)
keepass: fix parseContent doc comment and add importBytes ctx note
- Replace the inaccurate "Go compiler can optimise away" claim in parseContent's doc with an accurate explanation: callers that already hold a string pass it directly, while []byte callers make a single explicit conversion at the call site, keeping the conversion visible rather than hidden inside the function (100 Go Mistakes #40). - Add a comment in importBytes explaining why ctx is not threaded into addAttachment or addTextEntry: both are synchronous, purely in-memory operations with no I/O or blocking calls that could respect cancellation. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'internal/keepass/format.go')
-rw-r--r--internal/keepass/format.go19
1 files changed, 15 insertions, 4 deletions
diff --git a/internal/keepass/format.go b/internal/keepass/format.go
index 98f5af1..bda4826 100644
--- a/internal/keepass/format.go
+++ b/internal/keepass/format.go
@@ -5,6 +5,7 @@
package keepass
import (
+ "bytes"
"regexp"
"strings"
)
@@ -35,8 +36,12 @@ var (
// URL: <value>
// Notes:
// <notes lines>
+//
+// Uses bytes.Buffer rather than strings.Builder to avoid the final
+// []byte(builder.String()) allocation that would otherwise copy the result
+// (100 Go Mistakes #40: unnecessary string/byte conversions).
func formatContent(password, user, url, notes string) []byte {
- var b strings.Builder
+ var b bytes.Buffer
b.WriteString("Password: ")
b.WriteString(password)
b.WriteByte('\n')
@@ -53,7 +58,7 @@ func formatContent(password, user, url, notes string) []byte {
b.WriteByte('\n')
}
}
- return []byte(b.String())
+ return b.Bytes()
}
// parseContent is the inverse of formatContent. It tolerates missing or
@@ -61,10 +66,16 @@ func formatContent(password, user, url, notes string) []byte {
// before a "Notes:" header are matched against the password/user/url patterns;
// everything after "Notes:" is collected verbatim.
//
+// The parameter is a string rather than []byte so that callers which already
+// hold a string (the common case) pass it directly with no conversion. Callers
+// that hold a []byte perform a single explicit string(b) conversion at the call
+// site, making the conversion visible rather than hiding it inside this function
+// (100 Go Mistakes #40).
+//
// This generalises extractPasswordFromContent from internal/cli/migrate_kdbx.go
// to also handle User: and URL: fields.
-func parseContent(content []byte) (password, user, url, notes string) {
- lines := strings.Split(string(content), "\n")
+func parseContent(content string) (password, user, url, notes string) {
+ lines := strings.Split(content, "\n")
inNotes := false
var notesLines []string