alps/plugins/base/strconv.go

70 lines
1.3 KiB
Go
Raw Normal View History

2020-05-13 12:07:44 +00:00
package alpsbase
2019-12-03 10:54:43 +00:00
import (
"fmt"
"net/url"
2019-12-03 10:54:43 +00:00
"strconv"
"strings"
)
func parseUid(s string) (uint32, error) {
uid, err := strconv.ParseUint(s, 10, 32)
if err != nil {
2019-12-03 17:41:23 +00:00
return 0, fmt.Errorf("invalid UID: %v", err)
2019-12-03 10:54:43 +00:00
}
if uid == 0 {
return 0, fmt.Errorf("UID must be non-zero")
}
return uint32(uid), nil
}
2019-12-03 17:41:23 +00:00
func parseMboxAndUid(mboxString, uidString string) (string, uint32, error) {
mboxName, err := url.PathUnescape(mboxString)
if err != nil {
return "", 0, fmt.Errorf("invalid mailbox name: %v", err)
}
uid, err := parseUid(uidString)
return mboxName, uid, err
}
func parseUidList(values []string) ([]uint32, error) {
var uids []uint32
for _, v := range values {
uid, err := parseUid(v)
if err != nil {
return nil, err
}
uids = append(uids, uid)
}
return uids, nil
}
2019-12-03 10:54:43 +00:00
func parsePartPath(s string) ([]int, error) {
2019-12-03 12:07:25 +00:00
if s == "" {
return nil, nil
}
2019-12-03 10:54:43 +00:00
l := strings.Split(s, ".")
path := make([]int, len(l))
for i, s := range l {
var err error
path[i], err = strconv.Atoi(s)
if err != nil {
return nil, err
}
if path[i] <= 0 {
return nil, fmt.Errorf("part num must be strictly positive")
}
}
return path, nil
}
func parseAddressList(s string) []string {
l := strings.Split(s, ",")
for i, addr := range l {
l[i] = strings.TrimSpace(addr)
}
return l
}