Skip to content

Commit c59e0f6

Browse files
committed
feat(string): add Clone — detach string from backing memory
Adds core.Clone, the canonical wrapper for strings.Clone. Used when a string aliases a reusable scratch buffer (e.g. via core.AsString over a re-sliceable byte buffer) and the resulting string must outlive the buffer's next reuse — map keys, struct fields, channel sends across goroutines. Driven by the gguf metadata loop in go-inference: the parser reads keys into a reusable byte buffer and only stores the ~7 keys ReadGGUFInfo queries. Without Clone, mapping a zero-copy view into a map[string]any would risk later mutation aliasing the key. Co-Authored-By: Virgil <virgil@lethean.io>
1 parent ad23494 commit c59e0f6

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

string.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ func TrimSuffix(s, suffix string) string {
4040
return strings.TrimSuffix(s, suffix)
4141
}
4242

43+
// Clone returns a fresh copy of s, detached from its backing memory.
44+
// Use this when s aliases a reusable buffer (e.g. via core.AsString
45+
// over a scratch slice) and the result must outlive the buffer's
46+
// next reuse — map keys, struct fields, channel sends.
47+
//
48+
// key := core.Clone(core.AsString(scratch)) // map[key] = ... is safe
49+
func Clone(s string) string {
50+
return strings.Clone(s)
51+
}
52+
4353
// Contains returns true if s contains substr.
4454
//
4555
// core.Contains("hello world", "world") // true

0 commit comments

Comments
 (0)