// ParseMagnetURI parses Magnet-formatted URIs into a Magnet instance
977863
func ParseMagnetURI(uri string) (m Magnet, err error) {
	u, err := url.Parse(uri)
	if err != nil {
		err = fmt.Errorf("error parsing uri: %s", err)
		return
	}
	if u.Scheme != "magnet" {
		err = fmt.Errorf("unexpected scheme: %q", u.Scheme)
		return
	}
	xt := u.Query().Get("xt")
	if !strings.HasPrefix(xt, xtPrefix) {
		err = fmt.Errorf("bad xt parameter")
		return
	}
	infoHash := xt[len(xtPrefix):]

	// BTIH hash can be in HEX or BASE32 encoding
	// will assign appropriate func judging from symbol length
	var decode func(dst, src []byte) (int, error)
	switch len(infoHash) {
	case 40:
		decode = hex.Decode
	case 32:
		decode = base32.StdEncoding.Decode
	}

	if decode == nil {
		err = fmt.Errorf("unhandled xt parameter encoding: encoded length %d", len(infoHash))
		return
	}
	n, err := decode(m.InfoHash[:], []byte(infoHash))
	if err != nil {
		err = fmt.Errorf("error decoding xt: %s", err)
		return
	}
	if n != 20 {
		panic(n)
	}
	m.DisplayName = u.Query().Get("dn")
	m.Trackers = u.Query()["tr"]
	return
}
1284469
func ParseGenericResources(value []string) ([]swarm.GenericResource, error) {
	if len(value) == 0 {
		return nil, nil
	}

	resources, err := genericresource.Parse(value)
	if err != nil {
		return nil, err
	}

	obj := convert.GenericResourcesFromGRPC(resources)
	return obj, nil
}
548549
func ParseMapFromAzure(clusterLister ClusterLister) (*Map, error) {
	listResult, err := clusterLister.List()
	if err != nil {
		log.Printf("Parse Map From Azure: %v", err)
		return nil, err
	}

	return &Map{nameMap: clusterNamesToMap(listResult.Value)}, nil
}
54239
func (m *configurableRegexMetricDeconstructor) Parse(originalMetric string) (string, datapoint.MetricType, map[string]string, error) {
	for _, metric := range m.MetricRules {
		if metric.Compiled.MatchString(originalMetric) {
			ms := metric.Compiled.FindStringSubmatch(originalMetric)
			nms := metric.Compiled.SubexpNames()
			dimensions := map[string]string{}
			metricNamePieces := []string{}
			metricNameLookup := map[string]string{}

			for i := 1; i < len(ms); i++ {
				if strings.HasPrefix(nms[i], "sf_metric") {
					metricNamePieces = append(metricNamePieces, nms[i])
					metricNameLookup[nms[i]] = ms[i]
				} else {
					dimensions[nms[i]] = ms[i]
				}
			}
			for k, v := range metric.AdditionalDimensions {
				dimensions[k] = v
			}

			sort.Strings(metricNamePieces)
			metricName := []string{metric.MetricName}
			for _, piece := range metricNamePieces {
				metricName = append(metricName, metricNameLookup[piece])
			}
			actualMetricName := strings.Join(metricName, "")
			if actualMetricName == "" {
				actualMetricName = originalMetric
			}
			return actualMetricName, metric.MetricType, dimensions, nil
		}
	}

	return m.FallBackDeconstructor.Parse(originalMetric)
}
411219
func (p *Parser) parseMsgId() (string, error) {
	return parseUpToLen(p.buff, &p.cursor, p.l, 32, ErrInvalidMsgId)
}
12870
func ParseEUI64(input string) (eui EUI64, err error) {
	bytes, err := ParseHEX(input, 8)
	if err != nil {
		return
	}
	copy(eui[:], bytes)
	return
}
46101
func ParseUrl(inUrl string) (*url.URL, error) {
	parsedUrl, err := url.Parse(inUrl)
	if err != nil {
		return nil, err
	}

	if parsedUrl.Host == "" || parsedUrl.Scheme == "" {
		return nil, fmt.Errorf("Empty Url is not allowed")
	}
	return parsedUrl, nil
}
136298
func (g *HttpGetter) parseMeta(r io.Reader) (string, error) {
	d := xml.NewDecoder(r)
	d.CharsetReader = charsetReader
	d.Strict = false
	var err error
	var t xml.Token
	for {
		t, err = d.Token()
		if err != nil {
			if err == io.EOF {
				err = nil
			}
			return "", err
		}
		if e, ok := t.(xml.StartElement); ok && strings.EqualFold(e.Name.Local, "body") {
			return "", nil
		}
		if e, ok := t.(xml.EndElement); ok && strings.EqualFold(e.Name.Local, "head") {
			return "", nil
		}
		e, ok := t.(xml.StartElement)
		if !ok || !strings.EqualFold(e.Name.Local, "meta") {
			continue
		}
		if attrValue(e.Attr, "name") != "terraform-get" {
			continue
		}
		if f := attrValue(e.Attr, "content"); f != "" {
			return f, nil
		}
	}
}
569580
func URLMustParse(s string) *url.URL {
	u, err := url.Parse(s)
	if err != nil {
		log.Errorf("Expected URL to parse: %q, got error: %v", s, err)
	}
	return u
}
527717
func ParseURL(s string) (*url.URL, error) {
	var err error
	var u *url.URL

	if s != "" {
		// Default the scheme to https
		if !schemeMatch.MatchString(s) {
			s = "https://" + s
		}

		u, err = url.Parse(s)
		if err != nil {
			return nil, err
		}

		// Default the path to /sdk
		if u.Path == "" {
			u.Path = "/sdk"
		}

		if u.User == nil {
			u.User = url.UserPassword("", "")
		}
	}

	return u, nil
}
384693
func (po *Po) parsePluralID(l string) {
	po.trBuffer.PluralID, _ = strconv.Unquote(strings.TrimSpace(strings.TrimPrefix(l, "msgid_plural")))
}
1092573
func parseGMT(value string) int {
	value = value[3:]
	if len(value) == 0 {
		return 3
	}

	return 3 + parseSignedOffset(value)
}
1173376
func ParseGenericResources(value []string) ([]swarm.GenericResource, error) {
	if len(value) == 0 {
		return nil, nil
	}

	resources, err := genericresource.Parse(value)
	if err != nil {
		return nil, errors.Wrapf(err, "invalid generic resource specification")
	}

	swarmResources := genericResourcesFromGRPC(resources)
	for _, res := range swarmResources {
		if res.NamedResourceSpec != nil {
			return nil, fmt.Errorf("invalid generic-resource request `%s=%s`, Named Generic Resources is not supported for service create or update", res.NamedResourceSpec.Kind, res.NamedResourceSpec.Value)
		}
	}

	return swarmResources, nil
}
1145134
func (h Header) parseMediaType(typeField string) (string, map[string]string, error) {
	if content := h.Get(typeField); len(content) > 0 {
		mediaType, mediaTypeParams, err := mime.ParseMediaType(content)
		if err != nil {
			return "", map[string]string{}, err
		}
		return mediaType, mediaTypeParams, nil
	}
	return "", map[string]string{}, ErrHeadersMissingField
}
775896
func ValidateMirror(val string) (string, error) {
	uri, err := url.Parse(val)
	if err != nil {
		return "", fmt.Errorf("invalid mirror: %q is not a valid URI", val)
	}
	if uri.Scheme != "http" && uri.Scheme != "https" {
		return "", fmt.Errorf("invalid mirror: unsupported scheme %q in %q", uri.Scheme, uri)
	}
	if (uri.Path != "" && uri.Path != "/") || uri.RawQuery != "" || uri.Fragment != "" {
		return "", fmt.Errorf("invalid mirror: path, query, or fragment at end of the URI %q", uri)
	}
	if uri.User != nil {
		// strip password from output
		uri.User = url.UserPassword(uri.User.Username(), "xxxxx")
		return "", fmt.Errorf("invalid mirror: username/password not allowed in URI %q", uri)
	}
	return strings.TrimSuffix(val, "/") + "/", nil
}
655722
func parseMetadata(md map[string]string) map[string]interface{} {
	m := make(map[string]interface{}, len(md))
	for key, value := range md {
		m[key] = value
	}
	return m
}
914727
func ParseURL(daemonURL string) (*Spec, error) {
	u, err := url.Parse(daemonURL)
	if err != nil {
		return nil, err
	}
	if u.Scheme != "ssh" {
		return nil, errors.Errorf("expected scheme ssh, got %q", u.Scheme)
	}

	var sp Spec

	if u.User != nil {
		sp.User = u.User.Username()
		if _, ok := u.User.Password(); ok {
			return nil, errors.New("plain-text password is not supported")
		}
	}
	sp.Host = u.Hostname()
	if sp.Host == "" {
		return nil, errors.Errorf("no host specified")
	}
	sp.Port = u.Port()
	if u.Path != "" {
		return nil, errors.Errorf("extra path after the host: %q", u.Path)
	}
	if u.RawQuery != "" {
		return nil, errors.Errorf("extra query after the host: %q", u.RawQuery)
	}
	if u.Fragment != "" {
		return nil, errors.Errorf("extra fragment after the host: %q", u.Fragment)
	}
	return &sp, err
}