aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/tdewolff/minify/common.go
diff options
context:
space:
mode:
authorGravatar Frédéric Guillot <fred@miniflux.net>2018-07-05 22:18:51 -0700
committerGravatar Frédéric Guillot <fred@miniflux.net>2018-07-05 22:18:51 -0700
commit53deb0b8cd1899ec325eca93631b3e137bdd3ec3 (patch)
tree23894ed57040ea689e9f60243656e1889d39a275 /vendor/github.com/tdewolff/minify/common.go
parente1c56b2e53ba3c6f48d5e159d18ae59c180cc388 (diff)
Refactor assets bundler and split Javascript files
Diffstat (limited to 'vendor/github.com/tdewolff/minify/common.go')
-rw-r--r--vendor/github.com/tdewolff/minify/common.go151
1 files changed, 137 insertions, 14 deletions
diff --git a/vendor/github.com/tdewolff/minify/common.go b/vendor/github.com/tdewolff/minify/common.go
index fa3c91d..d6b7689 100644
--- a/vendor/github.com/tdewolff/minify/common.go
+++ b/vendor/github.com/tdewolff/minify/common.go
@@ -12,8 +12,8 @@ import (
// Epsilon is the closest number to zero that is not considered to be zero.
var Epsilon = 0.00001
-// ContentType minifies a given mediatype by removing all whitespace.
-func ContentType(b []byte) []byte {
+// Mediatype minifies a given mediatype by removing all whitespace.
+func Mediatype(b []byte) []byte {
j := 0
start := 0
inString := false
@@ -79,6 +79,107 @@ func DataURI(m *M, dataURI []byte) []byte {
const MaxInt = int(^uint(0) >> 1)
const MinInt = -MaxInt - 1
+// Decimal minifies a given byte slice containing a number (see parse.Number) and removes superfluous characters.
+// It does not parse or output exponents.
+func Decimal(num []byte, prec int) []byte {
+ // omit first + and register mantissa start and end, whether it's negative and the exponent
+ neg := false
+ start := 0
+ dot := -1
+ end := len(num)
+ if 0 < end && (num[0] == '+' || num[0] == '-') {
+ if num[0] == '-' {
+ neg = true
+ }
+ start++
+ }
+ for i, c := range num[start:] {
+ if c == '.' {
+ dot = start + i
+ break
+ }
+ }
+ if dot == -1 {
+ dot = end
+ }
+
+ // trim leading zeros but leave at least one digit
+ for start < end-1 && num[start] == '0' {
+ start++
+ }
+ // trim trailing zeros
+ i := end - 1
+ for ; i > dot; i-- {
+ if num[i] != '0' {
+ end = i + 1
+ break
+ }
+ }
+ if i == dot {
+ end = dot
+ if start == end {
+ num[start] = '0'
+ return num[start : start+1]
+ }
+ } else if start == end-1 && num[start] == '0' {
+ return num[start:end]
+ }
+
+ // apply precision
+ if prec > -1 && dot+1+prec < end {
+ end = dot + 1 + prec
+ inc := num[end] >= '5'
+ if inc || num[end-1] == '0' {
+ for i := end - 1; i > start; i-- {
+ if i == dot {
+ end--
+ } else if inc {
+ if num[i] == '9' {
+ if i > dot {
+ end--
+ } else {
+ num[i] = '0'
+ }
+ } else {
+ num[i]++
+ inc = false
+ break
+ }
+ } else if i > dot && num[i] == '0' {
+ end--
+ }
+ }
+ }
+ if dot == start && end == start+1 {
+ if inc {
+ num[start] = '1'
+ } else {
+ num[start] = '0'
+ }
+ } else {
+ if dot+1 == end {
+ end--
+ }
+ if inc {
+ if num[start] == '9' {
+ num[start] = '0'
+ copy(num[start+1:], num[start:end])
+ end++
+ num[start] = '1'
+ } else {
+ num[start]++
+ }
+ }
+ }
+ }
+
+ if neg {
+ start--
+ num[start] = '-'
+ }
+ return num[start:end]
+}
+
// Number minifies a given byte slice containing a number (see parse.Number) and removes superfluous characters.
func Number(num []byte, prec int) []byte {
// omit first + and register mantissa start and end, whether it's negative and the exponent
@@ -311,24 +412,46 @@ func Number(num []byte, prec int) []byte {
}
} else {
// case 3
- if dot < end {
- if dot == start {
- copy(num[start:], num[end-n:end])
- end = start + n
- } else {
- copy(num[dot:], num[dot+1:end])
- end--
+
+ // find new end, considering moving numbers to the front, removing the dot and increasing the length of the exponent
+ newEnd := end
+ if dot == start {
+ newEnd = start + n
+ } else {
+ newEnd--
+ }
+ newEnd += 2 + lenIntExp
+
+ exp := intExp
+ lenExp := lenIntExp
+ if newEnd < len(num) {
+ // it saves space to convert the decimal to an integer and decrease the exponent
+ if dot < end {
+ if dot == start {
+ copy(num[start:], num[end-n:end])
+ end = start + n
+ } else {
+ copy(num[dot:], num[dot+1:end])
+ end--
+ }
+ }
+ } else {
+ // it does not save space and will panic, so we revert to the original representation
+ exp = origExp
+ lenExp = 1
+ if origExp <= -10 || origExp >= 10 {
+ lenExp = strconv.LenInt(int64(origExp))
}
}
num[end] = 'e'
num[end+1] = '-'
end += 2
- intExp = -intExp
- for i := end + lenIntExp - 1; i >= end; i-- {
- num[i] = byte(intExp%10) + '0'
- intExp /= 10
+ exp = -exp
+ for i := end + lenExp - 1; i >= end; i-- {
+ num[i] = byte(exp%10) + '0'
+ exp /= 10
}
- end += lenIntExp
+ end += lenExp
}
if neg {