github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/golang/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 "github.com/insionng/yougam/libraries/x/text/cases"
     9  
    10  import (
    11  	"github.com/insionng/yougam/libraries/x/text/language"
    12  	"github.com/insionng/yougam/libraries/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. The returned Caser
    82  // is stateless and safe to use concurrently by multiple goroutines.
    83  //
    84  // Case folding does not normalize the input and may not preserve a normal form.
    85  // Use the collate or search package for more convenient and linguistically
    86  // sound comparisons.  Use unicode/precis for string comparisons where security
    87  // aspects are a concern.
    88  func Fold(opts ...Option) Caser {
    89  	return Caser{makeFold(getOpts(opts...))}
    90  }
    91  
    92  // An Option is used to modify the behavior of a Caser.
    93  type Option func(o *options)
    94  
    95  var (
    96  	// NoLower disables the lowercasing of non-leading letters for a title
    97  	// caser.
    98  	NoLower Option = noLower
    99  
   100  	// Compact omits mappings in case folding for characters that would grow the
   101  	// input. (Unimplemented.)
   102  	Compact Option = compact
   103  )
   104  
   105  // TODO: option to preserve a normal form, if applicable?
   106  
   107  type options struct {
   108  	noLower bool
   109  	simple  bool
   110  
   111  	// TODO: segmenter, max ignorable, alternative versions, etc.
   112  
   113  	noFinalSigma bool // Only used for testing.
   114  }
   115  
   116  func getOpts(o ...Option) (res options) {
   117  	for _, f := range o {
   118  		f(&res)
   119  	}
   120  	return
   121  }
   122  
   123  func noLower(o *options) {
   124  	o.noLower = true
   125  }
   126  
   127  func compact(o *options) {
   128  	o.simple = true
   129  }