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.
10 "golang.org/x/text/transform"
13 // Note: below we pass invalid UTF-8 to the tIn and tNotIn transformers as is.
14 // This is done for various reasons:
15 // - To retain the semantics of the Nop transformer: if input is passed to a Nop
16 // one would expect it to be unchanged.
17 // - It would be very expensive to pass a converted RuneError to a transformer:
18 // a transformer might need more source bytes after RuneError, meaning that
19 // the only way to pass it safely is to create a new buffer and manage the
20 // intermingling of RuneErrors and normal input.
21 // - Many transformers leave ill-formed UTF-8 as is, so this is not
22 // inconsistent. Generally ill-formed UTF-8 is only replaced if it is a
23 // logical consequence of the operation (as for Map) or if it otherwise would
24 // pose security concerns (as for Remove).
25 // - An alternative would be to return an error on ill-formed UTF-8, but this
26 // would be inconsistent with other operations.
28 // If returns a transformer that applies tIn to consecutive runes for which
29 // s.Contains(r) and tNotIn to consecutive runes for which !s.Contains(r). Reset
30 // is called on tIn and tNotIn at the start of each run. A Nop transformer will
31 // substitute a nil value passed to tIn or tNotIn. Invalid UTF-8 is translated
32 // to RuneError to determine which transformer to apply, but is passed as is to
33 // the respective transformer.
34 func If(s Set, tIn, tNotIn transform.Transformer) Transformer {
35 if tIn == nil && tNotIn == nil {
36 return Transformer{transform.Nop}
42 tNotIn = transform.Nop
44 sIn, ok := tIn.(transform.SpanningTransformer)
48 sNotIn, ok := tNotIn.(transform.SpanningTransformer)
50 sNotIn = dummySpan{tNotIn}
62 type dummySpan struct{ transform.Transformer }
64 func (d dummySpan) Span(src []byte, atEOF bool) (n int, err error) {
65 return 0, transform.ErrEndOfSpan
69 tIn, tNotIn transform.SpanningTransformer
71 check func(rune) bool // current check to perform
72 t transform.SpanningTransformer // current transformer to use
75 // Reset implements transform.Transformer.
76 func (t *cond) Reset() {
79 t.t.Reset() // notIn will be reset on first usage.
82 func (t *cond) is(r rune) bool {
92 func (t *cond) isNot(r rune) bool {
102 // This implementation of Span doesn't help all too much, but it needs to be
103 // there to satisfy this package's Transformer interface.
104 // TODO: there are certainly room for improvements, though. For example, if
105 // t.t == transform.Nop (which will a common occurrence) it will save a bundle
106 // to special-case that loop.
107 func (t *cond) Span(src []byte, atEOF bool) (n int, err error) {
109 for n < len(src) && err == nil {
110 // Don't process too much at a time as the Spanner that will be
111 // called on this block may terminate early.
112 const maxChunk = 4096
114 if v := n + maxChunk; v < max {
120 for ; p < max; p += size {
122 if r < utf8.RuneSelf {
124 } else if r, size = utf8.DecodeRune(src[p:]); size == 1 {
125 if !atEOF && !utf8.FullRune(src[p:]) {
126 err = transform.ErrShortSrc
131 // The next rune will be the start of a new run.
136 n2, err2 := current.Span(src[n:p], atEnd || (atEOF && p == len(src)))
141 // At this point either err != nil or t.check will pass for the rune at p.
147 func (t *cond) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
149 for nSrc < len(src) && err == nil {
150 // Don't process too much at a time, as the work might be wasted if the
151 // destination buffer isn't large enough to hold the result or a
152 // transform returns an error early.
153 const maxChunk = 4096
155 if n := nSrc + maxChunk; n < len(src) {
161 for ; p < max; p += size {
163 if r < utf8.RuneSelf {
165 } else if r, size = utf8.DecodeRune(src[p:]); size == 1 {
166 if !atEOF && !utf8.FullRune(src[p:]) {
167 err = transform.ErrShortSrc
172 // The next rune will be the start of a new run.
177 nDst2, nSrc2, err2 := current.Transform(dst[nDst:], src[nSrc:p], atEnd || (atEOF && p == len(src)))
181 return nDst, nSrc, err2
183 // At this point either err != nil or t.check will pass for the rune at p.
186 return nDst, nSrc, err