-
Notifications
You must be signed in to change notification settings - Fork 2
/
name_addr.go
50 lines (42 loc) · 968 Bytes
/
name_addr.go
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"bytes"
"errors"
"fmt"
"io"
"strings"
)
type NameAddr struct {
DisplayName string
Addr *AddrSpec
}
func ParseNameAddr(nameAddr string) (*NameAddr, error) {
pos1 := strings.IndexByte(nameAddr, '<')
pos2 := strings.IndexByte(nameAddr, '>')
if pos1 == -1 || pos2 == -1 || pos2 < pos1 {
return nil, errors.New("malformatted name-addr")
}
addr, err := ParseAddrSpec(nameAddr[pos1+1 : pos2])
if err != nil {
return nil, err
}
return &NameAddr{DisplayName: nameAddr[0:pos1], Addr: addr}, nil
}
func (na *NameAddr) GetAddress() *AddrSpec {
return na.Addr
}
func (na *NameAddr) Write(writer io.Writer) (int, error) {
n, _ := fmt.Fprintf(writer, na.DisplayName)
m, _ := fmt.Fprintf(writer, "<")
n += m
m, _ = na.Addr.Write(writer)
n += m
m, err := fmt.Fprintf(writer, ">")
n += m
return n, err
}
func (na *NameAddr) String() string {
buf := bytes.NewBuffer(make([]byte, 0))
na.Write(buf)
return buf.String()
}