package pop3 import ( "bytes" "fmt" "strconv" ) const ( // POP3 uses CRLF-terminated lines CRLF = "\r\n" // POP3's multi-line responses are byte-stuffed and dot-terminated DOT = '.' // POP3 does not use tabs SP = ' ' ) // [2023-09-14, jfrech] XXX Maybe be more lenient and allow only "\n"? func Lines(resp []byte) ([]string, error) { buf := bytes.NewBuffer(resp) var lines []string for buf.Len() > 0 { k := bytes.Index(buf.Bytes(), []byte(CRLF)) if k == -1 { return nil, fmt.Errorf("%w: expected CRLF", ErrSyntax) } lines = append(lines, string(buf.Next(k))) // discard CRLF buf.Next(len(CRLF)) } return lines, nil } func IsASCIIWord(raw string) bool { for _, c := range []byte(raw) { if c <= ' ' || c > '~' { return false } } return true } func ParseNonNegativeInt(raw string) (int, bool) { n64, err := strconv.ParseInt(raw, 10, 64) if err != nil { return 0, false } if n64 < 0 || int64(int(n64)) != n64 { return 0, false } return int(n64), true } func ParsePositiveInt(raw string) (int, bool) { n, ok := ParseNonNegativeInt(raw) if !ok && n <= 0 { return 0, false } return n, true }