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.
5 //go:generate go run ../collate/maketables.go -cldr=23 -unicode=6.2.0 -types=search,searchjl -package=search
7 // Package search provides language-specific search and string matching.
9 // Natural language matching can be intricate. For example, Danish will insist
10 // "Århus" and "Aarhus" are the same name and Turkish will match I to ı (note
11 // the lack of a dot) in a case-insensitive match. This package handles such
12 // language-specific details.
14 // Text passed to any of the calls in this message does not need to be
16 package search // import "golang.org/x/text/search"
21 "golang.org/x/text/internal/colltab"
22 "golang.org/x/text/language"
25 // An Option configures a Matcher.
26 type Option func(*Matcher)
29 // WholeWord restricts matches to complete words. The default is to match at
30 // the character level.
31 WholeWord Option = nil
33 // Exact requires that two strings are their exact equivalent. For example
34 // å would not match aa in Danish. It overrides any of the ignore options.
37 // Loose causes case, diacritics and width to be ignored.
40 // IgnoreCase enables case-insensitive search.
41 IgnoreCase Option = ignoreCase
43 // IgnoreDiacritics causes diacritics to be ignored ("ö" == "o").
44 IgnoreDiacritics Option = ignoreDiacritics
46 // IgnoreWidth equates narrow with wide variants.
47 IgnoreWidth Option = ignoreWidth
50 func ignoreDiacritics(m *Matcher) { m.ignoreDiacritics = true }
51 func ignoreCase(m *Matcher) { m.ignoreCase = true }
52 func ignoreWidth(m *Matcher) { m.ignoreWidth = true }
53 func loose(m *Matcher) {
60 // Supported lists the languages for which search differs from its parent.
61 Supported language.Coverage
67 ids := strings.Split(availableLocales, ",")
68 tags = make([]language.Tag, len(ids))
69 for i, s := range ids {
70 tags[i] = language.Raw.MustParse(s)
72 Supported = language.NewCoverage(tags)
75 // New returns a new Matcher for the given language and options.
76 func New(t language.Tag, opts ...Option) *Matcher {
78 w: getTable(locales[colltab.MatchLang(t, tags)]),
80 for _, f := range opts {
86 // A Matcher implements language-specific string matching.
94 // An IndexOption specifies how the Index methods of Pattern or Matcher should
99 // Anchor restricts the search to the start (or end for Backwards) of the
101 Anchor IndexOption = 1 << iota
103 // Backwards starts the search from the end of the text.
106 anchorBackwards = Anchor | Backwards
109 // Index reports the start and end position of the first occurrence of pat in b
110 // or -1, -1 if pat is not present.
111 func (m *Matcher) Index(b, pat []byte, opts ...IndexOption) (start, end int) {
112 // TODO: implement optimized version that does not use a pattern.
113 return m.Compile(pat).Index(b, opts...)
116 // IndexString reports the start and end position of the first occurrence of pat
117 // in s or -1, -1 if pat is not present.
118 func (m *Matcher) IndexString(s, pat string, opts ...IndexOption) (start, end int) {
119 // TODO: implement optimized version that does not use a pattern.
120 return m.CompileString(pat).IndexString(s, opts...)
123 // Equal reports whether a and b are equivalent.
124 func (m *Matcher) Equal(a, b []byte) bool {
125 _, end := m.Index(a, b, Anchor)
129 // EqualString reports whether a and b are equivalent.
130 func (m *Matcher) EqualString(a, b string) bool {
131 _, end := m.IndexString(a, b, Anchor)
135 // Compile compiles and returns a pattern that can be used for faster searching.
136 func (m *Matcher) Compile(b []byte) *Pattern {
138 iter := colltab.Iter{Weighter: m.w}
139 for iter.SetInput(b); iter.Next(); {
142 p.deleteEmptyElements()
146 // CompileString compiles and returns a pattern that can be used for faster
148 func (m *Matcher) CompileString(s string) *Pattern {
150 iter := colltab.Iter{Weighter: m.w}
151 for iter.SetInputString(s); iter.Next(); {
154 p.deleteEmptyElements()
158 // A Pattern is a compiled search string. It is safe for concurrent use.
159 type Pattern struct {
164 // Design note (TODO remove):
165 // The cost of retrieving collation elements for each rune, which is used for
166 // search as well, is not trivial. Also, algorithms like Boyer-Moore and
167 // Sunday require some additional precomputing.
169 // Index reports the start and end position of the first occurrence of p in b
170 // or -1, -1 if p is not present.
171 func (p *Pattern) Index(b []byte, opts ...IndexOption) (start, end int) {
172 // Pick a large enough buffer such that we likely do not need to allocate
173 // and small enough to not cause too much overhead initializing.
174 var buf [8]colltab.Elem
182 var optMask IndexOption
183 for _, o := range opts {
189 return p.forwardSearch(it)
191 return p.anchoredForwardSearch(it)
192 case Backwards, anchorBackwards:
193 panic("TODO: implement")
195 panic("unrecognized option")
199 // IndexString reports the start and end position of the first occurrence of p
200 // in s or -1, -1 if p is not present.
201 func (p *Pattern) IndexString(s string, opts ...IndexOption) (start, end int) {
202 // Pick a large enough buffer such that we likely do not need to allocate
203 // and small enough to not cause too much overhead initializing.
204 var buf [8]colltab.Elem
212 var optMask IndexOption
213 for _, o := range opts {
219 return p.forwardSearch(it)
221 return p.anchoredForwardSearch(it)
222 case Backwards, anchorBackwards:
223 panic("TODO: implement")
225 panic("unrecognized option")
230 // - Maybe IndexAll methods (probably not necessary).
231 // - Some way to match patterns in a Reader (a bit tricky).
232 // - Some fold transformer that folds text to comparable text, based on the
233 // search options. This is a common technique, though very different from the
234 // collation-based design of this package. It has a somewhat different use
235 // case, so probably makes sense to support both. Should probably be in a
236 // different package, though, as it uses completely different kind of tables
237 // (based on norm, cases, width and range tables.)