profiles.go (1655B)
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 "unicode" 9 10 "golang.org/x/text/runes" 11 "golang.org/x/text/transform" 12 "golang.org/x/text/unicode/norm" 13 ) 14 15 var ( 16 // Implements the Nickname profile specified in RFC 8266. 17 Nickname *Profile = nickname 18 19 // Implements the UsernameCaseMapped profile specified in RFC 8265. 20 UsernameCaseMapped *Profile = usernameCaseMap 21 22 // Implements the UsernameCasePreserved profile specified in RFC 8265. 23 UsernameCasePreserved *Profile = usernameNoCaseMap 24 25 // Implements the OpaqueString profile defined in RFC 8265 for passwords and 26 // other secure labels. 27 OpaqueString *Profile = opaquestring 28 ) 29 30 var ( 31 nickname = &Profile{ 32 options: getOpts( 33 AdditionalMapping(func() transform.Transformer { 34 return &nickAdditionalMapping{} 35 }), 36 IgnoreCase, 37 Norm(norm.NFKC), 38 DisallowEmpty, 39 repeat, 40 ), 41 class: freeform, 42 } 43 usernameCaseMap = &Profile{ 44 options: getOpts( 45 FoldWidth, 46 LowerCase(), 47 Norm(norm.NFC), 48 BidiRule, 49 ), 50 class: identifier, 51 } 52 usernameNoCaseMap = &Profile{ 53 options: getOpts( 54 FoldWidth, 55 Norm(norm.NFC), 56 BidiRule, 57 ), 58 class: identifier, 59 } 60 opaquestring = &Profile{ 61 options: getOpts( 62 AdditionalMapping(func() transform.Transformer { 63 return mapSpaces 64 }), 65 Norm(norm.NFC), 66 DisallowEmpty, 67 ), 68 class: freeform, 69 } 70 ) 71 72 // mapSpaces is a shared value of a runes.Map transformer. 73 var mapSpaces transform.Transformer = runes.Map(func(r rune) rune { 74 if unicode.Is(unicode.Zs, r) { 75 return ' ' 76 } 77 return r 78 })