~samwhited/xmpp

e1ad0a876c00eb0e2711f48796ff123878b2849f — Sam Whited 5 years ago 381584d
jid: Add a `MustParse' function
1 files changed, 16 insertions(+), 0 deletions(-)

M jid/jid.go
M jid/jid.go => jid/jid.go +16 -0
@@ 8,6 8,7 @@ import (
	"encoding/xml"
	"errors"
	"net"
	"strconv"
	"strings"
	"unicode/utf8"



@@ 35,6 36,21 @@ func ParseString(s string) (*JID, error) {
	return New(localpart, domainpart, resourcepart)
}

// MustParse is like ParseString but panics if the JID cannot be parsed.
// It simplifies safe initialization of JIDs from known-good constant strings.
func MustParse(s string) *JID {
	j, err := ParseString(s)
	if err != nil {
		if strconv.CanBackquote(s) {
			s = "`" + s + "`"
		} else {
			s = strconv.Quote(s)
		}
		panic(`jid: Parse(` + s + `): ` + err.Error())
	}
	return j
}

// New constructs a new JID from the given localpart, domainpart, and
// resourcepart.
func New(localpart, domainpart, resourcepart string) (*JID, error) {