github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/golang/text/secure/precis/class.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  	"unicode/utf8"
     9  )
    10  
    11  // TODO: Add contextual character rules from Appendix A of RFC5892.
    12  
    13  // A class is a set of characters that match certain derived properties. The
    14  // PRECIS framework defines two classes: The Freeform class and the Identifier
    15  // class. The freeform class should be used for profiles where expressiveness is
    16  // prioritized over safety such as nicknames or passwords. The identifier class
    17  // should be used for profiles where safety is the first priority such as
    18  // addressable network labels and usernames.
    19  type class struct {
    20  	extraAllowed    property
    21  	extraDisallowed property
    22  }
    23  
    24  // Contains satisfies the runes.Set interface and returns whether the given rune
    25  // is a member of the class.
    26  func (c class) Contains(r rune) bool {
    27  	b := make([]byte, 4)
    28  	n := utf8.EncodeRune(b, r)
    29  
    30  	trieval, _ := dpTrie.lookup(b[:n])
    31  	switch p := property(trieval); {
    32  	case p&c.extraDisallowed != 0:
    33  		return false
    34  	case p&c.extraAllowed != 0:
    35  		return true
    36  	case p&disallowed != 0, p&unassigned != 0:
    37  		return false
    38  	case p&pValid != 0:
    39  		return true
    40  	default:
    41  		return false
    42  	}
    43  }
    44  
    45  var (
    46  	identifier = &class{
    47  		extraDisallowed: idDis,
    48  	}
    49  	freeform = &class{
    50  		extraAllowed: freePVal,
    51  	}
    52  )