Skip to content

Commit

Permalink
textproto: add Header.Copy
Browse files Browse the repository at this point in the history
  • Loading branch information
emersion committed May 14, 2019
1 parent 547c922 commit 34d549a
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions textproto/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ type Header struct {
m map[string][]*headerField
}

func makeHeaderMap(fs []headerField) map[string][]*headerField {
if len(fs) == 0 {
return nil
}

m := make(map[string][]*headerField)
for i, f := range fs {
m[f.k] = append(m[f.k], &fs[i])
}
return m
}

func newHeader(fs []headerField) Header {
// Reverse order
for i := len(fs)/2 - 1; i >= 0; i-- {
Expand All @@ -44,13 +56,7 @@ func newHeader(fs []headerField) Header {
}

// Populate map
var m map[string][]*headerField
if len(fs) > 0 {
m = make(map[string][]*headerField)
for i, f := range fs {
m[f.k] = append(m[f.k], &fs[i])
}
}
m := makeHeaderMap(fs)

return Header{l: fs, m: m}
}
Expand Down Expand Up @@ -106,6 +112,14 @@ func (h *Header) Has(k string) bool {
return ok
}

// Copy creates an independent copy of the header.
func (h *Header) Copy() Header {
l := make([]headerField, len(h.l))
copy(l, h.l)
m := makeHeaderMap(l)
return Header{l: l, m: m}
}

// HeaderFields iterates over header fields. Its cursor starts before the first
// field of the header. Use Next to advance from field to field.
type HeaderFields interface {
Expand Down

0 comments on commit 34d549a

Please sign in to comment.