common.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. package minify // import "github.com/tdewolff/minify"
  2. import (
  3. "bytes"
  4. "encoding/base64"
  5. "net/url"
  6. "github.com/tdewolff/parse"
  7. "github.com/tdewolff/parse/strconv"
  8. )
  9. // Epsilon is the closest number to zero that is not considered to be zero.
  10. var Epsilon = 0.00001
  11. // ContentType minifies a given mediatype by removing all whitespace.
  12. func ContentType(b []byte) []byte {
  13. j := 0
  14. start := 0
  15. inString := false
  16. for i, c := range b {
  17. if !inString && parse.IsWhitespace(c) {
  18. if start != 0 {
  19. j += copy(b[j:], b[start:i])
  20. } else {
  21. j += i
  22. }
  23. start = i + 1
  24. } else if c == '"' {
  25. inString = !inString
  26. }
  27. }
  28. if start != 0 {
  29. j += copy(b[j:], b[start:])
  30. return parse.ToLower(b[:j])
  31. }
  32. return parse.ToLower(b)
  33. }
  34. // DataURI minifies a data URI and calls a minifier by the specified mediatype. Specifications: https://www.ietf.org/rfc/rfc2397.txt.
  35. func DataURI(m *M, dataURI []byte) []byte {
  36. if mediatype, data, err := parse.DataURI(dataURI); err == nil {
  37. dataURI, _ = m.Bytes(string(mediatype), data)
  38. base64Len := len(";base64") + base64.StdEncoding.EncodedLen(len(dataURI))
  39. asciiLen := len(dataURI)
  40. for _, c := range dataURI {
  41. if 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || '0' <= c && c <= '9' || c == '-' || c == '_' || c == '.' || c == '~' || c == ' ' {
  42. asciiLen++
  43. } else {
  44. asciiLen += 2
  45. }
  46. if asciiLen > base64Len {
  47. break
  48. }
  49. }
  50. if asciiLen > base64Len {
  51. encoded := make([]byte, base64Len-len(";base64"))
  52. base64.StdEncoding.Encode(encoded, dataURI)
  53. dataURI = encoded
  54. mediatype = append(mediatype, []byte(";base64")...)
  55. } else {
  56. dataURI = []byte(url.QueryEscape(string(dataURI)))
  57. dataURI = bytes.Replace(dataURI, []byte("\""), []byte("\\\""), -1)
  58. }
  59. if len("text/plain") <= len(mediatype) && parse.EqualFold(mediatype[:len("text/plain")], []byte("text/plain")) {
  60. mediatype = mediatype[len("text/plain"):]
  61. }
  62. for i := 0; i+len(";charset=us-ascii") <= len(mediatype); i++ {
  63. // must start with semicolon and be followed by end of mediatype or semicolon
  64. if mediatype[i] == ';' && parse.EqualFold(mediatype[i+1:i+len(";charset=us-ascii")], []byte("charset=us-ascii")) && (i+len(";charset=us-ascii") >= len(mediatype) || mediatype[i+len(";charset=us-ascii")] == ';') {
  65. mediatype = append(mediatype[:i], mediatype[i+len(";charset=us-ascii"):]...)
  66. break
  67. }
  68. }
  69. dataURI = append(append(append([]byte("data:"), mediatype...), ','), dataURI...)
  70. }
  71. return dataURI
  72. }
  73. const MaxInt = int(^uint(0) >> 1)
  74. const MinInt = -MaxInt - 1
  75. // Number minifies a given byte slice containing a number (see parse.Number) and removes superfluous characters.
  76. func Number(num []byte, prec int) []byte {
  77. // omit first + and register mantissa start and end, whether it's negative and the exponent
  78. neg := false
  79. start := 0
  80. dot := -1
  81. end := len(num)
  82. origExp := 0
  83. if 0 < end && (num[0] == '+' || num[0] == '-') {
  84. if num[0] == '-' {
  85. neg = true
  86. }
  87. start++
  88. }
  89. for i, c := range num[start:] {
  90. if c == '.' {
  91. dot = start + i
  92. } else if c == 'e' || c == 'E' {
  93. end = start + i
  94. i += start + 1
  95. if i < len(num) && num[i] == '+' {
  96. i++
  97. }
  98. if tmpOrigExp, n := strconv.ParseInt(num[i:]); n > 0 && tmpOrigExp >= int64(MinInt) && tmpOrigExp <= int64(MaxInt) {
  99. // range checks for when int is 32 bit
  100. origExp = int(tmpOrigExp)
  101. } else {
  102. return num
  103. }
  104. break
  105. }
  106. }
  107. if dot == -1 {
  108. dot = end
  109. }
  110. // trim leading zeros but leave at least one digit
  111. for start < end-1 && num[start] == '0' {
  112. start++
  113. }
  114. // trim trailing zeros
  115. i := end - 1
  116. for ; i > dot; i-- {
  117. if num[i] != '0' {
  118. end = i + 1
  119. break
  120. }
  121. }
  122. if i == dot {
  123. end = dot
  124. if start == end {
  125. num[start] = '0'
  126. return num[start : start+1]
  127. }
  128. } else if start == end-1 && num[start] == '0' {
  129. return num[start:end]
  130. }
  131. // n is the number of significant digits
  132. // normExp would be the exponent if it were normalised (0.1 <= f < 1)
  133. n := 0
  134. normExp := 0
  135. if dot == start {
  136. for i = dot + 1; i < end; i++ {
  137. if num[i] != '0' {
  138. n = end - i
  139. normExp = dot - i + 1
  140. break
  141. }
  142. }
  143. } else if dot == end {
  144. normExp = end - start
  145. for i = end - 1; i >= start; i-- {
  146. if num[i] != '0' {
  147. n = i + 1 - start
  148. end = i + 1
  149. break
  150. }
  151. }
  152. } else {
  153. n = end - start - 1
  154. normExp = dot - start
  155. }
  156. if origExp < 0 && (normExp < MinInt-origExp || normExp-n < MinInt-origExp) || origExp > 0 && (normExp > MaxInt-origExp || normExp-n > MaxInt-origExp) {
  157. return num
  158. }
  159. normExp += origExp
  160. // intExp would be the exponent if it were an integer
  161. intExp := normExp - n
  162. lenIntExp := 1
  163. if intExp <= -10 || intExp >= 10 {
  164. lenIntExp = strconv.LenInt(int64(intExp))
  165. }
  166. // there are three cases to consider when printing the number
  167. // case 1: without decimals and with an exponent (large numbers)
  168. // case 2: with decimals and without an exponent (around zero)
  169. // case 3: without decimals and with a negative exponent (small numbers)
  170. if normExp >= n {
  171. // case 1
  172. if dot < end {
  173. if dot == start {
  174. start = end - n
  175. } else {
  176. // TODO: copy the other part if shorter?
  177. copy(num[dot:], num[dot+1:end])
  178. end--
  179. }
  180. }
  181. if normExp >= n+3 {
  182. num[end] = 'e'
  183. end++
  184. for i := end + lenIntExp - 1; i >= end; i-- {
  185. num[i] = byte(intExp%10) + '0'
  186. intExp /= 10
  187. }
  188. end += lenIntExp
  189. } else if normExp == n+2 {
  190. num[end] = '0'
  191. num[end+1] = '0'
  192. end += 2
  193. } else if normExp == n+1 {
  194. num[end] = '0'
  195. end++
  196. }
  197. } else if normExp >= -lenIntExp-1 {
  198. // case 2
  199. zeroes := -normExp
  200. newDot := 0
  201. if zeroes > 0 {
  202. // dot placed at the front and add zeroes
  203. newDot = end - n - zeroes - 1
  204. if newDot != dot {
  205. d := start - newDot
  206. if d > 0 {
  207. if dot < end {
  208. // copy original digits behind the dot backwards
  209. copy(num[dot+1+d:], num[dot+1:end])
  210. if dot > start {
  211. // copy original digits before the dot backwards
  212. copy(num[start+d+1:], num[start:dot])
  213. }
  214. } else if dot > start {
  215. // copy original digits before the dot backwards
  216. copy(num[start+d:], num[start:dot])
  217. }
  218. newDot = start
  219. end += d
  220. } else {
  221. start += -d
  222. }
  223. num[newDot] = '.'
  224. for i := 0; i < zeroes; i++ {
  225. num[newDot+1+i] = '0'
  226. }
  227. }
  228. } else {
  229. // placed in the middle
  230. if dot == start {
  231. // TODO: try if placing at the end reduces copying
  232. // when there are zeroes after the dot
  233. dot = end - n - 1
  234. start = dot
  235. } else if dot >= end {
  236. // TODO: try if placing at the start reduces copying
  237. // when input has no dot in it
  238. dot = end
  239. end++
  240. }
  241. newDot = start + normExp
  242. if newDot > dot {
  243. // copy digits forwards
  244. copy(num[dot:], num[dot+1:newDot+1])
  245. } else if newDot < dot {
  246. // copy digits backwards
  247. copy(num[newDot+1:], num[newDot:dot])
  248. }
  249. num[newDot] = '.'
  250. }
  251. // apply precision
  252. dot = newDot
  253. if prec > -1 && dot+1+prec < end {
  254. end = dot + 1 + prec
  255. inc := num[end] >= '5'
  256. if inc || num[end-1] == '0' {
  257. for i := end - 1; i > start; i-- {
  258. if i == dot {
  259. end--
  260. } else if inc {
  261. if num[i] == '9' {
  262. if i > dot {
  263. end--
  264. } else {
  265. num[i] = '0'
  266. }
  267. } else {
  268. num[i]++
  269. inc = false
  270. break
  271. }
  272. } else if i > dot && num[i] == '0' {
  273. end--
  274. }
  275. }
  276. }
  277. if dot == start && end == start+1 {
  278. if inc {
  279. num[start] = '1'
  280. } else {
  281. num[start] = '0'
  282. }
  283. } else {
  284. if dot+1 == end {
  285. end--
  286. }
  287. if inc {
  288. if num[start] == '9' {
  289. num[start] = '0'
  290. copy(num[start+1:], num[start:end])
  291. end++
  292. num[start] = '1'
  293. } else {
  294. num[start]++
  295. }
  296. }
  297. }
  298. }
  299. } else {
  300. // case 3
  301. if dot < end {
  302. if dot == start {
  303. copy(num[start:], num[end-n:end])
  304. end = start + n
  305. } else {
  306. copy(num[dot:], num[dot+1:end])
  307. end--
  308. }
  309. }
  310. num[end] = 'e'
  311. num[end+1] = '-'
  312. end += 2
  313. intExp = -intExp
  314. for i := end + lenIntExp - 1; i >= end; i-- {
  315. num[i] = byte(intExp%10) + '0'
  316. intExp /= 10
  317. }
  318. end += lenIntExp
  319. }
  320. if neg {
  321. start--
  322. num[start] = '-'
  323. }
  324. return num[start:end]
  325. }