clear.go 950 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // clear
  2. package util
  3. import (
  4. "strings"
  5. )
  6. var at = rune('&')
  7. var ed = rune(';')
  8. var lableMap = map[string]rune{
  9. "&": rune('&'),
  10. " ": rune(' '),
  11. ">": rune('>'),
  12. "&lt;": rune('<'),
  13. }
  14. //处理转义标签
  15. func CutLableStr(con string) string {
  16. for i := 0; i < 3; i++ {
  17. runes := []rune{}
  18. pools := []rune{}
  19. bpool := false
  20. strings.IndexFunc(con, func(s rune) bool {
  21. if !bpool && s == at {
  22. bpool = true
  23. pools = []rune{}
  24. }
  25. if bpool {
  26. pools = append(pools, s)
  27. if s == ed { //结束
  28. lb := lableMap[string(pools)]
  29. if lb != 0 {
  30. runes = append(runes, lb)
  31. } else {
  32. runes = append(runes, pools...)
  33. }
  34. bpool = false
  35. } else if len(pools) > 6 {
  36. bpool = false
  37. runes = append(runes, pools...)
  38. }
  39. } else {
  40. runes = append(runes, s)
  41. }
  42. return false
  43. })
  44. str1 := string(runes)
  45. if i > 0 && con == str1 {
  46. break
  47. }
  48. con = str1
  49. }
  50. return con
  51. }