| 1 | // Copyright 2015 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 | |
| 5 | package searchscip-go gomod golang.org/x/text . `golang.org/x/text/search`/ |
| 6 | |
| 7 | import ( |
| 8 | "golang.org/x/text/internal/colltabscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/" |
| 9 | ) |
| 10 | |
| 11 | // TODO: handle variable primary weights? |
| 12 | |
| 13 | func (p *Patternscip-go gomod golang.org/x/text . `golang.org/x/text/search`/Pattern#) deleteEmptyElementsscip-go gomod golang.org/x/text . `golang.org/x/text/search`/Pattern#deleteEmptyElements().() { |
| 14 | k := 0 |
| 15 | for _, e := range p.cescip-go gomod golang.org/x/text . `golang.org/x/text/search`/Pattern#ce. { |
| 16 | if !isIgnorablescip-go gomod golang.org/x/text . `golang.org/x/text/search`/isIgnorable().(p.mscip-go gomod golang.org/x/text . `golang.org/x/text/search`/Pattern#m., e) { |
| 17 | p.cescip-go gomod golang.org/x/text . `golang.org/x/text/search`/Pattern#ce.[k] = e |
| 18 | k++ |
| 19 | } |
| 20 | } |
| 21 | p.cescip-go gomod golang.org/x/text . `golang.org/x/text/search`/Pattern#ce. = p.cescip-go gomod golang.org/x/text . `golang.org/x/text/search`/Pattern#ce.[:k] |
| 22 | } |
| 23 | |
| 24 | func isIgnorablescip-go gomod golang.org/x/text . `golang.org/x/text/search`/isIgnorable().(m *Matcherscip-go gomod golang.org/x/text . `golang.org/x/text/search`/Matcher#, e colltabscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/.Elemscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/Elem#) bool { |
| 25 | if e.Primaryscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/Elem#Primary().() > 0 { |
| 26 | return false |
| 27 | } |
| 28 | if e.Secondaryscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/Elem#Secondary().() > 0 { |
| 29 | if !m.ignoreDiacriticsscip-go gomod golang.org/x/text . `golang.org/x/text/search`/Matcher#ignoreDiacritics. { |
| 30 | return false |
| 31 | } |
| 32 | // Primary value is 0 and ignoreDiacritics is true. In this case we |
| 33 | // ignore the tertiary element, as it only pertains to the modifier. |
| 34 | return true |
| 35 | } |
| 36 | // TODO: further distinguish once we have the new implementation. |
| 37 | if !(m.ignoreWidthscip-go gomod golang.org/x/text . `golang.org/x/text/search`/Matcher#ignoreWidth. || m.ignoreCasescip-go gomod golang.org/x/text . `golang.org/x/text/search`/Matcher#ignoreCase.) && e.Tertiaryscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/Elem#Tertiary().() > 0 { |
| 38 | return false |
| 39 | } |
| 40 | // TODO: we ignore the Quaternary level for now. |
| 41 | return true |
| 42 | } |
| 43 | |
| 44 | // TODO: Use a Boyer-Moore-like algorithm (probably Sunday) for searching. |
| 45 | |
| 46 | func (p *Patternscip-go gomod golang.org/x/text . `golang.org/x/text/search`/Pattern#) forwardSearchscip-go gomod golang.org/x/text . `golang.org/x/text/search`/Pattern#forwardSearch().(it *colltabscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/.Iterscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/Iter#) (start, end int) { |
| 47 | for start := 0; it.Nextscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/Iter#Next().(); it.Resetscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/Iter#Reset().(start) { |
| 48 | nextStart := it.Endscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/Iter#End().() |
| 49 | if end := p.searchOncescip-go gomod golang.org/x/text . `golang.org/x/text/search`/Pattern#searchOnce().(it); end != -1 { |
| 50 | return start, end |
| 51 | } |
| 52 | start = nextStart |
| 53 | } |
| 54 | return -1, -1 |
| 55 | } |
| 56 | |
| 57 | func (p *Patternscip-go gomod golang.org/x/text . `golang.org/x/text/search`/Pattern#) anchoredForwardSearchscip-go gomod golang.org/x/text . `golang.org/x/text/search`/Pattern#anchoredForwardSearch().(it *colltabscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/.Iterscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/Iter#) (start, end int) { |
| 58 | if it.Nextscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/Iter#Next().() { |
| 59 | if end := p.searchOncescip-go gomod golang.org/x/text . `golang.org/x/text/search`/Pattern#searchOnce().(it); end != -1 { |
| 60 | return 0, end |
| 61 | } |
| 62 | } |
| 63 | return -1, -1 |
| 64 | } |
| 65 | |
| 66 | // next advances to the next weight in a pattern. f must return one of the |
| 67 | // weights of a collation element. next will advance to the first non-zero |
| 68 | // weight and return this weight and true if it exists, or 0, false otherwise. |
| 69 | func (p *Patternscip-go gomod golang.org/x/text . `golang.org/x/text/search`/Pattern#) nextscip-go gomod golang.org/x/text . `golang.org/x/text/search`/Pattern#next().(i *int, f func(colltabscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/.Elemscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/Elem#) int) (weight int, ok bool) { |
| 70 | for *i < len(p.cescip-go gomod golang.org/x/text . `golang.org/x/text/search`/Pattern#ce.) { |
| 71 | v := f(p.cescip-go gomod golang.org/x/text . `golang.org/x/text/search`/Pattern#ce.[*i]) |
| 72 | *i++ |
| 73 | if v != 0 { |
| 74 | // Skip successive ignorable values. |
| 75 | for ; *i < len(p.cescip-go gomod golang.org/x/text . `golang.org/x/text/search`/Pattern#ce.) && f(p.cescip-go gomod golang.org/x/text . `golang.org/x/text/search`/Pattern#ce.[*i]) == 0; *i++ { |
| 76 | } |
| 77 | return v, true |
| 78 | } |
| 79 | } |
| 80 | return 0, false |
| 81 | } |
| 82 | |
| 83 | // TODO: remove this function once Elem is internal and Tertiary returns int. |
| 84 | func tertiaryscip-go gomod golang.org/x/text . `golang.org/x/text/search`/tertiary().(e colltabscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/.Elemscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/Elem#) int { |
| 85 | return int(e.Tertiaryscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/Elem#Tertiary().()) |
| 86 | } |
| 87 | |
| 88 | // searchOnce tries to match the pattern s.p at the text position i. s.buf needs |
| 89 | // to be filled with collation elements of the first segment, where n is the |
| 90 | // number of source bytes consumed for this segment. It will return the end |
| 91 | // position of the match or -1. |
| 92 | func (p *Patternscip-go gomod golang.org/x/text . `golang.org/x/text/search`/Pattern#) searchOncescip-go gomod golang.org/x/text . `golang.org/x/text/search`/Pattern#searchOnce().(it *colltabscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/.Iterscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/Iter#) (end int) { |
| 93 | var pLevel [4]int |
| 94 | |
| 95 | m := p.mscip-go gomod golang.org/x/text . `golang.org/x/text/search`/Pattern#m. |
| 96 | for { |
| 97 | k := 0 |
| 98 | for ; k < it.Nscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/Iter#N.; k++ { |
| 99 | if v := it.Elemsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/Iter#Elems.[k].Primaryscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/Elem#Primary().(); v > 0 { |
| 100 | if w, ok := p.nextscip-go gomod golang.org/x/text . `golang.org/x/text/search`/Pattern#next().(&pLevel[0], colltabscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/.Elemscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/Elem#.Primaryscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/Elem#Primary().); !ok || v != w { |
| 101 | return -1 |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | if !m.ignoreDiacriticsscip-go gomod golang.org/x/text . `golang.org/x/text/search`/Matcher#ignoreDiacritics. { |
| 106 | if v := it.Elemsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/Iter#Elems.[k].Secondaryscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/Elem#Secondary().(); v > 0 { |
| 107 | if w, ok := p.nextscip-go gomod golang.org/x/text . `golang.org/x/text/search`/Pattern#next().(&pLevel[1], colltabscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/.Elemscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/Elem#.Secondaryscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/Elem#Secondary().); !ok || v != w { |
| 108 | return -1 |
| 109 | } |
| 110 | } |
| 111 | } else if it.Elemsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/Iter#Elems.[k].Primaryscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/Elem#Primary().() == 0 { |
| 112 | // We ignore tertiary values of collation elements of the |
| 113 | // secondary level. |
| 114 | continue |
| 115 | } |
| 116 | |
| 117 | // TODO: distinguish between case and width. This will be easier to |
| 118 | // implement after we moved to the new collation implementation. |
| 119 | if !m.ignoreWidthscip-go gomod golang.org/x/text . `golang.org/x/text/search`/Matcher#ignoreWidth. && !m.ignoreCasescip-go gomod golang.org/x/text . `golang.org/x/text/search`/Matcher#ignoreCase. { |
| 120 | if v := it.Elemsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/Iter#Elems.[k].Tertiaryscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/Elem#Tertiary().(); v > 0 { |
| 121 | if w, ok := p.nextscip-go gomod golang.org/x/text . `golang.org/x/text/search`/Pattern#next().(&pLevel[2], tertiaryscip-go gomod golang.org/x/text . `golang.org/x/text/search`/tertiary().); !ok || int(v) != w { |
| 122 | return -1 |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | // TODO: check quaternary weight |
| 127 | } |
| 128 | it.Discardscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/Iter#Discard().() // Remove the current segment from the buffer. |
| 129 | |
| 130 | // Check for completion. |
| 131 | switch { |
| 132 | // If any of these cases match, we are not at the end. |
| 133 | case pLevel[0] < len(p.cescip-go gomod golang.org/x/text . `golang.org/x/text/search`/Pattern#ce.): |
| 134 | case !m.ignoreDiacriticsscip-go gomod golang.org/x/text . `golang.org/x/text/search`/Matcher#ignoreDiacritics. && pLevel[1] < len(p.cescip-go gomod golang.org/x/text . `golang.org/x/text/search`/Pattern#ce.): |
| 135 | case !(m.ignoreWidthscip-go gomod golang.org/x/text . `golang.org/x/text/search`/Matcher#ignoreWidth. || m.ignoreCasescip-go gomod golang.org/x/text . `golang.org/x/text/search`/Matcher#ignoreCase.) && pLevel[2] < len(p.cescip-go gomod golang.org/x/text . `golang.org/x/text/search`/Pattern#ce.): |
| 136 | default: |
| 137 | // At this point, both the segment and pattern has matched fully. |
| 138 | // However, the segment may still be have trailing modifiers. |
| 139 | // This can be verified by another call to next. |
| 140 | end = it.Endscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/Iter#End().() |
| 141 | if it.Nextscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/Iter#Next().() && it.Elemsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/Iter#Elems.[0].Primaryscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/Elem#Primary().() == 0 { |
| 142 | if !m.ignoreDiacriticsscip-go gomod golang.org/x/text . `golang.org/x/text/search`/Matcher#ignoreDiacritics. { |
| 143 | return -1 |
| 144 | } |
| 145 | end = it.Endscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/Iter#End().() |
| 146 | } |
| 147 | return end |
| 148 | } |
| 149 | |
| 150 | // Fill the buffer with the next batch of collation elements. |
| 151 | if !it.Nextscip-go gomod golang.org/x/text . `golang.org/x/text/internal/colltab`/Iter#Next().() { |
| 152 | return -1 |
| 153 | } |
| 154 | } |
| 155 | } |