<commit_before>package xco

import (
	"bytes"
	"encoding/xml"
	"errors"
	"fmt"
	"strings"
)

// Address is an XMPP JID address
type Address struct {
	LocalPart    string
	DomainPart   string
	ResourcePart string
}

// ParseAddress parses the address from the given string
func ParseAddress(s string) (Address, error) {
	var addr Address
	err := addr.parse(s)
	return addr, err
}

// Equals compares the given address
func (a *Address) Equals(o *Address) bool {
	return (a == o) || ((a != nil && o != nil) && (a.LocalPart == o.LocalPart && a.DomainPart == o.DomainPart && a.ResourcePart == o.ResourcePart))
}

// String formats the address as an XMPP JID
func (a *Address) String() string {
	buf := bytes.NewBufferString("")
	if a.LocalPart != "" {
		buf.WriteString(a.LocalPart)
		buf.WriteString("@")
	}

	buf.WriteString(a.DomainPart)

	if a.ResourcePart != "" {
		buf.WriteString("/")
		buf.WriteString(a.ResourcePart)
	}

	return buf.String()
}

// UnmarshalXMLAttr marks the Address struct as being able to be parsed as an XML attribute
func (a *Address) UnmarshalXMLAttr(attr xml.Attr) error {
	return a.parse(attr.Value)
}

// MarshalXMLAttr marks the Address struct as being able to be written as an XML attribute
func (a *Address) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
	errs := a.validate()
	if len(errs) != 0 {
		return xml.Attr{}, fmt.Errorf("Malformed Address for Attribute %s: %s", name, errs)
	}

	return xml.Attr{
		Name:  name,
		Value: a.String(),
	}, nil
}

func (a *Address) validate() []error {

	var errs []error

	if a.DomainPart == "" {
		errs = append(errs, errors.New("Domain is empty"))
	}

	return errs
}

func (a *Address) parse(s string) error {

	// normalization

	s = strings.TrimSpace(s)

	if len(s) == 0 {
		return errors.New("Address is empty")
	}

	// parsing

	domainStart := 0
	domainEnd := len(s)

	if idx := strings.IndexAny(s, "@"); idx != -1 {
		a.LocalPart = s[0:idx]
		domainStart = idx + 1
	}

	if idx := strings.IndexAny(s, "/"); idx != -1 {
		a.ResourcePart = s[idx+1 : len(s)]
		domainEnd = idx
	}

	if domainStart != domainEnd {
		a.DomainPart = s[domainStart:domainEnd]
	}

	// validation

	errs := a.validate()

	if a.LocalPart == "" && domainStart != 0 {
		errs = append(errs, errors.New("Localpart is empty"))
	}

	if a.ResourcePart == "" && domainEnd != len(s) {
		errs = append(errs, errors.New("Resourcepart is empty"))
	}
<commit_msg>Remove unnecessary second section of slice in address parsing<commit_after>package xco

import (
	"bytes"
	"encoding/xml"
	"errors"
	"fmt"
	"strings"
)

// Address is an XMPP JID address
type Address struct {
	LocalPart    string
	DomainPart   string
	ResourcePart string
}

// ParseAddress parses the address from the given string
func ParseAddress(s string) (Address, error) {
	var addr Address
	err := addr.parse(s)
	return addr, err
}

// Equals compares the given address
func (a *Address) Equals(o *Address) bool {
	return (a == o) || ((a != nil && o != nil) && (a.LocalPart == o.LocalPart && a.DomainPart == o.DomainPart && a.ResourcePart == o.ResourcePart))
}

// String formats the address as an XMPP JID
func (a *Address) String() string {
	buf := bytes.NewBufferString("")
	if a.LocalPart != "" {
		buf.WriteString(a.LocalPart)
		buf.WriteString("@")
	}

	buf.WriteString(a.DomainPart)

	if a.ResourcePart != "" {
		buf.WriteString("/")
		buf.WriteString(a.ResourcePart)
	}

	return buf.String()
}

// UnmarshalXMLAttr marks the Address struct as being able to be parsed as an XML attribute
func (a *Address) UnmarshalXMLAttr(attr xml.Attr) error {
	return a.parse(attr.Value)
}

// MarshalXMLAttr marks the Address struct as being able to be written as an XML attribute
func (a *Address) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
	errs := a.validate()
	if len(errs) != 0 {
		return xml.Attr{}, fmt.Errorf("Malformed Address for Attribute %s: %s", name, errs)
	}

	return xml.Attr{
		Name:  name,
		Value: a.String(),
	}, nil
}

func (a *Address) validate() []error {

	var errs []error

	if a.DomainPart == "" {
		errs = append(errs, errors.New("Domain is empty"))
	}

	return errs
}

func (a *Address) parse(s string) error {

	// normalization

	s = strings.TrimSpace(s)

	if len(s) == 0 {
		return errors.New("Address is empty")
	}

	// parsing

	domainStart := 0
	domainEnd := len(s)

	if idx := strings.IndexAny(s, "@"); idx != -1 {
		a.LocalPart = s[0:idx]
		domainStart = idx + 1
	}

	if idx := strings.IndexAny(s, "/"); idx != -1 {
		a.ResourcePart = s[idx+1:]
		domainEnd = idx
	}

	if domainStart != domainEnd {
		a.DomainPart = s[domainStart:domainEnd]
	}

	// validation

	errs := a.validate()

	if a.LocalPart == "" && domainStart != 0 {
		errs = append(errs, errors.New("Localpart is empty"))
	}

	if a.ResourcePart == "" && domainEnd != len(s) {
		errs = append(errs, errors.New("Resourcepart is empty"))
	}

{'fraction_non_alphanumeric': 0.14981427981840692, 'fraction_numerical': 0.003714403631861329, 'mean_word_length': 3.6030389363722697, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '40560', 'n_tokens_mistral': 1785, 'n_tokens_neox': 1690, 'n_words': 497}