The reconcile package is used for DOM reconcilation in Isomorphic Go web applications.

atom.go 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2012 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package atom provides integer codes (also known as atoms) for a fixed set of
  5. // frequently occurring HTML strings: tag names and attribute keys such as "p"
  6. // and "id".
  7. //
  8. // Sharing an atom's name between all elements with the same tag can result in
  9. // fewer string allocations when tokenizing and parsing HTML. Integer
  10. // comparisons are also generally faster than string comparisons.
  11. //
  12. // The value of an atom's particular code is not guaranteed to stay the same
  13. // between versions of this package. Neither is any ordering guaranteed:
  14. // whether atom.H1 < atom.H2 may also change. The codes are not guaranteed to
  15. // be dense. The only guarantees are that e.g. looking up "div" will yield
  16. // atom.Div, calling atom.Div.String will return "div", and atom.Div != 0.
  17. package atom // import "golang.org/x/net/html/atom"
  18. // Atom is an integer code for a string. The zero value maps to "".
  19. type Atom uint32
  20. // String returns the atom's name.
  21. func (a Atom) String() string {
  22. start := uint32(a >> 8)
  23. n := uint32(a & 0xff)
  24. if start+n > uint32(len(atomText)) {
  25. return ""
  26. }
  27. return atomText[start : start+n]
  28. }
  29. func (a Atom) string() string {
  30. return atomText[a>>8 : a>>8+a&0xff]
  31. }
  32. // fnv computes the FNV hash with an arbitrary starting value h.
  33. func fnv(h uint32, s []byte) uint32 {
  34. for i := range s {
  35. h ^= uint32(s[i])
  36. h *= 16777619
  37. }
  38. return h
  39. }
  40. func match(s string, t []byte) bool {
  41. for i, c := range t {
  42. if s[i] != c {
  43. return false
  44. }
  45. }
  46. return true
  47. }
  48. // Lookup returns the atom whose name is s. It returns zero if there is no
  49. // such atom. The lookup is case sensitive.
  50. func Lookup(s []byte) Atom {
  51. if len(s) == 0 || len(s) > maxAtomLen {
  52. return 0
  53. }
  54. h := fnv(hash0, s)
  55. if a := table[h&uint32(len(table)-1)]; int(a&0xff) == len(s) && match(a.string(), s) {
  56. return a
  57. }
  58. if a := table[(h>>16)&uint32(len(table)-1)]; int(a&0xff) == len(s) && match(a.string(), s) {
  59. return a
  60. }
  61. return 0
  62. }
  63. // String returns a string whose contents are equal to s. In that sense, it is
  64. // equivalent to string(s) but may be more efficient.
  65. func String(s []byte) string {
  66. if a := Lookup(s); a != 0 {
  67. return a.String()
  68. }
  69. return string(s)
  70. }