/vendor/github.com/tdewolff/parse/common.go

https://github.com/dabio/min · Go · 230 lines · 213 code · 7 blank · 10 comment · 160 complexity · 1572ed4ffda582ff030dc72d73641314 MD5 · raw file

  1. // Package parse contains a collection of parsers for various formats in its subpackages.
  2. package parse // import "github.com/tdewolff/parse"
  3. import (
  4. "encoding/base64"
  5. "errors"
  6. "net/url"
  7. )
  8. // ErrBadDataURI is returned by DataURI when the byte slice does not start with 'data:' or is too short.
  9. var ErrBadDataURI = errors.New("not a data URI")
  10. // Number returns the number of bytes that parse as a number of the regex format (+|-)?([0-9]+(\.[0-9]+)?|\.[0-9]+)((e|E)(+|-)?[0-9]+)?.
  11. func Number(b []byte) int {
  12. if len(b) == 0 {
  13. return 0
  14. }
  15. i := 0
  16. if b[i] == '+' || b[i] == '-' {
  17. i++
  18. if i >= len(b) {
  19. return 0
  20. }
  21. }
  22. firstDigit := (b[i] >= '0' && b[i] <= '9')
  23. if firstDigit {
  24. i++
  25. for i < len(b) && b[i] >= '0' && b[i] <= '9' {
  26. i++
  27. }
  28. }
  29. if i < len(b) && b[i] == '.' {
  30. i++
  31. if i < len(b) && b[i] >= '0' && b[i] <= '9' {
  32. i++
  33. for i < len(b) && b[i] >= '0' && b[i] <= '9' {
  34. i++
  35. }
  36. } else if firstDigit {
  37. // . could belong to the next token
  38. i--
  39. return i
  40. } else {
  41. return 0
  42. }
  43. } else if !firstDigit {
  44. return 0
  45. }
  46. iOld := i
  47. if i < len(b) && (b[i] == 'e' || b[i] == 'E') {
  48. i++
  49. if i < len(b) && (b[i] == '+' || b[i] == '-') {
  50. i++
  51. }
  52. if i >= len(b) || b[i] < '0' || b[i] > '9' {
  53. // e could belong to next token
  54. return iOld
  55. }
  56. for i < len(b) && b[i] >= '0' && b[i] <= '9' {
  57. i++
  58. }
  59. }
  60. return i
  61. }
  62. // Dimension parses a byte-slice and returns the length of the number and its unit.
  63. func Dimension(b []byte) (int, int) {
  64. num := Number(b)
  65. if num == 0 || num == len(b) {
  66. return num, 0
  67. } else if b[num] == '%' {
  68. return num, 1
  69. } else if b[num] >= 'a' && b[num] <= 'z' || b[num] >= 'A' && b[num] <= 'Z' {
  70. i := num + 1
  71. for i < len(b) && (b[i] >= 'a' && b[i] <= 'z' || b[i] >= 'A' && b[i] <= 'Z') {
  72. i++
  73. }
  74. return num, i - num
  75. }
  76. return num, 0
  77. }
  78. // Mediatype parses a given mediatype and splits the mimetype from the parameters.
  79. // It works similar to mime.ParseMediaType but is faster.
  80. func Mediatype(b []byte) ([]byte, map[string]string) {
  81. i := 0
  82. for i < len(b) && b[i] == ' ' {
  83. i++
  84. }
  85. b = b[i:]
  86. n := len(b)
  87. mimetype := b
  88. var params map[string]string
  89. for i := 3; i < n; i++ { // mimetype is at least three characters long
  90. if b[i] == ';' || b[i] == ' ' {
  91. mimetype = b[:i]
  92. if b[i] == ' ' {
  93. i++
  94. for i < n && b[i] == ' ' {
  95. i++
  96. }
  97. if i < n && b[i] != ';' {
  98. break
  99. }
  100. }
  101. params = map[string]string{}
  102. s := string(b)
  103. PARAM:
  104. i++
  105. for i < n && s[i] == ' ' {
  106. i++
  107. }
  108. start := i
  109. for i < n && s[i] != '=' && s[i] != ';' && s[i] != ' ' {
  110. i++
  111. }
  112. key := s[start:i]
  113. for i < n && s[i] == ' ' {
  114. i++
  115. }
  116. if i < n && s[i] == '=' {
  117. i++
  118. for i < n && s[i] == ' ' {
  119. i++
  120. }
  121. start = i
  122. for i < n && s[i] != ';' && s[i] != ' ' {
  123. i++
  124. }
  125. } else {
  126. start = i
  127. }
  128. params[key] = s[start:i]
  129. for i < n && s[i] == ' ' {
  130. i++
  131. }
  132. if i < n && s[i] == ';' {
  133. goto PARAM
  134. }
  135. break
  136. }
  137. }
  138. return mimetype, params
  139. }
  140. // DataURI parses the given data URI and returns the mediatype, data and ok.
  141. func DataURI(dataURI []byte) ([]byte, []byte, error) {
  142. if len(dataURI) > 5 && Equal(dataURI[:5], []byte("data:")) {
  143. dataURI = dataURI[5:]
  144. inBase64 := false
  145. var mediatype []byte
  146. i := 0
  147. for j := 0; j < len(dataURI); j++ {
  148. c := dataURI[j]
  149. if c == '=' || c == ';' || c == ',' {
  150. if c != '=' && Equal(TrimWhitespace(dataURI[i:j]), []byte("base64")) {
  151. if len(mediatype) > 0 {
  152. mediatype = mediatype[:len(mediatype)-1]
  153. }
  154. inBase64 = true
  155. i = j
  156. } else if c != ',' {
  157. mediatype = append(append(mediatype, TrimWhitespace(dataURI[i:j])...), c)
  158. i = j + 1
  159. } else {
  160. mediatype = append(mediatype, TrimWhitespace(dataURI[i:j])...)
  161. }
  162. if c == ',' {
  163. if len(mediatype) == 0 || mediatype[0] == ';' {
  164. mediatype = []byte("text/plain")
  165. }
  166. data := dataURI[j+1:]
  167. if inBase64 {
  168. decoded := make([]byte, base64.StdEncoding.DecodedLen(len(data)))
  169. n, err := base64.StdEncoding.Decode(decoded, data)
  170. if err != nil {
  171. return nil, nil, err
  172. }
  173. data = decoded[:n]
  174. } else if unescaped, err := url.QueryUnescape(string(data)); err == nil {
  175. data = []byte(unescaped)
  176. }
  177. return mediatype, data, nil
  178. }
  179. }
  180. }
  181. }
  182. return nil, nil, ErrBadDataURI
  183. }
  184. // QuoteEntity parses the given byte slice and returns the quote that got matched (' or ") and its entity length.
  185. func QuoteEntity(b []byte) (quote byte, n int) {
  186. if len(b) < 5 || b[0] != '&' {
  187. return 0, 0
  188. }
  189. if b[1] == '#' {
  190. if b[2] == 'x' {
  191. i := 3
  192. for i < len(b) && b[i] == '0' {
  193. i++
  194. }
  195. if i+2 < len(b) && b[i] == '2' && b[i+2] == ';' {
  196. if b[i+1] == '2' {
  197. return '"', i + 3 // &#x22;
  198. } else if b[i+1] == '7' {
  199. return '\'', i + 3 // &#x27;
  200. }
  201. }
  202. } else {
  203. i := 2
  204. for i < len(b) && b[i] == '0' {
  205. i++
  206. }
  207. if i+2 < len(b) && b[i] == '3' && b[i+2] == ';' {
  208. if b[i+1] == '4' {
  209. return '"', i + 3 // &#34;
  210. } else if b[i+1] == '9' {
  211. return '\'', i + 3 // &#39;
  212. }
  213. }
  214. }
  215. } else if len(b) >= 6 && b[5] == ';' {
  216. if EqualFold(b[1:5], []byte{'q', 'u', 'o', 't'}) {
  217. return '"', 6 // &quot;
  218. } else if EqualFold(b[1:5], []byte{'a', 'p', 'o', 's'}) {
  219. return '\'', 6 // &apos;
  220. }
  221. }
  222. return 0, 0
  223. }