github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/golang/text/secure/precis/options.go (about) 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. 4 5 package precis 6 7 import ( 8 "github.com/insionng/yougam/libraries/x/text/cases" 9 "github.com/insionng/yougam/libraries/x/text/runes" 10 "github.com/insionng/yougam/libraries/x/text/transform" 11 "github.com/insionng/yougam/libraries/x/text/unicode/norm" 12 "github.com/insionng/yougam/libraries/x/text/width" 13 ) 14 15 // An Option is used to define the behavior and rules of a Profile. 16 type Option func(*options) 17 18 type options struct { 19 // Preparation options 20 allowwidechars bool 21 22 // Enforcement options 23 cases transform.Transformer 24 disallow runes.Set 25 norm norm.Form 26 additional []func() transform.Transformer 27 width *width.Transformer 28 disallowEmpty bool 29 30 // Comparison options 31 ignorecase bool 32 } 33 34 func getOpts(o ...Option) (res options) { 35 for _, f := range o { 36 f(&res) 37 } 38 return 39 } 40 41 var ( 42 // The IgnoreCase option causes the profile to perform a case insensitive 43 // comparison during the PRECIS comparison step. 44 IgnoreCase Option = ignoreCase 45 46 // The AllowWide option causes the profile to allow full-width and half-width 47 // characters by mapping them to their decomposition mappings. This is useful 48 // for profiles that are based on the identifier class which would otherwise 49 // disallow wide characters. 50 AllowWide Option = allowWide 51 52 // The DisallowEmpty option causes the enforcement step to return an error if 53 // the resulting string would be empty. 54 DisallowEmpty Option = disallowEmpty 55 ) 56 57 var ( 58 ignoreCase = func(o *options) { 59 o.ignorecase = true 60 } 61 allowWide = func(o *options) { 62 o.allowwidechars = true 63 } 64 disallowEmpty = func(o *options) { 65 o.disallowEmpty = true 66 } 67 ) 68 69 // The AdditionalMapping option defines the additional mapping rule for the 70 // Profile by applying Transformer's in sequence. 71 func AdditionalMapping(t ...func() transform.Transformer) Option { 72 return func(o *options) { 73 o.additional = t 74 } 75 } 76 77 // The Norm option defines a Profile's normalization rule. Defaults to NFC. 78 func Norm(f norm.Form) Option { 79 return func(o *options) { 80 o.norm = f 81 } 82 } 83 84 // The Width option defines a Profile's width mapping rule. 85 func Width(w width.Transformer) Option { 86 return func(o *options) { 87 o.width = &w 88 } 89 } 90 91 // The FoldCase option defines a Profile's case mapping rule. Options can be 92 // provided to determine the type of case folding used. 93 func FoldCase(opts ...cases.Option) Option { 94 return func(o *options) { 95 o.cases = cases.Fold(opts...) 96 } 97 } 98 99 // The Disallow option further restricts a Profile's allowed characters beyond 100 // what is disallowed by the underlying string class. 101 func Disallow(set runes.Set) Option { 102 return func(o *options) { 103 o.disallow = set 104 } 105 } 106 107 // TODO: Pending finalization of the unicode/bidi API 108 // // The Dir option defines a Profile's directionality mapping rule. Generally 109 // // profiles based on the Identifier string class will want to use the "Bidi 110 // // Rule" defined in RFC5893, and profiles based on the Freeform string class 111 // // will want to use the Unicode bidirectional algorithm defined in UAX9. 112 // func Dir() Option { 113 // panic("unimplemented") 114 // }