forked from tdewolff/minify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefault.go
More file actions
52 lines (45 loc) · 1.15 KB
/
Copy pathdefault.go
File metadata and controls
52 lines (45 loc) · 1.15 KB
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
package minify // import "github.com/tdewolff/minify"
import (
"bytes"
"io"
"unicode"
"github.com/tdewolff/parse"
)
const bufSize = 1024
// Default is a default minifier used to minify an unknown media type.
// It remove all whitespace at the beginning and end of the strea.
func (m Minifier) Default(w io.Writer, r io.Reader) error {
if fr, ok := r.(interface {
Bytes() []byte
}); ok {
b := bytes.TrimSpace(fr.Bytes())
if _, errWrite := w.Write(b); errWrite != nil {
return errWrite
}
return nil
}
head := true
sb := parse.NewShiftBuffer(r)
for {
// cause a read
if sb.Peek(0) == 0 && sb.Err() != nil {
if sb.Err() == io.EOF {
return nil
}
return sb.Err()
}
// consume whole buffer and unconsume trailing whitespace
sb.MoveTo(sb.Len())
trailingLen := sb.Len() - len(bytes.TrimRightFunc(sb.Buffered(), unicode.IsSpace))
sb.Move(-trailingLen)
b := sb.Shift()
if head {
b = bytes.TrimLeftFunc(b, unicode.IsSpace)
head = len(b) == 0 // if it's all whitespace, we still need to trim leading whitespace next time
}
if _, errWrite := w.Write(b); errWrite != nil {
return errWrite
}
sb.Move(trailingLen)
}
}