github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/text/cases/cases.go (about)

     1  // Copyright 2014 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  //go:generate go run gen.go gen_trieval.go
     6  
     7  // Package cases provides general and language-specific case mappers.
     8  package cases // import "golang.org/x/text/cases"
     9  
    10  import (
    11  	"golang.org/x/text/language"
    12  	"golang.org/x/text/transform"
    13  )
    14  
    15  // References:
    16  // - Unicode Reference Manual Chapter 3.13, 4.2, and 5.18.
    17  // - http://www.unicode.org/reports/tr29/
    18  // - http://www.unicode.org/Public/6.3.0/ucd/CaseFolding.txt
    19  // - http://www.unicode.org/Public/6.3.0/ucd/SpecialCasing.txt
    20  // - http://www.unicode.org/Public/6.3.0/ucd/DerivedCoreProperties.txt
    21  // - http://www.unicode.org/Public/6.3.0/ucd/auxiliary/WordBreakProperty.txt
    22  // - http://www.unicode.org/Public/6.3.0/ucd/auxiliary/WordBreakTest.txt
    23  // - http://userguide.icu-project.org/transforms/casemappings
    24  
    25  // TODO:
    26  // - Case folding
    27  // - Wide and Narrow?
    28  // - Segmenter option for title casing.
    29  // - ASCII fast paths
    30  // - Encode Soft-Dotted property within trie somehow.
    31  
    32  // A Caser transforms given input to a certain case. It implements
    33  // transform.Transformer.
    34  //
    35  // A Caser may be stateful and should therefore not be shared between
    36  // goroutines.
    37  type Caser struct {
    38  	t transform.Transformer
    39  }
    40  
    41  // Bytes returns a new byte slice with the result of converting b to the case
    42  // form implemented by c.
    43  func (c Caser) Bytes(b []byte) []byte {
    44  	b, _, _ = transform.Bytes(c.t, b)
    45  	return b
    46  }
    47  
    48  // String returns a string with the result of transforming s to the case form
    49  // implemented by c.
    50  func (c Caser) String(s string) string {
    51  	s, _, _ = transform.String(c.t, s)
    52  	return s
    53  }
    54  
    55  // Reset resets the Caser to be reused for new input after a previous call to
    56  // Transform.
    57  func (c Caser) Reset() { c.t.Reset() }
    58  
    59  // Transform implements the Transformer interface and transforms the given input
    60  // to the case form implemented by c.
    61  func (c Caser) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
    62  	return c.t.Transform(dst, src, atEOF)
    63  }
    64  
    65  // Upper returns a Caser for language-specific uppercasing.
    66  func Upper(t language.Tag, opts ...Option) Caser {
    67  	return Caser{makeUpper(t, getOpts(opts...))}
    68  }
    69  
    70  // Lower returns a Caser for language-specific lowercasing.
    71  func Lower(t language.Tag, opts ...Option) Caser {
    72  	return Caser{makeLower(t, getOpts(opts...))}
    73  }
    74  
    75  // Title returns a Caser for language-specific title casing. It uses an
    76  // approximation of the default Unicode Word Break algorithm.
    77  func Title(t language.Tag, opts ...Option) Caser {
    78  	return Caser{makeTitle(t, getOpts(opts...))}
    79  }
    80  
    81  // Fold returns a Caser that implements Unicode case folding.
    82  //
    83  // Folding is like mapping to lowercase without context-dependent mappings. Its
    84  // primary use is for caseless matching. Note that case folding does not
    85  // normalize the input and may not preserve a normal form.
    86  func Fold(t language.Tag, opts ...Option) Caser {
    87  	panic("TODO: implement")
    88  }
    89  
    90  // An Option is used to modify the behavior of a Caser.
    91  type Option func(o *options)
    92  
    93  var (
    94  	// NoLower disables the lowercasing of non-leading letters for a title
    95  	// caser.
    96  	NoLower Option = noLower
    97  
    98  	// Compact omits mappings in case folding for characters that would grow the
    99  	// input.
   100  	Compact Option = compact
   101  )
   102  
   103  // TODO: option to preserve a normal form, if applicable?
   104  
   105  type options struct {
   106  	noLower bool
   107  	simple  bool
   108  
   109  	// TODO: segmenter, max ignorable, alternative versions, etc.
   110  
   111  	noFinalSigma bool // Only used for testing.
   112  }
   113  
   114  func getOpts(o ...Option) (res options) {
   115  	for _, f := range o {
   116  		f(&res)
   117  	}
   118  	return
   119  }
   120  
   121  func noLower(o *options) {
   122  	o.noLower = true
   123  }
   124  
   125  func compact(o *options) {
   126  	o.simple = true
   127  }