MCPcopy
hub / github.com/tdewolff/minify / DataURI

Function DataURI

common.go:55–95  ·  view source on GitHub ↗

DataURI minifies a data URI and calls a minifier by the specified mediatype. Specifications: https://www.ietf.org/rfc/rfc2397.txt.

(m *M, dataURI []byte)

Source from the content-addressed store, hash-verified

53
54// DataURI minifies a data URI and calls a minifier by the specified mediatype. Specifications: https://www.ietf.org/rfc/rfc2397.txt.
55func DataURI(m *M, dataURI []byte) []byte {
56 origData := parse.Copy(dataURI)
57 mediatype, data, err := parse.DataURI(dataURI)
58 if err != nil {
59 return dataURI
60 }
61
62 data, _ = m.Bytes(string(mediatype), data)
63 base64Len := len(";base64") + base64.StdEncoding.EncodedLen(len(data))
64 asciiLen := len(data)
65 for _, c := range data {
66 if parse.DataURIEncodingTable[c] {
67 asciiLen += 2
68 }
69 if asciiLen > base64Len {
70 break
71 }
72 }
73 if len(origData) < base64Len && len(origData) < asciiLen {
74 return origData
75 }
76 if base64Len < asciiLen {
77 encoded := make([]byte, base64Len-len(";base64"))
78 base64.StdEncoding.Encode(encoded, data)
79 data = encoded
80 mediatype = append(mediatype, base64Bytes...)
81 } else {
82 data = parse.EncodeURL(data, parse.DataURIEncodingTable)
83 }
84 if len("text/plain") <= len(mediatype) && parse.EqualFold(mediatype[:len("text/plain")], textMimeBytes) {
85 mediatype = mediatype[len("text/plain"):]
86 }
87 for i := 0; i+len(";charset=us-ascii") <= len(mediatype); i++ {
88 // must start with semicolon and be followed by end of mediatype or semicolon
89 if mediatype[i] == ';' && parse.EqualFold(mediatype[i+1:i+len(";charset=us-ascii")], charsetASCIIBytes) && (i+len(";charset=us-ascii") >= len(mediatype) || mediatype[i+len(";charset=us-ascii")] == ';') {
90 mediatype = append(mediatype[:i], mediatype[i+len(";charset=us-ascii"):]...)
91 break
92 }
93 }
94 return append(append(append(dataBytes, mediatype...), ','), data...)
95}
96
97// MaxInt is the maximum value of int.
98const MaxInt = int(^uint(0) >> 1)

Callers 1

TestDataURIFunction · 0.85

Calls 4

lenFunction · 0.85
appendFunction · 0.85
stringFunction · 0.50
BytesMethod · 0.45

Tested by 1

TestDataURIFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…