buffer.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package svg // import "github.com/tdewolff/minify/svg"
  2. import (
  3. "github.com/tdewolff/parse"
  4. "github.com/tdewolff/parse/svg"
  5. "github.com/tdewolff/parse/xml"
  6. )
  7. // Token is a single token unit with an attribute value (if given) and hash of the data.
  8. type Token struct {
  9. xml.TokenType
  10. Hash svg.Hash
  11. Data []byte
  12. Text []byte
  13. AttrVal []byte
  14. }
  15. // TokenBuffer is a buffer that allows for token look-ahead.
  16. type TokenBuffer struct {
  17. l *xml.Lexer
  18. buf []Token
  19. pos int
  20. attrBuffer []*Token
  21. }
  22. // NewTokenBuffer returns a new TokenBuffer.
  23. func NewTokenBuffer(l *xml.Lexer) *TokenBuffer {
  24. return &TokenBuffer{
  25. l: l,
  26. buf: make([]Token, 0, 8),
  27. }
  28. }
  29. func (z *TokenBuffer) read(t *Token) {
  30. t.TokenType, t.Data = z.l.Next()
  31. t.Text = z.l.Text()
  32. if t.TokenType == xml.AttributeToken {
  33. t.AttrVal = z.l.AttrVal()
  34. if len(t.AttrVal) > 1 && (t.AttrVal[0] == '"' || t.AttrVal[0] == '\'') {
  35. t.AttrVal = parse.ReplaceMultipleWhitespace(parse.TrimWhitespace(t.AttrVal[1 : len(t.AttrVal)-1])) // quotes will be readded in attribute loop if necessary
  36. }
  37. t.Hash = svg.ToHash(t.Text)
  38. } else if t.TokenType == xml.StartTagToken || t.TokenType == xml.EndTagToken {
  39. t.AttrVal = nil
  40. t.Hash = svg.ToHash(t.Text)
  41. } else {
  42. t.AttrVal = nil
  43. t.Hash = 0
  44. }
  45. }
  46. // Peek returns the ith element and possibly does an allocation.
  47. // Peeking past an error will panic.
  48. func (z *TokenBuffer) Peek(pos int) *Token {
  49. pos += z.pos
  50. if pos >= len(z.buf) {
  51. if len(z.buf) > 0 && z.buf[len(z.buf)-1].TokenType == xml.ErrorToken {
  52. return &z.buf[len(z.buf)-1]
  53. }
  54. c := cap(z.buf)
  55. d := len(z.buf) - z.pos
  56. p := pos - z.pos + 1 // required peek length
  57. var buf []Token
  58. if 2*p > c {
  59. buf = make([]Token, 0, 2*c+p)
  60. } else {
  61. buf = z.buf
  62. }
  63. copy(buf[:d], z.buf[z.pos:])
  64. buf = buf[:p]
  65. pos -= z.pos
  66. for i := d; i < p; i++ {
  67. z.read(&buf[i])
  68. if buf[i].TokenType == xml.ErrorToken {
  69. buf = buf[:i+1]
  70. pos = i
  71. break
  72. }
  73. }
  74. z.pos, z.buf = 0, buf
  75. }
  76. return &z.buf[pos]
  77. }
  78. // Shift returns the first element and advances position.
  79. func (z *TokenBuffer) Shift() *Token {
  80. if z.pos >= len(z.buf) {
  81. t := &z.buf[:1][0]
  82. z.read(t)
  83. return t
  84. }
  85. t := &z.buf[z.pos]
  86. z.pos++
  87. return t
  88. }
  89. // Attributes extracts the gives attribute hashes from a tag.
  90. // It returns in the same order pointers to the requested token data or nil.
  91. func (z *TokenBuffer) Attributes(hashes ...svg.Hash) ([]*Token, *Token) {
  92. n := 0
  93. for {
  94. if t := z.Peek(n); t.TokenType != xml.AttributeToken {
  95. break
  96. }
  97. n++
  98. }
  99. if len(hashes) > cap(z.attrBuffer) {
  100. z.attrBuffer = make([]*Token, len(hashes))
  101. } else {
  102. z.attrBuffer = z.attrBuffer[:len(hashes)]
  103. for i := range z.attrBuffer {
  104. z.attrBuffer[i] = nil
  105. }
  106. }
  107. var replacee *Token
  108. for i := z.pos; i < z.pos+n; i++ {
  109. attr := &z.buf[i]
  110. for j, hash := range hashes {
  111. if hash == attr.Hash {
  112. z.attrBuffer[j] = attr
  113. replacee = attr
  114. }
  115. }
  116. }
  117. return z.attrBuffer, replacee
  118. }