github.com/icodeface/tls@v0.0.0-20230910023335-34df9250cd12/internal/x/text/unicode/norm/transform.go (about)

     1  // Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
     2  
     3  // Copyright 2013 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  package norm
     8  
     9  import (
    10  	"unicode/utf8"
    11  
    12  	"github.com/icodeface/tls/internal/x/text/transform"
    13  )
    14  
    15  // Reset implements the Reset method of the transform.Transformer interface.
    16  func (Form) Reset() {}
    17  
    18  // Transform implements the Transform method of the transform.Transformer
    19  // interface. It may need to write segments of up to MaxSegmentSize at once.
    20  // Users should either catch ErrShortDst and allow dst to grow or have dst be at
    21  // least of size MaxTransformChunkSize to be guaranteed of progress.
    22  func (f Form) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
    23  	n := 0
    24  	// Cap the maximum number of src bytes to check.
    25  	b := src
    26  	eof := atEOF
    27  	if ns := len(dst); ns < len(b) {
    28  		err = transform.ErrShortDst
    29  		eof = false
    30  		b = b[:ns]
    31  	}
    32  	i, ok := formTable[f].quickSpan(inputBytes(b), n, len(b), eof)
    33  	n += copy(dst[n:], b[n:i])
    34  	if !ok {
    35  		nDst, nSrc, err = f.transform(dst[n:], src[n:], atEOF)
    36  		return nDst + n, nSrc + n, err
    37  	}
    38  	if n < len(src) && !atEOF {
    39  		err = transform.ErrShortSrc
    40  	}
    41  	return n, n, err
    42  }
    43  
    44  func flushTransform(rb *reorderBuffer) bool {
    45  	// Write out (must fully fit in dst, or else it is an ErrShortDst).
    46  	if len(rb.out) < rb.nrune*utf8.UTFMax {
    47  		return false
    48  	}
    49  	rb.out = rb.out[rb.flushCopy(rb.out):]
    50  	return true
    51  }
    52  
    53  var errs = []error{nil, transform.ErrShortDst, transform.ErrShortSrc}
    54  
    55  // transform implements the transform.Transformer interface. It is only called
    56  // when quickSpan does not pass for a given string.
    57  func (f Form) transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
    58  	// TODO: get rid of reorderBuffer. See CL 23460044.
    59  	rb := reorderBuffer{}
    60  	rb.init(f, src)
    61  	for {
    62  		// Load segment into reorder buffer.
    63  		rb.setFlusher(dst[nDst:], flushTransform)
    64  		end := decomposeSegment(&rb, nSrc, atEOF)
    65  		if end < 0 {
    66  			return nDst, nSrc, errs[-end]
    67  		}
    68  		nDst = len(dst) - len(rb.out)
    69  		nSrc = end
    70  
    71  		// Next quickSpan.
    72  		end = rb.nsrc
    73  		eof := atEOF
    74  		if n := nSrc + len(dst) - nDst; n < end {
    75  			err = transform.ErrShortDst
    76  			end = n
    77  			eof = false
    78  		}
    79  		end, ok := rb.f.quickSpan(rb.src, nSrc, end, eof)
    80  		n := copy(dst[nDst:], rb.src.bytes[nSrc:end])
    81  		nSrc += n
    82  		nDst += n
    83  		if ok {
    84  			if n < rb.nsrc && !atEOF {
    85  				err = transform.ErrShortSrc
    86  			}
    87  			return nDst, nSrc, err
    88  		}
    89  	}
    90  }