simplifySet reduces the size of the given set (either prefix or suffix). There is no need to pass around enormous prefix or suffix sets, since they will only be used to create trigrams. As they get too big, simplifySet moves the information they contain into the match query, which is more efficient
(s *stringSet)
| 664 | // moves the information they contain into the match query, which is |
| 665 | // more efficient to pass around. |
| 666 | func (info *regexpInfo) simplifySet(s *stringSet) { |
| 667 | t := *s |
| 668 | t.clean(s == &info.suffix) |
| 669 | |
| 670 | // Add the OR of the current prefix/suffix set to the query. |
| 671 | info.match = info.match.andTrigrams(t) |
| 672 | |
| 673 | for n := 3; n == 3 || t.size() > maxSet; n-- { |
| 674 | // Replace set by strings of length n-1. |
| 675 | w := 0 |
| 676 | for _, str := range t { |
| 677 | if len(str) >= n { |
| 678 | if s == &info.prefix { |
| 679 | str = str[:n-1] |
| 680 | } else { |
| 681 | str = str[len(str)-n+1:] |
| 682 | } |
| 683 | } |
| 684 | if w == 0 || t[w-1] != str { |
| 685 | t[w] = str |
| 686 | w++ |
| 687 | } |
| 688 | } |
| 689 | t = t[:w] |
| 690 | t.clean(s == &info.suffix) |
| 691 | } |
| 692 | |
| 693 | // Now make sure that the prefix/suffix sets aren't redundant. |
| 694 | // For example, if we know "ab" is a possible prefix, then it |
| 695 | // doesn't help at all to know that "abc" is also a possible |
| 696 | // prefix, so delete "abc". |
| 697 | w := 0 |
| 698 | f := strings.HasPrefix |
| 699 | if s == &info.suffix { |
| 700 | f = strings.HasSuffix |
| 701 | } |
| 702 | for _, str := range t { |
| 703 | if w == 0 || !f(str, t[w-1]) { |
| 704 | t[w] = str |
| 705 | w++ |
| 706 | } |
| 707 | } |
| 708 | t = t[:w] |
| 709 | |
| 710 | *s = t |
| 711 | } |
| 712 | |
| 713 | func (info regexpInfo) String() string { |
| 714 | s := "" |
no test coverage detected