本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
1.2KB

  1. package match
  2. // todo common table of rune's length
  3. import (
  4. "fmt"
  5. "strings"
  6. )
  7. const lenOne = 1
  8. const lenZero = 0
  9. const lenNo = -1
  10. type Matcher interface {
  11. Match(string) bool
  12. Index(string) (int, []int)
  13. Len() int
  14. String() string
  15. }
  16. type Matchers []Matcher
  17. func (m Matchers) String() string {
  18. var s []string
  19. for _, matcher := range m {
  20. s = append(s, fmt.Sprint(matcher))
  21. }
  22. return fmt.Sprintf("%s", strings.Join(s, ","))
  23. }
  24. // appendMerge merges and sorts given already SORTED and UNIQUE segments.
  25. func appendMerge(target, sub []int) []int {
  26. lt, ls := len(target), len(sub)
  27. out := make([]int, 0, lt+ls)
  28. for x, y := 0, 0; x < lt || y < ls; {
  29. if x >= lt {
  30. out = append(out, sub[y:]...)
  31. break
  32. }
  33. if y >= ls {
  34. out = append(out, target[x:]...)
  35. break
  36. }
  37. xValue := target[x]
  38. yValue := sub[y]
  39. switch {
  40. case xValue == yValue:
  41. out = append(out, xValue)
  42. x++
  43. y++
  44. case xValue < yValue:
  45. out = append(out, xValue)
  46. x++
  47. case yValue < xValue:
  48. out = append(out, yValue)
  49. y++
  50. }
  51. }
  52. target = append(target[:0], out...)
  53. return target
  54. }
  55. func reverseSegments(input []int) {
  56. l := len(input)
  57. m := l / 2
  58. for i := 0; i < m; i++ {
  59. input[i], input[l-i-1] = input[l-i-1], input[i]
  60. }
  61. }
上海开阖软件有限公司 沪ICP备12045867号-1