Add vendor folder to git

This commit is contained in:
Lucas Käldström 2017-06-26 19:23:05 +03:00
parent 66cf5eaafb
commit 183585f56f
No known key found for this signature in database
GPG key ID: 600FEFBBD0D40D21
6916 changed files with 2629581 additions and 1 deletions

290
vendor/golang.org/x/text/secure/bidirule/bidirule.go generated vendored Normal file
View file

@ -0,0 +1,290 @@
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package bidirule implements the Bidi Rule defined by RFC 5893.
//
// This package is under development. The API may change without notice and
// without preserving backward compatibility.
package bidirule
import (
"errors"
"unicode/utf8"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/bidi"
)
// This file contains an implementation of RFC 5893: Right-to-Left Scripts for
// Internationalized Domain Names for Applications (IDNA)
//
// A label is an individual component of a domain name. Labels are usually
// shown separated by dots; for example, the domain name "www.example.com" is
// composed of three labels: "www", "example", and "com".
//
// An RTL label is a label that contains at least one character of class R, AL,
// or AN. An LTR label is any label that is not an RTL label.
//
// A "Bidi domain name" is a domain name that contains at least one RTL label.
//
// The following guarantees can be made based on the above:
//
// o In a domain name consisting of only labels that satisfy the rule,
// the requirements of Section 3 are satisfied. Note that even LTR
// labels and pure ASCII labels have to be tested.
//
// o In a domain name consisting of only LDH labels (as defined in the
// Definitions document [RFC5890]) and labels that satisfy the rule,
// the requirements of Section 3 are satisfied as long as a label
// that starts with an ASCII digit does not come after a
// right-to-left label.
//
// No guarantee is given for other combinations.
// ErrInvalid indicates a label is invalid according to the Bidi Rule.
var ErrInvalid = errors.New("bidirule: failed Bidi Rule")
type ruleState uint8
const (
ruleInitial ruleState = iota
ruleLTR
ruleLTRFinal
ruleRTL
ruleRTLFinal
ruleInvalid
)
type ruleTransition struct {
next ruleState
mask uint16
}
var transitions = [...][2]ruleTransition{
// [2.1] The first character must be a character with Bidi property L, R, or
// AL. If it has the R or AL property, it is an RTL label; if it has the L
// property, it is an LTR label.
ruleInitial: {
{ruleLTRFinal, 1 << bidi.L},
{ruleRTLFinal, 1<<bidi.R | 1<<bidi.AL},
},
ruleRTL: {
// [2.3] In an RTL label, the end of the label must be a character with
// Bidi property R, AL, EN, or AN, followed by zero or more characters
// with Bidi property NSM.
{ruleRTLFinal, 1<<bidi.R | 1<<bidi.AL | 1<<bidi.EN | 1<<bidi.AN},
// [2.2] In an RTL label, only characters with the Bidi properties R,
// AL, AN, EN, ES, CS, ET, ON, BN, or NSM are allowed.
// We exclude the entries from [2.3]
{ruleRTL, 1<<bidi.ES | 1<<bidi.CS | 1<<bidi.ET | 1<<bidi.ON | 1<<bidi.BN | 1<<bidi.NSM},
},
ruleRTLFinal: {
// [2.3] In an RTL label, the end of the label must be a character with
// Bidi property R, AL, EN, or AN, followed by zero or more characters
// with Bidi property NSM.
{ruleRTLFinal, 1<<bidi.R | 1<<bidi.AL | 1<<bidi.EN | 1<<bidi.AN | 1<<bidi.NSM},
// [2.2] In an RTL label, only characters with the Bidi properties R,
// AL, AN, EN, ES, CS, ET, ON, BN, or NSM are allowed.
// We exclude the entries from [2.3] and NSM.
{ruleRTL, 1<<bidi.ES | 1<<bidi.CS | 1<<bidi.ET | 1<<bidi.ON | 1<<bidi.BN},
},
ruleLTR: {
// [2.6] In an LTR label, the end of the label must be a character with
// Bidi property L or EN, followed by zero or more characters with Bidi
// property NSM.
{ruleLTRFinal, 1<<bidi.L | 1<<bidi.EN},
// [2.5] In an LTR label, only characters with the Bidi properties L,
// EN, ES, CS, ET, ON, BN, or NSM are allowed.
// We exclude the entries from [2.6].
{ruleLTR, 1<<bidi.ES | 1<<bidi.CS | 1<<bidi.ET | 1<<bidi.ON | 1<<bidi.BN | 1<<bidi.NSM},
},
ruleLTRFinal: {
// [2.6] In an LTR label, the end of the label must be a character with
// Bidi property L or EN, followed by zero or more characters with Bidi
// property NSM.
{ruleLTRFinal, 1<<bidi.L | 1<<bidi.EN | 1<<bidi.NSM},
// [2.5] In an LTR label, only characters with the Bidi properties L,
// EN, ES, CS, ET, ON, BN, or NSM are allowed.
// We exclude the entries from [2.6].
{ruleLTR, 1<<bidi.ES | 1<<bidi.CS | 1<<bidi.ET | 1<<bidi.ON | 1<<bidi.BN},
},
ruleInvalid: {
{ruleInvalid, 0},
{ruleInvalid, 0},
},
}
// [2.4] In an RTL label, if an EN is present, no AN may be present, and
// vice versa.
const exclusiveRTL = uint16(1<<bidi.EN | 1<<bidi.AN)
// Direction reports the direction of the given label as defined by RFC 5893 or
// an error if b is not a valid label according to the Bidi Rule.
func Direction(b []byte) (bidi.Direction, error) {
t := Transformer{}
if n, ok := t.advance(b); ok && n == len(b) {
switch t.state {
case ruleLTRFinal, ruleInitial:
return bidi.LeftToRight, nil
case ruleRTLFinal:
return bidi.RightToLeft, nil
}
}
return bidi.Neutral, ErrInvalid
}
// DirectionString reports the direction of the given label as defined by RFC
// 5893 or an error if s is not a valid label according to the Bidi Rule.
func DirectionString(s string) (bidi.Direction, error) {
t := Transformer{}
if n, ok := t.advanceString(s); ok && n == len(s) {
switch t.state {
case ruleLTRFinal, ruleInitial:
return bidi.LeftToRight, nil
case ruleRTLFinal:
return bidi.RightToLeft, nil
}
}
return bidi.Neutral, ErrInvalid
}
// New returns a Transformer that verifies that input adheres to the Bidi Rule.
func New() *Transformer {
return &Transformer{}
}
// Transformer implements transform.Transform.
type Transformer struct {
state ruleState
seen uint16
}
// Reset implements transform.Transformer.
func (t *Transformer) Reset() { *t = Transformer{} }
// Transform implements transform.Transformer. This Transformer has state and
// needs to be reset between uses.
func (t *Transformer) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
if len(dst) < len(src) {
src = src[:len(dst)]
atEOF = false
err = transform.ErrShortDst
}
n, err1 := t.Span(src, atEOF)
copy(dst, src[:n])
if err == nil || err1 != nil && err1 != transform.ErrShortSrc {
err = err1
}
return n, n, err
}
// Span returns the first n bytes of src that conform to the Bidi rule.
func (t *Transformer) Span(src []byte, atEOF bool) (n int, err error) {
if t.state == ruleInvalid {
return 0, ErrInvalid
}
n, ok := t.advance(src)
switch {
case !ok:
err = ErrInvalid
case n < len(src):
if !atEOF {
err = transform.ErrShortSrc
break
}
err = ErrInvalid
case t.state != ruleLTRFinal && t.state != ruleRTLFinal && t.state != ruleInitial:
err = ErrInvalid
}
return n, err
}
// Precomputing the ASCII values decreases running time for the ASCII fast path
// by about 30%.
var asciiTable [128]bidi.Properties
func init() {
for i := range asciiTable {
p, _ := bidi.LookupRune(rune(i))
asciiTable[i] = p
}
}
func (t *Transformer) advance(s []byte) (n int, ok bool) {
var e bidi.Properties
var sz int
for n < len(s) {
if s[n] < utf8.RuneSelf {
e, sz = asciiTable[s[n]], 1
} else {
e, sz = bidi.Lookup(s[n:])
if sz <= 1 {
if sz == 1 {
return n, false // invalid UTF-8
}
return n, true // incomplete UTF-8 encoding
}
}
// TODO: using CompactClass results in noticeable speedup.
// See unicode/bidi/prop.go:Properties.CompactClass.
c := uint16(1 << e.Class())
t.seen |= c
if t.seen&exclusiveRTL == exclusiveRTL {
t.state = ruleInvalid
return n, false
}
switch tr := transitions[t.state]; {
case tr[0].mask&c != 0:
t.state = tr[0].next
case tr[1].mask&c != 0:
t.state = tr[1].next
default:
t.state = ruleInvalid
return n, false
}
n += sz
}
return n, true
}
func (t *Transformer) advanceString(s string) (n int, ok bool) {
var e bidi.Properties
var sz int
for n < len(s) {
if s[n] < utf8.RuneSelf {
e, sz = asciiTable[s[n]], 1
} else {
e, sz = bidi.LookupString(s[n:])
if sz <= 1 {
if sz == 1 {
return n, false // invalid UTF-8
}
return n, true // incomplete UTF-8 encoding
}
}
// TODO: using CompactClass results in noticeable speedup.
// See unicode/bidi/prop.go:Properties.CompactClass.
c := uint16(1 << e.Class())
t.seen |= c
if t.seen&exclusiveRTL == exclusiveRTL {
t.state = ruleInvalid
return n, false
}
switch tr := transitions[t.state]; {
case tr[0].mask&c != 0:
t.state = tr[0].next
case tr[1].mask&c != 0:
t.state = tr[1].next
default:
t.state = ruleInvalid
return n, false
}
n += sz
}
return n, true
}

View file

@ -0,0 +1,697 @@
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package bidirule
import (
"testing"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/bidi"
)
const (
strL = "ABC" // Left to right - most letters in LTR scripts
strR = "עברית" // Right to left - most letters in non-Arabic RTL scripts
strAL = "دبي" // Arabic letters - most letters in the Arabic script
strEN = "123" // European Number (0-9, and Extended Arabic-Indic numbers)
strES = "+-" // European Number Separator (+ and -)
strET = "$" // European Number Terminator (currency symbols, the hash sign, the percent sign and so on)
strAN = "\u0660" // Arabic Number; this encompasses the Arabic-Indic numbers, but not the Extended Arabic-Indic numbers
strCS = "," // Common Number Separator (. , / : et al)
strNSM = "\u0300" // Nonspacing Mark - most combining accents
strBN = "\u200d" // Boundary Neutral - control characters (ZWNJ, ZWJ, and others)
strB = "\u2029" // Paragraph Separator
strS = "\u0009" // Segment Separator
strWS = " " // Whitespace, including the SPACE character
strON = "@" // Other Neutrals, including @, &, parentheses, MIDDLE DOT
)
type ruleTest struct {
in string
dir bidi.Direction
n int // position at which the rule fails
err error
// For tests that split the string in two.
pSrc int // number of source bytes to consume first
szDst int // size of destination buffer
nSrc int // source bytes consumed and bytes written
err0 error // error after first run
}
var testCases = [][]ruleTest{
// Go-specific rules.
// Invalid UTF-8 is invalid.
0: []ruleTest{{
in: "",
dir: bidi.LeftToRight,
}, {
in: "\x80",
dir: bidi.Neutral,
err: ErrInvalid,
n: 0,
}, {
in: "\xcc",
dir: bidi.Neutral,
err: ErrInvalid,
n: 0,
}, {
in: "abc\x80",
dir: bidi.Neutral,
err: ErrInvalid,
n: 3,
}, {
in: "abc\xcc",
dir: bidi.Neutral,
err: ErrInvalid,
n: 3,
}, {
in: "abc\xccdef",
dir: bidi.Neutral,
err: ErrInvalid,
n: 3,
}, {
in: "\xccdef",
dir: bidi.Neutral,
err: ErrInvalid,
n: 0,
}, {
in: strR + "\x80",
dir: bidi.Neutral,
err: ErrInvalid,
n: len(strR),
}, {
in: strR + "\xcc",
dir: bidi.Neutral,
err: ErrInvalid,
n: len(strR),
}, {
in: strAL + "\xcc" + strR,
dir: bidi.Neutral,
err: ErrInvalid,
n: len(strAL),
}, {
in: "\xcc" + strR,
dir: bidi.Neutral,
err: ErrInvalid,
n: 0,
}},
// Rule 2.1: The first character must be a character with Bidi property L,
// R, or AL. If it has the R or AL property, it is an RTL label; if it has
// the L property, it is an LTR label.
1: []ruleTest{{
in: strL,
dir: bidi.LeftToRight,
}, {
in: strR,
dir: bidi.RightToLeft,
}, {
in: strAL,
dir: bidi.RightToLeft,
}, {
in: strEN,
dir: bidi.Neutral,
err: ErrInvalid,
}, {
in: strES,
dir: bidi.Neutral,
err: ErrInvalid,
}, {
in: strET,
dir: bidi.Neutral,
err: ErrInvalid,
}, {
in: strAN,
dir: bidi.Neutral,
err: ErrInvalid,
}, {
in: strCS,
dir: bidi.Neutral,
err: ErrInvalid,
}, {
in: strNSM,
dir: bidi.Neutral,
err: ErrInvalid,
}, {
in: strBN,
dir: bidi.Neutral,
err: ErrInvalid,
}, {
in: strB,
dir: bidi.Neutral,
err: ErrInvalid,
}, {
in: strS,
dir: bidi.Neutral,
err: ErrInvalid,
}, {
in: strWS,
dir: bidi.Neutral,
err: ErrInvalid,
}, {
in: strON,
dir: bidi.Neutral,
err: ErrInvalid,
}},
// Rule 2.2: In an RTL label, only characters with the Bidi properties R,
// AL, AN, EN, ES, CS, ET, ON, BN, or NSM are allowed.
2: []ruleTest{{
in: strR + strR + strAL,
dir: bidi.RightToLeft,
}, {
in: strR + strAL + strR,
dir: bidi.RightToLeft,
}, {
in: strR + strAN + strAL,
dir: bidi.RightToLeft,
}, {
in: strR + strEN + strR,
dir: bidi.RightToLeft,
}, {
in: strR + strES + strR,
dir: bidi.RightToLeft,
}, {
in: strR + strCS + strR,
dir: bidi.RightToLeft,
}, {
in: strR + strET + strAL,
dir: bidi.RightToLeft,
}, {
in: strR + strON + strR,
dir: bidi.RightToLeft,
}, {
in: strR + strBN + strR,
dir: bidi.RightToLeft,
}, {
in: strR + strNSM + strAL,
dir: bidi.RightToLeft,
}, {
in: strR + strL + strR,
dir: bidi.Neutral,
n: len(strR),
err: ErrInvalid,
}, {
in: strR + strB + strR,
dir: bidi.Neutral,
n: len(strR),
err: ErrInvalid,
}, {
in: strR + strS + strAL,
dir: bidi.Neutral,
n: len(strR),
err: ErrInvalid,
}, {
in: strR + strWS + strAL,
dir: bidi.Neutral,
n: len(strR),
err: ErrInvalid,
}, {
in: strAL + strR + strAL,
dir: bidi.RightToLeft,
}, {
in: strAL + strAL + strR,
dir: bidi.RightToLeft,
}, {
in: strAL + strAN + strAL,
dir: bidi.RightToLeft,
}, {
in: strAL + strEN + strR,
dir: bidi.RightToLeft,
}, {
in: strAL + strES + strR,
dir: bidi.RightToLeft,
}, {
in: strAL + strCS + strR,
dir: bidi.RightToLeft,
}, {
in: strAL + strET + strAL,
dir: bidi.RightToLeft,
}, {
in: strAL + strON + strR,
dir: bidi.RightToLeft,
}, {
in: strAL + strBN + strR,
dir: bidi.RightToLeft,
}, {
in: strAL + strNSM + strAL,
dir: bidi.RightToLeft,
}, {
in: strAL + strL + strR,
dir: bidi.Neutral,
n: len(strAL),
err: ErrInvalid,
}, {
in: strAL + strB + strR,
dir: bidi.Neutral,
n: len(strAL),
err: ErrInvalid,
}, {
in: strAL + strS + strAL,
dir: bidi.Neutral,
n: len(strAL),
err: ErrInvalid,
}, {
in: strAL + strWS + strAL,
dir: bidi.Neutral,
n: len(strAL),
err: ErrInvalid,
}},
// Rule 2.3: In an RTL label, the end of the label must be a character with
// Bidi property R, AL, EN, or AN, followed by zero or more characters with
// Bidi property NSM.
3: []ruleTest{{
in: strR + strNSM,
dir: bidi.RightToLeft,
}, {
in: strR + strR,
dir: bidi.RightToLeft,
}, {
in: strR + strAL + strNSM,
dir: bidi.RightToLeft,
}, {
in: strR + strEN + strNSM + strNSM,
dir: bidi.RightToLeft,
}, {
in: strR + strAN,
dir: bidi.RightToLeft,
}, {
in: strR + strES + strNSM,
dir: bidi.Neutral,
n: len(strR + strES + strNSM),
err: ErrInvalid,
}, {
in: strR + strCS + strNSM + strNSM,
dir: bidi.Neutral,
n: len(strR + strCS + strNSM + strNSM),
err: ErrInvalid,
}, {
in: strR + strET,
dir: bidi.Neutral,
n: len(strR + strET),
err: ErrInvalid,
}, {
in: strR + strON + strNSM,
dir: bidi.Neutral,
n: len(strR + strON + strNSM),
err: ErrInvalid,
}, {
in: strR + strBN + strNSM + strNSM,
dir: bidi.Neutral,
n: len(strR + strBN + strNSM + strNSM),
err: ErrInvalid,
}, {
in: strR + strL + strNSM,
dir: bidi.Neutral,
n: len(strR),
err: ErrInvalid,
}, {
in: strR + strB + strNSM + strNSM,
dir: bidi.Neutral,
n: len(strR),
err: ErrInvalid,
}, {
in: strR + strS,
dir: bidi.Neutral,
n: len(strR),
err: ErrInvalid,
}, {
in: strR + strWS,
dir: bidi.Neutral,
n: len(strR),
err: ErrInvalid,
}, {
in: strAL + strNSM,
dir: bidi.RightToLeft,
}, {
in: strAL + strR,
dir: bidi.RightToLeft,
}, {
in: strAL + strAL + strNSM,
dir: bidi.RightToLeft,
}, {
in: strAL + strEN + strNSM + strNSM,
dir: bidi.RightToLeft,
}, {
in: strAL + strAN,
dir: bidi.RightToLeft,
}, {
in: strAL + strES + strNSM,
dir: bidi.Neutral,
n: len(strAL + strES + strNSM),
err: ErrInvalid,
}, {
in: strAL + strCS + strNSM + strNSM,
dir: bidi.Neutral,
n: len(strAL + strCS + strNSM + strNSM),
err: ErrInvalid,
}, {
in: strAL + strET,
dir: bidi.Neutral,
n: len(strAL + strET),
err: ErrInvalid,
}, {
in: strAL + strON + strNSM,
dir: bidi.Neutral,
n: len(strAL + strON + strNSM),
err: ErrInvalid,
}, {
in: strAL + strBN + strNSM + strNSM,
dir: bidi.Neutral,
n: len(strAL + strBN + strNSM + strNSM),
err: ErrInvalid,
}, {
in: strAL + strL + strNSM,
dir: bidi.Neutral,
n: len(strAL),
err: ErrInvalid,
}, {
in: strAL + strB + strNSM + strNSM,
dir: bidi.Neutral,
n: len(strAL),
err: ErrInvalid,
}, {
in: strAL + strS,
dir: bidi.Neutral,
n: len(strAL),
err: ErrInvalid,
}, {
in: strAL + strWS,
dir: bidi.Neutral,
n: len(strAL),
err: ErrInvalid,
}},
// Rule 2.4: In an RTL label, if an EN is present, no AN may be present,
// and vice versa.
4: []ruleTest{{
in: strR + strEN + strAN,
dir: bidi.Neutral,
n: len(strR + strEN),
err: ErrInvalid,
}, {
in: strR + strAN + strEN + strNSM,
dir: bidi.Neutral,
n: len(strR + strAN),
err: ErrInvalid,
}, {
in: strAL + strEN + strAN,
dir: bidi.Neutral,
n: len(strAL + strEN),
err: ErrInvalid,
}, {
in: strAL + strAN + strEN + strNSM,
dir: bidi.Neutral,
n: len(strAL + strAN),
err: ErrInvalid,
}},
// Rule 2.5: In an LTR label, only characters with the Bidi properties L,
// EN, ES, CS, ET, ON, BN, or NSM are allowed.
5: []ruleTest{{
in: strL + strL + strL,
dir: bidi.LeftToRight,
}, {
in: strL + strEN + strL,
dir: bidi.LeftToRight,
}, {
in: strL + strES + strL,
dir: bidi.LeftToRight,
}, {
in: strL + strCS + strL,
dir: bidi.LeftToRight,
}, {
in: strL + strET + strL,
dir: bidi.LeftToRight,
}, {
in: strL + strON + strL,
dir: bidi.LeftToRight,
}, {
in: strL + strBN + strL,
dir: bidi.LeftToRight,
}, {
in: strL + strNSM + strL,
dir: bidi.LeftToRight,
}, {
in: strL + strR + strL,
dir: bidi.Neutral,
n: len(strL),
err: ErrInvalid,
}, {
in: strL + strAL + strL,
dir: bidi.Neutral,
n: len(strL),
err: ErrInvalid,
}, {
in: strL + strAN + strL,
dir: bidi.Neutral,
n: len(strL),
err: ErrInvalid,
}, {
in: strL + strB + strL,
dir: bidi.Neutral,
n: len(strL),
err: ErrInvalid,
}, {
in: strL + strS + strL,
dir: bidi.Neutral,
n: len(strL),
err: ErrInvalid,
}, {
in: strL + strWS + strL,
dir: bidi.Neutral,
n: len(strL),
err: ErrInvalid,
}},
// Rule 2.6: In an LTR label, the end of the label must be a character with
// Bidi property L or EN, followed by zero or more characters with Bidi
// property NSM.
6: []ruleTest{{
in: strL,
dir: bidi.LeftToRight,
}, {
in: strL + strNSM,
dir: bidi.LeftToRight,
}, {
in: strL + strNSM + strNSM,
dir: bidi.LeftToRight,
}, {
in: strL + strEN,
dir: bidi.LeftToRight,
}, {
in: strL + strEN + strNSM,
dir: bidi.LeftToRight,
}, {
in: strL + strEN + strNSM + strNSM,
dir: bidi.LeftToRight,
}, {
in: strL + strES,
dir: bidi.Neutral,
n: len(strL + strES),
err: ErrInvalid,
}, {
in: strL + strCS,
dir: bidi.Neutral,
n: len(strL + strCS),
err: ErrInvalid,
}, {
in: strL + strET,
dir: bidi.Neutral,
n: len(strL + strET),
err: ErrInvalid,
}, {
in: strL + strON,
dir: bidi.Neutral,
n: len(strL + strON),
err: ErrInvalid,
}, {
in: strL + strBN,
dir: bidi.Neutral,
n: len(strL + strBN),
err: ErrInvalid,
}, {
in: strL + strR,
dir: bidi.Neutral,
n: len(strL),
err: ErrInvalid,
}, {
in: strL + strAL,
dir: bidi.Neutral,
n: len(strL),
err: ErrInvalid,
}, {
in: strL + strAN,
dir: bidi.Neutral,
n: len(strL),
err: ErrInvalid,
}, {
in: strL + strB,
dir: bidi.Neutral,
n: len(strL),
err: ErrInvalid,
}, {
in: strL + strS,
dir: bidi.Neutral,
n: len(strL),
err: ErrInvalid,
}, {
in: strL + strWS,
dir: bidi.Neutral,
n: len(strL),
err: ErrInvalid,
}},
// Incremental processing.
9: []ruleTest{{
in: "e\u0301", // é
dir: bidi.LeftToRight,
pSrc: 2,
nSrc: 1,
err0: transform.ErrShortSrc,
}, {
in: "e\u1000f", // é
dir: bidi.LeftToRight,
pSrc: 3,
nSrc: 1,
err0: transform.ErrShortSrc,
}, {
// Remain invalid once invalid.
in: strR + "ab",
dir: bidi.Neutral,
n: len(strR),
err: ErrInvalid,
pSrc: len(strR) + 1,
nSrc: len(strR),
err0: ErrInvalid,
}, {
// Short destination
in: "abcdefghij",
dir: bidi.LeftToRight,
pSrc: 10,
szDst: 5,
nSrc: 5,
err0: transform.ErrShortDst,
}, {
// Short destination splitting input rune
in: "e\u0301",
dir: bidi.LeftToRight,
pSrc: 3,
szDst: 2,
nSrc: 1,
err0: transform.ErrShortDst,
}},
}
func init() {
for rule, cases := range testCases {
for i, tc := range cases {
if tc.err == nil {
testCases[rule][i].n = len(tc.in)
}
}
}
}
func TestDirection(t *testing.T) {
doTests(t, func(t *testing.T, tc ruleTest) {
dir, err := Direction([]byte(tc.in))
if err != tc.err {
t.Errorf("error was %v; want %v", err, tc.err)
}
if dir != tc.dir {
t.Errorf("dir was %v; want %v", dir, tc.dir)
}
})
}
func TestDirectionString(t *testing.T) {
doTests(t, func(t *testing.T, tc ruleTest) {
dir, err := DirectionString(tc.in)
if err != tc.err {
t.Errorf("error was %v; want %v", err, tc.err)
}
if dir != tc.dir {
t.Errorf("dir was %v; want %v", dir, tc.dir)
}
})
}
func TestSpan(t *testing.T) {
doTests(t, func(t *testing.T, tc ruleTest) {
// Skip tests that test for limited destination buffer size.
if tc.szDst > 0 {
return
}
r := New()
src := []byte(tc.in)
n, err := r.Span(src[:tc.pSrc], tc.pSrc == len(tc.in))
if err != tc.err0 {
t.Errorf("err0 was %v; want %v", err, tc.err0)
}
if n != tc.nSrc {
t.Fatalf("nSrc was %d; want %d", n, tc.nSrc)
}
n, err = r.Span(src[n:], true)
if err != tc.err {
t.Errorf("error was %v; want %v", err, tc.err)
}
if got := n + tc.nSrc; got != tc.n {
t.Errorf("n was %d; want %d", got, tc.n)
}
})
}
func TestTransform(t *testing.T) {
doTests(t, func(t *testing.T, tc ruleTest) {
r := New()
src := []byte(tc.in)
dst := make([]byte, len(tc.in))
if tc.szDst > 0 {
dst = make([]byte, tc.szDst)
}
// First transform operates on a zero-length string for most tests.
nDst, nSrc, err := r.Transform(dst, src[:tc.pSrc], tc.pSrc == len(tc.in))
if err != tc.err0 {
t.Errorf("err0 was %v; want %v", err, tc.err0)
}
if nDst != nSrc {
t.Fatalf("nDst (%d) and nSrc (%d) should match", nDst, nSrc)
}
if nSrc != tc.nSrc {
t.Fatalf("nSrc was %d; want %d", nSrc, tc.nSrc)
}
dst1 := make([]byte, len(tc.in))
copy(dst1, dst[:nDst])
nDst, nSrc, err = r.Transform(dst1[nDst:], src[nSrc:], true)
if err != tc.err {
t.Errorf("error was %v; want %v", err, tc.err)
}
if nDst != nSrc {
t.Fatalf("nDst (%d) and nSrc (%d) should match", nDst, nSrc)
}
n := nSrc + tc.nSrc
if n != tc.n {
t.Fatalf("n was %d; want %d", n, tc.n)
}
if got, want := string(dst1[:n]), tc.in[:tc.n]; got != want {
t.Errorf("got %+q; want %+q", got, want)
}
})
}

24
vendor/golang.org/x/text/secure/bidirule/go1_6_test.go generated vendored Normal file
View file

@ -0,0 +1,24 @@
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build !go1.7
package bidirule
import (
"fmt"
"testing"
)
// doTests runs all tests without using t.Run. As a result, context may be
// missing, but at least all tests are run.
func doTests(t *testing.T, fn func(t *testing.T, tc ruleTest)) {
for rule, cases := range testCases {
for i, tc := range cases {
name := fmt.Sprintf("%d/%d:%+q:%s", rule, i, tc.in, tc.in)
t.Log("Testing ", name)
fn(t, tc)
}
}
}

66
vendor/golang.org/x/text/secure/bidirule/go1_7_test.go generated vendored Normal file
View file

@ -0,0 +1,66 @@
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build go1.7
package bidirule
import (
"fmt"
"testing"
)
func doTests(t *testing.T, fn func(t *testing.T, tc ruleTest)) {
for rule, cases := range testCases {
for i, tc := range cases {
name := fmt.Sprintf("%d/%d:%+q:%s", rule, i, tc.in, tc.in)
t.Run(name, func(t *testing.T) {
fn(t, tc)
})
}
}
}
var benchData = []struct{ name, data string }{
{"ascii", "Scheveningen"},
{"arabic", "دبي"},
{"hangul", "다음과"},
}
func doBench(b *testing.B, fn func(b *testing.B, data string)) {
for _, d := range benchData {
b.Run(d.name, func(b *testing.B) { fn(b, d.data) })
}
}
func BenchmarkSpan(b *testing.B) {
r := New()
doBench(b, func(b *testing.B, str string) {
b.SetBytes(int64(len(str)))
data := []byte(str)
for i := 0; i < b.N; i++ {
r.Reset()
r.Span(data, true)
}
})
}
func BenchmarkDirectionASCII(b *testing.B) {
doBench(b, func(b *testing.B, str string) {
b.SetBytes(int64(len(str)))
data := []byte(str)
for i := 0; i < b.N; i++ {
Direction(data)
}
})
}
func BenchmarkDirectionStringASCII(b *testing.B) {
doBench(b, func(b *testing.B, str string) {
b.SetBytes(int64(len(str)))
for i := 0; i < b.N; i++ {
DirectionString(str)
}
})
}

6
vendor/golang.org/x/text/secure/doc.go generated vendored Normal file
View file

@ -0,0 +1,6 @@
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// secure is a repository of text security related packages.
package secure // import "golang.org/x/text/secure"

View file

@ -0,0 +1,78 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build go1.7
package precis
import "testing"
var benchData = []struct{ name, str string }{
{"ASCII", "Malvolio"},
{"NotNormalized", "abcdefg\u0301\u031f"},
{"Arabic", "دبي"},
{"Hangul", "동일조건변경허락"},
}
var benchProfiles = []struct {
name string
p *Profile
}{
{"FreeForm", NewFreeform()},
{"Nickname", Nickname},
{"OpaqueString", OpaqueString},
{"UsernameCaseMapped", UsernameCaseMapped},
{"UsernameCasePreserved", UsernameCasePreserved},
}
func doBench(b *testing.B, f func(b *testing.B, p *Profile, s string)) {
for _, bp := range benchProfiles {
for _, d := range benchData {
b.Run(bp.name+"/"+d.name, func(b *testing.B) {
f(b, bp.p, d.str)
})
}
}
}
func BenchmarkString(b *testing.B) {
doBench(b, func(b *testing.B, p *Profile, s string) {
for i := 0; i < b.N; i++ {
p.String(s)
}
})
}
func BenchmarkBytes(b *testing.B) {
doBench(b, func(b *testing.B, p *Profile, s string) {
src := []byte(s)
b.ResetTimer()
for i := 0; i < b.N; i++ {
p.Bytes(src)
}
})
}
func BenchmarkAppend(b *testing.B) {
doBench(b, func(b *testing.B, p *Profile, s string) {
src := []byte(s)
dst := make([]byte, 0, 4096)
b.ResetTimer()
for i := 0; i < b.N; i++ {
p.Append(dst, src)
}
})
}
func BenchmarkTransform(b *testing.B) {
doBench(b, func(b *testing.B, p *Profile, s string) {
src := []byte(s)
dst := make([]byte, 2*len(s))
t := p.NewTransformer()
b.ResetTimer()
for i := 0; i < b.N; i++ {
t.Transform(dst, src, true)
}
})
}

36
vendor/golang.org/x/text/secure/precis/class.go generated vendored Normal file
View file

@ -0,0 +1,36 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package precis
import (
"unicode/utf8"
)
// TODO: Add contextual character rules from Appendix A of RFC5892.
// A class is a set of characters that match certain derived properties. The
// PRECIS framework defines two classes: The Freeform class and the Identifier
// class. The freeform class should be used for profiles where expressiveness is
// prioritized over safety such as nicknames or passwords. The identifier class
// should be used for profiles where safety is the first priority such as
// addressable network labels and usernames.
type class struct {
validFrom property
}
// Contains satisfies the runes.Set interface and returns whether the given rune
// is a member of the class.
func (c class) Contains(r rune) bool {
b := make([]byte, 4)
n := utf8.EncodeRune(b, r)
trieval, _ := dpTrie.lookup(b[:n])
return c.validFrom <= property(trieval)
}
var (
identifier = &class{validFrom: pValid}
freeform = &class{validFrom: idDisOrFreePVal}
)

50
vendor/golang.org/x/text/secure/precis/class_test.go generated vendored Normal file
View file

@ -0,0 +1,50 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package precis
import (
"testing"
"golang.org/x/text/runes"
)
// Compile-time regression test to ensure that Class is a Set
var _ runes.Set = (*class)(nil)
// Ensure that certain characters are (or are not) in the identifer class.
func TestClassContains(t *testing.T) {
tests := []struct {
name string
class *class
allowed []rune
disallowed []rune
}{
{
name: "Identifier",
class: identifier,
allowed: []rune("Aa0\u0021\u007e\u00df\u3007"),
disallowed: []rune("\u2150\u2100\u2200\u3164\u2190\u2600\u303b\u1e9b"),
},
{
name: "Freeform",
class: freeform,
allowed: []rune("Aa0\u0021\u007e\u00df\u3007 \u2150\u2100\u2200\u2190\u2600\u1e9b"),
disallowed: []rune("\u3164\u303b"),
},
}
for _, rt := range tests {
for _, r := range rt.allowed {
if !rt.class.Contains(r) {
t.Errorf("Class %s should contain %U", rt.name, r)
}
}
for _, r := range rt.disallowed {
if rt.class.Contains(r) {
t.Errorf("Class %s should not contain %U", rt.name, r)
}
}
}
}

139
vendor/golang.org/x/text/secure/precis/context.go generated vendored Normal file
View file

@ -0,0 +1,139 @@
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package precis
import "errors"
// This file contains tables and code related to context rules.
type catBitmap uint16
const (
// These bits, once set depending on the current value, are never unset.
bJapanese catBitmap = 1 << iota
bArabicIndicDigit
bExtendedArabicIndicDigit
// These bits are set on each iteration depending on the current value.
bJoinStart
bJoinMid
bJoinEnd
bVirama
bLatinSmallL
bGreek
bHebrew
// These bits indicated which of the permanent bits need to be set at the
// end of the checks.
bMustHaveJapn
permanent = bJapanese | bArabicIndicDigit | bExtendedArabicIndicDigit | bMustHaveJapn
)
const finalShift = 10
var errContext = errors.New("precis: contextual rule violated")
func init() {
// Programmatically set these required bits as, manually setting them seems
// too error prone.
for i, ct := range categoryTransitions {
categoryTransitions[i].keep |= permanent
categoryTransitions[i].accept |= ct.term
}
}
var categoryTransitions = []struct {
keep catBitmap // mask selecting which bits to keep from the previous state
set catBitmap // mask for which bits to set for this transition
// These bitmaps are used for rules that require lookahead.
// term&accept == term must be true, which is enforced programmatically.
term catBitmap // bits accepted as termination condition
accept catBitmap // bits that pass, but not sufficient as termination
// The rule function cannot take a *context as an argument, as it would
// cause the context to escape, adding significant overhead.
rule func(beforeBits catBitmap) (doLookahead bool, err error)
}{
joiningL: {set: bJoinStart},
joiningD: {set: bJoinStart | bJoinEnd},
joiningT: {keep: bJoinStart, set: bJoinMid},
joiningR: {set: bJoinEnd},
viramaModifier: {set: bVirama},
viramaJoinT: {set: bVirama | bJoinMid},
latinSmallL: {set: bLatinSmallL},
greek: {set: bGreek},
greekJoinT: {set: bGreek | bJoinMid},
hebrew: {set: bHebrew},
hebrewJoinT: {set: bHebrew | bJoinMid},
japanese: {set: bJapanese},
katakanaMiddleDot: {set: bMustHaveJapn},
zeroWidthNonJoiner: {
term: bJoinEnd,
accept: bJoinMid,
rule: func(before catBitmap) (doLookAhead bool, err error) {
if before&bVirama != 0 {
return false, nil
}
if before&bJoinStart == 0 {
return false, errContext
}
return true, nil
},
},
zeroWidthJoiner: {
rule: func(before catBitmap) (doLookAhead bool, err error) {
if before&bVirama == 0 {
err = errContext
}
return false, err
},
},
middleDot: {
term: bLatinSmallL,
rule: func(before catBitmap) (doLookAhead bool, err error) {
if before&bLatinSmallL == 0 {
return false, errContext
}
return true, nil
},
},
greekLowerNumeralSign: {
set: bGreek,
term: bGreek,
rule: func(before catBitmap) (doLookAhead bool, err error) {
return true, nil
},
},
hebrewPreceding: {
set: bHebrew,
rule: func(before catBitmap) (doLookAhead bool, err error) {
if before&bHebrew == 0 {
err = errContext
}
return false, err
},
},
arabicIndicDigit: {
set: bArabicIndicDigit,
rule: func(before catBitmap) (doLookAhead bool, err error) {
if before&bExtendedArabicIndicDigit != 0 {
err = errContext
}
return false, err
},
},
extendedArabicIndicDigit: {
set: bExtendedArabicIndicDigit,
rule: func(before catBitmap) (doLookAhead bool, err error) {
if before&bArabicIndicDigit != 0 {
err = errContext
}
return false, err
},
},
}

14
vendor/golang.org/x/text/secure/precis/doc.go generated vendored Normal file
View file

@ -0,0 +1,14 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package precis contains types and functions for the preparation,
// enforcement, and comparison of internationalized strings ("PRECIS") as
// defined in RFC 7564. It also contains several pre-defined profiles for
// passwords, nicknames, and usernames as defined in RFC 7613 and RFC 7700.
//
// BE ADVISED: This package is under construction and the API may change in
// backwards incompatible ways and without notice.
package precis // import "golang.org/x/text/secure/precis"
//go:generate go run gen.go gen_trieval.go

280
vendor/golang.org/x/text/secure/precis/enforce_test.go generated vendored Normal file
View file

@ -0,0 +1,280 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package precis
import (
"reflect"
"testing"
"golang.org/x/text/secure/bidirule"
)
type testCase struct {
input string
output string
err error
}
var testCases = []struct {
name string
p *Profile
cases []testCase
}{
{"Basic", NewFreeform(), []testCase{
{"e\u0301\u031f", "\u00e9\u031f", nil}, // normalize
}},
{"Context Rule 1", NewFreeform(), []testCase{
// Rule 1: zero-width non-joiner (U+200C)
// From RFC:
// False
// If Canonical_Combining_Class(Before(cp)) .eq. Virama Then True;
// If RegExpMatch((Joining_Type:{L,D})(Joining_Type:T)*\u200C
// (Joining_Type:T)*(Joining_Type:{R,D})) Then True;
//
// Example runes for different joining types:
// Join L: U+A872; PHAGS-PA SUPERFIXED LETTER RA
// Join D: U+062C; HAH WITH DOT BELOW
// Join T: U+0610; ARABIC SIGN SALLALLAHOU ALAYHE WASSALLAM
// Join R: U+0627; ALEF
// Virama: U+0A4D; GURMUKHI SIGN VIRAMA
// Virama and Join T: U+0ACD; GUJARATI SIGN VIRAMA
{"\u200c", "", errContext},
{"\u200ca", "", errContext},
{"a\u200c", "", errContext},
{"\u200c\u0627", "", errContext}, // missing JoinStart
{"\u062c\u200c", "", errContext}, // missing JoinEnd
{"\u0610\u200c\u0610\u0627", "", errContext}, // missing JoinStart
{"\u062c\u0610\u200c\u0610", "", errContext}, // missing JoinEnd
// Variants of: D T* U+200c T* R
{"\u062c\u200c\u0627", "\u062c\u200c\u0627", nil},
{"\u062c\u0610\u200c\u0610\u0627", "\u062c\u0610\u200c\u0610\u0627", nil},
{"\u062c\u0610\u0610\u200c\u0610\u0610\u0627", "\u062c\u0610\u0610\u200c\u0610\u0610\u0627", nil},
{"\u062c\u0610\u200c\u0627", "\u062c\u0610\u200c\u0627", nil},
{"\u062c\u200c\u0610\u0627", "\u062c\u200c\u0610\u0627", nil},
// Variants of: L T* U+200c T* D
{"\ua872\u200c\u062c", "\ua872\u200c\u062c", nil},
{"\ua872\u0610\u200c\u0610\u062c", "\ua872\u0610\u200c\u0610\u062c", nil},
{"\ua872\u0610\u0610\u200c\u0610\u0610\u062c", "\ua872\u0610\u0610\u200c\u0610\u0610\u062c", nil},
{"\ua872\u0610\u200c\u062c", "\ua872\u0610\u200c\u062c", nil},
{"\ua872\u200c\u0610\u062c", "\ua872\u200c\u0610\u062c", nil},
// Virama
{"\u0a4d\u200c", "\u0a4d\u200c", nil},
{"\ua872\u0a4d\u200c", "\ua872\u0a4d\u200c", nil},
{"\ua872\u0a4d\u0610\u200c", "", errContext},
{"\ua872\u0a4d\u0610\u200c", "", errContext},
{"\u0acd\u200c", "\u0acd\u200c", nil},
{"\ua872\u0acd\u200c", "\ua872\u0acd\u200c", nil},
{"\ua872\u0acd\u0610\u200c", "", errContext},
{"\ua872\u0acd\u0610\u200c", "", errContext},
// Using Virama as join T
{"\ua872\u0acd\u200c\u062c", "\ua872\u0acd\u200c\u062c", nil},
{"\ua872\u200c\u0acd\u062c", "\ua872\u200c\u0acd\u062c", nil},
}},
{"Context Rule 2", NewFreeform(), []testCase{
// Rule 2: zero-width joiner (U+200D)
{"\u200d", "", errContext},
{"\u200da", "", errContext},
{"a\u200d", "", errContext},
{"\u0a4d\u200d", "\u0a4d\u200d", nil},
{"\ua872\u0a4d\u200d", "\ua872\u0a4d\u200d", nil},
{"\u0a4da\u200d", "", errContext},
}},
{"Context Rule 3", NewFreeform(), []testCase{
// Rule 3: middle dot
{"·", "", errContext},
{"l·", "", errContext},
{"·l", "", errContext},
{"a·", "", errContext},
{"l·a", "", errContext},
{"a·a", "", errContext},
{"l·l", "l·l", nil},
{"al·la", "al·la", nil},
}},
{"Context Rule 4", NewFreeform(), []testCase{
// Rule 4: Greek lower numeral U+0375
{"͵", "", errContext},
{"͵a", "", errContext},
{"α͵", "", errContext},
{"͵α", "͵α", nil},
{"α͵α", "α͵α", nil},
{"͵͵α", "͵͵α", nil}, // The numeric sign is itself Greek.
{"α͵͵α", "α͵͵α", nil},
}},
{"Context Rule 5+6", NewFreeform(), []testCase{
// Rule 5+6: Hebrew preceding
// U+05f3: Geresh
{"׳", "", errContext},
{"׳ה", "", errContext},
{"a׳b", "", errContext},
{"ש׳", "ש׳", nil}, // U+05e9 U+05f3
{"ש׳׳׳", "ש׳׳׳", nil}, // U+05e9 U+05f3
// U+05f4: Gershayim
{"״", "", errContext},
{"״ה", "", errContext},
{"a״b", "", errContext},
{"ש״", "ש״", nil}, // U+05e9 U+05f4
{"ש״״״", "ש״״״", nil}, // U+05e9 U+05f4
{"aש״״״", "aש״״״", nil}, // U+05e9 U+05f4
}},
{"Context Rule 7", NewFreeform(), []testCase{
// Rule 7: Katakana middle Dot
{"・", "", errContext},
{"abc・", "", errContext},
{"・def", "", errContext},
{"abc・def", "", errContext},
{"aヅc・def", "aヅc・def", nil},
{"abc・dぶf", "abc・dぶf", nil},
{"⺐bc・def", "⺐bc・def", nil},
}},
{"Context Rule 8+9", NewFreeform(), []testCase{
// Rule 8+9: Arabic Indic Digit
{"١٢٣٤٥۶", "", errContext},
{"۱۲۳۴۵٦", "", errContext},
{"١٢٣٤٥", "١٢٣٤٥", nil},
{"۱۲۳۴۵", "۱۲۳۴۵", nil},
}},
{"Nickname", Nickname, []testCase{
{" Swan of Avon ", "Swan of Avon", nil},
{"", "", errEmptyString},
{" ", "", errEmptyString},
{" ", "", errEmptyString},
{"a\u00A0a\u1680a\u2000a\u2001a\u2002a\u2003a\u2004a\u2005a\u2006a\u2007a\u2008a\u2009a\u200Aa\u202Fa\u205Fa\u3000a", "a a a a a a a a a a a a a a a a a", nil},
{"Foo", "Foo", nil},
{"foo", "foo", nil},
{"Foo Bar", "Foo Bar", nil},
{"foo bar", "foo bar", nil},
{"\u03C3", "\u03C3", nil},
// Greek final sigma is left as is (do not fold!)
{"\u03C2", "\u03C2", nil},
{"\u265A", "♚", nil},
{"Richard \u2163", "Richard IV", nil},
{"\u212B", "Å", nil},
{"\uFB00", "ff", nil}, // because of NFKC
{"שa", "שa", nil}, // no bidi rule
{"동일조건변경허락", "동일조건변경허락", nil},
}},
{"OpaqueString", OpaqueString, []testCase{
{" Swan of Avon ", " Swan of Avon ", nil},
{"", "", errEmptyString},
{" ", " ", nil},
{" ", " ", nil},
{"a\u00A0a\u1680a\u2000a\u2001a\u2002a\u2003a\u2004a\u2005a\u2006a\u2007a\u2008a\u2009a\u200Aa\u202Fa\u205Fa\u3000a", "a a a a a a a a a a a a a a a a a", nil},
{"Foo", "Foo", nil},
{"foo", "foo", nil},
{"Foo Bar", "Foo Bar", nil},
{"foo bar", "foo bar", nil},
{"\u03C3", "\u03C3", nil},
{"Richard \u2163", "Richard \u2163", nil},
{"\u212B", "Å", nil},
{"Jack of \u2666s", "Jack of \u2666s", nil},
{"my cat is a \u0009by", "", errDisallowedRune},
{"שa", "שa", nil}, // no bidi rule
}},
{"UsernameCaseMapped", UsernameCaseMapped, []testCase{
// TODO: Should this work?
// {UsernameCaseMapped, "", "", errDisallowedRune},
{"juliet@example.com", "juliet@example.com", nil},
{"fussball", "fussball", nil},
{"fu\u00DFball", "fussball", nil},
{"\u03C0", "\u03C0", nil},
{"\u03A3", "\u03C3", nil},
{"\u03C3", "\u03C3", nil},
{"\u03C2", "\u03C3", nil},
{"\u0049", "\u0069", nil},
{"\u0049", "\u0069", nil},
{"\u03D2", "", errDisallowedRune},
{"\u03B0", "\u03B0", nil},
{"foo bar", "", bidirule.ErrInvalid},
{"♚", "", bidirule.ErrInvalid},
{"\u007E", "", bidirule.ErrInvalid}, // disallowed by bidi rule
{"a", "a", nil},
{"!", "", bidirule.ErrInvalid}, // disallowed by bidi rule
{"²", "", bidirule.ErrInvalid},
{"\t", "", bidirule.ErrInvalid},
{"\n", "", bidirule.ErrInvalid},
{"\u26D6", "", bidirule.ErrInvalid},
{"\u26FF", "", bidirule.ErrInvalid},
{"\uFB00", "ff", nil}, // Side effect of case folding.
{"\u1680", "", bidirule.ErrInvalid},
{" ", "", bidirule.ErrInvalid},
{" ", "", bidirule.ErrInvalid},
{"\u01C5", "", errDisallowedRune},
{"\u16EE", "", errDisallowedRune}, // Nl RUNIC ARLAUG SYMBOL
{"\u0488", "", bidirule.ErrInvalid}, // Me COMBINING CYRILLIC HUNDRED THOUSANDS SIGN
{"\u212B", "\u00e5", nil}, // Angstrom sign, NFC -> U+00E5
{"A\u030A", "å", nil}, // A + ring
{"\u00C5", "å", nil}, // A with ring
{"\u00E7", "ç", nil}, // c cedille
{"\u0063\u0327", "ç", nil}, // c + cedille
{"\u0158", "ř", nil},
{"\u0052\u030C", "ř", nil},
{"\u1E61", "\u1E61", nil}, // LATIN SMALL LETTER S WITH DOT ABOVE
// U+1e9B: case folded.
{"ẛ", "\u1E61", nil}, // LATIN SMALL LETTER LONG S WITH DOT ABOVE
// Confusable characters ARE allowed and should NOT be mapped.
{"\u0410", "\u0430", nil}, // CYRILLIC CAPITAL LETTER A
// Full width should be mapped to the canonical decomposition.
{"", "ab", nil},
{"שc", "", bidirule.ErrInvalid}, // bidi rule
}},
{"UsernameCasePreserved", UsernameCasePreserved, []testCase{
{"ABC", "ABC", nil},
{"", "AB", nil},
{"שc", "", bidirule.ErrInvalid}, // bidi rule
{"\uFB00", "", errDisallowedRune},
{"\u212B", "\u00c5", nil}, // Angstrom sign, NFC -> U+00E5
{"ẛ", "", errDisallowedRune}, // LATIN SMALL LETTER LONG S WITH DOT ABOVE
}},
}
func TestString(t *testing.T) {
doTests(t, func(t *testing.T, p *Profile, tc testCase) {
if e, err := p.String(tc.input); tc.err != err || e != tc.output {
t.Errorf("got %+q (err: %v); want %+q (err: %v)", e, err, tc.output, tc.err)
}
})
}
func TestBytes(t *testing.T) {
doTests(t, func(t *testing.T, p *Profile, tc testCase) {
if e, err := p.Bytes([]byte(tc.input)); tc.err != err || string(e) != tc.output {
t.Errorf("got %+q (err: %v); want %+q (err: %v)", string(e), err, tc.output, tc.err)
}
})
// Test that calling Bytes with something that doesn't transform returns a
// copy.
orig := []byte("hello")
b, _ := NewFreeform().Bytes(orig)
if reflect.ValueOf(b).Pointer() == reflect.ValueOf(orig).Pointer() {
t.Error("original and result are the same slice; should be a copy")
}
}
func TestAppend(t *testing.T) {
doTests(t, func(t *testing.T, p *Profile, tc testCase) {
if e, err := p.Append(nil, []byte(tc.input)); tc.err != err || string(e) != tc.output {
t.Errorf("got %+q (err: %v); want %+q (err: %v)", string(e), err, tc.output, tc.err)
}
})
}

310
vendor/golang.org/x/text/secure/precis/gen.go generated vendored Normal file
View file

@ -0,0 +1,310 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Unicode table generator.
// Data read from the web.
// +build ignore
package main
import (
"flag"
"log"
"unicode"
"unicode/utf8"
"golang.org/x/text/internal/gen"
"golang.org/x/text/internal/triegen"
"golang.org/x/text/internal/ucd"
"golang.org/x/text/unicode/norm"
"golang.org/x/text/unicode/rangetable"
)
var outputFile = flag.String("output", "tables.go", "output file for generated tables; default tables.go")
var assigned, disallowedRunes *unicode.RangeTable
var runeCategory = map[rune]category{}
var overrides = map[category]category{
viramaModifier: viramaJoinT,
greek: greekJoinT,
hebrew: hebrewJoinT,
}
func setCategory(r rune, cat category) {
if c, ok := runeCategory[r]; ok {
if override, ok := overrides[c]; cat == joiningT && ok {
cat = override
} else {
log.Fatalf("%U: multiple categories for rune (%v and %v)", r, c, cat)
}
}
runeCategory[r] = cat
}
func init() {
if numCategories > 1<<propShift {
log.Fatalf("Number of categories is %d; may at most be %d", numCategories, 1<<propShift)
}
}
func main() {
gen.Init()
// Load data
runes := []rune{}
// PrecisIgnorableProperties: https://tools.ietf.org/html/rfc7564#section-9.13
ucd.Parse(gen.OpenUCDFile("DerivedCoreProperties.txt"), func(p *ucd.Parser) {
if p.String(1) == "Default_Ignorable_Code_Point" {
runes = append(runes, p.Rune(0))
}
})
ucd.Parse(gen.OpenUCDFile("PropList.txt"), func(p *ucd.Parser) {
switch p.String(1) {
case "Noncharacter_Code_Point":
runes = append(runes, p.Rune(0))
}
})
// OldHangulJamo: https://tools.ietf.org/html/rfc5892#section-2.9
ucd.Parse(gen.OpenUCDFile("HangulSyllableType.txt"), func(p *ucd.Parser) {
switch p.String(1) {
case "L", "V", "T":
runes = append(runes, p.Rune(0))
}
})
disallowedRunes = rangetable.New(runes...)
assigned = rangetable.Assigned(unicode.Version)
// Load category data.
runeCategory['l'] = latinSmallL
ucd.Parse(gen.OpenUCDFile("UnicodeData.txt"), func(p *ucd.Parser) {
const cccVirama = 9
if p.Int(ucd.CanonicalCombiningClass) == cccVirama {
setCategory(p.Rune(0), viramaModifier)
}
})
ucd.Parse(gen.OpenUCDFile("Scripts.txt"), func(p *ucd.Parser) {
switch p.String(1) {
case "Greek":
setCategory(p.Rune(0), greek)
case "Hebrew":
setCategory(p.Rune(0), hebrew)
case "Hiragana", "Katakana", "Han":
setCategory(p.Rune(0), japanese)
}
})
// Set the rule categories associated with exceptions. This overrides any
// previously set categories. The original categories are manually
// reintroduced in the categoryTransitions table.
for r, e := range exceptions {
if e.cat != 0 {
runeCategory[r] = e.cat
}
}
cat := map[string]category{
"L": joiningL,
"D": joiningD,
"T": joiningT,
"R": joiningR,
}
ucd.Parse(gen.OpenUCDFile("extracted/DerivedJoiningType.txt"), func(p *ucd.Parser) {
switch v := p.String(1); v {
case "L", "D", "T", "R":
setCategory(p.Rune(0), cat[v])
}
})
writeTables()
gen.Repackage("gen_trieval.go", "trieval.go", "precis")
}
type exception struct {
prop property
cat category
}
func init() {
// Programmatically add the Arabic and Indic digits to the exceptions map.
// See comment in the exceptions map below why these are marked disallowed.
for i := rune(0); i <= 9; i++ {
exceptions[0x0660+i] = exception{
prop: disallowed,
cat: arabicIndicDigit,
}
exceptions[0x06F0+i] = exception{
prop: disallowed,
cat: extendedArabicIndicDigit,
}
}
}
// The Exceptions class as defined in RFC 5892
// https://tools.ietf.org/html/rfc5892#section-2.6
var exceptions = map[rune]exception{
0x00DF: {prop: pValid},
0x03C2: {prop: pValid},
0x06FD: {prop: pValid},
0x06FE: {prop: pValid},
0x0F0B: {prop: pValid},
0x3007: {prop: pValid},
// ContextO|J rules are marked as disallowed, taking a "guilty until proven
// innocent" approach. The main reason for this is that the check for
// whether a context rule should be applied can be moved to the logic for
// handing disallowed runes, taken it off the common path. The exception to
// this rule is for katakanaMiddleDot, as the rule logic is handled without
// using a rule function.
// ContextJ (Join control)
0x200C: {prop: disallowed, cat: zeroWidthNonJoiner},
0x200D: {prop: disallowed, cat: zeroWidthJoiner},
// ContextO
0x00B7: {prop: disallowed, cat: middleDot},
0x0375: {prop: disallowed, cat: greekLowerNumeralSign},
0x05F3: {prop: disallowed, cat: hebrewPreceding}, // punctuation Geresh
0x05F4: {prop: disallowed, cat: hebrewPreceding}, // punctuation Gershayim
0x30FB: {prop: pValid, cat: katakanaMiddleDot},
// These are officially ContextO, but the implementation does not require
// special treatment of these, so we simply mark them as valid.
0x0660: {prop: pValid},
0x0661: {prop: pValid},
0x0662: {prop: pValid},
0x0663: {prop: pValid},
0x0664: {prop: pValid},
0x0665: {prop: pValid},
0x0666: {prop: pValid},
0x0667: {prop: pValid},
0x0668: {prop: pValid},
0x0669: {prop: pValid},
0x06F0: {prop: pValid},
0x06F1: {prop: pValid},
0x06F2: {prop: pValid},
0x06F3: {prop: pValid},
0x06F4: {prop: pValid},
0x06F5: {prop: pValid},
0x06F6: {prop: pValid},
0x06F7: {prop: pValid},
0x06F8: {prop: pValid},
0x06F9: {prop: pValid},
0x0640: {prop: disallowed},
0x07FA: {prop: disallowed},
0x302E: {prop: disallowed},
0x302F: {prop: disallowed},
0x3031: {prop: disallowed},
0x3032: {prop: disallowed},
0x3033: {prop: disallowed},
0x3034: {prop: disallowed},
0x3035: {prop: disallowed},
0x303B: {prop: disallowed},
}
// LetterDigits: https://tools.ietf.org/html/rfc5892#section-2.1
// r in {Ll, Lu, Lo, Nd, Lm, Mn, Mc}.
func isLetterDigits(r rune) bool {
return unicode.In(r,
unicode.Ll, unicode.Lu, unicode.Lm, unicode.Lo, // Letters
unicode.Mn, unicode.Mc, // Modifiers
unicode.Nd, // Digits
)
}
func isIdDisAndFreePVal(r rune) bool {
return unicode.In(r,
// OtherLetterDigits: https://tools.ietf.org/html/rfc7564#section-9.18
// r in in {Lt, Nl, No, Me}
unicode.Lt, unicode.Nl, unicode.No, // Other letters / numbers
unicode.Me, // Modifiers
// Spaces: https://tools.ietf.org/html/rfc7564#section-9.14
// r in in {Zs}
unicode.Zs,
// Symbols: https://tools.ietf.org/html/rfc7564#section-9.15
// r in {Sm, Sc, Sk, So}
unicode.Sm, unicode.Sc, unicode.Sk, unicode.So,
// Punctuation: https://tools.ietf.org/html/rfc7564#section-9.16
// r in {Pc, Pd, Ps, Pe, Pi, Pf, Po}
unicode.Pc, unicode.Pd, unicode.Ps, unicode.Pe,
unicode.Pi, unicode.Pf, unicode.Po,
)
}
// HasCompat: https://tools.ietf.org/html/rfc7564#section-9.17
func hasCompat(r rune) bool {
return !norm.NFKC.IsNormalString(string(r))
}
// From https://tools.ietf.org/html/rfc5892:
//
// If .cp. .in. Exceptions Then Exceptions(cp);
// Else If .cp. .in. BackwardCompatible Then BackwardCompatible(cp);
// Else If .cp. .in. Unassigned Then UNASSIGNED;
// Else If .cp. .in. ASCII7 Then PVALID;
// Else If .cp. .in. JoinControl Then CONTEXTJ;
// Else If .cp. .in. OldHangulJamo Then DISALLOWED;
// Else If .cp. .in. PrecisIgnorableProperties Then DISALLOWED;
// Else If .cp. .in. Controls Then DISALLOWED;
// Else If .cp. .in. HasCompat Then ID_DIS or FREE_PVAL;
// Else If .cp. .in. LetterDigits Then PVALID;
// Else If .cp. .in. OtherLetterDigits Then ID_DIS or FREE_PVAL;
// Else If .cp. .in. Spaces Then ID_DIS or FREE_PVAL;
// Else If .cp. .in. Symbols Then ID_DIS or FREE_PVAL;
// Else If .cp. .in. Punctuation Then ID_DIS or FREE_PVAL;
// Else DISALLOWED;
func writeTables() {
propTrie := triegen.NewTrie("derivedProperties")
w := gen.NewCodeWriter()
defer w.WriteGoFile(*outputFile, "precis")
gen.WriteUnicodeVersion(w)
// Iterate over all the runes...
for i := rune(0); i < unicode.MaxRune; i++ {
r := rune(i)
if !utf8.ValidRune(r) {
continue
}
e, ok := exceptions[i]
p := e.prop
switch {
case ok:
case !unicode.In(r, assigned):
p = unassigned
case r >= 0x0021 && r <= 0x007e: // Is ASCII 7
p = pValid
case unicode.In(r, disallowedRunes, unicode.Cc):
p = disallowed
case hasCompat(r):
p = idDisOrFreePVal
case isLetterDigits(r):
p = pValid
case isIdDisAndFreePVal(r):
p = idDisOrFreePVal
default:
p = disallowed
}
cat := runeCategory[r]
// Don't set category for runes that are disallowed.
if p == disallowed {
cat = exceptions[r].cat
}
propTrie.Insert(r, uint64(p)|uint64(cat))
}
sz, err := propTrie.Gen(w)
if err != nil {
log.Fatal(err)
}
w.Size += sz
}

68
vendor/golang.org/x/text/secure/precis/gen_trieval.go generated vendored Normal file
View file

@ -0,0 +1,68 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build ignore
package main
// entry is the entry of a trie table
// 7..6 property (unassigned, disallowed, maybe, valid)
// 5..0 category
type entry uint8
const (
propShift = 6
propMask = 0xc0
catMask = 0x3f
)
func (e entry) property() property { return property(e & propMask) }
func (e entry) category() category { return category(e & catMask) }
type property uint8
// The order of these constants matter. A Profile may consider runes to be
// allowed either from pValid or idDisOrFreePVal.
const (
unassigned property = iota << propShift
disallowed
idDisOrFreePVal // disallowed for Identifier, pValid for FreeForm
pValid
)
// compute permutations of all properties and specialCategories.
type category uint8
const (
other category = iota
// Special rune types
joiningL
joiningD
joiningT
joiningR
viramaModifier
viramaJoinT // Virama + JoiningT
latinSmallL // U+006c
greek
greekJoinT // Greek + JoiningT
hebrew
hebrewJoinT // Hebrew + JoiningT
japanese // hirigana, katakana, han
// Special rune types associated with contextual rules defined in
// https://tools.ietf.org/html/rfc5892#appendix-A.
// ContextO
zeroWidthNonJoiner // rule 1
zeroWidthJoiner // rule 2
// ContextJ
middleDot // rule 3
greekLowerNumeralSign // rule 4
hebrewPreceding // rule 5 and 6
katakanaMiddleDot // rule 7
arabicIndicDigit // rule 8
extendedArabicIndicDigit // rule 9
numCategories
)

24
vendor/golang.org/x/text/secure/precis/go1_6_test.go generated vendored Normal file
View file

@ -0,0 +1,24 @@
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build !go1.7
package precis
import (
"fmt"
"testing"
)
// doTests runs all tests without using t.Run. As a result, context may be
// missing, but at least all tests are run.
func doTests(t *testing.T, fn func(t *testing.T, p *Profile, tc testCase)) {
for _, g := range testCases {
for i, tc := range g.cases {
name := fmt.Sprintf("%s:%d:%+q", g.name, i, tc.input)
t.Log("Testing ", name)
fn(t, g.p, tc)
}
}
}

23
vendor/golang.org/x/text/secure/precis/go1_7_test.go generated vendored Normal file
View file

@ -0,0 +1,23 @@
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build go1.7
package precis
import (
"fmt"
"testing"
)
func doTests(t *testing.T, fn func(t *testing.T, p *Profile, tc testCase)) {
for _, g := range testCases {
for i, tc := range g.cases {
name := fmt.Sprintf("%s:%d:%+q", g.name, i, tc.input)
t.Run(name, func(t *testing.T) {
fn(t, g.p, tc)
})
}
}
}

70
vendor/golang.org/x/text/secure/precis/nickname.go generated vendored Normal file
View file

@ -0,0 +1,70 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package precis
import (
"unicode"
"unicode/utf8"
"golang.org/x/text/transform"
)
type nickAdditionalMapping struct {
// TODO: This transformer needs to be stateless somehow…
notStart bool
prevSpace bool
}
func (t *nickAdditionalMapping) Reset() {
t.prevSpace = false
t.notStart = false
}
func (t *nickAdditionalMapping) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
// RFC 7700 §2.1. Rules
//
// 2. Additional Mapping Rule: The additional mapping rule consists of
// the following sub-rules.
//
// 1. Any instances of non-ASCII space MUST be mapped to ASCII
// space (U+0020); a non-ASCII space is any Unicode code point
// having a general category of "Zs", naturally with the
// exception of U+0020.
//
// 2. Any instances of the ASCII space character at the beginning
// or end of a nickname MUST be removed (e.g., "stpeter " is
// mapped to "stpeter").
//
// 3. Interior sequences of more than one ASCII space character
// MUST be mapped to a single ASCII space character (e.g.,
// "St Peter" is mapped to "St Peter").
for nSrc < len(src) {
r, size := utf8.DecodeRune(src[nSrc:])
if size == 0 { // Incomplete UTF-8 encoding
if !atEOF {
return nDst, nSrc, transform.ErrShortSrc
}
size = 1
}
if unicode.Is(unicode.Zs, r) {
t.prevSpace = true
} else {
if t.prevSpace && t.notStart {
dst[nDst] = ' '
nDst += 1
}
if size != copy(dst[nDst:], src[nSrc:nSrc+size]) {
nDst += size
return nDst, nSrc, transform.ErrShortDst
}
nDst += size
t.prevSpace = false
t.notStart = true
}
nSrc += size
}
return nDst, nSrc, nil
}

106
vendor/golang.org/x/text/secure/precis/options.go generated vendored Normal file
View file

@ -0,0 +1,106 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package precis
import (
"golang.org/x/text/cases"
"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
"golang.org/x/text/width"
)
// An Option is used to define the behavior and rules of a Profile.
type Option func(*options)
type options struct {
// Preparation options
foldWidth bool
// Enforcement options
cases transform.Transformer
disallow runes.Set
norm norm.Form
additional []func() transform.Transformer
width *width.Transformer
disallowEmpty bool
bidiRule bool
// Comparison options
ignorecase bool
}
func getOpts(o ...Option) (res options) {
for _, f := range o {
f(&res)
}
return
}
var (
// The IgnoreCase option causes the profile to perform a case insensitive
// comparison during the PRECIS comparison step.
IgnoreCase Option = ignoreCase
// The FoldWidth option causes the profile to map non-canonical wide and
// narrow variants to their decomposition mapping. This is useful for
// profiles that are based on the identifier class which would otherwise
// disallow such characters.
FoldWidth Option = foldWidth
// The DisallowEmpty option causes the enforcement step to return an error if
// the resulting string would be empty.
DisallowEmpty Option = disallowEmpty
// The BidiRule option causes the Bidi Rule defined in RFC 5893 to be
// applied.
BidiRule Option = bidiRule
)
var (
ignoreCase = func(o *options) {
o.ignorecase = true
}
foldWidth = func(o *options) {
o.foldWidth = true
}
disallowEmpty = func(o *options) {
o.disallowEmpty = true
}
bidiRule = func(o *options) {
o.bidiRule = true
}
)
// The AdditionalMapping option defines the additional mapping rule for the
// Profile by applying Transformer's in sequence.
func AdditionalMapping(t ...func() transform.Transformer) Option {
return func(o *options) {
o.additional = t
}
}
// The Norm option defines a Profile's normalization rule. Defaults to NFC.
func Norm(f norm.Form) Option {
return func(o *options) {
o.norm = f
}
}
// The FoldCase option defines a Profile's case mapping rule. Options can be
// provided to determine the type of case folding used.
func FoldCase(opts ...cases.Option) Option {
return func(o *options) {
o.cases = cases.Fold(opts...)
}
}
// The Disallow option further restricts a Profile's allowed characters beyond
// what is disallowed by the underlying string class.
func Disallow(set runes.Set) Option {
return func(o *options) {
o.disallow = set
}
}

330
vendor/golang.org/x/text/secure/precis/profile.go generated vendored Normal file
View file

@ -0,0 +1,330 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package precis
import (
"errors"
"unicode/utf8"
"golang.org/x/text/runes"
"golang.org/x/text/secure/bidirule"
"golang.org/x/text/transform"
"golang.org/x/text/width"
)
var (
errDisallowedRune = errors.New("precis: disallowed rune encountered")
)
var dpTrie = newDerivedPropertiesTrie(0)
// A Profile represents a set of rules for normalizing and validating strings in
// the PRECIS framework.
type Profile struct {
options
class *class
}
// NewIdentifier creates a new PRECIS profile based on the Identifier string
// class. Profiles created from this class are suitable for use where safety is
// prioritized over expressiveness like network identifiers, user accounts, chat
// rooms, and file names.
func NewIdentifier(opts ...Option) *Profile {
return &Profile{
options: getOpts(opts...),
class: identifier,
}
}
// NewFreeform creates a new PRECIS profile based on the Freeform string class.
// Profiles created from this class are suitable for use where expressiveness is
// prioritized over safety like passwords, and display-elements such as
// nicknames in a chat room.
func NewFreeform(opts ...Option) *Profile {
return &Profile{
options: getOpts(opts...),
class: freeform,
}
}
// NewTransformer creates a new transform.Transformer that performs the PRECIS
// preparation and enforcement steps on the given UTF-8 encoded bytes.
func (p *Profile) NewTransformer() *Transformer {
var ts []transform.Transformer
// These transforms are applied in the order defined in
// https://tools.ietf.org/html/rfc7564#section-7
if p.options.foldWidth {
ts = append(ts, width.Fold)
}
for _, f := range p.options.additional {
ts = append(ts, f())
}
if p.options.cases != nil {
ts = append(ts, p.options.cases)
}
ts = append(ts, p.options.norm)
if p.options.bidiRule {
ts = append(ts, bidirule.New())
}
ts = append(ts, &checker{p: p, allowed: p.Allowed()})
// TODO: Add the disallow empty rule with a dummy transformer?
return &Transformer{transform.Chain(ts...)}
}
var errEmptyString = errors.New("precis: transformation resulted in empty string")
type buffers struct {
src []byte
buf [2][]byte
next int
}
func (b *buffers) init(n int) {
b.buf[0] = make([]byte, 0, n)
b.buf[1] = make([]byte, 0, n)
}
func (b *buffers) apply(t transform.Transformer) (err error) {
// TODO: use Span, once available.
x := b.next & 1
b.src, _, err = transform.Append(t, b.buf[x][:0], b.src)
b.buf[x] = b.src
b.next++
return err
}
func (b *buffers) enforce(p *Profile, src []byte) (str []byte, err error) {
b.src = src
// These transforms are applied in the order defined in
// https://tools.ietf.org/html/rfc7564#section-7
// TODO: allow different width transforms options.
if p.options.foldWidth {
// TODO: use Span, once available.
if err = b.apply(width.Fold); err != nil {
return nil, err
}
}
for _, f := range p.options.additional {
if err = b.apply(f()); err != nil {
return nil, err
}
}
if p.options.cases != nil {
if err = b.apply(p.options.cases); err != nil {
return nil, err
}
}
if n := p.norm.QuickSpan(b.src); n < len(b.src) {
x := b.next & 1
n = copy(b.buf[x], b.src[:n])
b.src, _, err = transform.Append(p.norm, b.buf[x][:n], b.src[n:])
b.buf[x] = b.src
b.next++
if err != nil {
return nil, err
}
}
if p.options.bidiRule {
if err := b.apply(bidirule.New()); err != nil {
return nil, err
}
}
c := checker{p: p}
if _, err := c.span(b.src, true); err != nil {
return nil, err
}
if p.disallow != nil {
for i := 0; i < len(b.src); {
r, size := utf8.DecodeRune(b.src[i:])
if p.disallow.Contains(r) {
return nil, errDisallowedRune
}
i += size
}
}
// TODO: Add the disallow empty rule with a dummy transformer?
if p.options.disallowEmpty && len(b.src) == 0 {
return nil, errEmptyString
}
return b.src, nil
}
// Append appends the result of applying p to src writing the result to dst.
// It returns an error if the input string is invalid.
func (p *Profile) Append(dst, src []byte) ([]byte, error) {
var buf buffers
buf.init(8 + len(src) + len(src)>>2)
b, err := buf.enforce(p, src)
if err != nil {
return nil, err
}
return append(dst, b...), nil
}
// Bytes returns a new byte slice with the result of applying the profile to b.
func (p *Profile) Bytes(b []byte) ([]byte, error) {
var buf buffers
buf.init(8 + len(b) + len(b)>>2)
b, err := buf.enforce(p, b)
if err != nil {
return nil, err
}
if buf.next == 0 {
c := make([]byte, len(b))
copy(c, b)
return c, nil
}
return b, nil
}
// String returns a string with the result of applying the profile to s.
func (p *Profile) String(s string) (string, error) {
var buf buffers
buf.init(8 + len(s) + len(s)>>2)
b, err := buf.enforce(p, []byte(s))
if err != nil {
return "", err
}
return string(b), nil
}
// Compare enforces both strings, and then compares them for bit-string identity
// (byte-for-byte equality). If either string cannot be enforced, the comparison
// is false.
func (p *Profile) Compare(a, b string) bool {
a, err := p.String(a)
if err != nil {
return false
}
b, err = p.String(b)
if err != nil {
return false
}
// TODO: This is out of order. Need to extract the transformation logic and
// put this in where the normal case folding would go (but only for
// comparison).
if p.options.ignorecase {
a = width.Fold.String(a)
b = width.Fold.String(a)
}
return a == b
}
// Allowed returns a runes.Set containing every rune that is a member of the
// underlying profile's string class and not disallowed by any profile specific
// rules.
func (p *Profile) Allowed() runes.Set {
if p.options.disallow != nil {
return runes.Predicate(func(r rune) bool {
return p.class.Contains(r) && !p.options.disallow.Contains(r)
})
}
return p.class
}
type checker struct {
p *Profile
allowed runes.Set
beforeBits catBitmap
termBits catBitmap
acceptBits catBitmap
}
func (c *checker) Reset() {
c.beforeBits = 0
c.termBits = 0
c.acceptBits = 0
}
func (c *checker) span(src []byte, atEOF bool) (n int, err error) {
for n < len(src) {
e, sz := dpTrie.lookup(src[n:])
d := categoryTransitions[category(e&catMask)]
if sz == 0 {
if !atEOF {
return n, transform.ErrShortSrc
}
return n, errDisallowedRune
}
if property(e) < c.p.class.validFrom {
if d.rule == nil {
return n, errDisallowedRune
}
doLookAhead, err := d.rule(c.beforeBits)
if err != nil {
return n, err
}
if doLookAhead {
c.beforeBits &= d.keep
c.beforeBits |= d.set
// We may still have a lookahead rule which we will require to
// complete (by checking termBits == 0) before setting the new
// bits.
if c.termBits != 0 && (!c.checkLookahead() || c.termBits == 0) {
return n, err
}
c.termBits = d.term
c.acceptBits = d.accept
n += sz
continue
}
}
c.beforeBits &= d.keep
c.beforeBits |= d.set
if c.termBits != 0 && !c.checkLookahead() {
return n, errContext
}
n += sz
}
if m := c.beforeBits >> finalShift; c.beforeBits&m != m || c.termBits != 0 {
err = errContext
}
return n, err
}
func (c *checker) checkLookahead() bool {
switch {
case c.beforeBits&c.termBits != 0:
c.termBits = 0
c.acceptBits = 0
case c.beforeBits&c.acceptBits != 0:
default:
return false
}
return true
}
// TODO: we may get rid of this transform if transform.Chain understands
// something like a Spanner interface.
func (c checker) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
short := false
if len(dst) < len(src) {
src = src[:len(dst)]
atEOF = false
short = true
}
nSrc, err = c.span(src, atEOF)
nDst = copy(dst, src[:nSrc])
if short && (err == transform.ErrShortSrc || err == nil) {
err = transform.ErrShortDst
}
return nDst, nSrc, err
}

56
vendor/golang.org/x/text/secure/precis/profiles.go generated vendored Normal file
View file

@ -0,0 +1,56 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package precis
import (
"unicode"
"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
)
var (
Nickname *Profile = nickname // Implements the Nickname profile specified in RFC 7700.
UsernameCaseMapped *Profile = usernameCaseMap // Implements the UsernameCaseMapped profile specified in RFC 7613.
UsernameCasePreserved *Profile = usernameNoCaseMap // Implements the UsernameCasePreserved profile specified in RFC 7613.
OpaqueString *Profile = opaquestring // Implements the OpaqueString profile defined in RFC 7613 for passwords and other secure labels.
)
// TODO: mvl: "Ultimately, I would manually define the structs for the internal
// profiles. This avoid pulling in unneeded tables when they are not used."
var (
nickname = NewFreeform(
AdditionalMapping(func() transform.Transformer {
return &nickAdditionalMapping{}
}),
IgnoreCase,
Norm(norm.NFKC),
DisallowEmpty,
)
usernameCaseMap = NewIdentifier(
FoldWidth,
FoldCase(),
Norm(norm.NFC),
BidiRule,
)
usernameNoCaseMap = NewIdentifier(
FoldWidth,
Norm(norm.NFC),
BidiRule,
)
opaquestring = NewFreeform(
AdditionalMapping(func() transform.Transformer {
return runes.Map(func(r rune) rune {
if unicode.Is(unicode.Zs, r) {
return ' '
}
return r
})
}),
Norm(norm.NFC),
DisallowEmpty,
)
)

3788
vendor/golang.org/x/text/secure/precis/tables.go generated vendored Normal file

File diff suppressed because it is too large Load diff

69
vendor/golang.org/x/text/secure/precis/tables_test.go generated vendored Normal file
View file

@ -0,0 +1,69 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package precis
import (
"testing"
"unicode"
"unicode/utf8"
"golang.org/x/text/runes"
"golang.org/x/text/unicode/rangetable"
)
type tableTest struct {
rangeTable *unicode.RangeTable
prop property
}
var exceptions = runes.Predicate(func(r rune) bool {
switch uint32(r) {
case 0x00DF, 0x03C2, 0x06FD, 0x06FE, 0x0F0B, 0x3007, 0x00B7, 0x0375, 0x05F3,
0x05F4, 0x30FB, 0x0660, 0x0661, 0x0662, 0x0663, 0x0664, 0x0665, 0x0666,
0x0667, 0x0668, 0x0669, 0x06F0, 0x06F1, 0x06F2, 0x06F3, 0x06F4, 0x06F5,
0x06F6, 0x06F7, 0x06F8, 0x06F9, 0x0640, 0x07FA, 0x302E, 0x302F, 0x3031,
0x3032, 0x3033, 0x3034, 0x3035, 0x303B:
return true
default:
return false
}
})
// Ensure that ceratain properties were generated correctly.
func TestTable(t *testing.T) {
tests := []tableTest{
tableTest{
rangetable.Merge(
unicode.Lt, unicode.Nl, unicode.No, // Other letter digits
unicode.Me, // Modifiers
unicode.Zs, // Spaces
unicode.So, // Symbols
unicode.Pi, unicode.Pf, // Punctuation
),
idDisOrFreePVal,
},
tableTest{
rangetable.New(0x30000, 0x30101, 0xDFFFF),
unassigned,
},
}
assigned := rangetable.Assigned(UnicodeVersion)
for _, test := range tests {
rangetable.Visit(test.rangeTable, func(r rune) {
if !unicode.In(r, assigned) {
return
}
b := make([]byte, 4)
n := utf8.EncodeRune(b, r)
trieval, _ := dpTrie.lookup(b[:n])
p := entry(trieval).property()
if p != test.prop && !exceptions.Contains(r) {
t.Errorf("%U: got %+x; want %+x", r, test.prop, p)
}
})
}
}

32
vendor/golang.org/x/text/secure/precis/transformer.go generated vendored Normal file
View file

@ -0,0 +1,32 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package precis
import "golang.org/x/text/transform"
// Transformer implements the transform.Transformer interface.
type Transformer struct {
t transform.Transformer
}
// Reset implements the transform.Transformer interface.
func (t Transformer) Reset() { t.t.Reset() }
// Transform implements the transform.Transformer interface.
func (t Transformer) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
return t.t.Transform(dst, src, atEOF)
}
// Bytes returns a new byte slice with the result of applying t to b.
func (t Transformer) Bytes(b []byte) []byte {
b, _, _ = transform.Bytes(t, b)
return b
}
// String returns a string with the result of applying t to s.
func (t Transformer) String(s string) string {
s, _, _ = transform.String(t, s)
return s
}

64
vendor/golang.org/x/text/secure/precis/trieval.go generated vendored Normal file
View file

@ -0,0 +1,64 @@
// This file was generated by go generate; DO NOT EDIT
package precis
// entry is the entry of a trie table
// 7..6 property (unassigned, disallowed, maybe, valid)
// 5..0 category
type entry uint8
const (
propShift = 6
propMask = 0xc0
catMask = 0x3f
)
func (e entry) property() property { return property(e & propMask) }
func (e entry) category() category { return category(e & catMask) }
type property uint8
// The order of these constants matter. A Profile may consider runes to be
// allowed either from pValid or idDisOrFreePVal.
const (
unassigned property = iota << propShift
disallowed
idDisOrFreePVal // disallowed for Identifier, pValid for FreeForm
pValid
)
// compute permutations of all properties and specialCategories.
type category uint8
const (
other category = iota
// Special rune types
joiningL
joiningD
joiningT
joiningR
viramaModifier
viramaJoinT // Virama + JoiningT
latinSmallL // U+006c
greek
greekJoinT // Greek + JoiningT
hebrew
hebrewJoinT // Hebrew + JoiningT
japanese // hirigana, katakana, han
// Special rune types associated with contextual rules defined in
// https://tools.ietf.org/html/rfc5892#appendix-A.
// ContextO
zeroWidthNonJoiner // rule 1
zeroWidthJoiner // rule 2
// ContextJ
middleDot // rule 3
greekLowerNumeralSign // rule 4
hebrewPreceding // rule 5 and 6
katakanaMiddleDot // rule 7
arabicIndicDigit // rule 8
extendedArabicIndicDigit // rule 9
numCategories
)