summaryrefslogtreecommitdiffstats
path: root/vendor/gitlab.com/golang-commonmark/mdurl/url.go
blob: 72c3f58aa9dd07fb5417a28dc7119ec1f1c07c78 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2015 The Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package url provides functions for parsing, decoding and encoding URLs.
package mdurl

// A URL represents a parsed URL.
type URL struct {
	Scheme      string
	RawScheme   string
	Slashes     bool
	Auth        string
	Host        string
	Port        string
	Path        string
	RawQuery    string
	HasQuery    bool
	Fragment    string
	HasFragment bool
	IPv6        bool
}

// String reassembles the URL into a URL string.
func (u *URL) String() string {
	size := len(u.Path)
	if u.Scheme != "" {
		size += len(u.Scheme) + 1
	}
	if u.Slashes {
		size += 2
	}
	if u.Auth != "" {
		size += len(u.Auth) + 1
	}
	if u.Host != "" {
		size += len(u.Host)
		if u.IPv6 {
			size += 2
		}
	}
	if u.Port != "" {
		size += len(u.Port) + 1
	}
	if u.HasQuery {
		size += len(u.RawQuery) + 1
	}
	if u.HasFragment {
		size += len(u.Fragment) + 1
	}
	if size == 0 {
		return ""
	}

	buf := make([]byte, size)
	i := 0
	if u.Scheme != "" {
		i += copy(buf, u.Scheme)
		buf[i] = ':'
		i++
	}
	if u.Slashes {
		buf[i] = '/'
		i++
		buf[i] = '/'
		i++
	}
	if u.Auth != "" {
		i += copy(buf[i:], u.Auth)
		buf[i] = '@'
		i++
	}
	if u.Host != "" {
		if u.IPv6 {
			buf[i] = '['
			i++
			i += copy(buf[i:], u.Host)
			buf[i] = ']'
			i++
		} else {
			i += copy(buf[i:], u.Host)
		}
	}
	if u.Port != "" {
		buf[i] = ':'
		i++
		i += copy(buf[i:], u.Port)
	}
	i += copy(buf[i:], u.Path)
	if u.HasQuery {
		buf[i] = '?'
		i++
		i += copy(buf[i:], u.RawQuery)
	}
	if u.HasFragment {
		buf[i] = '#'
		i++
		i += copy(buf[i:], u.Fragment)
	}
	return string(buf)
}