-
Notifications
You must be signed in to change notification settings - Fork 5
/
type_string.go
41 lines (33 loc) · 962 Bytes
/
type_string.go
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
package hashuri
import (
"bytes"
)
// String reassembles the Hash URI into a valid Hash URI string. The general form of the result is:
//
// hash://sha256/9f86d081884c7d659a2feaa0?type=text/plain#top
// \__/ \____/ \______________________/ \_____________/ \_/
// | | | | |
// scheme algorithm hash query fragment
func (receiver Type) String() string {
var buffer bytes.Buffer
buffer.WriteString("hash:")
if "" != receiver.Algorithm {
buffer.WriteString("//")
buffer.WriteString(receiver.Algorithm)
}
if "" != receiver.Algorithm || "" != receiver.Hash {
buffer.WriteRune('/')
}
if "" != receiver.Hash {
buffer.WriteString(receiver.Hash)
}
if "" != receiver.RawQuery {
buffer.WriteRune('?')
buffer.WriteString(receiver.RawQuery)
}
if "" != receiver.Fragment {
buffer.WriteRune('#')
buffer.WriteString(receiver.Fragment)
}
return buffer.String()
}