Merge pull request #5262 from hashicorp/f-remove-lgpl-dependencies

Remove LGPL dependencies
This commit is contained in:
Megan Marsh 2017-08-30 13:45:41 -07:00 committed by GitHub
commit cde70ca2c1
103 changed files with 168287 additions and 3654 deletions

View File

@ -13,7 +13,8 @@ import (
"strings"
"time"
"gopkg.in/xmlpath.v2"
"github.com/ChrisTrenkamp/goxpath"
"github.com/ChrisTrenkamp/goxpath/tree/xmltree"
)
// Parallels9Driver is a base type for Parallels builders.
@ -78,13 +79,19 @@ func getConfigValueFromXpath(path, xpath string) (string, error) {
if err != nil {
return "", err
}
xpathComp := xmlpath.MustCompile(xpath)
root, err := xmlpath.Parse(file)
doc, err := xmltree.ParseXML(file)
if err != nil {
return "", err
}
value, _ := xpathComp.String(root)
return value, nil
xpExec := goxpath.MustParse(xpath)
node, err := xpExec.Exec(doc)
if err != nil {
return "", err
}
return node.String(), nil
}
// Finds an application bundle by identifier (for "darwin" platform only)

View File

@ -58,3 +58,29 @@ func TestIPAddress(t *testing.T) {
t.Fatalf("Should have found 10.211.55.124, not %s!\n", ip)
}
}
func TestXMLParseConfig(t *testing.T) {
td, err := ioutil.TempDir("", "configpvs")
if err != nil {
t.Fatalf("Error creating temp file: %s", err)
}
defer os.Remove(td)
config := []byte(`
<ExampleParallelsConfig>
<SystemConfig>
<DiskSize>20</DiskSize>
</SystemConfig>
</ExampleParallelsConfig>
`)
ioutil.WriteFile(td+"/config.pvs", config, 0666)
result, err := getConfigValueFromXpath(td, "//DiskSize")
if err != nil {
t.Fatalf("Error parsing XML: %s", err)
}
if result != "20" {
t.Fatalf("Expected %q, got %q", "20", result)
}
}

22
vendor/github.com/ChrisTrenkamp/goxpath/LICENSE generated vendored Normal file
View File

@ -0,0 +1,22 @@
The MIT License (MIT)
Copyright (c) 2015 ChrisTrenkamp
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

2
vendor/github.com/ChrisTrenkamp/goxpath/README.md generated vendored Normal file
View File

@ -0,0 +1,2 @@
# goxpath [![GoDoc](https://godoc.org/gopkg.in/src-d/go-git.v2?status.svg)](https://godoc.org/github.com/ChrisTrenkamp/goxpath) [![Build Status](https://travis-ci.org/ChrisTrenkamp/goxpath.svg?branch=master)](https://travis-ci.org/ChrisTrenkamp/goxpath) [![codecov.io](https://codecov.io/github/ChrisTrenkamp/goxpath/coverage.svg?branch=master)](https://codecov.io/github/ChrisTrenkamp/goxpath?branch=master) [![Go Report Card](https://goreportcard.com/badge/github.com/ChrisTrenkamp/goxpath)](https://goreportcard.com/report/github.com/ChrisTrenkamp/goxpath)
An XPath 1.0 implementation written in Go. See the [wiki](https://github.com/ChrisTrenkamp/goxpath/wiki) for more information.

17
vendor/github.com/ChrisTrenkamp/goxpath/coverage.sh generated vendored Executable file
View File

@ -0,0 +1,17 @@
#!/bin/bash
cd "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
go get github.com/ChrisTrenkamp/goxpath/cmd/goxpath
if [ $? != 0 ]; then
exit 1
fi
go test >/dev/null 2>&1
if [ $? != 0 ]; then
go test
exit 1
fi
gometalinter --deadline=1m ./...
go list -f '{{if gt (len .TestGoFiles) 0}}"go test -covermode count -coverprofile {{.Name}}.coverprofile -coverpkg ./... {{.ImportPath}}"{{end}} >/dev/null' ./... | xargs -I {} bash -c {} 2>/dev/null
gocovmerge `ls *.coverprofile` > coverage.txt
go tool cover -html=coverage.txt -o coverage.html
firefox coverage.html
rm coverage.html coverage.txt *.coverprofile

117
vendor/github.com/ChrisTrenkamp/goxpath/goxpath.go generated vendored Normal file
View File

@ -0,0 +1,117 @@
package goxpath
import (
"encoding/xml"
"fmt"
"github.com/ChrisTrenkamp/goxpath/internal/execxp"
"github.com/ChrisTrenkamp/goxpath/internal/parser"
"github.com/ChrisTrenkamp/goxpath/tree"
)
//Opts defines namespace mappings and custom functions for XPath expressions.
type Opts struct {
NS map[string]string
Funcs map[xml.Name]tree.Wrap
Vars map[string]tree.Result
}
//FuncOpts is a function wrapper for Opts.
type FuncOpts func(*Opts)
//XPathExec is the XPath executor, compiled from an XPath string
type XPathExec struct {
n *parser.Node
}
//Parse parses the XPath expression, xp, returning an XPath executor.
func Parse(xp string) (XPathExec, error) {
n, err := parser.Parse(xp)
return XPathExec{n: n}, err
}
//MustParse is like Parse, but panics instead of returning an error.
func MustParse(xp string) XPathExec {
ret, err := Parse(xp)
if err != nil {
panic(err)
}
return ret
}
//Exec executes the XPath expression, xp, against the tree, t, with the
//namespace mappings, ns, and returns the result as a stringer.
func (xp XPathExec) Exec(t tree.Node, opts ...FuncOpts) (tree.Result, error) {
o := &Opts{
NS: make(map[string]string),
Funcs: make(map[xml.Name]tree.Wrap),
Vars: make(map[string]tree.Result),
}
for _, i := range opts {
i(o)
}
return execxp.Exec(xp.n, t, o.NS, o.Funcs, o.Vars)
}
//ExecBool is like Exec, except it will attempt to convert the result to its boolean value.
func (xp XPathExec) ExecBool(t tree.Node, opts ...FuncOpts) (bool, error) {
res, err := xp.Exec(t, opts...)
if err != nil {
return false, err
}
b, ok := res.(tree.IsBool)
if !ok {
return false, fmt.Errorf("Cannot convert result to a boolean")
}
return bool(b.Bool()), nil
}
//ExecNum is like Exec, except it will attempt to convert the result to its number value.
func (xp XPathExec) ExecNum(t tree.Node, opts ...FuncOpts) (float64, error) {
res, err := xp.Exec(t, opts...)
if err != nil {
return 0, err
}
n, ok := res.(tree.IsNum)
if !ok {
return 0, fmt.Errorf("Cannot convert result to a number")
}
return float64(n.Num()), nil
}
//ExecNode is like Exec, except it will attempt to return the result as a node-set.
func (xp XPathExec) ExecNode(t tree.Node, opts ...FuncOpts) (tree.NodeSet, error) {
res, err := xp.Exec(t, opts...)
if err != nil {
return nil, err
}
n, ok := res.(tree.NodeSet)
if !ok {
return nil, fmt.Errorf("Cannot convert result to a node-set")
}
return n, nil
}
//MustExec is like Exec, but panics instead of returning an error.
func (xp XPathExec) MustExec(t tree.Node, opts ...FuncOpts) tree.Result {
res, err := xp.Exec(t, opts...)
if err != nil {
panic(err)
}
return res
}
//ParseExec parses the XPath string, xpstr, and runs Exec.
func ParseExec(xpstr string, t tree.Node, opts ...FuncOpts) (tree.Result, error) {
xp, err := Parse(xpstr)
if err != nil {
return nil, err
}
return xp.Exec(t, opts...)
}

View File

@ -0,0 +1,27 @@
package execxp
import (
"encoding/xml"
"github.com/ChrisTrenkamp/goxpath/internal/parser"
"github.com/ChrisTrenkamp/goxpath/tree"
)
//Exec executes the XPath expression, xp, against the tree, t, with the
//namespace mappings, ns.
func Exec(n *parser.Node, t tree.Node, ns map[string]string, fns map[xml.Name]tree.Wrap, v map[string]tree.Result) (tree.Result, error) {
f := xpFilt{
t: t,
ns: ns,
ctx: tree.NodeSet{t},
fns: fns,
variables: v,
}
return exec(&f, n)
}
func exec(f *xpFilt, n *parser.Node) (tree.Result, error) {
err := xfExec(f, n)
return f.ctx, err
}

View File

@ -0,0 +1,212 @@
package execxp
import (
"fmt"
"math"
"github.com/ChrisTrenkamp/goxpath/tree"
)
func bothNodeOperator(left tree.NodeSet, right tree.NodeSet, f *xpFilt, op string) error {
var err error
for _, l := range left {
for _, r := range right {
lStr := l.ResValue()
rStr := r.ResValue()
if eqOps[op] {
err = equalsOperator(tree.String(lStr), tree.String(rStr), f, op)
if err == nil && f.ctx.String() == tree.True {
return nil
}
} else {
err = numberOperator(tree.String(lStr), tree.String(rStr), f, op)
if err == nil && f.ctx.String() == tree.True {
return nil
}
}
}
}
f.ctx = tree.Bool(false)
return nil
}
func leftNodeOperator(left tree.NodeSet, right tree.Result, f *xpFilt, op string) error {
var err error
for _, l := range left {
lStr := l.ResValue()
if eqOps[op] {
err = equalsOperator(tree.String(lStr), right, f, op)
if err == nil && f.ctx.String() == tree.True {
return nil
}
} else {
err = numberOperator(tree.String(lStr), right, f, op)
if err == nil && f.ctx.String() == tree.True {
return nil
}
}
}
f.ctx = tree.Bool(false)
return nil
}
func rightNodeOperator(left tree.Result, right tree.NodeSet, f *xpFilt, op string) error {
var err error
for _, r := range right {
rStr := r.ResValue()
if eqOps[op] {
err = equalsOperator(left, tree.String(rStr), f, op)
if err == nil && f.ctx.String() == "true" {
return nil
}
} else {
err = numberOperator(left, tree.String(rStr), f, op)
if err == nil && f.ctx.String() == "true" {
return nil
}
}
}
f.ctx = tree.Bool(false)
return nil
}
func equalsOperator(left, right tree.Result, f *xpFilt, op string) error {
_, lOK := left.(tree.Bool)
_, rOK := right.(tree.Bool)
if lOK || rOK {
lTest, lt := left.(tree.IsBool)
rTest, rt := right.(tree.IsBool)
if !lt || !rt {
return fmt.Errorf("Cannot convert argument to boolean")
}
if op == "=" {
f.ctx = tree.Bool(lTest.Bool() == rTest.Bool())
} else {
f.ctx = tree.Bool(lTest.Bool() != rTest.Bool())
}
return nil
}
_, lOK = left.(tree.Num)
_, rOK = right.(tree.Num)
if lOK || rOK {
return numberOperator(left, right, f, op)
}
lStr := left.String()
rStr := right.String()
if op == "=" {
f.ctx = tree.Bool(lStr == rStr)
} else {
f.ctx = tree.Bool(lStr != rStr)
}
return nil
}
func numberOperator(left, right tree.Result, f *xpFilt, op string) error {
lt, lOK := left.(tree.IsNum)
rt, rOK := right.(tree.IsNum)
if !lOK || !rOK {
return fmt.Errorf("Cannot convert data type to number")
}
ln, rn := lt.Num(), rt.Num()
switch op {
case "*":
f.ctx = ln * rn
case "div":
if rn != 0 {
f.ctx = ln / rn
} else {
if ln == 0 {
f.ctx = tree.Num(math.NaN())
} else {
if math.Signbit(float64(ln)) == math.Signbit(float64(rn)) {
f.ctx = tree.Num(math.Inf(1))
} else {
f.ctx = tree.Num(math.Inf(-1))
}
}
}
case "mod":
f.ctx = tree.Num(int(ln) % int(rn))
case "+":
f.ctx = ln + rn
case "-":
f.ctx = ln - rn
case "=":
f.ctx = tree.Bool(ln == rn)
case "!=":
f.ctx = tree.Bool(ln != rn)
case "<":
f.ctx = tree.Bool(ln < rn)
case "<=":
f.ctx = tree.Bool(ln <= rn)
case ">":
f.ctx = tree.Bool(ln > rn)
case ">=":
f.ctx = tree.Bool(ln >= rn)
}
return nil
}
func andOrOperator(left, right tree.Result, f *xpFilt, op string) error {
lt, lOK := left.(tree.IsBool)
rt, rOK := right.(tree.IsBool)
if !lOK || !rOK {
return fmt.Errorf("Cannot convert argument to boolean")
}
l, r := lt.Bool(), rt.Bool()
if op == "and" {
f.ctx = l && r
} else {
f.ctx = l || r
}
return nil
}
func unionOperator(left, right tree.Result, f *xpFilt, op string) error {
lNode, lOK := left.(tree.NodeSet)
rNode, rOK := right.(tree.NodeSet)
if !lOK || !rOK {
return fmt.Errorf("Cannot convert data type to node-set")
}
uniq := make(map[int]tree.Node)
for _, i := range lNode {
uniq[i.Pos()] = i
}
for _, i := range rNode {
uniq[i.Pos()] = i
}
res := make(tree.NodeSet, 0, len(uniq))
for _, v := range uniq {
res = append(res, v)
}
f.ctx = res
return nil
}

View File

@ -0,0 +1,397 @@
package execxp
import (
"encoding/xml"
"fmt"
"strconv"
"strings"
"github.com/ChrisTrenkamp/goxpath/internal/parser"
"github.com/ChrisTrenkamp/goxpath/internal/parser/findutil"
"github.com/ChrisTrenkamp/goxpath/internal/parser/intfns"
"github.com/ChrisTrenkamp/goxpath/internal/xconst"
"github.com/ChrisTrenkamp/goxpath/internal/xsort"
"github.com/ChrisTrenkamp/goxpath/internal/lexer"
"github.com/ChrisTrenkamp/goxpath/internal/parser/pathexpr"
"github.com/ChrisTrenkamp/goxpath/tree"
)
type xpFilt struct {
t tree.Node
ctx tree.Result
expr pathexpr.PathExpr
ns map[string]string
ctxPos int
ctxSize int
proxPos map[int]int
fns map[xml.Name]tree.Wrap
variables map[string]tree.Result
}
type xpExecFn func(*xpFilt, string)
var xpFns = map[lexer.XItemType]xpExecFn{
lexer.XItemAbsLocPath: xfAbsLocPath,
lexer.XItemAbbrAbsLocPath: xfAbbrAbsLocPath,
lexer.XItemRelLocPath: xfRelLocPath,
lexer.XItemAbbrRelLocPath: xfAbbrRelLocPath,
lexer.XItemAxis: xfAxis,
lexer.XItemAbbrAxis: xfAbbrAxis,
lexer.XItemNCName: xfNCName,
lexer.XItemQName: xfQName,
lexer.XItemNodeType: xfNodeType,
lexer.XItemProcLit: xfProcInstLit,
lexer.XItemStrLit: xfStrLit,
lexer.XItemNumLit: xfNumLit,
}
func xfExec(f *xpFilt, n *parser.Node) (err error) {
for n != nil {
if fn, ok := xpFns[n.Val.Typ]; ok {
fn(f, n.Val.Val)
n = n.Left
} else if n.Val.Typ == lexer.XItemPredicate {
if err = xfPredicate(f, n.Left); err != nil {
return
}
n = n.Right
} else if n.Val.Typ == lexer.XItemFunction {
if err = xfFunction(f, n); err != nil {
return
}
n = n.Right
} else if n.Val.Typ == lexer.XItemOperator {
lf := xpFilt{
t: f.t,
ns: f.ns,
ctx: f.ctx,
ctxPos: f.ctxPos,
ctxSize: f.ctxSize,
proxPos: f.proxPos,
fns: f.fns,
variables: f.variables,
}
left, err := exec(&lf, n.Left)
if err != nil {
return err
}
rf := xpFilt{
t: f.t,
ns: f.ns,
ctx: f.ctx,
fns: f.fns,
variables: f.variables,
}
right, err := exec(&rf, n.Right)
if err != nil {
return err
}
return xfOperator(left, right, f, n.Val.Val)
} else if n.Val.Typ == lexer.XItemVariable {
if res, ok := f.variables[n.Val.Val]; ok {
f.ctx = res
return nil
}
return fmt.Errorf("Invalid variable '%s'", n.Val.Val)
} else if string(n.Val.Typ) == "" {
n = n.Left
//} else {
// return fmt.Errorf("Cannot process " + string(n.Val.Typ))
}
}
return
}
func xfPredicate(f *xpFilt, n *parser.Node) (err error) {
res := f.ctx.(tree.NodeSet)
newRes := make(tree.NodeSet, 0, len(res))
for i := range res {
pf := xpFilt{
t: f.t,
ns: f.ns,
ctxPos: i,
ctxSize: f.ctxSize,
ctx: tree.NodeSet{res[i]},
fns: f.fns,
variables: f.variables,
}
predRes, err := exec(&pf, n)
if err != nil {
return err
}
ok, err := checkPredRes(predRes, f, res[i])
if err != nil {
return err
}
if ok {
newRes = append(newRes, res[i])
}
}
f.proxPos = make(map[int]int)
for pos, j := range newRes {
f.proxPos[j.Pos()] = pos + 1
}
f.ctx = newRes
f.ctxSize = len(newRes)
return
}
func checkPredRes(ret tree.Result, f *xpFilt, node tree.Node) (bool, error) {
if num, ok := ret.(tree.Num); ok {
if float64(f.proxPos[node.Pos()]) == float64(num) {
return true, nil
}
return false, nil
}
if b, ok := ret.(tree.IsBool); ok {
return bool(b.Bool()), nil
}
return false, fmt.Errorf("Cannot convert argument to boolean")
}
func xfFunction(f *xpFilt, n *parser.Node) error {
spl := strings.Split(n.Val.Val, ":")
var name xml.Name
if len(spl) == 1 {
name.Local = spl[0]
} else {
name.Space = f.ns[spl[0]]
name.Local = spl[1]
}
fn, ok := intfns.BuiltIn[name]
if !ok {
fn, ok = f.fns[name]
}
if ok {
args := []tree.Result{}
param := n.Left
for param != nil {
pf := xpFilt{
t: f.t,
ctx: f.ctx,
ns: f.ns,
ctxPos: f.ctxPos,
ctxSize: f.ctxSize,
fns: f.fns,
variables: f.variables,
}
res, err := exec(&pf, param.Left)
if err != nil {
return err
}
args = append(args, res)
param = param.Right
}
filt, err := fn.Call(tree.Ctx{NodeSet: f.ctx.(tree.NodeSet), Size: f.ctxSize, Pos: f.ctxPos + 1}, args...)
f.ctx = filt
return err
}
return fmt.Errorf("Unknown function: %s", n.Val.Val)
}
var eqOps = map[string]bool{
"=": true,
"!=": true,
}
var booleanOps = map[string]bool{
"=": true,
"!=": true,
"<": true,
"<=": true,
">": true,
">=": true,
}
var numOps = map[string]bool{
"*": true,
"div": true,
"mod": true,
"+": true,
"-": true,
"=": true,
"!=": true,
"<": true,
"<=": true,
">": true,
">=": true,
}
var andOrOps = map[string]bool{
"and": true,
"or": true,
}
func xfOperator(left, right tree.Result, f *xpFilt, op string) error {
if booleanOps[op] {
lNode, lOK := left.(tree.NodeSet)
rNode, rOK := right.(tree.NodeSet)
if lOK && rOK {
return bothNodeOperator(lNode, rNode, f, op)
}
if lOK {
return leftNodeOperator(lNode, right, f, op)
}
if rOK {
return rightNodeOperator(left, rNode, f, op)
}
if eqOps[op] {
return equalsOperator(left, right, f, op)
}
}
if numOps[op] {
return numberOperator(left, right, f, op)
}
if andOrOps[op] {
return andOrOperator(left, right, f, op)
}
//if op == "|" {
return unionOperator(left, right, f, op)
//}
//return fmt.Errorf("Unknown operator " + op)
}
func xfAbsLocPath(f *xpFilt, val string) {
i := f.t
for i.GetNodeType() != tree.NtRoot {
i = i.GetParent()
}
f.ctx = tree.NodeSet{i}
}
func xfAbbrAbsLocPath(f *xpFilt, val string) {
i := f.t
for i.GetNodeType() != tree.NtRoot {
i = i.GetParent()
}
f.ctx = tree.NodeSet{i}
f.expr = abbrPathExpr()
find(f)
}
func xfRelLocPath(f *xpFilt, val string) {
}
func xfAbbrRelLocPath(f *xpFilt, val string) {
f.expr = abbrPathExpr()
find(f)
}
func xfAxis(f *xpFilt, val string) {
f.expr.Axis = val
}
func xfAbbrAxis(f *xpFilt, val string) {
f.expr.Axis = xconst.AxisAttribute
}
func xfNCName(f *xpFilt, val string) {
f.expr.Name.Space = val
}
func xfQName(f *xpFilt, val string) {
f.expr.Name.Local = val
find(f)
}
func xfNodeType(f *xpFilt, val string) {
f.expr.NodeType = val
find(f)
}
func xfProcInstLit(f *xpFilt, val string) {
filt := tree.NodeSet{}
for _, i := range f.ctx.(tree.NodeSet) {
if i.GetToken().(xml.ProcInst).Target == val {
filt = append(filt, i)
}
}
f.ctx = filt
}
func xfStrLit(f *xpFilt, val string) {
f.ctx = tree.String(val)
}
func xfNumLit(f *xpFilt, val string) {
num, _ := strconv.ParseFloat(val, 64)
f.ctx = tree.Num(num)
}
func abbrPathExpr() pathexpr.PathExpr {
return pathexpr.PathExpr{
Name: xml.Name{},
Axis: xconst.AxisDescendentOrSelf,
NodeType: xconst.NodeTypeNode,
}
}
func find(f *xpFilt) {
dupFilt := make(map[int]tree.Node)
f.proxPos = make(map[int]int)
if f.expr.Axis == "" && f.expr.NodeType == "" && f.expr.Name.Space == "" {
if f.expr.Name.Local == "." {
f.expr = pathexpr.PathExpr{
Name: xml.Name{},
Axis: xconst.AxisSelf,
NodeType: xconst.NodeTypeNode,
}
}
if f.expr.Name.Local == ".." {
f.expr = pathexpr.PathExpr{
Name: xml.Name{},
Axis: xconst.AxisParent,
NodeType: xconst.NodeTypeNode,
}
}
}
f.expr.NS = f.ns
for _, i := range f.ctx.(tree.NodeSet) {
for pos, j := range findutil.Find(i, f.expr) {
dupFilt[j.Pos()] = j
f.proxPos[j.Pos()] = pos + 1
}
}
res := make(tree.NodeSet, 0, len(dupFilt))
for _, i := range dupFilt {
res = append(res, i)
}
xsort.SortNodes(res)
f.expr = pathexpr.PathExpr{}
f.ctxSize = len(res)
f.ctx = res
}

View File

@ -0,0 +1,419 @@
package lexer
import (
"fmt"
"strings"
"unicode"
"unicode/utf8"
)
const (
//XItemError is an error with the parser input
XItemError XItemType = "Error"
//XItemAbsLocPath is an absolute path
XItemAbsLocPath = "Absolute path"
//XItemAbbrAbsLocPath represents an abbreviated absolute path
XItemAbbrAbsLocPath = "Abbreviated absolute path"
//XItemAbbrRelLocPath marks the start of a path expression
XItemAbbrRelLocPath = "Abbreviated relative path"
//XItemRelLocPath represents a relative location path
XItemRelLocPath = "Relative path"
//XItemEndPath marks the end of a path
XItemEndPath = "End path instruction"
//XItemAxis marks an axis specifier of a path
XItemAxis = "Axis"
//XItemAbbrAxis marks an abbreviated axis specifier (just @ at this point)
XItemAbbrAxis = "Abbreviated attribute axis"
//XItemNCName marks a namespace name in a node test
XItemNCName = "Namespace"
//XItemQName marks the local name in an a node test
XItemQName = "Local name"
//XItemNodeType marks a node type in a node test
XItemNodeType = "Node type"
//XItemProcLit marks a processing-instruction literal
XItemProcLit = "processing-instruction"
//XItemFunction marks a function call
XItemFunction = "function"
//XItemArgument marks a function argument
XItemArgument = "function argument"
//XItemEndFunction marks the end of a function
XItemEndFunction = "end of function"
//XItemPredicate marks a predicate in an axis
XItemPredicate = "predicate"
//XItemEndPredicate marks a predicate in an axis
XItemEndPredicate = "end of predicate"
//XItemStrLit marks a string literal
XItemStrLit = "string literal"
//XItemNumLit marks a numeric literal
XItemNumLit = "numeric literal"
//XItemOperator marks an operator
XItemOperator = "operator"
//XItemVariable marks a variable reference
XItemVariable = "variable"
)
const (
eof = -(iota + 1)
)
//XItemType is the parser token types
type XItemType string
//XItem is the token emitted from the parser
type XItem struct {
Typ XItemType
Val string
}
type stateFn func(*Lexer) stateFn
//Lexer lexes out XPath expressions
type Lexer struct {
input string
start int
pos int
width int
items chan XItem
}
//Lex an XPath expresion on the io.Reader
func Lex(xpath string) chan XItem {
l := &Lexer{
input: xpath,
items: make(chan XItem),
}
go l.run()
return l.items
}
func (l *Lexer) run() {
for state := startState; state != nil; {
state = state(l)
}
if l.peek() != eof {
l.errorf("Malformed XPath expression")
}
close(l.items)
}
func (l *Lexer) emit(t XItemType) {
l.items <- XItem{t, l.input[l.start:l.pos]}
l.start = l.pos
}
func (l *Lexer) emitVal(t XItemType, val string) {
l.items <- XItem{t, val}
l.start = l.pos
}
func (l *Lexer) next() (r rune) {
if l.pos >= len(l.input) {
l.width = 0
return eof
}
r, l.width = utf8.DecodeRuneInString(l.input[l.pos:])
l.pos += l.width
return r
}
func (l *Lexer) ignore() {
l.start = l.pos
}
func (l *Lexer) backup() {
l.pos -= l.width
}
func (l *Lexer) peek() rune {
r := l.next()
l.backup()
return r
}
func (l *Lexer) peekAt(n int) rune {
if n <= 1 {
return l.peek()
}
width := 0
var ret rune
for count := 0; count < n; count++ {
r, s := utf8.DecodeRuneInString(l.input[l.pos+width:])
width += s
if l.pos+width > len(l.input) {
return eof
}
ret = r
}
return ret
}
func (l *Lexer) accept(valid string) bool {
if strings.ContainsRune(valid, l.next()) {
return true
}
l.backup()
return false
}
func (l *Lexer) acceptRun(valid string) {
for strings.ContainsRune(valid, l.next()) {
}
l.backup()
}
func (l *Lexer) skip(num int) {
for i := 0; i < num; i++ {
l.next()
}
l.ignore()
}
func (l *Lexer) skipWS(ig bool) {
for {
n := l.next()
if n == eof || !unicode.IsSpace(n) {
break
}
}
l.backup()
if ig {
l.ignore()
}
}
func (l *Lexer) errorf(format string, args ...interface{}) stateFn {
l.items <- XItem{
XItemError,
fmt.Sprintf(format, args...),
}
return nil
}
func isElemChar(r rune) bool {
return string(r) != ":" && string(r) != "/" &&
(unicode.Is(first, r) || unicode.Is(second, r) || string(r) == "*") &&
r != eof
}
func startState(l *Lexer) stateFn {
l.skipWS(true)
if string(l.peek()) == "/" {
l.next()
l.ignore()
if string(l.next()) == "/" {
l.ignore()
return abbrAbsLocPathState
}
l.backup()
return absLocPathState
} else if string(l.peek()) == `'` || string(l.peek()) == `"` {
if err := getStrLit(l, XItemStrLit); err != nil {
return l.errorf(err.Error())
}
if l.peek() != eof {
return startState
}
} else if getNumLit(l) {
l.skipWS(true)
if l.peek() != eof {
return startState
}
} else if string(l.peek()) == "$" {
l.next()
l.ignore()
r := l.peek()
for unicode.Is(first, r) || unicode.Is(second, r) {
l.next()
r = l.peek()
}
tok := l.input[l.start:l.pos]
if len(tok) == 0 {
return l.errorf("Empty variable name")
}
l.emit(XItemVariable)
l.skipWS(true)
if l.peek() != eof {
return startState
}
} else if st := findOperatorState(l); st != nil {
return st
} else {
if isElemChar(l.peek()) {
colons := 0
for {
if isElemChar(l.peek()) {
l.next()
} else if string(l.peek()) == ":" {
l.next()
colons++
} else {
break
}
}
if string(l.peek()) == "(" && colons <= 1 {
tok := l.input[l.start:l.pos]
err := procFunc(l, tok)
if err != nil {
return l.errorf(err.Error())
}
l.skipWS(true)
if string(l.peek()) == "/" {
l.next()
l.ignore()
if string(l.next()) == "/" {
l.ignore()
return abbrRelLocPathState
}
l.backup()
return relLocPathState
}
return startState
}
l.pos = l.start
return relLocPathState
} else if string(l.peek()) == "@" {
return relLocPathState
}
}
return nil
}
func strPeek(str string, l *Lexer) bool {
for i := 0; i < len(str); i++ {
if string(l.peekAt(i+1)) != string(str[i]) {
return false
}
}
return true
}
func findOperatorState(l *Lexer) stateFn {
l.skipWS(true)
switch string(l.peek()) {
case ">", "<", "!":
l.next()
if string(l.peek()) == "=" {
l.next()
}
l.emit(XItemOperator)
return startState
case "|", "+", "-", "*", "=":
l.next()
l.emit(XItemOperator)
return startState
case "(":
l.next()
l.emit(XItemOperator)
for state := startState; state != nil; {
state = state(l)
}
l.skipWS(true)
if string(l.next()) != ")" {
return l.errorf("Missing end )")
}
l.emit(XItemOperator)
return startState
}
if strPeek("and", l) {
l.next()
l.next()
l.next()
l.emit(XItemOperator)
return startState
}
if strPeek("or", l) {
l.next()
l.next()
l.emit(XItemOperator)
return startState
}
if strPeek("mod", l) {
l.next()
l.next()
l.next()
l.emit(XItemOperator)
return startState
}
if strPeek("div", l) {
l.next()
l.next()
l.next()
l.emit(XItemOperator)
return startState
}
return nil
}
func getStrLit(l *Lexer, tok XItemType) error {
q := l.next()
var r rune
l.ignore()
for r != q {
r = l.next()
if r == eof {
return fmt.Errorf("Unexpected end of string literal.")
}
}
l.backup()
l.emit(tok)
l.next()
l.ignore()
return nil
}
func getNumLit(l *Lexer) bool {
const dig = "0123456789"
l.accept("-")
start := l.pos
l.acceptRun(dig)
if l.pos == start {
return false
}
if l.accept(".") {
l.acceptRun(dig)
}
l.emit(XItemNumLit)
return true
}

View File

@ -0,0 +1,219 @@
package lexer
import (
"fmt"
"github.com/ChrisTrenkamp/goxpath/internal/xconst"
)
func absLocPathState(l *Lexer) stateFn {
l.emit(XItemAbsLocPath)
return stepState
}
func abbrAbsLocPathState(l *Lexer) stateFn {
l.emit(XItemAbbrAbsLocPath)
return stepState
}
func relLocPathState(l *Lexer) stateFn {
l.emit(XItemRelLocPath)
return stepState
}
func abbrRelLocPathState(l *Lexer) stateFn {
l.emit(XItemAbbrRelLocPath)
return stepState
}
func stepState(l *Lexer) stateFn {
l.skipWS(true)
r := l.next()
for isElemChar(r) {
r = l.next()
}
l.backup()
tok := l.input[l.start:l.pos]
state, err := parseSeparators(l, tok)
if err != nil {
return l.errorf(err.Error())
}
return getNextPathState(l, state)
}
func parseSeparators(l *Lexer, tok string) (XItemType, error) {
l.skipWS(false)
state := XItemType(XItemQName)
r := l.peek()
if string(r) == ":" && string(l.peekAt(2)) == ":" {
var err error
if state, err = getAxis(l, tok); err != nil {
return state, fmt.Errorf(err.Error())
}
} else if string(r) == ":" {
state = XItemNCName
l.emitVal(state, tok)
l.skip(1)
l.skipWS(true)
} else if string(r) == "@" {
state = XItemAbbrAxis
l.emitVal(state, tok)
l.skip(1)
l.skipWS(true)
} else if string(r) == "(" {
var err error
if state, err = getNT(l, tok); err != nil {
return state, fmt.Errorf(err.Error())
}
} else if len(tok) > 0 {
l.emitVal(state, tok)
}
return state, nil
}
func getAxis(l *Lexer, tok string) (XItemType, error) {
var state XItemType
for i := range xconst.AxisNames {
if tok == xconst.AxisNames[i] {
state = XItemAxis
}
}
if state != XItemAxis {
return state, fmt.Errorf("Invalid Axis specifier, %s", tok)
}
l.emitVal(state, tok)
l.skip(2)
l.skipWS(true)
return state, nil
}
func getNT(l *Lexer, tok string) (XItemType, error) {
isNT := false
for _, i := range xconst.NodeTypes {
if tok == i {
isNT = true
break
}
}
if isNT {
return procNT(l, tok)
}
return XItemError, fmt.Errorf("Invalid node-type " + tok)
}
func procNT(l *Lexer, tok string) (XItemType, error) {
state := XItemType(XItemNodeType)
l.emitVal(state, tok)
l.skip(1)
l.skipWS(true)
n := l.peek()
if tok == xconst.NodeTypeProcInst && (string(n) == `"` || string(n) == `'`) {
if err := getStrLit(l, XItemProcLit); err != nil {
return state, fmt.Errorf(err.Error())
}
l.skipWS(true)
n = l.next()
}
if string(n) != ")" {
return state, fmt.Errorf("Missing ) at end of NodeType declaration.")
}
l.skip(1)
return state, nil
}
func procFunc(l *Lexer, tok string) error {
state := XItemType(XItemFunction)
l.emitVal(state, tok)
l.skip(1)
l.skipWS(true)
if string(l.peek()) != ")" {
l.emit(XItemArgument)
for {
for state := startState; state != nil; {
state = state(l)
}
l.skipWS(true)
if string(l.peek()) == "," {
l.emit(XItemArgument)
l.skip(1)
} else if string(l.peek()) == ")" {
l.emit(XItemEndFunction)
l.skip(1)
break
} else if l.peek() == eof {
return fmt.Errorf("Missing ) at end of function declaration.")
}
}
} else {
l.emit(XItemEndFunction)
l.skip(1)
}
return nil
}
func getNextPathState(l *Lexer, state XItemType) stateFn {
isMultiPart := state == XItemAxis || state == XItemAbbrAxis || state == XItemNCName
l.skipWS(true)
for string(l.peek()) == "[" {
if err := getPred(l); err != nil {
return l.errorf(err.Error())
}
}
if string(l.peek()) == "/" && !isMultiPart {
l.skip(1)
if string(l.peek()) == "/" {
l.skip(1)
return abbrRelLocPathState
}
l.skipWS(true)
return relLocPathState
} else if isMultiPart && isElemChar(l.peek()) {
return stepState
}
if isMultiPart {
return l.errorf("Step is not complete")
}
l.emit(XItemEndPath)
return findOperatorState
}
func getPred(l *Lexer) error {
l.emit(XItemPredicate)
l.skip(1)
l.skipWS(true)
if string(l.peek()) == "]" {
return fmt.Errorf("Missing content in predicate.")
}
for state := startState; state != nil; {
state = state(l)
}
l.skipWS(true)
if string(l.peek()) != "]" {
return fmt.Errorf("Missing ] at end of predicate.")
}
l.skip(1)
l.emit(XItemEndPredicate)
l.skipWS(true)
return nil
}

View File

@ -0,0 +1,316 @@
package lexer
import "unicode"
//first and second was copied from src/encoding/xml/xml.go
var first = &unicode.RangeTable{
R16: []unicode.Range16{
{0x003A, 0x003A, 1},
{0x0041, 0x005A, 1},
{0x005F, 0x005F, 1},
{0x0061, 0x007A, 1},
{0x00C0, 0x00D6, 1},
{0x00D8, 0x00F6, 1},
{0x00F8, 0x00FF, 1},
{0x0100, 0x0131, 1},
{0x0134, 0x013E, 1},
{0x0141, 0x0148, 1},
{0x014A, 0x017E, 1},
{0x0180, 0x01C3, 1},
{0x01CD, 0x01F0, 1},
{0x01F4, 0x01F5, 1},
{0x01FA, 0x0217, 1},
{0x0250, 0x02A8, 1},
{0x02BB, 0x02C1, 1},
{0x0386, 0x0386, 1},
{0x0388, 0x038A, 1},
{0x038C, 0x038C, 1},
{0x038E, 0x03A1, 1},
{0x03A3, 0x03CE, 1},
{0x03D0, 0x03D6, 1},
{0x03DA, 0x03E0, 2},
{0x03E2, 0x03F3, 1},
{0x0401, 0x040C, 1},
{0x040E, 0x044F, 1},
{0x0451, 0x045C, 1},
{0x045E, 0x0481, 1},
{0x0490, 0x04C4, 1},
{0x04C7, 0x04C8, 1},
{0x04CB, 0x04CC, 1},
{0x04D0, 0x04EB, 1},
{0x04EE, 0x04F5, 1},
{0x04F8, 0x04F9, 1},
{0x0531, 0x0556, 1},
{0x0559, 0x0559, 1},
{0x0561, 0x0586, 1},
{0x05D0, 0x05EA, 1},
{0x05F0, 0x05F2, 1},
{0x0621, 0x063A, 1},
{0x0641, 0x064A, 1},
{0x0671, 0x06B7, 1},
{0x06BA, 0x06BE, 1},
{0x06C0, 0x06CE, 1},
{0x06D0, 0x06D3, 1},
{0x06D5, 0x06D5, 1},
{0x06E5, 0x06E6, 1},
{0x0905, 0x0939, 1},
{0x093D, 0x093D, 1},
{0x0958, 0x0961, 1},
{0x0985, 0x098C, 1},
{0x098F, 0x0990, 1},
{0x0993, 0x09A8, 1},
{0x09AA, 0x09B0, 1},
{0x09B2, 0x09B2, 1},
{0x09B6, 0x09B9, 1},
{0x09DC, 0x09DD, 1},
{0x09DF, 0x09E1, 1},
{0x09F0, 0x09F1, 1},
{0x0A05, 0x0A0A, 1},
{0x0A0F, 0x0A10, 1},
{0x0A13, 0x0A28, 1},
{0x0A2A, 0x0A30, 1},
{0x0A32, 0x0A33, 1},
{0x0A35, 0x0A36, 1},
{0x0A38, 0x0A39, 1},
{0x0A59, 0x0A5C, 1},
{0x0A5E, 0x0A5E, 1},
{0x0A72, 0x0A74, 1},
{0x0A85, 0x0A8B, 1},
{0x0A8D, 0x0A8D, 1},
{0x0A8F, 0x0A91, 1},
{0x0A93, 0x0AA8, 1},
{0x0AAA, 0x0AB0, 1},
{0x0AB2, 0x0AB3, 1},
{0x0AB5, 0x0AB9, 1},
{0x0ABD, 0x0AE0, 0x23},
{0x0B05, 0x0B0C, 1},
{0x0B0F, 0x0B10, 1},
{0x0B13, 0x0B28, 1},
{0x0B2A, 0x0B30, 1},
{0x0B32, 0x0B33, 1},
{0x0B36, 0x0B39, 1},
{0x0B3D, 0x0B3D, 1},
{0x0B5C, 0x0B5D, 1},
{0x0B5F, 0x0B61, 1},
{0x0B85, 0x0B8A, 1},
{0x0B8E, 0x0B90, 1},
{0x0B92, 0x0B95, 1},
{0x0B99, 0x0B9A, 1},
{0x0B9C, 0x0B9C, 1},
{0x0B9E, 0x0B9F, 1},
{0x0BA3, 0x0BA4, 1},
{0x0BA8, 0x0BAA, 1},
{0x0BAE, 0x0BB5, 1},
{0x0BB7, 0x0BB9, 1},
{0x0C05, 0x0C0C, 1},
{0x0C0E, 0x0C10, 1},
{0x0C12, 0x0C28, 1},
{0x0C2A, 0x0C33, 1},
{0x0C35, 0x0C39, 1},
{0x0C60, 0x0C61, 1},
{0x0C85, 0x0C8C, 1},
{0x0C8E, 0x0C90, 1},
{0x0C92, 0x0CA8, 1},
{0x0CAA, 0x0CB3, 1},
{0x0CB5, 0x0CB9, 1},
{0x0CDE, 0x0CDE, 1},
{0x0CE0, 0x0CE1, 1},
{0x0D05, 0x0D0C, 1},
{0x0D0E, 0x0D10, 1},
{0x0D12, 0x0D28, 1},
{0x0D2A, 0x0D39, 1},
{0x0D60, 0x0D61, 1},
{0x0E01, 0x0E2E, 1},
{0x0E30, 0x0E30, 1},
{0x0E32, 0x0E33, 1},
{0x0E40, 0x0E45, 1},
{0x0E81, 0x0E82, 1},
{0x0E84, 0x0E84, 1},
{0x0E87, 0x0E88, 1},
{0x0E8A, 0x0E8D, 3},
{0x0E94, 0x0E97, 1},
{0x0E99, 0x0E9F, 1},
{0x0EA1, 0x0EA3, 1},
{0x0EA5, 0x0EA7, 2},
{0x0EAA, 0x0EAB, 1},
{0x0EAD, 0x0EAE, 1},
{0x0EB0, 0x0EB0, 1},
{0x0EB2, 0x0EB3, 1},
{0x0EBD, 0x0EBD, 1},
{0x0EC0, 0x0EC4, 1},
{0x0F40, 0x0F47, 1},
{0x0F49, 0x0F69, 1},
{0x10A0, 0x10C5, 1},
{0x10D0, 0x10F6, 1},
{0x1100, 0x1100, 1},
{0x1102, 0x1103, 1},
{0x1105, 0x1107, 1},
{0x1109, 0x1109, 1},
{0x110B, 0x110C, 1},
{0x110E, 0x1112, 1},
{0x113C, 0x1140, 2},
{0x114C, 0x1150, 2},
{0x1154, 0x1155, 1},
{0x1159, 0x1159, 1},
{0x115F, 0x1161, 1},
{0x1163, 0x1169, 2},
{0x116D, 0x116E, 1},
{0x1172, 0x1173, 1},
{0x1175, 0x119E, 0x119E - 0x1175},
{0x11A8, 0x11AB, 0x11AB - 0x11A8},
{0x11AE, 0x11AF, 1},
{0x11B7, 0x11B8, 1},
{0x11BA, 0x11BA, 1},
{0x11BC, 0x11C2, 1},
{0x11EB, 0x11F0, 0x11F0 - 0x11EB},
{0x11F9, 0x11F9, 1},
{0x1E00, 0x1E9B, 1},
{0x1EA0, 0x1EF9, 1},
{0x1F00, 0x1F15, 1},
{0x1F18, 0x1F1D, 1},
{0x1F20, 0x1F45, 1},
{0x1F48, 0x1F4D, 1},
{0x1F50, 0x1F57, 1},
{0x1F59, 0x1F5B, 0x1F5B - 0x1F59},
{0x1F5D, 0x1F5D, 1},
{0x1F5F, 0x1F7D, 1},
{0x1F80, 0x1FB4, 1},
{0x1FB6, 0x1FBC, 1},
{0x1FBE, 0x1FBE, 1},
{0x1FC2, 0x1FC4, 1},
{0x1FC6, 0x1FCC, 1},
{0x1FD0, 0x1FD3, 1},
{0x1FD6, 0x1FDB, 1},
{0x1FE0, 0x1FEC, 1},
{0x1FF2, 0x1FF4, 1},
{0x1FF6, 0x1FFC, 1},
{0x2126, 0x2126, 1},
{0x212A, 0x212B, 1},
{0x212E, 0x212E, 1},
{0x2180, 0x2182, 1},
{0x3007, 0x3007, 1},
{0x3021, 0x3029, 1},
{0x3041, 0x3094, 1},
{0x30A1, 0x30FA, 1},
{0x3105, 0x312C, 1},
{0x4E00, 0x9FA5, 1},
{0xAC00, 0xD7A3, 1},
},
}
var second = &unicode.RangeTable{
R16: []unicode.Range16{
{0x002D, 0x002E, 1},
{0x0030, 0x0039, 1},
{0x00B7, 0x00B7, 1},
{0x02D0, 0x02D1, 1},
{0x0300, 0x0345, 1},
{0x0360, 0x0361, 1},
{0x0387, 0x0387, 1},
{0x0483, 0x0486, 1},
{0x0591, 0x05A1, 1},
{0x05A3, 0x05B9, 1},
{0x05BB, 0x05BD, 1},
{0x05BF, 0x05BF, 1},
{0x05C1, 0x05C2, 1},
{0x05C4, 0x0640, 0x0640 - 0x05C4},
{0x064B, 0x0652, 1},
{0x0660, 0x0669, 1},
{0x0670, 0x0670, 1},
{0x06D6, 0x06DC, 1},
{0x06DD, 0x06DF, 1},
{0x06E0, 0x06E4, 1},
{0x06E7, 0x06E8, 1},
{0x06EA, 0x06ED, 1},
{0x06F0, 0x06F9, 1},
{0x0901, 0x0903, 1},
{0x093C, 0x093C, 1},
{0x093E, 0x094C, 1},
{0x094D, 0x094D, 1},
{0x0951, 0x0954, 1},
{0x0962, 0x0963, 1},
{0x0966, 0x096F, 1},
{0x0981, 0x0983, 1},
{0x09BC, 0x09BC, 1},
{0x09BE, 0x09BF, 1},
{0x09C0, 0x09C4, 1},
{0x09C7, 0x09C8, 1},
{0x09CB, 0x09CD, 1},
{0x09D7, 0x09D7, 1},
{0x09E2, 0x09E3, 1},
{0x09E6, 0x09EF, 1},
{0x0A02, 0x0A3C, 0x3A},
{0x0A3E, 0x0A3F, 1},
{0x0A40, 0x0A42, 1},
{0x0A47, 0x0A48, 1},
{0x0A4B, 0x0A4D, 1},
{0x0A66, 0x0A6F, 1},
{0x0A70, 0x0A71, 1},
{0x0A81, 0x0A83, 1},
{0x0ABC, 0x0ABC, 1},
{0x0ABE, 0x0AC5, 1},
{0x0AC7, 0x0AC9, 1},
{0x0ACB, 0x0ACD, 1},
{0x0AE6, 0x0AEF, 1},
{0x0B01, 0x0B03, 1},
{0x0B3C, 0x0B3C, 1},
{0x0B3E, 0x0B43, 1},
{0x0B47, 0x0B48, 1},
{0x0B4B, 0x0B4D, 1},
{0x0B56, 0x0B57, 1},
{0x0B66, 0x0B6F, 1},
{0x0B82, 0x0B83, 1},
{0x0BBE, 0x0BC2, 1},
{0x0BC6, 0x0BC8, 1},
{0x0BCA, 0x0BCD, 1},
{0x0BD7, 0x0BD7, 1},
{0x0BE7, 0x0BEF, 1},
{0x0C01, 0x0C03, 1},
{0x0C3E, 0x0C44, 1},
{0x0C46, 0x0C48, 1},
{0x0C4A, 0x0C4D, 1},
{0x0C55, 0x0C56, 1},
{0x0C66, 0x0C6F, 1},
{0x0C82, 0x0C83, 1},
{0x0CBE, 0x0CC4, 1},
{0x0CC6, 0x0CC8, 1},
{0x0CCA, 0x0CCD, 1},
{0x0CD5, 0x0CD6, 1},
{0x0CE6, 0x0CEF, 1},
{0x0D02, 0x0D03, 1},
{0x0D3E, 0x0D43, 1},
{0x0D46, 0x0D48, 1},
{0x0D4A, 0x0D4D, 1},
{0x0D57, 0x0D57, 1},
{0x0D66, 0x0D6F, 1},
{0x0E31, 0x0E31, 1},
{0x0E34, 0x0E3A, 1},
{0x0E46, 0x0E46, 1},
{0x0E47, 0x0E4E, 1},
{0x0E50, 0x0E59, 1},
{0x0EB1, 0x0EB1, 1},
{0x0EB4, 0x0EB9, 1},
{0x0EBB, 0x0EBC, 1},
{0x0EC6, 0x0EC6, 1},
{0x0EC8, 0x0ECD, 1},
{0x0ED0, 0x0ED9, 1},
{0x0F18, 0x0F19, 1},
{0x0F20, 0x0F29, 1},
{0x0F35, 0x0F39, 2},
{0x0F3E, 0x0F3F, 1},
{0x0F71, 0x0F84, 1},
{0x0F86, 0x0F8B, 1},
{0x0F90, 0x0F95, 1},
{0x0F97, 0x0F97, 1},
{0x0F99, 0x0FAD, 1},
{0x0FB1, 0x0FB7, 1},
{0x0FB9, 0x0FB9, 1},
{0x20D0, 0x20DC, 1},
{0x20E1, 0x3005, 0x3005 - 0x20E1},
{0x302A, 0x302F, 1},
{0x3031, 0x3035, 1},
{0x3099, 0x309A, 1},
{0x309D, 0x309E, 1},
{0x30FC, 0x30FE, 1},
},
}

View File

@ -0,0 +1,113 @@
package parser
import "github.com/ChrisTrenkamp/goxpath/internal/lexer"
//NodeType enumerations
const (
Empty lexer.XItemType = ""
)
//Node builds an AST tree for operating on XPath expressions
type Node struct {
Val lexer.XItem
Left *Node
Right *Node
Parent *Node
next *Node
}
var beginPathType = map[lexer.XItemType]bool{
lexer.XItemAbsLocPath: true,
lexer.XItemAbbrAbsLocPath: true,
lexer.XItemAbbrRelLocPath: true,
lexer.XItemRelLocPath: true,
lexer.XItemFunction: true,
}
func (n *Node) add(i lexer.XItem) {
if n.Val.Typ == Empty {
n.Val = i
} else if n.Left == nil {
n.Left = &Node{Val: n.Val, Parent: n}
n.Val = i
} else if beginPathType[n.Val.Typ] {
next := &Node{Val: n.Val, Left: n.Left, Parent: n}
n.Left = next
n.Val = i
} else if n.Right == nil {
n.Right = &Node{Val: i, Parent: n}
} else {
next := &Node{Val: n.Val, Left: n.Left, Right: n.Right, Parent: n}
n.Left, n.Right = next, nil
n.Val = i
}
n.next = n
}
func (n *Node) push(i lexer.XItem) {
if n.Left == nil {
n.Left = &Node{Val: i, Parent: n}
n.next = n.Left
} else if n.Right == nil {
n.Right = &Node{Val: i, Parent: n}
n.next = n.Right
} else {
next := &Node{Val: i, Left: n.Right, Parent: n}
n.Right = next
n.next = n.Right
}
}
func (n *Node) pushNotEmpty(i lexer.XItem) {
if n.Val.Typ == Empty {
n.add(i)
} else {
n.push(i)
}
}
/*
func (n *Node) prettyPrint(depth, width int) {
nodes := []*Node{}
n.getLine(depth, &nodes)
fmt.Printf("%*s", (width-depth)*2, "")
toggle := true
if len(nodes) > 1 {
for _, i := range nodes {
if i != nil {
if toggle {
fmt.Print("/ ")
} else {
fmt.Print("\\ ")
}
}
toggle = !toggle
}
fmt.Println()
fmt.Printf("%*s", (width-depth)*2, "")
}
for _, i := range nodes {
if i != nil {
fmt.Print(i.Val.Val, " ")
}
}
fmt.Println()
}
func (n *Node) getLine(depth int, ret *[]*Node) {
if depth <= 0 && n != nil {
*ret = append(*ret, n)
return
}
if n.Left != nil {
n.Left.getLine(depth-1, ret)
} else if depth-1 <= 0 {
*ret = append(*ret, nil)
}
if n.Right != nil {
n.Right.getLine(depth-1, ret)
} else if depth-1 <= 0 {
*ret = append(*ret, nil)
}
}
*/

View File

@ -0,0 +1,307 @@
package findutil
import (
"encoding/xml"
"github.com/ChrisTrenkamp/goxpath/internal/parser/pathexpr"
"github.com/ChrisTrenkamp/goxpath/internal/xconst"
"github.com/ChrisTrenkamp/goxpath/tree"
)
const (
wildcard = "*"
)
type findFunc func(tree.Node, *pathexpr.PathExpr, *[]tree.Node)
var findMap = map[string]findFunc{
xconst.AxisAncestor: findAncestor,
xconst.AxisAncestorOrSelf: findAncestorOrSelf,
xconst.AxisAttribute: findAttribute,
xconst.AxisChild: findChild,
xconst.AxisDescendent: findDescendent,
xconst.AxisDescendentOrSelf: findDescendentOrSelf,
xconst.AxisFollowing: findFollowing,
xconst.AxisFollowingSibling: findFollowingSibling,
xconst.AxisNamespace: findNamespace,
xconst.AxisParent: findParent,
xconst.AxisPreceding: findPreceding,
xconst.AxisPrecedingSibling: findPrecedingSibling,
xconst.AxisSelf: findSelf,
}
//Find finds nodes based on the pathexpr.PathExpr
func Find(x tree.Node, p pathexpr.PathExpr) []tree.Node {
ret := []tree.Node{}
if p.Axis == "" {
findChild(x, &p, &ret)
return ret
}
f := findMap[p.Axis]
f(x, &p, &ret)
return ret
}
func findAncestor(x tree.Node, p *pathexpr.PathExpr, ret *[]tree.Node) {
if x.GetNodeType() == tree.NtRoot {
return
}
addNode(x.GetParent(), p, ret)
findAncestor(x.GetParent(), p, ret)
}
func findAncestorOrSelf(x tree.Node, p *pathexpr.PathExpr, ret *[]tree.Node) {
findSelf(x, p, ret)
findAncestor(x, p, ret)
}
func findAttribute(x tree.Node, p *pathexpr.PathExpr, ret *[]tree.Node) {
if ele, ok := x.(tree.Elem); ok {
for _, i := range ele.GetAttrs() {
addNode(i, p, ret)
}
}
}
func findChild(x tree.Node, p *pathexpr.PathExpr, ret *[]tree.Node) {
if ele, ok := x.(tree.Elem); ok {
ch := ele.GetChildren()
for i := range ch {
addNode(ch[i], p, ret)
}
}
}
func findDescendent(x tree.Node, p *pathexpr.PathExpr, ret *[]tree.Node) {
if ele, ok := x.(tree.Elem); ok {
ch := ele.GetChildren()
for i := range ch {
addNode(ch[i], p, ret)
findDescendent(ch[i], p, ret)
}
}
}
func findDescendentOrSelf(x tree.Node, p *pathexpr.PathExpr, ret *[]tree.Node) {
findSelf(x, p, ret)
findDescendent(x, p, ret)
}
func findFollowing(x tree.Node, p *pathexpr.PathExpr, ret *[]tree.Node) {
if x.GetNodeType() == tree.NtRoot {
return
}
par := x.GetParent()
ch := par.GetChildren()
i := 0
for x != ch[i] {
i++
}
i++
for i < len(ch) {
findDescendentOrSelf(ch[i], p, ret)
i++
}
findFollowing(par, p, ret)
}
func findFollowingSibling(x tree.Node, p *pathexpr.PathExpr, ret *[]tree.Node) {
if x.GetNodeType() == tree.NtRoot {
return
}
par := x.GetParent()
ch := par.GetChildren()
i := 0
for x != ch[i] {
i++
}
i++
for i < len(ch) {
findSelf(ch[i], p, ret)
i++
}
}
func findNamespace(x tree.Node, p *pathexpr.PathExpr, ret *[]tree.Node) {
if ele, ok := x.(tree.NSElem); ok {
ns := tree.BuildNS(ele)
for _, i := range ns {
addNode(i, p, ret)
}
}
}
func findParent(x tree.Node, p *pathexpr.PathExpr, ret *[]tree.Node) {
if x.GetNodeType() != tree.NtRoot {
addNode(x.GetParent(), p, ret)
}
}
func findPreceding(x tree.Node, p *pathexpr.PathExpr, ret *[]tree.Node) {
if x.GetNodeType() == tree.NtRoot {
return
}
par := x.GetParent()
ch := par.GetChildren()
i := len(ch) - 1
for x != ch[i] {
i--
}
i--
for i >= 0 {
findDescendentOrSelf(ch[i], p, ret)
i--
}
findPreceding(par, p, ret)
}
func findPrecedingSibling(x tree.Node, p *pathexpr.PathExpr, ret *[]tree.Node) {
if x.GetNodeType() == tree.NtRoot {
return
}
par := x.GetParent()
ch := par.GetChildren()
i := len(ch) - 1
for x != ch[i] {
i--
}
i--
for i >= 0 {
findSelf(ch[i], p, ret)
i--
}
}
func findSelf(x tree.Node, p *pathexpr.PathExpr, ret *[]tree.Node) {
addNode(x, p, ret)
}
func addNode(x tree.Node, p *pathexpr.PathExpr, ret *[]tree.Node) {
add := false
tok := x.GetToken()
switch x.GetNodeType() {
case tree.NtAttr:
add = evalAttr(p, tok.(xml.Attr))
case tree.NtChd:
add = evalChd(p)
case tree.NtComm:
add = evalComm(p)
case tree.NtElem, tree.NtRoot:
add = evalEle(p, tok.(xml.StartElement))
case tree.NtNs:
add = evalNS(p, tok.(xml.Attr))
case tree.NtPi:
add = evalPI(p)
}
if add {
*ret = append(*ret, x)
}
}
func evalAttr(p *pathexpr.PathExpr, a xml.Attr) bool {
if p.NodeType == "" {
if p.Name.Space != wildcard {
if a.Name.Space != p.NS[p.Name.Space] {
return false
}
}
if p.Name.Local == wildcard && p.Axis == xconst.AxisAttribute {
return true
}
if p.Name.Local == a.Name.Local {
return true
}
} else {
if p.NodeType == xconst.NodeTypeNode {
return true
}
}
return false
}
func evalChd(p *pathexpr.PathExpr) bool {
if p.NodeType == xconst.NodeTypeText || p.NodeType == xconst.NodeTypeNode {
return true
}
return false
}
func evalComm(p *pathexpr.PathExpr) bool {
if p.NodeType == xconst.NodeTypeComment || p.NodeType == xconst.NodeTypeNode {
return true
}
return false
}
func evalEle(p *pathexpr.PathExpr, ele xml.StartElement) bool {
if p.NodeType == "" {
return checkNameAndSpace(p, ele)
}
if p.NodeType == xconst.NodeTypeNode {
return true
}
return false
}
func checkNameAndSpace(p *pathexpr.PathExpr, ele xml.StartElement) bool {
if p.Name.Local == wildcard && p.Name.Space == "" {
return true
}
if p.Name.Space != wildcard && ele.Name.Space != p.NS[p.Name.Space] {
return false
}
if p.Name.Local == wildcard && p.Axis != xconst.AxisAttribute && p.Axis != xconst.AxisNamespace {
return true
}
if p.Name.Local == ele.Name.Local {
return true
}
return false
}
func evalNS(p *pathexpr.PathExpr, ns xml.Attr) bool {
if p.NodeType == "" {
if p.Name.Space != "" && p.Name.Space != wildcard {
return false
}
if p.Name.Local == wildcard && p.Axis == xconst.AxisNamespace {
return true
}
if p.Name.Local == ns.Name.Local {
return true
}
} else {
if p.NodeType == xconst.NodeTypeNode {
return true
}
}
return false
}
func evalPI(p *pathexpr.PathExpr) bool {
if p.NodeType == xconst.NodeTypeProcInst || p.NodeType == xconst.NodeTypeNode {
return true
}
return false
}

View File

@ -0,0 +1,74 @@
package intfns
import (
"fmt"
"github.com/ChrisTrenkamp/goxpath/tree"
"golang.org/x/text/language"
)
func boolean(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
if b, ok := args[0].(tree.IsBool); ok {
return b.Bool(), nil
}
return nil, fmt.Errorf("Cannot convert object to a boolean")
}
func not(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
b, ok := args[0].(tree.IsBool)
if !ok {
return nil, fmt.Errorf("Cannot convert object to a boolean")
}
return !b.Bool(), nil
}
func _true(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
return tree.Bool(true), nil
}
func _false(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
return tree.Bool(false), nil
}
func lang(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
lStr := args[0].String()
var n tree.Elem
for _, i := range c.NodeSet {
if i.GetNodeType() == tree.NtElem {
n = i.(tree.Elem)
} else {
n = i.GetParent()
}
for n.GetNodeType() != tree.NtRoot {
if attr, ok := tree.GetAttribute(n, "lang", tree.XMLSpace); ok {
return checkLang(lStr, attr.Value), nil
}
n = n.GetParent()
}
}
return tree.Bool(false), nil
}
func checkLang(srcStr, targStr string) tree.Bool {
srcLang := language.Make(srcStr)
srcRegion, srcRegionConf := srcLang.Region()
targLang := language.Make(targStr)
targRegion, targRegionConf := targLang.Region()
if srcRegionConf == language.Exact && targRegionConf != language.Exact {
return tree.Bool(false)
}
if srcRegion != targRegion && srcRegionConf == language.Exact && targRegionConf == language.Exact {
return tree.Bool(false)
}
_, _, conf := language.NewMatcher([]language.Tag{srcLang}).Match(targLang)
return tree.Bool(conf >= language.High)
}

View File

@ -0,0 +1,41 @@
package intfns
import (
"encoding/xml"
"github.com/ChrisTrenkamp/goxpath/tree"
)
//BuiltIn contains the list of built-in XPath functions
var BuiltIn = map[xml.Name]tree.Wrap{
//String functions
{Local: "string"}: {Fn: _string, NArgs: 1, LastArgOpt: tree.Optional},
{Local: "concat"}: {Fn: concat, NArgs: 3, LastArgOpt: tree.Variadic},
{Local: "starts-with"}: {Fn: startsWith, NArgs: 2},
{Local: "contains"}: {Fn: contains, NArgs: 2},
{Local: "substring-before"}: {Fn: substringBefore, NArgs: 2},
{Local: "substring-after"}: {Fn: substringAfter, NArgs: 2},
{Local: "substring"}: {Fn: substring, NArgs: 3, LastArgOpt: tree.Optional},
{Local: "string-length"}: {Fn: stringLength, NArgs: 1, LastArgOpt: tree.Optional},
{Local: "normalize-space"}: {Fn: normalizeSpace, NArgs: 1, LastArgOpt: tree.Optional},
{Local: "translate"}: {Fn: translate, NArgs: 3},
//Node set functions
{Local: "last"}: {Fn: last},
{Local: "position"}: {Fn: position},
{Local: "count"}: {Fn: count, NArgs: 1},
{Local: "local-name"}: {Fn: localName, NArgs: 1, LastArgOpt: tree.Optional},
{Local: "namespace-uri"}: {Fn: namespaceURI, NArgs: 1, LastArgOpt: tree.Optional},
{Local: "name"}: {Fn: name, NArgs: 1, LastArgOpt: tree.Optional},
//boolean functions
{Local: "boolean"}: {Fn: boolean, NArgs: 1},
{Local: "not"}: {Fn: not, NArgs: 1},
{Local: "true"}: {Fn: _true},
{Local: "false"}: {Fn: _false},
{Local: "lang"}: {Fn: lang, NArgs: 1},
//number functions
{Local: "number"}: {Fn: number, NArgs: 1, LastArgOpt: tree.Optional},
{Local: "sum"}: {Fn: sum, NArgs: 1},
{Local: "floor"}: {Fn: floor, NArgs: 1},
{Local: "ceiling"}: {Fn: ceiling, NArgs: 1},
{Local: "round"}: {Fn: round, NArgs: 1},
}

View File

@ -0,0 +1,131 @@
package intfns
import (
"encoding/xml"
"fmt"
"github.com/ChrisTrenkamp/goxpath/tree"
)
func last(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
return tree.Num(c.Size), nil
}
func position(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
return tree.Num(c.Pos), nil
}
func count(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
n, ok := args[0].(tree.NodeSet)
if !ok {
return nil, fmt.Errorf("Cannot convert object to a node-set")
}
return tree.Num(len(n)), nil
}
func localName(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
var n tree.NodeSet
ok := true
if len(args) == 1 {
n, ok = args[0].(tree.NodeSet)
} else {
n = c.NodeSet
}
if !ok {
return nil, fmt.Errorf("Cannot convert object to a node-set")
}
ret := ""
if len(n) == 0 {
return tree.String(ret), nil
}
node := n[0]
tok := node.GetToken()
switch node.GetNodeType() {
case tree.NtElem:
ret = tok.(xml.StartElement).Name.Local
case tree.NtAttr:
ret = tok.(xml.Attr).Name.Local
case tree.NtPi:
ret = tok.(xml.ProcInst).Target
}
return tree.String(ret), nil
}
func namespaceURI(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
var n tree.NodeSet
ok := true
if len(args) == 1 {
n, ok = args[0].(tree.NodeSet)
} else {
n = c.NodeSet
}
if !ok {
return nil, fmt.Errorf("Cannot convert object to a node-set")
}
ret := ""
if len(n) == 0 {
return tree.String(ret), nil
}
node := n[0]
tok := node.GetToken()
switch node.GetNodeType() {
case tree.NtElem:
ret = tok.(xml.StartElement).Name.Space
case tree.NtAttr:
ret = tok.(xml.Attr).Name.Space
}
return tree.String(ret), nil
}
func name(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
var n tree.NodeSet
ok := true
if len(args) == 1 {
n, ok = args[0].(tree.NodeSet)
} else {
n = c.NodeSet
}
if !ok {
return nil, fmt.Errorf("Cannot convert object to a node-set")
}
ret := ""
if len(n) == 0 {
return tree.String(ret), nil
}
node := n[0]
switch node.GetNodeType() {
case tree.NtElem:
t := node.GetToken().(xml.StartElement)
space := ""
if t.Name.Space != "" {
space = fmt.Sprintf("{%s}", t.Name.Space)
}
ret = fmt.Sprintf("%s%s", space, t.Name.Local)
case tree.NtAttr:
t := node.GetToken().(xml.Attr)
space := ""
if t.Name.Space != "" {
space = fmt.Sprintf("{%s}", t.Name.Space)
}
ret = fmt.Sprintf("%s%s", space, t.Name.Local)
case tree.NtPi:
ret = fmt.Sprintf("%s", node.GetToken().(xml.ProcInst).Target)
}
return tree.String(ret), nil
}

View File

@ -0,0 +1,71 @@
package intfns
import (
"fmt"
"math"
"github.com/ChrisTrenkamp/goxpath/tree"
)
func number(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
if b, ok := args[0].(tree.IsNum); ok {
return b.Num(), nil
}
return nil, fmt.Errorf("Cannot convert object to a number")
}
func sum(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
n, ok := args[0].(tree.NodeSet)
if !ok {
return nil, fmt.Errorf("Cannot convert object to a node-set")
}
ret := 0.0
for _, i := range n {
ret += float64(tree.GetNodeNum(i))
}
return tree.Num(ret), nil
}
func floor(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
n, ok := args[0].(tree.IsNum)
if !ok {
return nil, fmt.Errorf("Cannot convert object to a number")
}
return tree.Num(math.Floor(float64(n.Num()))), nil
}
func ceiling(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
n, ok := args[0].(tree.IsNum)
if !ok {
return nil, fmt.Errorf("Cannot convert object to a number")
}
return tree.Num(math.Ceil(float64(n.Num()))), nil
}
func round(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
isn, ok := args[0].(tree.IsNum)
if !ok {
return nil, fmt.Errorf("Cannot convert object to a number")
}
n := isn.Num()
if math.IsNaN(float64(n)) || math.IsInf(float64(n), 0) {
return n, nil
}
if n < -0.5 {
n = tree.Num(int(n - 0.5))
} else if n > 0.5 {
n = tree.Num(int(n + 0.5))
} else {
n = 0
}
return n, nil
}

View File

@ -0,0 +1,141 @@
package intfns
import (
"math"
"regexp"
"strings"
"github.com/ChrisTrenkamp/goxpath/tree"
)
func _string(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
if len(args) == 1 {
return tree.String(args[0].String()), nil
}
return tree.String(c.NodeSet.String()), nil
}
func concat(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
ret := ""
for _, i := range args {
ret += i.String()
}
return tree.String(ret), nil
}
func startsWith(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
return tree.Bool(strings.Index(args[0].String(), args[1].String()) == 0), nil
}
func contains(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
return tree.Bool(strings.Contains(args[0].String(), args[1].String())), nil
}
func substringBefore(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
ind := strings.Index(args[0].String(), args[1].String())
if ind == -1 {
return tree.String(""), nil
}
return tree.String(args[0].String()[:ind]), nil
}
func substringAfter(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
ind := strings.Index(args[0].String(), args[1].String())
if ind == -1 {
return tree.String(""), nil
}
return tree.String(args[0].String()[ind+len(args[1].String()):]), nil
}
func substring(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
str := args[0].String()
bNum, bErr := round(c, args[1])
if bErr != nil {
return nil, bErr
}
b := bNum.(tree.Num).Num()
if float64(b-1) >= float64(len(str)) || math.IsNaN(float64(b)) {
return tree.String(""), nil
}
if len(args) == 2 {
if b <= 1 {
b = 1
}
return tree.String(str[int(b)-1:]), nil
}
eNum, eErr := round(c, args[2])
if eErr != nil {
return nil, eErr
}
e := eNum.(tree.Num).Num()
if e <= 0 || math.IsNaN(float64(e)) || (math.IsInf(float64(b), 0) && math.IsInf(float64(e), 0)) {
return tree.String(""), nil
}
if b <= 1 {
e = b + e - 1
b = 1
}
if float64(b+e-1) >= float64(len(str)) {
e = tree.Num(len(str)) - b + 1
}
return tree.String(str[int(b)-1 : int(b+e)-1]), nil
}
func stringLength(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
var str string
if len(args) == 1 {
str = args[0].String()
} else {
str = c.NodeSet.String()
}
return tree.Num(len(str)), nil
}
var spaceTrim = regexp.MustCompile(`\s+`)
func normalizeSpace(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
var str string
if len(args) == 1 {
str = args[0].String()
} else {
str = c.NodeSet.String()
}
str = strings.TrimSpace(str)
return tree.String(spaceTrim.ReplaceAllString(str, " ")), nil
}
func translate(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
ret := args[0].String()
src := args[1].String()
repl := args[2].String()
for i := range src {
r := ""
if i < len(repl) {
r = string(repl[i])
}
ret = strings.Replace(ret, string(src[i]), r, -1)
}
return tree.String(ret), nil
}

View File

@ -0,0 +1,194 @@
package parser
import (
"fmt"
"github.com/ChrisTrenkamp/goxpath/internal/lexer"
)
type stateType int
const (
defState stateType = iota
xpathState
funcState
paramState
predState
parenState
)
type parseStack struct {
stack []*Node
stateTypes []stateType
cur *Node
}
func (p *parseStack) push(t stateType) {
p.stack = append(p.stack, p.cur)
p.stateTypes = append(p.stateTypes, t)
}
func (p *parseStack) pop() {
stackPos := len(p.stack) - 1
p.cur = p.stack[stackPos]
p.stack = p.stack[:stackPos]
p.stateTypes = p.stateTypes[:stackPos]
}
func (p *parseStack) curState() stateType {
if len(p.stateTypes) == 0 {
return defState
}
return p.stateTypes[len(p.stateTypes)-1]
}
type lexFn func(*parseStack, lexer.XItem)
var parseMap = map[lexer.XItemType]lexFn{
lexer.XItemAbsLocPath: xiXPath,
lexer.XItemAbbrAbsLocPath: xiXPath,
lexer.XItemAbbrRelLocPath: xiXPath,
lexer.XItemRelLocPath: xiXPath,
lexer.XItemEndPath: xiEndPath,
lexer.XItemAxis: xiXPath,
lexer.XItemAbbrAxis: xiXPath,
lexer.XItemNCName: xiXPath,
lexer.XItemQName: xiXPath,
lexer.XItemNodeType: xiXPath,
lexer.XItemProcLit: xiXPath,
lexer.XItemFunction: xiFunc,
lexer.XItemArgument: xiFuncArg,
lexer.XItemEndFunction: xiEndFunc,
lexer.XItemPredicate: xiPred,
lexer.XItemEndPredicate: xiEndPred,
lexer.XItemStrLit: xiValue,
lexer.XItemNumLit: xiValue,
lexer.XItemOperator: xiOp,
lexer.XItemVariable: xiValue,
}
var opPrecedence = map[string]int{
"|": 1,
"*": 2,
"div": 2,
"mod": 2,
"+": 3,
"-": 3,
"=": 4,
"!=": 4,
"<": 4,
"<=": 4,
">": 4,
">=": 4,
"and": 5,
"or": 6,
}
//Parse creates an AST tree for XPath expressions.
func Parse(xp string) (*Node, error) {
var err error
c := lexer.Lex(xp)
n := &Node{}
p := &parseStack{cur: n}
for next := range c {
if next.Typ != lexer.XItemError {
parseMap[next.Typ](p, next)
} else if err == nil {
err = fmt.Errorf(next.Val)
}
}
return n, err
}
func xiXPath(p *parseStack, i lexer.XItem) {
if p.curState() == xpathState {
p.cur.push(i)
p.cur = p.cur.next
return
}
if p.cur.Val.Typ == lexer.XItemFunction {
p.cur.Right = &Node{Val: i, Parent: p.cur}
p.cur.next = p.cur.Right
p.push(xpathState)
p.cur = p.cur.next
return
}
p.cur.pushNotEmpty(i)
p.push(xpathState)
p.cur = p.cur.next
}
func xiEndPath(p *parseStack, i lexer.XItem) {
p.pop()
}
func xiFunc(p *parseStack, i lexer.XItem) {
p.cur.push(i)
p.cur = p.cur.next
p.push(funcState)
}
func xiFuncArg(p *parseStack, i lexer.XItem) {
if p.curState() != funcState {
p.pop()
}
p.cur.push(i)
p.cur = p.cur.next
p.push(paramState)
p.cur.push(lexer.XItem{Typ: Empty, Val: ""})
p.cur = p.cur.next
}
func xiEndFunc(p *parseStack, i lexer.XItem) {
if p.curState() == paramState {
p.pop()
}
p.pop()
}
func xiPred(p *parseStack, i lexer.XItem) {
p.cur.push(i)
p.cur = p.cur.next
p.push(predState)
p.cur.push(lexer.XItem{Typ: Empty, Val: ""})
p.cur = p.cur.next
}
func xiEndPred(p *parseStack, i lexer.XItem) {
p.pop()
}
func xiValue(p *parseStack, i lexer.XItem) {
p.cur.add(i)
}
func xiOp(p *parseStack, i lexer.XItem) {
if i.Val == "(" {
p.cur.push(lexer.XItem{Typ: Empty, Val: ""})
p.push(parenState)
p.cur = p.cur.next
return
}
if i.Val == ")" {
p.pop()
return
}
if p.cur.Val.Typ == lexer.XItemOperator {
if opPrecedence[p.cur.Val.Val] <= opPrecedence[i.Val] {
p.cur.add(i)
} else {
p.cur.push(i)
}
} else {
p.cur.add(i)
}
p.cur = p.cur.next
}

View File

@ -0,0 +1,11 @@
package pathexpr
import "encoding/xml"
//PathExpr represents XPath step's. xmltree.XMLTree uses it to find nodes.
type PathExpr struct {
Name xml.Name
Axis string
NodeType string
NS map[string]string
}

View File

@ -0,0 +1,66 @@
package xconst
const (
//AxisAncestor represents the "ancestor" axis
AxisAncestor = "ancestor"
//AxisAncestorOrSelf represents the "ancestor-or-self" axis
AxisAncestorOrSelf = "ancestor-or-self"
//AxisAttribute represents the "attribute" axis
AxisAttribute = "attribute"
//AxisChild represents the "child" axis
AxisChild = "child"
//AxisDescendent represents the "descendant" axis
AxisDescendent = "descendant"
//AxisDescendentOrSelf represents the "descendant-or-self" axis
AxisDescendentOrSelf = "descendant-or-self"
//AxisFollowing represents the "following" axis
AxisFollowing = "following"
//AxisFollowingSibling represents the "following-sibling" axis
AxisFollowingSibling = "following-sibling"
//AxisNamespace represents the "namespace" axis
AxisNamespace = "namespace"
//AxisParent represents the "parent" axis
AxisParent = "parent"
//AxisPreceding represents the "preceding" axis
AxisPreceding = "preceding"
//AxisPrecedingSibling represents the "preceding-sibling" axis
AxisPrecedingSibling = "preceding-sibling"
//AxisSelf represents the "self" axis
AxisSelf = "self"
)
//AxisNames is all the possible Axis identifiers wrapped in an array for convenience
var AxisNames = []string{
AxisAncestor,
AxisAncestorOrSelf,
AxisAttribute,
AxisChild,
AxisDescendent,
AxisDescendentOrSelf,
AxisFollowing,
AxisFollowingSibling,
AxisNamespace,
AxisParent,
AxisPreceding,
AxisPrecedingSibling,
AxisSelf,
}
const (
//NodeTypeComment represents the "comment" node test
NodeTypeComment = "comment"
//NodeTypeText represents the "text" node test
NodeTypeText = "text"
//NodeTypeProcInst represents the "processing-instruction" node test
NodeTypeProcInst = "processing-instruction"
//NodeTypeNode represents the "node" node test
NodeTypeNode = "node"
)
//NodeTypes is all the possible node tests wrapped in an array for convenience
var NodeTypes = []string{
NodeTypeComment,
NodeTypeText,
NodeTypeProcInst,
NodeTypeNode,
}

View File

@ -0,0 +1,20 @@
package xsort
import (
"sort"
"github.com/ChrisTrenkamp/goxpath/tree"
)
type nodeSort []tree.Node
func (ns nodeSort) Len() int { return len(ns) }
func (ns nodeSort) Swap(i, j int) { ns[i], ns[j] = ns[j], ns[i] }
func (ns nodeSort) Less(i, j int) bool {
return ns[i].Pos() < ns[j].Pos()
}
//SortNodes sorts the array by the node document order
func SortNodes(res []tree.Node) {
sort.Sort(nodeSort(res))
}

105
vendor/github.com/ChrisTrenkamp/goxpath/marshal.go generated vendored Normal file
View File

@ -0,0 +1,105 @@
package goxpath
import (
"bytes"
"encoding/xml"
"io"
"github.com/ChrisTrenkamp/goxpath/tree"
)
//Marshal prints the result tree, r, in XML form to w.
func Marshal(n tree.Node, w io.Writer) error {
return marshal(n, w)
}
//MarshalStr is like Marhal, but returns a string.
func MarshalStr(n tree.Node) (string, error) {
ret := bytes.NewBufferString("")
err := marshal(n, ret)
return ret.String(), err
}
func marshal(n tree.Node, w io.Writer) error {
e := xml.NewEncoder(w)
err := encTok(n, e)
if err != nil {
return err
}
return e.Flush()
}
func encTok(n tree.Node, e *xml.Encoder) error {
switch n.GetNodeType() {
case tree.NtAttr:
return encAttr(n.GetToken().(xml.Attr), e)
case tree.NtElem:
return encEle(n.(tree.Elem), e)
case tree.NtNs:
return encNS(n.GetToken().(xml.Attr), e)
case tree.NtRoot:
for _, i := range n.(tree.Elem).GetChildren() {
err := encTok(i, e)
if err != nil {
return err
}
}
return nil
}
//case tree.NtChd, tree.NtComm, tree.NtPi:
return e.EncodeToken(n.GetToken())
}
func encAttr(a xml.Attr, e *xml.Encoder) error {
str := a.Name.Local + `="` + a.Value + `"`
if a.Name.Space != "" {
str += ` xmlns="` + a.Name.Space + `"`
}
pi := xml.ProcInst{
Target: "attribute",
Inst: ([]byte)(str),
}
return e.EncodeToken(pi)
}
func encNS(ns xml.Attr, e *xml.Encoder) error {
pi := xml.ProcInst{
Target: "namespace",
Inst: ([]byte)(ns.Value),
}
return e.EncodeToken(pi)
}
func encEle(n tree.Elem, e *xml.Encoder) error {
ele := xml.StartElement{
Name: n.GetToken().(xml.StartElement).Name,
}
attrs := n.GetAttrs()
ele.Attr = make([]xml.Attr, len(attrs))
for i := range attrs {
ele.Attr[i] = attrs[i].GetToken().(xml.Attr)
}
err := e.EncodeToken(ele)
if err != nil {
return err
}
if x, ok := n.(tree.Elem); ok {
for _, i := range x.GetChildren() {
err := encTok(i, e)
if err != nil {
return err
}
}
}
return e.EncodeToken(ele.End())
}

View File

@ -0,0 +1,18 @@
package tree
import "fmt"
//Result is used for all data types.
type Result interface {
fmt.Stringer
}
//IsBool is used for the XPath boolean function. It turns the data type to a bool.
type IsBool interface {
Bool() Bool
}
//IsNum is used for the XPath number function. It turns the data type to a number.
type IsNum interface {
Num() Num
}

221
vendor/github.com/ChrisTrenkamp/goxpath/tree/tree.go generated vendored Normal file
View File

@ -0,0 +1,221 @@
package tree
import (
"encoding/xml"
"sort"
)
//XMLSpace is the W3C XML namespace
const XMLSpace = "http://www.w3.org/XML/1998/namespace"
//NodePos is a helper for representing the node's document order
type NodePos int
//Pos returns the node's document order position
func (n NodePos) Pos() int {
return int(n)
}
//NodeType is a safer way to determine a node's type than type assertions.
type NodeType int
//GetNodeType returns the node's type.
func (t NodeType) GetNodeType() NodeType {
return t
}
//These are all the possible node types
const (
NtAttr NodeType = iota
NtChd
NtComm
NtElem
NtNs
NtRoot
NtPi
)
//Node is a XPath result that is a node except elements
type Node interface {
//ResValue prints the node's string value
ResValue() string
//Pos returns the node's position in the document order
Pos() int
//GetToken returns the xml.Token representation of the node
GetToken() xml.Token
//GetParent returns the parent node, which will always be an XML element
GetParent() Elem
//GetNodeType returns the node's type
GetNodeType() NodeType
}
//Elem is a XPath result that is an element node
type Elem interface {
Node
//GetChildren returns the elements children.
GetChildren() []Node
//GetAttrs returns the attributes of the element
GetAttrs() []Node
}
//NSElem is a node that keeps track of namespaces.
type NSElem interface {
Elem
GetNS() map[xml.Name]string
}
//NSBuilder is a helper-struct for satisfying the NSElem interface
type NSBuilder struct {
NS map[xml.Name]string
}
//GetNS returns the namespaces found on the current element. It should not be
//confused with BuildNS, which actually resolves the namespace nodes.
func (ns NSBuilder) GetNS() map[xml.Name]string {
return ns.NS
}
type nsValueSort []NS
func (ns nsValueSort) Len() int { return len(ns) }
func (ns nsValueSort) Swap(i, j int) {
ns[i], ns[j] = ns[j], ns[i]
}
func (ns nsValueSort) Less(i, j int) bool {
return ns[i].Value < ns[j].Value
}
//BuildNS resolves all the namespace nodes of the element and returns them
func BuildNS(t Elem) (ret []NS) {
vals := make(map[xml.Name]string)
if nselem, ok := t.(NSElem); ok {
buildNS(nselem, vals)
ret = make([]NS, 0, len(vals))
i := 1
for k, v := range vals {
if !(k.Local == "xmlns" && k.Space == "" && v == "") {
ret = append(ret, NS{
Attr: xml.Attr{Name: k, Value: v},
Parent: t,
NodeType: NtNs,
})
i++
}
}
sort.Sort(nsValueSort(ret))
for i := range ret {
ret[i].NodePos = NodePos(t.Pos() + i + 1)
}
}
return ret
}
func buildNS(x NSElem, ret map[xml.Name]string) {
if x.GetNodeType() == NtRoot {
return
}
if nselem, ok := x.GetParent().(NSElem); ok {
buildNS(nselem, ret)
}
for k, v := range x.GetNS() {
ret[k] = v
}
}
//NS is a namespace node.
type NS struct {
xml.Attr
Parent Elem
NodePos
NodeType
}
//GetToken returns the xml.Token representation of the namespace.
func (ns NS) GetToken() xml.Token {
return ns.Attr
}
//GetParent returns the parent node of the namespace.
func (ns NS) GetParent() Elem {
return ns.Parent
}
//ResValue returns the string value of the namespace
func (ns NS) ResValue() string {
return ns.Attr.Value
}
//GetAttribute is a convenience function for getting the specified attribute from an element.
//false is returned if the attribute is not found.
func GetAttribute(n Elem, local, space string) (xml.Attr, bool) {
attrs := n.GetAttrs()
for _, i := range attrs {
attr := i.GetToken().(xml.Attr)
if local == attr.Name.Local && space == attr.Name.Space {
return attr, true
}
}
return xml.Attr{}, false
}
//GetAttributeVal is like GetAttribute, except it returns the attribute's value.
func GetAttributeVal(n Elem, local, space string) (string, bool) {
attr, ok := GetAttribute(n, local, space)
return attr.Value, ok
}
//GetAttrValOrEmpty is like GetAttributeVal, except it returns an empty string if
//the attribute is not found instead of false.
func GetAttrValOrEmpty(n Elem, local, space string) string {
val, ok := GetAttributeVal(n, local, space)
if !ok {
return ""
}
return val
}
//FindNodeByPos finds a node from the given position. Returns nil if the node
//is not found.
func FindNodeByPos(n Node, pos int) Node {
if n.Pos() == pos {
return n
}
if elem, ok := n.(Elem); ok {
chldrn := elem.GetChildren()
for i := 1; i < len(chldrn); i++ {
if chldrn[i-1].Pos() <= pos && chldrn[i].Pos() > pos {
return FindNodeByPos(chldrn[i-1], pos)
}
}
if len(chldrn) > 0 {
if chldrn[len(chldrn)-1].Pos() <= pos {
return FindNodeByPos(chldrn[len(chldrn)-1], pos)
}
}
attrs := elem.GetAttrs()
for _, i := range attrs {
if i.Pos() == pos {
return i
}
}
ns := BuildNS(elem)
for _, i := range ns {
if i.Pos() == pos {
return i
}
}
}
return nil
}

52
vendor/github.com/ChrisTrenkamp/goxpath/tree/xfn.go generated vendored Normal file
View File

@ -0,0 +1,52 @@
package tree
import (
"fmt"
)
//Ctx represents the current context position, size, node, and the current filtered result
type Ctx struct {
NodeSet
Pos int
Size int
}
//Fn is a XPath function, written in Go
type Fn func(c Ctx, args ...Result) (Result, error)
//LastArgOpt sets whether the last argument in a function is optional, variadic, or neither
type LastArgOpt int
//LastArgOpt options
const (
None LastArgOpt = iota
Optional
Variadic
)
//Wrap interfaces XPath function calls with Go
type Wrap struct {
Fn Fn
//NArgs represents the number of arguments to the XPath function. -1 represents a single optional argument
NArgs int
LastArgOpt LastArgOpt
}
//Call checks the arguments and calls Fn if they are valid
func (w Wrap) Call(c Ctx, args ...Result) (Result, error) {
switch w.LastArgOpt {
case Optional:
if len(args) == w.NArgs || len(args) == w.NArgs-1 {
return w.Fn(c, args...)
}
case Variadic:
if len(args) >= w.NArgs-1 {
return w.Fn(c, args...)
}
default:
if len(args) == w.NArgs {
return w.Fn(c, args...)
}
}
return nil, fmt.Errorf("Invalid number of arguments")
}

View File

@ -0,0 +1,25 @@
package xmlbuilder
import (
"encoding/xml"
"github.com/ChrisTrenkamp/goxpath/tree"
)
//BuilderOpts supplies all the information needed to create an XML node.
type BuilderOpts struct {
Dec *xml.Decoder
Tok xml.Token
NodeType tree.NodeType
NS map[xml.Name]string
Attrs []*xml.Attr
NodePos int
AttrStartPos int
}
//XMLBuilder is an interface for creating XML structures.
type XMLBuilder interface {
tree.Node
CreateNode(*BuilderOpts) XMLBuilder
EndElem() XMLBuilder
}

View File

@ -0,0 +1,106 @@
package xmlele
import (
"encoding/xml"
"github.com/ChrisTrenkamp/goxpath/tree"
"github.com/ChrisTrenkamp/goxpath/tree/xmltree/xmlbuilder"
"github.com/ChrisTrenkamp/goxpath/tree/xmltree/xmlnode"
)
//XMLEle is an implementation of XPRes for XML elements
type XMLEle struct {
xml.StartElement
tree.NSBuilder
Attrs []tree.Node
Children []tree.Node
Parent tree.Elem
tree.NodePos
tree.NodeType
}
//Root is the default root node builder for xmltree.ParseXML
func Root() xmlbuilder.XMLBuilder {
return &XMLEle{NodeType: tree.NtRoot}
}
//CreateNode is an implementation of xmlbuilder.XMLBuilder. It appends the node
//specified in opts and returns the child if it is an element. Otherwise, it returns x.
func (x *XMLEle) CreateNode(opts *xmlbuilder.BuilderOpts) xmlbuilder.XMLBuilder {
if opts.NodeType == tree.NtElem {
ele := &XMLEle{
StartElement: opts.Tok.(xml.StartElement),
NSBuilder: tree.NSBuilder{NS: opts.NS},
Attrs: make([]tree.Node, len(opts.Attrs)),
Parent: x,
NodePos: tree.NodePos(opts.NodePos),
NodeType: opts.NodeType,
}
for i := range opts.Attrs {
ele.Attrs[i] = xmlnode.XMLNode{
Token: opts.Attrs[i],
NodePos: tree.NodePos(opts.AttrStartPos + i),
NodeType: tree.NtAttr,
Parent: ele,
}
}
x.Children = append(x.Children, ele)
return ele
}
node := xmlnode.XMLNode{
Token: opts.Tok,
NodePos: tree.NodePos(opts.NodePos),
NodeType: opts.NodeType,
Parent: x,
}
x.Children = append(x.Children, node)
return x
}
//EndElem is an implementation of xmlbuilder.XMLBuilder. It returns x's parent.
func (x *XMLEle) EndElem() xmlbuilder.XMLBuilder {
return x.Parent.(*XMLEle)
}
//GetToken returns the xml.Token representation of the node
func (x *XMLEle) GetToken() xml.Token {
return x.StartElement
}
//GetParent returns the parent node, or itself if it's the root
func (x *XMLEle) GetParent() tree.Elem {
return x.Parent
}
//GetChildren returns all child nodes of the element
func (x *XMLEle) GetChildren() []tree.Node {
ret := make([]tree.Node, len(x.Children))
for i := range x.Children {
ret[i] = x.Children[i]
}
return ret
}
//GetAttrs returns all attributes of the element
func (x *XMLEle) GetAttrs() []tree.Node {
ret := make([]tree.Node, len(x.Attrs))
for i := range x.Attrs {
ret[i] = x.Attrs[i]
}
return ret
}
//ResValue returns the string value of the element and children
func (x *XMLEle) ResValue() string {
ret := ""
for i := range x.Children {
switch x.Children[i].GetNodeType() {
case tree.NtChd, tree.NtElem, tree.NtRoot:
ret += x.Children[i].ResValue()
}
}
return ret
}

View File

@ -0,0 +1,43 @@
package xmlnode
import (
"encoding/xml"
"github.com/ChrisTrenkamp/goxpath/tree"
)
//XMLNode will represent an attribute, character data, comment, or processing instruction node
type XMLNode struct {
xml.Token
tree.NodePos
tree.NodeType
Parent tree.Elem
}
//GetToken returns the xml.Token representation of the node
func (a XMLNode) GetToken() xml.Token {
if a.NodeType == tree.NtAttr {
ret := a.Token.(*xml.Attr)
return *ret
}
return a.Token
}
//GetParent returns the parent node
func (a XMLNode) GetParent() tree.Elem {
return a.Parent
}
//ResValue returns the string value of the attribute
func (a XMLNode) ResValue() string {
switch a.NodeType {
case tree.NtAttr:
return a.Token.(*xml.Attr).Value
case tree.NtChd:
return string(a.Token.(xml.CharData))
case tree.NtComm:
return string(a.Token.(xml.Comment))
}
//case tree.NtPi:
return string(a.Token.(xml.ProcInst).Inst)
}

View File

@ -0,0 +1,158 @@
package xmltree
import (
"encoding/xml"
"io"
"golang.org/x/net/html/charset"
"github.com/ChrisTrenkamp/goxpath/tree"
"github.com/ChrisTrenkamp/goxpath/tree/xmltree/xmlbuilder"
"github.com/ChrisTrenkamp/goxpath/tree/xmltree/xmlele"
)
//ParseOptions is a set of methods and function pointers that alter
//the way the XML decoder works and the Node types that are created.
//Options that are not set will default to what is set in internal/defoverride.go
type ParseOptions struct {
Strict bool
XMLRoot func() xmlbuilder.XMLBuilder
}
//DirectiveParser is an optional interface extended from XMLBuilder that handles
//XML directives.
type DirectiveParser interface {
xmlbuilder.XMLBuilder
Directive(xml.Directive, *xml.Decoder)
}
//ParseSettings is a function for setting the ParseOptions you want when
//parsing an XML tree.
type ParseSettings func(s *ParseOptions)
//MustParseXML is like ParseXML, but panics instead of returning an error.
func MustParseXML(r io.Reader, op ...ParseSettings) tree.Node {
ret, err := ParseXML(r, op...)
if err != nil {
panic(err)
}
return ret
}
//ParseXML creates an XMLTree structure from an io.Reader.
func ParseXML(r io.Reader, op ...ParseSettings) (tree.Node, error) {
ov := ParseOptions{
Strict: true,
XMLRoot: xmlele.Root,
}
for _, i := range op {
i(&ov)
}
dec := xml.NewDecoder(r)
dec.CharsetReader = charset.NewReaderLabel
dec.Strict = ov.Strict
ordrPos := 1
xmlTree := ov.XMLRoot()
t, err := dec.Token()
if err != nil {
return nil, err
}
if head, ok := t.(xml.ProcInst); ok && head.Target == "xml" {
t, err = dec.Token()
}
opts := xmlbuilder.BuilderOpts{
Dec: dec,
}
for err == nil {
switch xt := t.(type) {
case xml.StartElement:
setEle(&opts, xmlTree, xt, &ordrPos)
xmlTree = xmlTree.CreateNode(&opts)
case xml.CharData:
setNode(&opts, xmlTree, xt, tree.NtChd, &ordrPos)
xmlTree = xmlTree.CreateNode(&opts)
case xml.Comment:
setNode(&opts, xmlTree, xt, tree.NtComm, &ordrPos)
xmlTree = xmlTree.CreateNode(&opts)
case xml.ProcInst:
setNode(&opts, xmlTree, xt, tree.NtPi, &ordrPos)
xmlTree = xmlTree.CreateNode(&opts)
case xml.EndElement:
xmlTree = xmlTree.EndElem()
case xml.Directive:
if dp, ok := xmlTree.(DirectiveParser); ok {
dp.Directive(xt.Copy(), dec)
}
}
t, err = dec.Token()
}
if err == io.EOF {
err = nil
}
return xmlTree, err
}
func setEle(opts *xmlbuilder.BuilderOpts, xmlTree xmlbuilder.XMLBuilder, ele xml.StartElement, ordrPos *int) {
opts.NodePos = *ordrPos
opts.Tok = ele
opts.Attrs = opts.Attrs[0:0:cap(opts.Attrs)]
opts.NS = make(map[xml.Name]string)
opts.NodeType = tree.NtElem
for i := range ele.Attr {
attr := ele.Attr[i].Name
val := ele.Attr[i].Value
if (attr.Local == "xmlns" && attr.Space == "") || attr.Space == "xmlns" {
opts.NS[attr] = val
} else {
opts.Attrs = append(opts.Attrs, &ele.Attr[i])
}
}
if nstree, ok := xmlTree.(tree.NSElem); ok {
ns := make(map[xml.Name]string)
for _, i := range tree.BuildNS(nstree) {
ns[i.Name] = i.Value
}
for k, v := range opts.NS {
ns[k] = v
}
if ns[xml.Name{Local: "xmlns"}] == "" {
delete(ns, xml.Name{Local: "xmlns"})
}
for k, v := range ns {
opts.NS[k] = v
}
if xmlTree.GetNodeType() == tree.NtRoot {
opts.NS[xml.Name{Space: "xmlns", Local: "xml"}] = tree.XMLSpace
}
}
opts.AttrStartPos = len(opts.NS) + len(opts.Attrs) + *ordrPos
*ordrPos = opts.AttrStartPos + 1
}
func setNode(opts *xmlbuilder.BuilderOpts, xmlTree xmlbuilder.XMLBuilder, tok xml.Token, nt tree.NodeType, ordrPos *int) {
opts.Tok = xml.CopyToken(tok)
opts.NodeType = nt
opts.NodePos = *ordrPos
*ordrPos++
}

113
vendor/github.com/ChrisTrenkamp/goxpath/tree/xtypes.go generated vendored Normal file
View File

@ -0,0 +1,113 @@
package tree
import (
"fmt"
"math"
"strconv"
"strings"
)
//Boolean strings
const (
True = "true"
False = "false"
)
//Bool is a boolean XPath type
type Bool bool
//ResValue satisfies the Res interface for Bool
func (b Bool) String() string {
if b {
return True
}
return False
}
//Bool satisfies the HasBool interface for Bool's
func (b Bool) Bool() Bool {
return b
}
//Num satisfies the HasNum interface for Bool's
func (b Bool) Num() Num {
if b {
return Num(1)
}
return Num(0)
}
//Num is a number XPath type
type Num float64
//ResValue satisfies the Res interface for Num
func (n Num) String() string {
if math.IsInf(float64(n), 0) {
if math.IsInf(float64(n), 1) {
return "Infinity"
}
return "-Infinity"
}
return fmt.Sprintf("%g", float64(n))
}
//Bool satisfies the HasBool interface for Num's
func (n Num) Bool() Bool {
return n != 0
}
//Num satisfies the HasNum interface for Num's
func (n Num) Num() Num {
return n
}
//String is string XPath type
type String string
//ResValue satisfies the Res interface for String
func (s String) String() string {
return string(s)
}
//Bool satisfies the HasBool interface for String's
func (s String) Bool() Bool {
return Bool(len(s) > 0)
}
//Num satisfies the HasNum interface for String's
func (s String) Num() Num {
num, err := strconv.ParseFloat(strings.TrimSpace(string(s)), 64)
if err != nil {
return Num(math.NaN())
}
return Num(num)
}
//NodeSet is a node-set XPath type
type NodeSet []Node
//GetNodeNum converts the node to a string-value and to a number
func GetNodeNum(n Node) Num {
return String(n.ResValue()).Num()
}
//String satisfies the Res interface for NodeSet
func (n NodeSet) String() string {
if len(n) == 0 {
return ""
}
return n[0].ResValue()
}
//Bool satisfies the HasBool interface for node-set's
func (n NodeSet) Bool() Bool {
return Bool(len(n) > 0)
}
//Num satisfies the HasNum interface for NodeSet's
func (n NodeSet) Num() Num {
return String(n.String()).Num()
}

17
vendor/github.com/antchfx/xpath/LICENSE generated vendored Normal file
View File

@ -0,0 +1,17 @@
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

119
vendor/github.com/antchfx/xpath/README.md generated vendored Normal file
View File

@ -0,0 +1,119 @@
XPath
====
[![GoDoc](https://godoc.org/github.com/antchfx/xpath?status.svg)](https://godoc.org/github.com/antchfx/xpath)
[![Coverage Status](https://coveralls.io/repos/github/antchfx/xpath/badge.svg?branch=master)](https://coveralls.io/github/antchfx/xpath?branch=master)
[![Build Status](https://travis-ci.org/antchfx/xpath.svg?branch=master)](https://travis-ci.org/antchfx/xpath)
[![Go Report Card](https://goreportcard.com/badge/github.com/antchfx/xpath)](https://goreportcard.com/report/github.com/antchfx/xpath)
XPath is Go package provides selecting nodes from XML, HTML or other documents using XPath expression.
[XQuery](https://github.com/antchfx/xquery) : lets you extract data from HTML/XML documents using XPath package.
### Features
#### The basic XPath patterns.
> The basic XPath patterns cover 90% of the cases that most stylesheets will need.
- `node` : Selects all child elements with nodeName of node.
- `*` : Selects all child elements.
- `@attr` : Selects the attribute attr.
- `@*` : Selects all attributes.
- `node()` : Matches an org.w3c.dom.Node.
- `text()` : Matches a org.w3c.dom.Text node.
- `comment()` : Matches a comment.
- `.` : Selects the current node.
- `..` : Selects the parent of current node.
- `/` : Selects the document node.
- `a[expr]` : Select only those nodes matching a which also satisfy the expression expr.
- `a[n]` : Selects the nth matching node matching a When a filter's expression is a number, XPath selects based on position.
- `a/b` : For each node matching a, add the nodes matching b to the result.
- `a//b` : For each node matching a, add the descendant nodes matching b to the result.
- `//b` : Returns elements in the entire document matching b.
- `a|b` : All nodes matching a or b.
#### Node Axes
- `child::*` : The child axis selects children of the current node.
- `descendant::*` : The descendant axis selects descendants of the current node. It is equivalent to '//'.
- `descendant-or-self::*` : Selects descendants including the current node.
- `attribute::*` : Selects attributes of the current element. It is equivalent to @*
- `following-sibling::*` : Selects nodes after the current node.
- `preceding-sibling::*` : Selects nodes before the current node.
- `following::*` : Selects the first matching node following in document order, excluding descendants.
- `preceding::*` : Selects the first matching node preceding in document order, excluding ancestors.
- `parent::*` : Selects the parent if it matches. The '..' pattern from the core is equivalent to 'parent::node()'.
- `ancestor::*` : Selects matching ancestors.
- `ancestor-or-self::*` : Selects ancestors including the current node.
- `self::*` : Selects the current node. '.' is equivalent to 'self::node()'.
#### Expressions
The gxpath supported three types: number, boolean, string.
- `path` : Selects nodes based on the path.
- `a = b` : Standard comparisons.
* a = b True if a equals b.
* a != b True if a is not equal to b.
* a < b True if a is less than b.
* a <= b True if a is less than or equal to b.
* a > b True if a is greater than b.
* a >= b True if a is greater than or equal to b.
- `a + b` : Arithmetic expressions.
* `- a` Unary minus
* a + b Add
* a - b Substract
* a * b Multiply
* a div b Divide
* a mod b Floating point mod, like Java.
- `(expr)` : Parenthesized expressions.
- `fun(arg1, ..., argn)` : Function calls.
* position()
* last()
* count( node-set )
* name()
* starts-with( string, string )
* normalize-space( string )
* substring( string , start [, length] )
* not( expression )
* string-length( [string] )
* contains( string, string )
* sum( node-set )
* concat( string1 , string2 [, stringn]* )
- `a or b` : Boolean or.
- `a and b` : Boolean and.

359
vendor/github.com/antchfx/xpath/build.go generated vendored Normal file
View File

@ -0,0 +1,359 @@
package xpath
import (
"errors"
"fmt"
)
type flag int
const (
noneFlag flag = iota
filterFlag
)
// builder provides building an XPath expressions.
type builder struct {
depth int
flag flag
firstInput query
}
// axisPredicate creates a predicate to predicating for this axis node.
func axisPredicate(root *axisNode) func(NodeNavigator) bool {
// get current axix node type.
typ := ElementNode
if root.AxeType == "attribute" {
typ = AttributeNode
} else {
switch root.Prop {
case "comment":
typ = CommentNode
case "text":
typ = TextNode
// case "processing-instruction":
// typ = ProcessingInstructionNode
case "node":
typ = ElementNode
}
}
predicate := func(n NodeNavigator) bool {
if typ == n.NodeType() || typ == TextNode {
if root.LocalName == "" || (root.LocalName == n.LocalName() && root.Prefix == n.Prefix()) {
return true
}
}
return false
}
return predicate
}
// processAxisNode processes a query for the XPath axis node.
func (b *builder) processAxisNode(root *axisNode) (query, error) {
var (
err error
qyInput query
qyOutput query
predicate = axisPredicate(root)
)
if root.Input == nil {
qyInput = &contextQuery{}
} else {
if b.flag&filterFlag == 0 {
if root.AxeType == "child" && (root.Input.Type() == nodeAxis) {
if input := root.Input.(*axisNode); input.AxeType == "descendant-or-self" {
var qyGrandInput query
if input.Input != nil {
qyGrandInput, _ = b.processNode(input.Input)
} else {
qyGrandInput = &contextQuery{}
}
qyOutput = &descendantQuery{Input: qyGrandInput, Predicate: predicate, Self: true}
return qyOutput, nil
}
}
}
qyInput, err = b.processNode(root.Input)
if err != nil {
return nil, err
}
}
switch root.AxeType {
case "ancestor":
qyOutput = &ancestorQuery{Input: qyInput, Predicate: predicate}
case "ancestor-or-self":
qyOutput = &ancestorQuery{Input: qyInput, Predicate: predicate, Self: true}
case "attribute":
qyOutput = &attributeQuery{Input: qyInput, Predicate: predicate}
case "child":
filter := func(n NodeNavigator) bool {
v := predicate(n)
switch root.Prop {
case "text":
v = v && n.NodeType() == TextNode
case "node":
v = v && (n.NodeType() == ElementNode || n.NodeType() == TextNode)
case "comment":
v = v && n.NodeType() == CommentNode
}
return v
}
qyOutput = &childQuery{Input: qyInput, Predicate: filter}
case "descendant":
qyOutput = &descendantQuery{Input: qyInput, Predicate: predicate}
case "descendant-or-self":
qyOutput = &descendantQuery{Input: qyInput, Predicate: predicate, Self: true}
case "following":
qyOutput = &followingQuery{Input: qyInput, Predicate: predicate}
case "following-sibling":
qyOutput = &followingQuery{Input: qyInput, Predicate: predicate, Sibling: true}
case "parent":
qyOutput = &parentQuery{Input: qyInput, Predicate: predicate}
case "preceding":
qyOutput = &precedingQuery{Input: qyInput, Predicate: predicate}
case "preceding-sibling":
qyOutput = &precedingQuery{Input: qyInput, Predicate: predicate, Sibling: true}
case "self":
qyOutput = &selfQuery{Input: qyInput, Predicate: predicate}
case "namespace":
// haha,what will you do someting??
default:
err = fmt.Errorf("unknown axe type: %s", root.AxeType)
return nil, err
}
return qyOutput, nil
}
// processFilterNode builds query for the XPath filter predicate.
func (b *builder) processFilterNode(root *filterNode) (query, error) {
b.flag |= filterFlag
qyInput, err := b.processNode(root.Input)
if err != nil {
return nil, err
}
qyCond, err := b.processNode(root.Condition)
if err != nil {
return nil, err
}
qyOutput := &filterQuery{Input: qyInput, Predicate: qyCond}
return qyOutput, nil
}
// processFunctionNode processes query for the XPath function node.
func (b *builder) processFunctionNode(root *functionNode) (query, error) {
var qyOutput query
switch root.FuncName {
case "starts-with":
arg1, err := b.processNode(root.Args[0])
if err != nil {
return nil, err
}
arg2, err := b.processNode(root.Args[1])
if err != nil {
return nil, err
}
qyOutput = &functionQuery{Input: b.firstInput, Func: startwithFunc(arg1, arg2)}
case "contains":
arg1, err := b.processNode(root.Args[0])
if err != nil {
return nil, err
}
arg2, err := b.processNode(root.Args[1])
if err != nil {
return nil, err
}
qyOutput = &functionQuery{Input: b.firstInput, Func: containsFunc(arg1, arg2)}
case "substring":
//substring( string , start [, length] )
if len(root.Args) < 2 {
return nil, errors.New("xpath: substring function must have at least two parameter")
}
var (
arg1, arg2, arg3 query
err error
)
if arg1, err = b.processNode(root.Args[0]); err != nil {
return nil, err
}
if arg2, err = b.processNode(root.Args[1]); err != nil {
return nil, err
}
if len(root.Args) == 3 {
if arg3, err = b.processNode(root.Args[2]); err != nil {
return nil, err
}
}
qyOutput = &functionQuery{Input: b.firstInput, Func: substringFunc(arg1, arg2, arg3)}
case "string-length":
// string-length( [string] )
if len(root.Args) < 1 {
return nil, errors.New("xpath: string-length function must have at least one parameter")
}
arg1, err := b.processNode(root.Args[0])
if err != nil {
return nil, err
}
qyOutput = &functionQuery{Input: b.firstInput, Func: stringLengthFunc(arg1)}
case "normalize-space":
if len(root.Args) == 0 {
return nil, errors.New("xpath: normalize-space function must have at least one parameter")
}
argQuery, err := b.processNode(root.Args[0])
if err != nil {
return nil, err
}
qyOutput = &functionQuery{Input: argQuery, Func: normalizespaceFunc}
case "not":
if len(root.Args) == 0 {
return nil, errors.New("xpath: not function must have at least one parameter")
}
argQuery, err := b.processNode(root.Args[0])
if err != nil {
return nil, err
}
qyOutput = &functionQuery{Input: argQuery, Func: notFunc}
case "name":
qyOutput = &functionQuery{Input: b.firstInput, Func: nameFunc}
case "last":
qyOutput = &functionQuery{Input: b.firstInput, Func: lastFunc}
case "position":
qyOutput = &functionQuery{Input: b.firstInput, Func: positionFunc}
case "count":
//if b.firstInput == nil {
// return nil, errors.New("xpath: expression must evaluate to node-set")
//}
if len(root.Args) == 0 {
return nil, fmt.Errorf("xpath: count(node-sets) function must with have parameters node-sets")
}
argQuery, err := b.processNode(root.Args[0])
if err != nil {
return nil, err
}
qyOutput = &functionQuery{Input: argQuery, Func: countFunc}
case "sum":
if len(root.Args) == 0 {
return nil, fmt.Errorf("xpath: sum(node-sets) function must with have parameters node-sets")
}
argQuery, err := b.processNode(root.Args[0])
if err != nil {
return nil, err
}
qyOutput = &functionQuery{Input: argQuery, Func: sumFunc}
case "concat":
if len(root.Args) < 2 {
return nil, fmt.Errorf("xpath: concat() must have at least two arguments")
}
var args []query
for _, v := range root.Args {
q, err := b.processNode(v)
if err != nil {
return nil, err
}
args = append(args, q)
}
qyOutput = &functionQuery{Input: b.firstInput, Func: concatFunc(args...)}
default:
return nil, fmt.Errorf("not yet support this function %s()", root.FuncName)
}
return qyOutput, nil
}
func (b *builder) processOperatorNode(root *operatorNode) (query, error) {
left, err := b.processNode(root.Left)
if err != nil {
return nil, err
}
right, err := b.processNode(root.Right)
if err != nil {
return nil, err
}
var qyOutput query
switch root.Op {
case "+", "-", "div", "mod": // Numeric operator
var exprFunc func(interface{}, interface{}) interface{}
switch root.Op {
case "+":
exprFunc = plusFunc
case "-":
exprFunc = minusFunc
case "div":
exprFunc = divFunc
case "mod":
exprFunc = modFunc
}
qyOutput = &numericQuery{Left: left, Right: right, Do: exprFunc}
case "=", ">", ">=", "<", "<=", "!=":
var exprFunc func(iterator, interface{}, interface{}) interface{}
switch root.Op {
case "=":
exprFunc = eqFunc
case ">":
exprFunc = gtFunc
case ">=":
exprFunc = geFunc
case "<":
exprFunc = ltFunc
case "<=":
exprFunc = leFunc
case "!=":
exprFunc = neFunc
}
qyOutput = &logicalQuery{Left: left, Right: right, Do: exprFunc}
case "or", "and", "|":
isOr := false
if root.Op == "or" || root.Op == "|" {
isOr = true
}
qyOutput = &booleanQuery{Left: left, Right: right, IsOr: isOr}
}
return qyOutput, nil
}
func (b *builder) processNode(root node) (q query, err error) {
if b.depth = b.depth + 1; b.depth > 1024 {
err = errors.New("the xpath expressions is too complex")
return
}
switch root.Type() {
case nodeConstantOperand:
n := root.(*operandNode)
q = &constantQuery{Val: n.Val}
case nodeRoot:
q = &contextQuery{Root: true}
case nodeAxis:
q, err = b.processAxisNode(root.(*axisNode))
b.firstInput = q
case nodeFilter:
q, err = b.processFilterNode(root.(*filterNode))
case nodeFunction:
q, err = b.processFunctionNode(root.(*functionNode))
case nodeOperator:
q, err = b.processOperatorNode(root.(*operatorNode))
}
return
}
// build builds a specified XPath expressions expr.
func build(expr string) (q query, err error) {
defer func() {
if e := recover(); e != nil {
switch x := e.(type) {
case string:
err = errors.New(x)
case error:
err = x
default:
err = errors.New("unknown panic")
}
}
}()
root := parse(expr)
b := &builder{}
return b.processNode(root)
}

254
vendor/github.com/antchfx/xpath/func.go generated vendored Normal file
View File

@ -0,0 +1,254 @@
package xpath
import (
"errors"
"strconv"
"strings"
)
// The XPath function list.
func predicate(q query) func(NodeNavigator) bool {
type Predicater interface {
Test(NodeNavigator) bool
}
if p, ok := q.(Predicater); ok {
return p.Test
}
return func(NodeNavigator) bool { return true }
}
// positionFunc is a XPath Node Set functions position().
func positionFunc(q query, t iterator) interface{} {
var (
count = 1
node = t.Current()
)
test := predicate(q)
for node.MoveToPrevious() {
if test(node) {
count++
}
}
return float64(count)
}
// lastFunc is a XPath Node Set functions last().
func lastFunc(q query, t iterator) interface{} {
var (
count = 0
node = t.Current()
)
node.MoveToFirst()
test := predicate(q)
for {
if test(node) {
count++
}
if !node.MoveToNext() {
break
}
}
return float64(count)
}
// countFunc is a XPath Node Set functions count(node-set).
func countFunc(q query, t iterator) interface{} {
var count = 0
test := predicate(q)
switch typ := q.Evaluate(t).(type) {
case query:
for node := typ.Select(t); node != nil; node = typ.Select(t) {
if test(node) {
count++
}
}
}
return float64(count)
}
// sumFunc is a XPath Node Set functions sum(node-set).
func sumFunc(q query, t iterator) interface{} {
var sum float64
switch typ := q.Evaluate(t).(type) {
case query:
for node := typ.Select(t); node != nil; node = typ.Select(t) {
if v, err := strconv.ParseFloat(node.Value(), 64); err == nil {
sum += v
}
}
case float64:
sum = typ
case string:
if v, err := strconv.ParseFloat(typ, 64); err != nil {
sum = v
}
}
return sum
}
// nameFunc is a XPath functions name([node-set]).
func nameFunc(q query, t iterator) interface{} {
return t.Current().LocalName()
}
// startwithFunc is a XPath functions starts-with(string, string).
func startwithFunc(arg1, arg2 query) func(query, iterator) interface{} {
return func(q query, t iterator) interface{} {
var (
m, n string
ok bool
)
switch typ := arg1.Evaluate(t).(type) {
case string:
m = typ
case query:
node := typ.Select(t)
if node == nil {
return false
}
m = node.Value()
default:
panic(errors.New("starts-with() function argument type must be string"))
}
n, ok = arg2.Evaluate(t).(string)
if !ok {
panic(errors.New("starts-with() function argument type must be string"))
}
return strings.HasPrefix(m, n)
}
}
// containsFunc is a XPath functions contains(string or @attr, string).
func containsFunc(arg1, arg2 query) func(query, iterator) interface{} {
return func(q query, t iterator) interface{} {
var (
m, n string
ok bool
)
switch typ := arg1.Evaluate(t).(type) {
case string:
m = typ
case query:
node := typ.Select(t)
if node == nil {
return false
}
m = node.Value()
default:
panic(errors.New("contains() function argument type must be string"))
}
n, ok = arg2.Evaluate(t).(string)
if !ok {
panic(errors.New("contains() function argument type must be string"))
}
return strings.Contains(m, n)
}
}
// normalizespaceFunc is XPath functions normalize-space(string?)
func normalizespaceFunc(q query, t iterator) interface{} {
var m string
switch typ := q.Evaluate(t).(type) {
case string:
m = typ
case query:
node := typ.Select(t)
if node == nil {
return false
}
m = node.Value()
}
return strings.TrimSpace(m)
}
// substringFunc is XPath functions substring function returns a part of a given string.
func substringFunc(arg1, arg2, arg3 query) func(query, iterator) interface{} {
return func(q query, t iterator) interface{} {
var m string
switch typ := arg1.Evaluate(t).(type) {
case string:
m = typ
case query:
node := typ.Select(t)
if node == nil {
return false
}
m = node.Value()
}
var start, length float64
var ok bool
if start, ok = arg2.Evaluate(t).(float64); !ok {
panic(errors.New("substring() function first argument type must be int"))
}
if arg3 != nil {
if length, ok = arg3.Evaluate(t).(float64); !ok {
panic(errors.New("substring() function second argument type must be int"))
}
}
if (len(m) - int(start)) < int(length) {
panic(errors.New("substring() function start and length argument out of range"))
}
if length > 0 {
return m[int(start):int(length+start)]
}
return m[int(start):]
}
}
// stringLengthFunc is XPATH string-length( [string] ) function that returns a number
// equal to the number of characters in a given string.
func stringLengthFunc(arg1 query) func(query, iterator) interface{} {
return func(q query, t iterator) interface{} {
switch v := arg1.Evaluate(t).(type) {
case string:
return float64(len(v))
case query:
node := v.Select(t)
if node == nil {
break
}
return float64(len(node.Value()))
}
return float64(0)
}
}
// notFunc is XPATH functions not(expression) function operation.
func notFunc(q query, t iterator) interface{} {
switch v := q.Evaluate(t).(type) {
case bool:
return !v
case query:
node := v.Select(t)
return node == nil
default:
return false
}
}
// concatFunc is the concat function concatenates two or more
// strings and returns the resulting string.
// concat( string1 , string2 [, stringn]* )
func concatFunc(args ...query) func(query, iterator) interface{} {
return func(q query, t iterator) interface{} {
var a []string
for _, v := range args {
switch v := v.Evaluate(t).(type) {
case string:
a = append(a, v)
case query:
node := v.Select(t)
if node != nil {
a = append(a, node.Value())
}
}
}
return strings.Join(a, "")
}
}

295
vendor/github.com/antchfx/xpath/operator.go generated vendored Normal file
View File

@ -0,0 +1,295 @@
package xpath
import (
"fmt"
"reflect"
"strconv"
)
// The XPath number operator function list.
// valueType is a return value type.
type valueType int
const (
booleanType valueType = iota
numberType
stringType
nodeSetType
)
func getValueType(i interface{}) valueType {
v := reflect.ValueOf(i)
switch v.Kind() {
case reflect.Float64:
return numberType
case reflect.String:
return stringType
case reflect.Bool:
return booleanType
default:
if _, ok := i.(query); ok {
return nodeSetType
}
}
panic(fmt.Errorf("xpath unknown value type: %v", v.Kind()))
}
type logical func(iterator, string, interface{}, interface{}) bool
var logicalFuncs = [][]logical{
{cmpBooleanBoolean, nil, nil, nil},
{nil, cmpNumericNumeric, cmpNumericString, cmpNumericNodeSet},
{nil, cmpStringNumeric, cmpStringString, cmpStringNodeSet},
{nil, cmpNodeSetNumeric, cmpNodeSetString, cmpNodeSetNodeSet},
}
// number vs number
func cmpNumberNumberF(op string, a, b float64) bool {
switch op {
case "=":
return a == b
case ">":
return a > b
case "<":
return a < b
case ">=":
return a >= b
case "<=":
return a <= b
case "!=":
return a != b
}
return false
}
// string vs string
func cmpStringStringF(op string, a, b string) bool {
switch op {
case "=":
return a == b
case ">":
return a > b
case "<":
return a < b
case ">=":
return a >= b
case "<=":
return a <= b
case "!=":
return a != b
}
return false
}
func cmpBooleanBooleanF(op string, a, b bool) bool {
switch op {
case "or":
return a || b
case "and":
return a && b
}
return false
}
func cmpNumericNumeric(t iterator, op string, m, n interface{}) bool {
a := m.(float64)
b := n.(float64)
return cmpNumberNumberF(op, a, b)
}
func cmpNumericString(t iterator, op string, m, n interface{}) bool {
a := m.(float64)
b := n.(string)
num, err := strconv.ParseFloat(b, 64)
if err != nil {
panic(err)
}
return cmpNumberNumberF(op, a, num)
}
func cmpNumericNodeSet(t iterator, op string, m, n interface{}) bool {
a := m.(float64)
b := n.(query)
for {
node := b.Select(t)
if node == nil {
break
}
num, err := strconv.ParseFloat(node.Value(), 64)
if err != nil {
panic(err)
}
if cmpNumberNumberF(op, a, num) {
return true
}
}
return false
}
func cmpNodeSetNumeric(t iterator, op string, m, n interface{}) bool {
a := m.(query)
b := n.(float64)
for {
node := a.Select(t)
if node == nil {
break
}
num, err := strconv.ParseFloat(node.Value(), 64)
if err != nil {
panic(err)
}
if cmpNumberNumberF(op, num, b) {
return true
}
}
return false
}
func cmpNodeSetString(t iterator, op string, m, n interface{}) bool {
a := m.(query)
b := n.(string)
for {
node := a.Select(t)
if node == nil {
break
}
if cmpStringStringF(op, b, node.Value()) {
return true
}
}
return false
}
func cmpNodeSetNodeSet(t iterator, op string, m, n interface{}) bool {
return false
}
func cmpStringNumeric(t iterator, op string, m, n interface{}) bool {
a := m.(string)
b := n.(float64)
num, err := strconv.ParseFloat(a, 64)
if err != nil {
panic(err)
}
return cmpNumberNumberF(op, b, num)
}
func cmpStringString(t iterator, op string, m, n interface{}) bool {
a := m.(string)
b := n.(string)
return cmpStringStringF(op, a, b)
}
func cmpStringNodeSet(t iterator, op string, m, n interface{}) bool {
a := m.(string)
b := n.(query)
for {
node := b.Select(t)
if node == nil {
break
}
if cmpStringStringF(op, a, node.Value()) {
return true
}
}
return false
}
func cmpBooleanBoolean(t iterator, op string, m, n interface{}) bool {
a := m.(bool)
b := n.(bool)
return cmpBooleanBooleanF(op, a, b)
}
// eqFunc is an `=` operator.
func eqFunc(t iterator, m, n interface{}) interface{} {
t1 := getValueType(m)
t2 := getValueType(n)
return logicalFuncs[t1][t2](t, "=", m, n)
}
// gtFunc is an `>` operator.
func gtFunc(t iterator, m, n interface{}) interface{} {
t1 := getValueType(m)
t2 := getValueType(n)
return logicalFuncs[t1][t2](t, ">", m, n)
}
// geFunc is an `>=` operator.
func geFunc(t iterator, m, n interface{}) interface{} {
t1 := getValueType(m)
t2 := getValueType(n)
return logicalFuncs[t1][t2](t, ">=", m, n)
}
// ltFunc is an `<` operator.
func ltFunc(t iterator, m, n interface{}) interface{} {
t1 := getValueType(m)
t2 := getValueType(n)
return logicalFuncs[t1][t2](t, "<", m, n)
}
// leFunc is an `<=` operator.
func leFunc(t iterator, m, n interface{}) interface{} {
t1 := getValueType(m)
t2 := getValueType(n)
return logicalFuncs[t1][t2](t, "<=", m, n)
}
// neFunc is an `!=` operator.
func neFunc(t iterator, m, n interface{}) interface{} {
t1 := getValueType(m)
t2 := getValueType(n)
return logicalFuncs[t1][t2](t, "!=", m, n)
}
// orFunc is an `or` operator.
var orFunc = func(t iterator, m, n interface{}) interface{} {
t1 := getValueType(m)
t2 := getValueType(n)
return logicalFuncs[t1][t2](t, "or", m, n)
}
func numericExpr(m, n interface{}, cb func(float64, float64) float64) float64 {
typ := reflect.TypeOf(float64(0))
a := reflect.ValueOf(m).Convert(typ)
b := reflect.ValueOf(n).Convert(typ)
return cb(a.Float(), b.Float())
}
// plusFunc is an `+` operator.
var plusFunc = func(m, n interface{}) interface{} {
return numericExpr(m, n, func(a, b float64) float64 {
return a + b
})
}
// minusFunc is an `-` operator.
var minusFunc = func(m, n interface{}) interface{} {
return numericExpr(m, n, func(a, b float64) float64 {
return a - b
})
}
// mulFunc is an `*` operator.
var mulFunc = func(m, n interface{}) interface{} {
return numericExpr(m, n, func(a, b float64) float64 {
return a * b
})
}
// divFunc is an `DIV` operator.
var divFunc = func(m, n interface{}) interface{} {
return numericExpr(m, n, func(a, b float64) float64 {
return a / b
})
}
// modFunc is an 'MOD' operator.
var modFunc = func(m, n interface{}) interface{} {
return numericExpr(m, n, func(a, b float64) float64 {
return float64(int(a) % int(b))
})
}

1164
vendor/github.com/antchfx/xpath/parse.go generated vendored Normal file

File diff suppressed because it is too large Load Diff

728
vendor/github.com/antchfx/xpath/query.go generated vendored Normal file
View File

@ -0,0 +1,728 @@
package xpath
import (
"reflect"
)
type iterator interface {
Current() NodeNavigator
}
// An XPath query interface.
type query interface {
// Select traversing iterator returns a query matched node NodeNavigator.
Select(iterator) NodeNavigator
// Evaluate evaluates query and returns values of the current query.
Evaluate(iterator) interface{}
Clone() query
}
// contextQuery is returns current node on the iterator object query.
type contextQuery struct {
count int
Root bool // Moving to root-level node in the current context iterator.
}
func (c *contextQuery) Select(t iterator) (n NodeNavigator) {
if c.count == 0 {
c.count++
n = t.Current().Copy()
if c.Root {
n.MoveToRoot()
}
}
return n
}
func (c *contextQuery) Evaluate(iterator) interface{} {
c.count = 0
return c
}
func (c *contextQuery) Clone() query {
return &contextQuery{count: 0, Root: c.Root}
}
// ancestorQuery is an XPath ancestor node query.(ancestor::*|ancestor-self::*)
type ancestorQuery struct {
iterator func() NodeNavigator
Self bool
Input query
Predicate func(NodeNavigator) bool
}
func (a *ancestorQuery) Select(t iterator) NodeNavigator {
for {
if a.iterator == nil {
node := a.Input.Select(t)
if node == nil {
return nil
}
first := true
a.iterator = func() NodeNavigator {
if first && a.Self {
first = false
if a.Predicate(node) {
return node
}
}
for node.MoveToParent() {
if !a.Predicate(node) {
break
}
return node
}
return nil
}
}
if node := a.iterator(); node != nil {
return node
}
a.iterator = nil
}
}
func (a *ancestorQuery) Evaluate(t iterator) interface{} {
a.Input.Evaluate(t)
a.iterator = nil
return a
}
func (a *ancestorQuery) Test(n NodeNavigator) bool {
return a.Predicate(n)
}
func (a *ancestorQuery) Clone() query {
return &ancestorQuery{Self: a.Self, Input: a.Input.Clone(), Predicate: a.Predicate}
}
// attributeQuery is an XPath attribute node query.(@*)
type attributeQuery struct {
iterator func() NodeNavigator
Input query
Predicate func(NodeNavigator) bool
}
func (a *attributeQuery) Select(t iterator) NodeNavigator {
for {
if a.iterator == nil {
node := a.Input.Select(t)
if node == nil {
return nil
}
node = node.Copy()
a.iterator = func() NodeNavigator {
for {
onAttr := node.MoveToNextAttribute()
if !onAttr {
return nil
}
if a.Predicate(node) {
return node
}
}
}
}
if node := a.iterator(); node != nil {
return node
}
a.iterator = nil
}
}
func (a *attributeQuery) Evaluate(t iterator) interface{} {
a.Input.Evaluate(t)
a.iterator = nil
return a
}
func (a *attributeQuery) Test(n NodeNavigator) bool {
return a.Predicate(n)
}
func (a *attributeQuery) Clone() query {
return &attributeQuery{Input: a.Input.Clone(), Predicate: a.Predicate}
}
// childQuery is an XPath child node query.(child::*)
type childQuery struct {
posit int
iterator func() NodeNavigator
Input query
Predicate func(NodeNavigator) bool
}
func (c *childQuery) Select(t iterator) NodeNavigator {
for {
if c.iterator == nil {
c.posit = 0
node := c.Input.Select(t)
if node == nil {
return nil
}
node = node.Copy()
first := true
c.iterator = func() NodeNavigator {
for {
if (first && !node.MoveToChild()) || (!first && !node.MoveToNext()) {
return nil
}
first = false
if c.Predicate(node) {
return node
}
}
}
}
if node := c.iterator(); node != nil {
c.posit++
return node
}
c.iterator = nil
}
}
func (c *childQuery) Evaluate(t iterator) interface{} {
c.Input.Evaluate(t)
c.iterator = nil
return c
}
func (c *childQuery) Test(n NodeNavigator) bool {
return c.Predicate(n)
}
func (c *childQuery) Clone() query {
return &childQuery{Input: c.Input.Clone(), Predicate: c.Predicate}
}
// position returns a position of current NodeNavigator.
func (c *childQuery) position() int {
return c.posit
}
// descendantQuery is an XPath descendant node query.(descendant::* | descendant-or-self::*)
type descendantQuery struct {
iterator func() NodeNavigator
posit int
Self bool
Input query
Predicate func(NodeNavigator) bool
}
func (d *descendantQuery) Select(t iterator) NodeNavigator {
for {
if d.iterator == nil {
d.posit = 0
node := d.Input.Select(t)
if node == nil {
return nil
}
node = node.Copy()
level := 0
first := true
d.iterator = func() NodeNavigator {
if first && d.Self {
first = false
if d.Predicate(node) {
return node
}
}
for {
if node.MoveToChild() {
level++
} else {
for {
if level == 0 {
return nil
}
if node.MoveToNext() {
break
}
node.MoveToParent()
level--
}
}
if d.Predicate(node) {
return node
}
}
}
}
if node := d.iterator(); node != nil {
d.posit++
return node
}
d.iterator = nil
}
}
func (d *descendantQuery) Evaluate(t iterator) interface{} {
d.Input.Evaluate(t)
d.iterator = nil
return d
}
func (d *descendantQuery) Test(n NodeNavigator) bool {
return d.Predicate(n)
}
// position returns a position of current NodeNavigator.
func (d *descendantQuery) position() int {
return d.posit
}
func (d *descendantQuery) Clone() query {
return &descendantQuery{Self: d.Self, Input: d.Input.Clone(), Predicate: d.Predicate}
}
// followingQuery is an XPath following node query.(following::*|following-sibling::*)
type followingQuery struct {
iterator func() NodeNavigator
Input query
Sibling bool // The matching sibling node of current node.
Predicate func(NodeNavigator) bool
}
func (f *followingQuery) Select(t iterator) NodeNavigator {
for {
if f.iterator == nil {
node := f.Input.Select(t)
if node == nil {
return nil
}
node = node.Copy()
if f.Sibling {
f.iterator = func() NodeNavigator {
for {
if !node.MoveToNext() {
return nil
}
if f.Predicate(node) {
return node
}
}
}
} else {
var q query // descendant query
f.iterator = func() NodeNavigator {
for {
if q == nil {
for !node.MoveToNext() {
if !node.MoveToParent() {
return nil
}
}
q = &descendantQuery{
Self: true,
Input: &contextQuery{},
Predicate: f.Predicate,
}
t.Current().MoveTo(node)
}
if node := q.Select(t); node != nil {
return node
}
q = nil
}
}
}
}
if node := f.iterator(); node != nil {
return node
}
f.iterator = nil
}
}
func (f *followingQuery) Evaluate(t iterator) interface{} {
f.Input.Evaluate(t)
return f
}
func (f *followingQuery) Test(n NodeNavigator) bool {
return f.Predicate(n)
}
func (f *followingQuery) Clone() query {
return &followingQuery{Input: f.Input.Clone(), Sibling: f.Sibling, Predicate: f.Predicate}
}
// precedingQuery is an XPath preceding node query.(preceding::*)
type precedingQuery struct {
iterator func() NodeNavigator
Input query
Sibling bool // The matching sibling node of current node.
Predicate func(NodeNavigator) bool
}
func (p *precedingQuery) Select(t iterator) NodeNavigator {
for {
if p.iterator == nil {
node := p.Input.Select(t)
if node == nil {
return nil
}
node = node.Copy()
if p.Sibling {
p.iterator = func() NodeNavigator {
for {
for !node.MoveToPrevious() {
return nil
}
if p.Predicate(node) {
return node
}
}
}
} else {
var q query
p.iterator = func() NodeNavigator {
for {
if q == nil {
for !node.MoveToPrevious() {
if !node.MoveToParent() {
return nil
}
}
q = &descendantQuery{
Self: true,
Input: &contextQuery{},
Predicate: p.Predicate,
}
t.Current().MoveTo(node)
}
if node := q.Select(t); node != nil {
return node
}
q = nil
}
}
}
}
if node := p.iterator(); node != nil {
return node
}
p.iterator = nil
}
}
func (p *precedingQuery) Evaluate(t iterator) interface{} {
p.Input.Evaluate(t)
return p
}
func (p *precedingQuery) Test(n NodeNavigator) bool {
return p.Predicate(n)
}
func (p *precedingQuery) Clone() query {
return &precedingQuery{Input: p.Input.Clone(), Sibling: p.Sibling, Predicate: p.Predicate}
}
// parentQuery is an XPath parent node query.(parent::*)
type parentQuery struct {
Input query
Predicate func(NodeNavigator) bool
}
func (p *parentQuery) Select(t iterator) NodeNavigator {
for {
node := p.Input.Select(t)
if node == nil {
return nil
}
node = node.Copy()
if node.MoveToParent() && p.Predicate(node) {
return node
}
}
}
func (p *parentQuery) Evaluate(t iterator) interface{} {
p.Input.Evaluate(t)
return p
}
func (p *parentQuery) Clone() query {
return &parentQuery{Input: p.Input.Clone(), Predicate: p.Predicate}
}
func (p *parentQuery) Test(n NodeNavigator) bool {
return p.Predicate(n)
}
// selfQuery is an Self node query.(self::*)
type selfQuery struct {
Input query
Predicate func(NodeNavigator) bool
}
func (s *selfQuery) Select(t iterator) NodeNavigator {
for {
node := s.Input.Select(t)
if node == nil {
return nil
}
if s.Predicate(node) {
return node
}
}
}
func (s *selfQuery) Evaluate(t iterator) interface{} {
s.Input.Evaluate(t)
return s
}
func (s *selfQuery) Test(n NodeNavigator) bool {
return s.Predicate(n)
}
func (s *selfQuery) Clone() query {
return &selfQuery{Input: s.Input.Clone(), Predicate: s.Predicate}
}
// filterQuery is an XPath query for predicate filter.
type filterQuery struct {
Input query
Predicate query
}
func (f *filterQuery) do(t iterator) bool {
val := reflect.ValueOf(f.Predicate.Evaluate(t))
switch val.Kind() {
case reflect.Bool:
return val.Bool()
case reflect.String:
return len(val.String()) > 0
case reflect.Float64:
pt := float64(getNodePosition(f.Input))
return int(val.Float()) == int(pt)
default:
if q, ok := f.Predicate.(query); ok {
return q.Select(t) != nil
}
}
return false
}
func (f *filterQuery) Select(t iterator) NodeNavigator {
for {
node := f.Input.Select(t)
if node == nil {
return node
}
node = node.Copy()
//fmt.Println(node.LocalName())
t.Current().MoveTo(node)
if f.do(t) {
return node
}
}
}
func (f *filterQuery) Evaluate(t iterator) interface{} {
f.Input.Evaluate(t)
return f
}
func (f *filterQuery) Clone() query {
return &filterQuery{Input: f.Input.Clone(), Predicate: f.Predicate.Clone()}
}
// functionQuery is an XPath function that call a function to returns
// value of current NodeNavigator node.
type functionQuery struct {
Input query // Node Set
Func func(query, iterator) interface{} // The xpath function.
}
func (f *functionQuery) Select(t iterator) NodeNavigator {
return nil
}
// Evaluate call a specified function that will returns the
// following value type: number,string,boolean.
func (f *functionQuery) Evaluate(t iterator) interface{} {
return f.Func(f.Input, t)
}
func (f *functionQuery) Clone() query {
return &functionQuery{Input: f.Input.Clone(), Func: f.Func}
}
// constantQuery is an XPath constant operand.
type constantQuery struct {
Val interface{}
}
func (c *constantQuery) Select(t iterator) NodeNavigator {
return nil
}
func (c *constantQuery) Evaluate(t iterator) interface{} {
return c.Val
}
func (c *constantQuery) Clone() query {
return c
}
// logicalQuery is an XPath logical expression.
type logicalQuery struct {
Left, Right query
Do func(iterator, interface{}, interface{}) interface{}
}
func (l *logicalQuery) Select(t iterator) NodeNavigator {
// When a XPath expr is logical expression.
node := t.Current().Copy()
val := l.Evaluate(t)
switch val.(type) {
case bool:
if val.(bool) == true {
return node
}
}
return nil
}
func (l *logicalQuery) Evaluate(t iterator) interface{} {
m := l.Left.Evaluate(t)
n := l.Right.Evaluate(t)
return l.Do(t, m, n)
}
func (l *logicalQuery) Clone() query {
return &logicalQuery{Left: l.Left.Clone(), Right: l.Right.Clone(), Do: l.Do}
}
// numericQuery is an XPath numeric operator expression.
type numericQuery struct {
Left, Right query
Do func(interface{}, interface{}) interface{}
}
func (n *numericQuery) Select(t iterator) NodeNavigator {
return nil
}
func (n *numericQuery) Evaluate(t iterator) interface{} {
m := n.Left.Evaluate(t)
k := n.Right.Evaluate(t)
return n.Do(m, k)
}
func (n *numericQuery) Clone() query {
return &numericQuery{Left: n.Left.Clone(), Right: n.Right.Clone(), Do: n.Do}
}
type booleanQuery struct {
IsOr bool
Left, Right query
iterator func() NodeNavigator
}
func (b *booleanQuery) Select(t iterator) NodeNavigator {
if b.iterator == nil {
var list []NodeNavigator
i := 0
root := t.Current().Copy()
if b.IsOr {
for {
node := b.Left.Select(t)
if node == nil {
break
}
node = node.Copy()
list = append(list, node)
}
t.Current().MoveTo(root)
for {
node := b.Right.Select(t)
if node == nil {
break
}
node = node.Copy()
list = append(list, node)
}
} else {
var m []NodeNavigator
var n []NodeNavigator
for {
node := b.Left.Select(t)
if node == nil {
break
}
node = node.Copy()
list = append(m, node)
}
t.Current().MoveTo(root)
for {
node := b.Right.Select(t)
if node == nil {
break
}
node = node.Copy()
list = append(n, node)
}
for _, k := range m {
for _, j := range n {
if k == j {
list = append(list, k)
}
}
}
}
b.iterator = func() NodeNavigator {
if i >= len(list) {
return nil
}
node := list[i]
i++
return node
}
}
return b.iterator()
}
func (b *booleanQuery) Evaluate(t iterator) interface{} {
m := b.Left.Evaluate(t)
if m.(bool) == b.IsOr {
return m
}
return b.Right.Evaluate(t)
}
func (b *booleanQuery) Clone() query {
return &booleanQuery{IsOr: b.IsOr, Left: b.Left.Clone(), Right: b.Right.Clone()}
}
func getNodePosition(q query) int {
type Position interface {
position() int
}
if count, ok := q.(Position); ok {
return count.position()
}
return 1
}

154
vendor/github.com/antchfx/xpath/xpath.go generated vendored Normal file
View File

@ -0,0 +1,154 @@
package xpath
import (
"errors"
)
// NodeType represents a type of XPath node.
type NodeType int
const (
// RootNode is a root node of the XML document or node tree.
RootNode NodeType = iota
// ElementNode is an element, such as <element>.
ElementNode
// AttributeNode is an attribute, such as id='123'.
AttributeNode
// TextNode is the text content of a node.
TextNode
// CommentNode is a comment node, such as <!-- my comment -->
CommentNode
)
// NodeNavigator provides cursor model for navigating XML data.
type NodeNavigator interface {
// NodeType returns the XPathNodeType of the current node.
NodeType() NodeType
// LocalName gets the Name of the current node.
LocalName() string
// Prefix returns namespace prefix associated with the current node.
Prefix() string
// Value gets the value of current node.
Value() string
// Copy does a deep copy of the NodeNavigator and all its components.
Copy() NodeNavigator
// MoveToRoot moves the NodeNavigator to the root node of the current node.
MoveToRoot()
// MoveToParent moves the NodeNavigator to the parent node of the current node.
MoveToParent() bool
// MoveToNextAttribute moves the NodeNavigator to the next attribute on current node.
MoveToNextAttribute() bool
// MoveToChild moves the NodeNavigator to the first child node of the current node.
MoveToChild() bool
// MoveToFirst moves the NodeNavigator to the first sibling node of the current node.
MoveToFirst() bool
// MoveToNext moves the NodeNavigator to the next sibling node of the current node.
MoveToNext() bool
// MoveToPrevious moves the NodeNavigator to the previous sibling node of the current node.
MoveToPrevious() bool
// MoveTo moves the NodeNavigator to the same position as the specified NodeNavigator.
MoveTo(NodeNavigator) bool
}
// NodeIterator holds all matched Node object.
type NodeIterator struct {
node NodeNavigator
query query
}
// Current returns current node which matched.
func (t *NodeIterator) Current() NodeNavigator {
return t.node
}
// MoveNext moves Navigator to the next match node.
func (t *NodeIterator) MoveNext() bool {
n := t.query.Select(t)
if n != nil {
if !t.node.MoveTo(n) {
t.node = n.Copy()
}
return true
}
return false
}
// Select selects a node set using the specified XPath expression.
// This method is deprecated, recommend using Expr.Select() method instead.
func Select(root NodeNavigator, expr string) *NodeIterator {
exp, err := Compile(expr)
if err != nil {
panic(err)
}
return exp.Select(root)
}
// Expr is an XPath expression for query.
type Expr struct {
s string
q query
}
type iteratorFunc func() NodeNavigator
func (f iteratorFunc) Current() NodeNavigator {
return f()
}
// Evaluate returns the result of the expression.
// The result type of the expression is one of the follow: bool,float64,string,NodeIterator).
func (expr *Expr) Evaluate(root NodeNavigator) interface{} {
val := expr.q.Evaluate(iteratorFunc(func() NodeNavigator { return root }))
switch val.(type) {
case query:
return &NodeIterator{query: expr.q.Clone(), node: root}
}
return val
}
// Select selects a node set using the specified XPath expression.
func (expr *Expr) Select(root NodeNavigator) *NodeIterator {
return &NodeIterator{query: expr.q.Clone(), node: root}
}
// String returns XPath expression string.
func (expr *Expr) String() string {
return expr.s
}
// Compile compiles an XPath expression string.
func Compile(expr string) (*Expr, error) {
if expr == "" {
return nil, errors.New("expr expression is nil")
}
qy, err := build(expr)
if err != nil {
return nil, err
}
return &Expr{s: expr, q: qy}, nil
}
// MustCompile compiles an XPath expression string and ignored error.
func MustCompile(expr string) *Expr {
exp, err := Compile(expr)
if err != nil {
return nil
}
return exp
}

17
vendor/github.com/antchfx/xquery/LICENSE generated vendored Normal file
View File

@ -0,0 +1,17 @@
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

252
vendor/github.com/antchfx/xquery/xml/node.go generated vendored Normal file
View File

@ -0,0 +1,252 @@
package xmlquery
import (
"bytes"
"encoding/xml"
"fmt"
"io"
"net/http"
"strings"
"golang.org/x/net/html/charset"
)
// A NodeType is the type of a Node.
type NodeType uint
const (
// DocumentNode is a document object that, as the root of the document tree,
// provides access to the entire XML document.
DocumentNode NodeType = iota
// DeclarationNode is the document type declaration, indicated by the following
// tag (for example, <!DOCTYPE...> ).
DeclarationNode
// ElementNode is an element (for example, <item> ).
ElementNode
// TextNode is the text content of a node.
TextNode
// CommentNode a comment (for example, <!-- my comment --> ).
CommentNode
)
// A Node consists of a NodeType and some Data (tag name for
// element nodes, content for text) and are part of a tree of Nodes.
type Node struct {
Parent, FirstChild, LastChild, PrevSibling, NextSibling *Node
Type NodeType
Data string
Prefix string
NamespaceURI string
Attr []xml.Attr
level int // node level in the tree
}
// InnerText returns the text between the start and end tags of the object.
func (n *Node) InnerText() string {
var output func(*bytes.Buffer, *Node)
output = func(buf *bytes.Buffer, n *Node) {
switch n.Type {
case TextNode:
buf.WriteString(n.Data)
return
case CommentNode:
return
}
for child := n.FirstChild; child != nil; child = child.NextSibling {
output(buf, child)
}
}
var buf bytes.Buffer
output(&buf, n)
return buf.String()
}
func outputXML(buf *bytes.Buffer, n *Node) {
if n.Type == TextNode || n.Type == CommentNode {
buf.WriteString(strings.TrimSpace(n.Data))
return
}
buf.WriteString("<" + n.Data)
for _, attr := range n.Attr {
if attr.Name.Space != "" {
buf.WriteString(fmt.Sprintf(` %s:%s="%s"`, attr.Name.Space, attr.Name.Local, attr.Value))
} else {
buf.WriteString(fmt.Sprintf(` %s="%s"`, attr.Name.Local, attr.Value))
}
}
buf.WriteString(">")
for child := n.FirstChild; child != nil; child = child.NextSibling {
outputXML(buf, child)
}
buf.WriteString(fmt.Sprintf("</%s>", n.Data))
}
// OutputXML returns the text that including tags name.
func (n *Node) OutputXML(self bool) string {
var buf bytes.Buffer
if self {
outputXML(&buf, n)
} else {
for n := n.FirstChild; n != nil; n = n.NextSibling {
outputXML(&buf, n)
}
}
return buf.String()
}
func addAttr(n *Node, key, val string) {
var attr xml.Attr
if i := strings.Index(key, ":"); i > 0 {
attr = xml.Attr{
Name: xml.Name{Space: key[:i], Local: key[i+1:]},
Value: val,
}
} else {
attr = xml.Attr{
Name: xml.Name{Local: key},
Value: val,
}
}
n.Attr = append(n.Attr, attr)
}
func addChild(parent, n *Node) {
n.Parent = parent
if parent.FirstChild == nil {
parent.FirstChild = n
} else {
parent.LastChild.NextSibling = n
n.PrevSibling = parent.LastChild
}
parent.LastChild = n
}
func addSibling(sibling, n *Node) {
n.Parent = sibling.Parent
sibling.NextSibling = n
n.PrevSibling = sibling
if sibling.Parent != nil {
sibling.Parent.LastChild = n
}
}
// LoadURL loads the XML document from the specified URL.
func LoadURL(url string) (*Node, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return parse(resp.Body)
}
func parse(r io.Reader) (*Node, error) {
var (
decoder = xml.NewDecoder(r)
doc = &Node{Type: DocumentNode}
space2prefix = make(map[string]string)
level = 0
)
decoder.CharsetReader = charset.NewReaderLabel
prev := doc
for {
tok, err := decoder.Token()
switch {
case err == io.EOF:
goto quit
case err != nil:
return nil, err
}
switch tok := tok.(type) {
case xml.StartElement:
if level == 0 {
// mising XML declaration
node := &Node{Type: DeclarationNode, Data: "xml", level: 1}
addChild(prev, node)
level = 1
prev = node
}
node := &Node{
Type: ElementNode,
Data: tok.Name.Local,
Prefix: space2prefix[tok.Name.Space],
NamespaceURI: tok.Name.Space,
Attr: tok.Attr,
level: level,
}
for _, att := range tok.Attr {
if att.Name.Space == "xmlns" {
space2prefix[att.Value] = att.Name.Local
}
}
//fmt.Println(fmt.Sprintf("start > %s : %d", node.Data, level))
if level == prev.level {
addSibling(prev, node)
} else if level > prev.level {
addChild(prev, node)
} else if level < prev.level {
for i := prev.level - level; i > 1; i-- {
prev = prev.Parent
}
addSibling(prev.Parent, node)
}
prev = node
level++
case xml.EndElement:
level--
case xml.CharData:
node := &Node{Type: TextNode, Data: string(tok), level: level}
if level == prev.level {
addSibling(prev, node)
} else if level > prev.level {
addChild(prev, node)
}
case xml.Comment:
node := &Node{Type: CommentNode, Data: string(tok), level: level}
if level == prev.level {
addSibling(prev, node)
} else if level > prev.level {
addChild(prev, node)
}
case xml.ProcInst: // Processing Instruction
if prev.Type != DeclarationNode {
level++
}
node := &Node{Type: DeclarationNode, Data: tok.Target, level: level}
pairs := strings.Split(string(tok.Inst), " ")
for _, pair := range pairs {
pair = strings.TrimSpace(pair)
if i := strings.Index(pair, "="); i > 0 {
addAttr(node, pair[:i], strings.Trim(pair[i+1:], `"`))
}
}
if level == prev.level {
addSibling(prev, node)
} else if level > prev.level {
addChild(prev, node)
}
prev = node
case xml.Directive:
}
}
quit:
return doc, nil
}
// Parse returns the parse tree for the XML from the given Reader.
func Parse(r io.Reader) (*Node, error) {
return parse(r)
}
// ParseXML returns the parse tree for the XML from the given Reader.Deprecated.
func ParseXML(r io.Reader) (*Node, error) {
return parse(r)
}

230
vendor/github.com/antchfx/xquery/xml/query.go generated vendored Normal file
View File

@ -0,0 +1,230 @@
/*
Package xmlquery provides extract data from XML documents using XPath expression.
*/
package xmlquery
import (
"fmt"
"strings"
"github.com/antchfx/xpath"
)
// SelectElements finds child elements with the specified name.
func (n *Node) SelectElements(name string) []*Node {
return Find(n, name)
}
// SelectElement finds child elements with the specified name.
func (n *Node) SelectElement(name string) *Node {
return FindOne(n, name)
}
// SelectAttr returns the attribute value with the specified name.
func (n *Node) SelectAttr(name string) string {
var local, space string
local = name
if i := strings.Index(name, ":"); i > 0 {
space = name[:i]
local = name[i+1:]
}
for _, attr := range n.Attr {
if attr.Name.Local == local && attr.Name.Space == space {
return attr.Value
}
}
return ""
}
var _ xpath.NodeNavigator = &NodeNavigator{}
// CreateXPathNavigator creates a new xpath.NodeNavigator for the specified html.Node.
func CreateXPathNavigator(top *Node) *NodeNavigator {
return &NodeNavigator{curr: top, root: top, attr: -1}
}
// Find searches the Node that matches by the specified XPath expr.
func Find(top *Node, expr string) []*Node {
exp, err := xpath.Compile(expr)
if err != nil {
panic(err)
}
t := exp.Select(CreateXPathNavigator(top))
var elems []*Node
for t.MoveNext() {
elems = append(elems, (t.Current().(*NodeNavigator)).curr)
}
return elems
}
// FindOne searches the Node that matches by the specified XPath expr,
// and returns first element of matched.
func FindOne(top *Node, expr string) *Node {
exp, err := xpath.Compile(expr)
if err != nil {
panic(err)
}
t := exp.Select(CreateXPathNavigator(top))
var elem *Node
if t.MoveNext() {
elem = (t.Current().(*NodeNavigator)).curr
}
return elem
}
// FindEach searches the html.Node and calls functions cb.
func FindEach(top *Node, expr string, cb func(int, *Node)) {
exp, err := xpath.Compile(expr)
if err != nil {
panic(err)
}
t := exp.Select(CreateXPathNavigator(top))
var i int
for t.MoveNext() {
cb(i, (t.Current().(*NodeNavigator)).curr)
i++
}
}
type NodeNavigator struct {
root, curr *Node
attr int
}
func (x *NodeNavigator) Current() *Node {
return x.curr
}
func (x *NodeNavigator) NodeType() xpath.NodeType {
switch x.curr.Type {
case CommentNode:
return xpath.CommentNode
case TextNode:
return xpath.TextNode
case DeclarationNode, DocumentNode:
return xpath.RootNode
case ElementNode:
if x.attr != -1 {
return xpath.AttributeNode
}
return xpath.ElementNode
}
panic(fmt.Sprintf("unknown XML node type: %v", x.curr.Type))
}
func (x *NodeNavigator) LocalName() string {
if x.attr != -1 {
return x.curr.Attr[x.attr].Name.Local
}
return x.curr.Data
}
func (x *NodeNavigator) Prefix() string {
return x.curr.Prefix
}
func (x *NodeNavigator) Value() string {
switch x.curr.Type {
case CommentNode:
return x.curr.Data
case ElementNode:
if x.attr != -1 {
return x.curr.Attr[x.attr].Value
}
return x.curr.InnerText()
case TextNode:
return x.curr.Data
}
return ""
}
func (x *NodeNavigator) Copy() xpath.NodeNavigator {
n := *x
return &n
}
func (x *NodeNavigator) MoveToRoot() {
x.curr = x.root
}
func (x *NodeNavigator) MoveToParent() bool {
if x.attr != -1 {
x.attr = -1
return true
} else if node := x.curr.Parent; node != nil {
x.curr = node
return true
}
return false
}
func (x *NodeNavigator) MoveToNextAttribute() bool {
if x.attr >= len(x.curr.Attr)-1 {
return false
}
x.attr++
return true
}
func (x *NodeNavigator) MoveToChild() bool {
if x.attr != -1 {
return false
}
if node := x.curr.FirstChild; node != nil {
x.curr = node
return true
}
return false
}
func (x *NodeNavigator) MoveToFirst() bool {
if x.attr != -1 || x.curr.PrevSibling == nil {
return false
}
for {
node := x.curr.PrevSibling
if node == nil {
break
}
x.curr = node
}
return true
}
func (x *NodeNavigator) String() string {
return x.Value()
}
func (x *NodeNavigator) MoveToNext() bool {
if x.attr != -1 {
return false
}
if node := x.curr.NextSibling; node != nil {
x.curr = node
return true
}
return false
}
func (x *NodeNavigator) MoveToPrevious() bool {
if x.attr != -1 {
return false
}
if node := x.curr.PrevSibling; node != nil {
x.curr = node
return true
}
return false
}
func (x *NodeNavigator) MoveTo(other xpath.NodeNavigator) bool {
node, ok := other.(*NodeNavigator)
if !ok || node.root != x.root {
return false
}
x.curr = node.curr
x.attr = node.attr
return true
}

View File

@ -8,8 +8,7 @@ import (
"strconv"
"strings"
"github.com/masterzen/winrm/soap"
"github.com/masterzen/xmlpath"
"github.com/antchfx/xquery/xml"
"github.com/satori/go.uuid"
)
@ -57,7 +56,7 @@ func (w *wsman) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
rw.Header().Add("Content-Type", "application/soap+xml")
defer r.Body.Close()
env, err := xmlpath.Parse(r.Body)
env, err := xmlquery.Parse(r.Body)
if err != nil {
return
@ -130,41 +129,32 @@ func (w *wsman) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
}
}
func readAction(env *xmlpath.Node) string {
xpath, err := xmlpath.CompileWithNamespace(
"//a:Action", soap.GetAllNamespaces())
if err != nil {
func readAction(env *xmlquery.Node) string {
xpath := xmlquery.FindOne(env, "//a:Action")
if xpath == nil {
return ""
}
action, _ := xpath.String(env)
return action
return xpath.InnerText()
}
func readCommand(env *xmlpath.Node) string {
xpath, err := xmlpath.CompileWithNamespace(
"//rsp:Command", soap.GetAllNamespaces())
if err != nil {
func readCommand(env *xmlquery.Node) string {
xpath := xmlquery.FindOne(env, "//rsp:Command")
if xpath == nil {
return ""
}
command, _ := xpath.String(env)
if unquoted, err := strconv.Unquote(command); err == nil {
if unquoted, err := strconv.Unquote(xpath.InnerText()); err == nil {
return unquoted
}
return command
return xpath.InnerText()
}
func readCommandIDFromDesiredStream(env *xmlpath.Node) string {
xpath, err := xmlpath.CompileWithNamespace(
"//rsp:DesiredStream/@CommandId", soap.GetAllNamespaces())
if err != nil {
func readCommandIDFromDesiredStream(env *xmlquery.Node) string {
xpath := xmlquery.FindOne(env, "//rsp:DesiredStream")
if xpath == nil {
return ""
}
id, _ := xpath.String(env)
return id
return xpath.SelectAttr("CommandId")
}

View File

@ -1,7 +1,7 @@
package winrm
import (
ntlmssp "github.com/Azure/go-ntlmssp"
"github.com/Azure/go-ntlmssp"
"github.com/masterzen/winrm/soap"
)

View File

@ -8,8 +8,8 @@ import (
)
func genUUID() string {
uuid, _ := uuid.NewV4()
return "uuid:" + uuid.String()
id, _ := uuid.NewV4()
return "uuid:" + id.String()
}
func defaultHeaders(message *soap.SoapMessage, url string, params *Parameters) *soap.SoapHeader {
@ -37,10 +37,10 @@ func NewOpenShellRequest(uri string, params *Parameters) *soap.SoapMessage {
AddOption(soap.NewHeaderOption("WINRS_CODEPAGE", "65001")).
Build()
body := message.CreateBodyElement("Shell", soap.NS_WIN_SHELL)
input := message.CreateElement(body, "InputStreams", soap.NS_WIN_SHELL)
body := message.CreateBodyElement("Shell", soap.DOM_NS_WIN_SHELL)
input := message.CreateElement(body, "InputStreams", soap.DOM_NS_WIN_SHELL)
input.SetContent("stdin")
output := message.CreateElement(body, "OutputStreams", soap.NS_WIN_SHELL)
output := message.CreateElement(body, "OutputStreams", soap.DOM_NS_WIN_SHELL)
output.SetContent("stdout stderr")
return message
@ -77,16 +77,16 @@ func NewExecuteCommandRequest(uri, shellId, command string, arguments []string,
AddOption(soap.NewHeaderOption("WINRS_SKIP_CMD_SHELL", "FALSE")).
Build()
body := message.CreateBodyElement("CommandLine", soap.NS_WIN_SHELL)
body := message.CreateBodyElement("CommandLine", soap.DOM_NS_WIN_SHELL)
// ensure special characters like & don't mangle the request XML
command = "<![CDATA[" + command + "]]>"
commandElement := message.CreateElement(body, "Command", soap.NS_WIN_SHELL)
commandElement := message.CreateElement(body, "Command", soap.DOM_NS_WIN_SHELL)
commandElement.SetContent(command)
for _, arg := range arguments {
arg = "<![CDATA[" + arg + "]]>"
argumentsElement := message.CreateElement(body, "Arguments", soap.NS_WIN_SHELL)
argumentsElement := message.CreateElement(body, "Arguments", soap.DOM_NS_WIN_SHELL)
argumentsElement.SetContent(arg)
}
@ -104,8 +104,8 @@ func NewGetOutputRequest(uri, shellId, commandId, streams string, params *Parame
ShellId(shellId).
Build()
receive := message.CreateBodyElement("Receive", soap.NS_WIN_SHELL)
desiredStreams := message.CreateElement(receive, "DesiredStream", soap.NS_WIN_SHELL)
receive := message.CreateBodyElement("Receive", soap.DOM_NS_WIN_SHELL)
desiredStreams := message.CreateElement(receive, "DesiredStream", soap.DOM_NS_WIN_SHELL)
desiredStreams.SetAttr("CommandId", commandId)
desiredStreams.SetContent(streams)
@ -126,8 +126,8 @@ func NewSendInputRequest(uri, shellId, commandId string, input []byte, params *P
content := base64.StdEncoding.EncodeToString(input)
send := message.CreateBodyElement("Send", soap.NS_WIN_SHELL)
streams := message.CreateElement(send, "Stream", soap.NS_WIN_SHELL)
send := message.CreateBodyElement("Send", soap.DOM_NS_WIN_SHELL)
streams := message.CreateElement(send, "Stream", soap.DOM_NS_WIN_SHELL)
streams.SetAttr("Name", "stdin")
streams.SetAttr("CommandId", commandId)
streams.SetContent(content)
@ -146,9 +146,9 @@ func NewSignalRequest(uri string, shellId string, commandId string, params *Para
ShellId(shellId).
Build()
signal := message.CreateBodyElement("Signal", soap.NS_WIN_SHELL)
signal := message.CreateBodyElement("Signal", soap.DOM_NS_WIN_SHELL)
signal.SetAttr("CommandId", commandId)
code := message.CreateElement(signal, "Code", soap.NS_WIN_SHELL)
code := message.CreateElement(signal, "Code", soap.DOM_NS_WIN_SHELL)
code.SetContent("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/signal/terminate")
return message

View File

@ -7,46 +7,45 @@ import (
"strconv"
"strings"
"github.com/ChrisTrenkamp/goxpath"
"github.com/ChrisTrenkamp/goxpath/tree"
"github.com/ChrisTrenkamp/goxpath/tree/xmltree"
"github.com/masterzen/winrm/soap"
"github.com/masterzen/xmlpath"
)
func first(node *xmlpath.Node, xpath string) (string, error) {
path, err := xmlpath.CompileWithNamespace(xpath, soap.GetAllNamespaces())
func first(node tree.Node, xpath string) (string, error) {
nodes, err := xPath(node, xpath)
if err != nil {
return "", err
}
content, _ := path.String(node)
return content, nil
if len(nodes) < 1 {
return "", err
}
return nodes[0].ResValue(), nil
}
func any(node *xmlpath.Node, xpath string) (bool, error) {
path, err := xmlpath.CompileWithNamespace(xpath, soap.GetAllNamespaces())
func any(node tree.Node, xpath string) (bool, error) {
nodes, err := xPath(node, xpath)
if err != nil {
return false, err
}
return path.Exists(node), nil
if len(nodes) > 0 {
return true, nil
}
return false, nil
}
func xpath(node *xmlpath.Node, xpath string) ([]xmlpath.Node, error) {
path, err := xmlpath.CompileWithNamespace(xpath, soap.GetAllNamespaces())
func xPath(node tree.Node, xpath string) (tree.NodeSet, error) {
xpExec := goxpath.MustParse(xpath)
nodes, err := xpExec.ExecNode(node, soap.GetAllXPathNamespaces())
if err != nil {
return nil, err
}
nodes := make([]xmlpath.Node, 0, 1)
iter := path.Iter(node)
for iter.Next() {
nodes = append(nodes, *(iter.Node()))
}
return nodes, nil
}
func ParseOpenShellResponse(response string) (string, error) {
doc, err := xmlpath.Parse(strings.NewReader(response))
doc, err := xmltree.ParseXML(strings.NewReader(response))
if err != nil {
return "", err
}
@ -54,7 +53,7 @@ func ParseOpenShellResponse(response string) (string, error) {
}
func ParseExecuteCommandResponse(response string) (string, error) {
doc, err := xmlpath.Parse(strings.NewReader(response))
doc, err := xmltree.ParseXML(strings.NewReader(response))
if err != nil {
return "", err
}
@ -67,16 +66,16 @@ func ParseSlurpOutputErrResponse(response string, stdout, stderr io.Writer) (boo
exitCode int
)
doc, err := xmlpath.Parse(strings.NewReader(response))
doc, err := xmltree.ParseXML(strings.NewReader(response))
stdouts, _ := xpath(doc, "//rsp:Stream[@Name='stdout']")
stdouts, _ := xPath(doc, "//rsp:Stream[@Name='stdout']")
for _, node := range stdouts {
content, _ := base64.StdEncoding.DecodeString(node.String())
content, _ := base64.StdEncoding.DecodeString(node.ResValue())
stdout.Write(content)
}
stderrs, _ := xpath(doc, "//rsp:Stream[@Name='stderr']")
stderrs, _ := xPath(doc, "//rsp:Stream[@Name='stderr']")
for _, node := range stderrs {
content, _ := base64.StdEncoding.DecodeString(node.String())
content, _ := base64.StdEncoding.DecodeString(node.ResValue())
stderr.Write(content)
}
@ -101,11 +100,11 @@ func ParseSlurpOutputResponse(response string, stream io.Writer, streamType stri
exitCode int
)
doc, err := xmlpath.Parse(strings.NewReader(response))
doc, err := xmltree.ParseXML(strings.NewReader(response))
nodes, _ := xpath(doc, fmt.Sprintf("//rsp:Stream[@Name='%s']", streamType))
nodes, _ := xPath(doc, fmt.Sprintf("//rsp:Stream[@Name='%s']", streamType))
for _, node := range nodes {
content, _ := base64.StdEncoding.DecodeString(node.String())
content, _ := base64.StdEncoding.DecodeString(node.ResValue())
stream.Write(content)
}

View File

@ -27,10 +27,10 @@ func (s *Shell) Execute(command string, arguments ...string) (*Command, error) {
}
// Close will terminate this shell. No commands can be issued once the shell is closed.
func (shell *Shell) Close() error {
request := NewDeleteShellRequest(shell.client.url, shell.id, &shell.client.Parameters)
func (s *Shell) Close() error {
request := NewDeleteShellRequest(s.client.url, s.id, &s.client.Parameters)
defer request.Free()
_, err := shell.client.sendRequest(request)
_, err := s.client.sendRequest(request)
return err
}

View File

@ -1,8 +1,9 @@
package soap
import (
"github.com/masterzen/simplexml/dom"
"strconv"
"github.com/masterzen/simplexml/dom"
)
type HeaderOption struct {
@ -99,62 +100,62 @@ func (self *SoapHeader) Options(options []HeaderOption) *SoapHeader {
}
func (self *SoapHeader) Build() *SoapMessage {
header := self.createElement(self.message.envelope, "Header", NS_SOAP_ENV)
header := self.createElement(self.message.envelope, "Header", DOM_NS_SOAP_ENV)
if self.to != "" {
to := self.createElement(header, "To", NS_ADDRESSING)
to := self.createElement(header, "To", DOM_NS_ADDRESSING)
to.SetContent(self.to)
}
if self.replyTo != "" {
replyTo := self.createElement(header, "ReplyTo", NS_ADDRESSING)
a := self.createMUElement(replyTo, "Address", NS_ADDRESSING, true)
replyTo := self.createElement(header, "ReplyTo", DOM_NS_ADDRESSING)
a := self.createMUElement(replyTo, "Address", DOM_NS_ADDRESSING, true)
a.SetContent(self.replyTo)
}
if self.maxEnvelopeSize != "" {
envelope := self.createMUElement(header, "MaxEnvelopeSize", NS_WSMAN_DMTF, true)
envelope := self.createMUElement(header, "MaxEnvelopeSize", DOM_NS_WSMAN_DMTF, true)
envelope.SetContent(self.maxEnvelopeSize)
}
if self.timeout != "" {
timeout := self.createElement(header, "OperationTimeout", NS_WSMAN_DMTF)
timeout := self.createElement(header, "OperationTimeout", DOM_NS_WSMAN_DMTF)
timeout.SetContent(self.timeout)
}
if self.id != "" {
id := self.createElement(header, "MessageID", NS_ADDRESSING)
id := self.createElement(header, "MessageID", DOM_NS_ADDRESSING)
id.SetContent(self.id)
}
if self.locale != "" {
locale := self.createMUElement(header, "Locale", NS_WSMAN_DMTF, false)
locale := self.createMUElement(header, "Locale", DOM_NS_WSMAN_DMTF, false)
locale.SetAttr("xml:lang", self.locale)
datalocale := self.createMUElement(header, "DataLocale", NS_WSMAN_MSFT, false)
datalocale := self.createMUElement(header, "DataLocale", DOM_NS_WSMAN_MSFT, false)
datalocale.SetAttr("xml:lang", self.locale)
}
if self.action != "" {
action := self.createMUElement(header, "Action", NS_ADDRESSING, true)
action := self.createMUElement(header, "Action", DOM_NS_ADDRESSING, true)
action.SetContent(self.action)
}
if self.shellId != "" {
selectorSet := self.createElement(header, "SelectorSet", NS_WSMAN_DMTF)
selector := self.createElement(selectorSet, "Selector", NS_WSMAN_DMTF)
selectorSet := self.createElement(header, "SelectorSet", DOM_NS_WSMAN_DMTF)
selector := self.createElement(selectorSet, "Selector", DOM_NS_WSMAN_DMTF)
selector.SetAttr("Name", "ShellId")
selector.SetContent(self.shellId)
}
if self.resourceURI != "" {
resource := self.createMUElement(header, "ResourceURI", NS_WSMAN_DMTF, true)
resource := self.createMUElement(header, "ResourceURI", DOM_NS_WSMAN_DMTF, true)
resource.SetContent(self.resourceURI)
}
if len(self.options) > 0 {
set := self.createElement(header, "OptionSet", NS_WSMAN_DMTF)
set := self.createElement(header, "OptionSet", DOM_NS_WSMAN_DMTF)
for _, option := range self.options {
e := self.createElement(set, "Option", NS_WSMAN_DMTF)
e := self.createElement(set, "Option", DOM_NS_WSMAN_DMTF)
e.SetAttr("Name", option.key)
e.SetContent(option.value)
}

View File

@ -27,7 +27,7 @@ func NewMessage() (message *SoapMessage) {
e := dom.CreateElement("Envelope")
doc.SetRoot(e)
AddUsualNamespaces(e)
NS_SOAP_ENV.SetTo(e)
DOM_NS_SOAP_ENV.SetTo(e)
message = &SoapMessage{document: doc, envelope: e}
return
@ -36,7 +36,7 @@ func NewMessage() (message *SoapMessage) {
func (message *SoapMessage) NewBody() (body *dom.Element) {
body = dom.CreateElement("Body")
message.envelope.AddChild(body)
NS_SOAP_ENV.SetTo(body)
DOM_NS_SOAP_ENV.SetTo(body)
return
}

View File

@ -1,24 +1,59 @@
package soap
import (
"github.com/ChrisTrenkamp/goxpath"
"github.com/masterzen/simplexml/dom"
"github.com/masterzen/xmlpath"
)
// Namespaces
const (
NS_SOAP_ENV = "http://www.w3.org/2003/05/soap-envelope"
NS_ADDRESSING = "http://schemas.xmlsoap.org/ws/2004/08/addressing"
NS_CIMBINDING = "http://schemas.dmtf.org/wbem/wsman/1/cimbinding.xsd"
NS_ENUM = "http://schemas.xmlsoap.org/ws/2004/09/enumeration"
NS_TRANSFER = "http://schemas.xmlsoap.org/ws/2004/09/transfer"
NS_WSMAN_DMTF = "http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd"
NS_WSMAN_MSFT = "http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd"
NS_SCHEMA_INST = "http://www.w3.org/2001/XMLSchema-instance"
NS_WIN_SHELL = "http://schemas.microsoft.com/wbem/wsman/1/windows/shell"
NS_WSMAN_FAULT = "http://schemas.microsoft.com/wbem/wsman/1/wsmanfault"
)
// Namespace Prefixes
const (
NSP_SOAP_ENV = "env"
NSP_ADDRESSING = "a"
NSP_CIMBINDING = "b"
NSP_ENUM = "n"
NSP_TRANSFER = "x"
NSP_WSMAN_DMTF = "w"
NSP_WSMAN_MSFT = "p"
NSP_SCHEMA_INST = "xsi"
NSP_WIN_SHELL = "rsp"
NSP_WSMAN_FAULT = "f"
)
// DOM Namespaces
var (
NS_SOAP_ENV = dom.Namespace{"env", "http://www.w3.org/2003/05/soap-envelope"}
NS_ADDRESSING = dom.Namespace{"a", "http://schemas.xmlsoap.org/ws/2004/08/addressing"}
NS_CIMBINDING = dom.Namespace{"b", "http://schemas.dmtf.org/wbem/wsman/1/cimbinding.xsd"}
NS_ENUM = dom.Namespace{"n", "http://schemas.xmlsoap.org/ws/2004/09/enumeration"}
NS_TRANSFER = dom.Namespace{"x", "http://schemas.xmlsoap.org/ws/2004/09/transfer"}
NS_WSMAN_DMTF = dom.Namespace{"w", "http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd"}
NS_WSMAN_MSFT = dom.Namespace{"p", "http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd"}
NS_SCHEMA_INST = dom.Namespace{"xsi", "http://www.w3.org/2001/XMLSchema-instance"}
NS_WIN_SHELL = dom.Namespace{"rsp", "http://schemas.microsoft.com/wbem/wsman/1/windows/shell"}
NS_WSMAN_FAULT = dom.Namespace{"f", "http://schemas.microsoft.com/wbem/wsman/1/wsmanfault"}
DOM_NS_SOAP_ENV = dom.Namespace{"env", "http://www.w3.org/2003/05/soap-envelope"}
DOM_NS_ADDRESSING = dom.Namespace{"a", "http://schemas.xmlsoap.org/ws/2004/08/addressing"}
DOM_NS_CIMBINDING = dom.Namespace{"b", "http://schemas.dmtf.org/wbem/wsman/1/cimbinding.xsd"}
DOM_NS_ENUM = dom.Namespace{"n", "http://schemas.xmlsoap.org/ws/2004/09/enumeration"}
DOM_NS_TRANSFER = dom.Namespace{"x", "http://schemas.xmlsoap.org/ws/2004/09/transfer"}
DOM_NS_WSMAN_DMTF = dom.Namespace{"w", "http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd"}
DOM_NS_WSMAN_MSFT = dom.Namespace{"p", "http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd"}
DOM_NS_SCHEMA_INST = dom.Namespace{"xsi", "http://www.w3.org/2001/XMLSchema-instance"}
DOM_NS_WIN_SHELL = dom.Namespace{"rsp", "http://schemas.microsoft.com/wbem/wsman/1/windows/shell"}
DOM_NS_WSMAN_FAULT = dom.Namespace{"f", "http://schemas.microsoft.com/wbem/wsman/1/wsmanfault"}
)
var MostUsed = [...]dom.Namespace{NS_SOAP_ENV, NS_ADDRESSING, NS_WIN_SHELL, NS_WSMAN_DMTF, NS_WSMAN_MSFT}
var MostUsed = [...]dom.Namespace{
DOM_NS_SOAP_ENV,
DOM_NS_ADDRESSING,
DOM_NS_WIN_SHELL,
DOM_NS_WSMAN_DMTF,
DOM_NS_WSMAN_MSFT,
}
func AddUsualNamespaces(node *dom.Element) {
for _, ns := range MostUsed {
@ -26,12 +61,21 @@ func AddUsualNamespaces(node *dom.Element) {
}
}
func GetAllNamespaces() []xmlpath.Namespace {
var ns = []dom.Namespace{NS_WIN_SHELL, NS_ADDRESSING, NS_WSMAN_DMTF, NS_WSMAN_MSFT, NS_SOAP_ENV}
var xmlpathNs = make([]xmlpath.Namespace, 0, 4)
for _, namespace := range ns {
xmlpathNs = append(xmlpathNs, xmlpath.Namespace{Prefix: namespace.Prefix, Uri: namespace.Uri})
func GetAllXPathNamespaces() func(o *goxpath.Opts) {
ns := map[string]string{
NSP_SOAP_ENV: NS_SOAP_ENV,
NSP_ADDRESSING: NS_ADDRESSING,
NSP_CIMBINDING: NS_CIMBINDING,
NSP_ENUM: NS_ENUM,
NSP_TRANSFER: NS_TRANSFER,
NSP_WSMAN_DMTF: NS_WSMAN_DMTF,
NSP_WSMAN_MSFT: NS_WSMAN_MSFT,
NSP_SCHEMA_INST: NS_SCHEMA_INST,
NSP_WIN_SHELL: NS_WIN_SHELL,
NSP_WSMAN_FAULT: NS_WSMAN_FAULT,
}
return func(o *goxpath.Opts) {
o.NS = ns
}
return xmlpathNs
}

View File

@ -1,185 +0,0 @@
This software is licensed under the LGPLv3, included below.
As a special exception to the GNU Lesser General Public License version 3
("LGPL3"), the copyright holders of this Library give you permission to
convey to a third party a Combined Work that links statically or dynamically
to this Library without providing any Minimal Corresponding Source or
Minimal Application Code as set out in 4d or providing the installation
information set out in section 4e, provided that you comply with the other
provisions of LGPL3 and provided that you meet, for the Application the
terms and conditions of the license(s) which apply to the Application.
Except as stated in this special exception, the provisions of LGPL3 will
continue to comply in full to this Library. If you modify this Library, you
may apply this exception to your version of this Library, but you are not
obliged to do so. If you do not wish to do so, delete this exception
statement from your version. This exception does not (and cannot) modify any
license terms which apply to the Application, with which you must still
comply.
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.

View File

@ -1,95 +0,0 @@
// Package xmlpath implements a strict subset of the XPath specification for the Go language.
//
// The XPath specification is available at:
//
// http://www.w3.org/TR/xpath
//
// Path expressions supported by this package are in the following format,
// with all components being optional:
//
// /axis-name::node-test[predicate]/axis-name::node-test[predicate]
//
// At the moment, xmlpath is compatible with the XPath specification
// to the following extent:
//
// - All axes are supported ("child", "following-sibling", etc)
// - All abbreviated forms are supported (".", "//", etc)
// - All node types except for namespace are supported
// - Predicates are restricted to [N], [path], and [path=literal] forms
// - Only a single predicate is supported per path step
// - Namespaces are experimentally supported
// - Richer expressions
//
// For example, assuming the following document:
//
// <library>
// <!-- Great book. -->
// <book id="b0836217462" available="true">
// <isbn>0836217462</isbn>
// <title lang="en">Being a Dog Is a Full-Time Job</title>
// <quote>I'd dog paddle the deepest ocean.</quote>
// <author id="CMS">
// <?echo "go rocks"?>
// <name>Charles M Schulz</name>
// <born>1922-11-26</born>
// <dead>2000-02-12</dead>
// </author>
// <character id="PP">
// <name>Peppermint Patty</name>
// <born>1966-08-22</born>
// <qualification>bold, brash and tomboyish</qualification>
// </character>
// <character id="Snoopy">
// <name>Snoopy</name>
// <born>1950-10-04</born>
// <qualification>extroverted beagle</qualification>
// </character>
// </book>
// </library>
//
// The following examples are valid path expressions, and the first
// match has the indicated value:
//
// /library/book/isbn => "0836217462"
// library/*/isbn => "0836217462"
// /library/book/../book/./isbn => "0836217462"
// /library/book/character[2]/name => "Snoopy"
// /library/book/character[born='1950-10-04']/name => "Snoopy"
// /library/book//node()[@id='PP']/name => "Peppermint Patty"
// //book[author/@id='CMS']/title => "Being a Dog Is a Full-Time Job"},
// /library/book/preceding::comment() => " Great book. "
//
// To run an expression, compile it, and then apply the compiled path to any
// number of context nodes, from one or more parsed xml documents:
//
// path := xmlpath.MustCompile("/library/book/isbn")
// root, err := xmlpath.Parse(file)
// if err != nil {
// log.Fatal(err)
// }
// if value, ok := path.String(root); ok {
// fmt.Println("Found:", value)
// }
//
// To use xmlpath with namespaces, it is required to give the supported set of namespace
// when compiling:
//
//
// var namespaces = []xmlpath.Namespace {
// { "s", "http://www.w3.org/2003/05/soap-envelope" },
// { "a", "http://schemas.xmlsoap.org/ws/2004/08/addressing" },
// }
// path, err := xmlpath.CompileWithNamespace("/s:Header/a:To", namespaces)
// if err != nil {
// log.Fatal(err)
// }
// root, err := xmlpath.Parse(file)
// if err != nil {
// log.Fatal(err)
// }
// if value, ok := path.String(root); ok {
// fmt.Println("Found:", value)
// }
//
package xmlpath

View File

@ -1,233 +0,0 @@
package xmlpath
import (
"encoding/xml"
"io"
)
// Node is an item in an xml tree that was compiled to
// be processed via xml paths. A node may represent:
//
// - An element in the xml document (<body>)
// - An attribute of an element in the xml document (href="...")
// - A comment in the xml document (<!--...-->)
// - A processing instruction in the xml document (<?...?>)
// - Some text within the xml document
//
type Node struct {
kind nodeKind
name xml.Name
attr string
text []byte
nodes []Node
pos int
end int
up *Node
down []*Node
}
type nodeKind int
const (
anyNode nodeKind = iota
startNode
endNode
attrNode
textNode
commentNode
procInstNode
)
// String returns the string value of node.
//
// The string value of a node is:
//
// - For element nodes, the concatenation of all text nodes within the element.
// - For text nodes, the text itself.
// - For attribute nodes, the attribute value.
// - For comment nodes, the text within the comment delimiters.
// - For processing instruction nodes, the content of the instruction.
//
func (node *Node) String() string {
if node.kind == attrNode {
return node.attr
}
return string(node.Bytes())
}
// Bytes returns the string value of node as a byte slice.
// See Node.String for a description of what the string value of a node is.
func (node *Node) Bytes() []byte {
if node.kind == attrNode {
return []byte(node.attr)
}
if node.kind != startNode {
return node.text
}
var text []byte
for i := node.pos; i < node.end; i++ {
if node.nodes[i].kind == textNode {
text = append(text, node.nodes[i].text...)
}
}
return text
}
// equals returns whether the string value of node is equal to s,
// without allocating memory.
func (node *Node) equals(s string) bool {
if node.kind == attrNode {
return s == node.attr
}
if node.kind != startNode {
if len(s) != len(node.text) {
return false
}
for i := range s {
if s[i] != node.text[i] {
return false
}
}
return true
}
si := 0
for i := node.pos; i < node.end; i++ {
if node.nodes[i].kind == textNode {
for _, c := range node.nodes[i].text {
if si > len(s) {
return false
}
if s[si] != c {
return false
}
si++
}
}
}
return si == len(s)
}
// Parse reads an xml document from r, parses it, and returns its root node.
func Parse(r io.Reader) (*Node, error) {
return ParseDecoder(xml.NewDecoder(r))
}
// ParseHTML reads an HTML-like document from r, parses it, and returns
// its root node.
func ParseHTML(r io.Reader) (*Node, error) {
d := xml.NewDecoder(r)
d.Strict = false
d.AutoClose = xml.HTMLAutoClose
d.Entity = xml.HTMLEntity
return ParseDecoder(d)
}
// ParseDecoder parses the xml document being decoded by d and returns
// its root node.
func ParseDecoder(d *xml.Decoder) (*Node, error) {
var nodes []Node
var text []byte
// The root node.
nodes = append(nodes, Node{kind: startNode})
for {
t, err := d.Token()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
switch t := t.(type) {
case xml.EndElement:
nodes = append(nodes, Node{
kind: endNode,
})
case xml.StartElement:
nodes = append(nodes, Node{
kind: startNode,
name: t.Name,
})
for _, attr := range t.Attr {
nodes = append(nodes, Node{
kind: attrNode,
name: attr.Name,
attr: attr.Value,
})
}
case xml.CharData:
texti := len(text)
text = append(text, t...)
nodes = append(nodes, Node{
kind: textNode,
text: text[texti : texti+len(t)],
})
case xml.Comment:
texti := len(text)
text = append(text, t...)
nodes = append(nodes, Node{
kind: commentNode,
text: text[texti : texti+len(t)],
})
case xml.ProcInst:
texti := len(text)
text = append(text, t.Inst...)
nodes = append(nodes, Node{
kind: procInstNode,
name: xml.Name{Local: t.Target},
text: text[texti : texti+len(t.Inst)],
})
}
}
// Close the root node.
nodes = append(nodes, Node{kind: endNode})
stack := make([]*Node, 0, len(nodes))
downs := make([]*Node, len(nodes))
downCount := 0
for pos := range nodes {
switch nodes[pos].kind {
case startNode, attrNode, textNode, commentNode, procInstNode:
node := &nodes[pos]
node.nodes = nodes
node.pos = pos
if len(stack) > 0 {
node.up = stack[len(stack)-1]
}
if node.kind == startNode {
stack = append(stack, node)
} else {
node.end = pos + 1
}
case endNode:
node := stack[len(stack)-1]
node.end = pos
stack = stack[:len(stack)-1]
// Compute downs. Doing that here is what enables the
// use of a slice of a contiguous pre-allocated block.
node.down = downs[downCount:downCount]
for i := node.pos + 1; i < node.end; i++ {
if nodes[i].up == node {
switch nodes[i].kind {
case startNode, textNode, commentNode, procInstNode:
node.down = append(node.down, &nodes[i])
downCount++
}
}
}
if len(stack) == 0 {
return node, nil
}
}
}
return nil, io.EOF
}

View File

@ -1,642 +0,0 @@
package xmlpath
import (
"fmt"
"strconv"
"unicode/utf8"
)
// Namespace represents a given XML Namespace
type Namespace struct {
Prefix string
Uri string
}
// Path is a compiled path that can be applied to a context
// node to obtain a matching node set.
// A single Path can be applied concurrently to any number
// of context nodes.
type Path struct {
path string
steps []pathStep
}
// Iter returns an iterator that goes over the list of nodes
// that p matches on the given context.
func (p *Path) Iter(context *Node) *Iter {
iter := Iter{
make([]pathStepState, len(p.steps)),
make([]bool, len(context.nodes)),
}
for i := range p.steps {
iter.state[i].step = &p.steps[i]
}
iter.state[0].init(context)
return &iter
}
// Exists returns whether any nodes match p on the given context.
func (p *Path) Exists(context *Node) bool {
return p.Iter(context).Next()
}
// String returns the string value of the first node matched
// by p on the given context.
//
// See the documentation of Node.String.
func (p *Path) String(context *Node) (s string, ok bool) {
iter := p.Iter(context)
if iter.Next() {
return iter.Node().String(), true
}
return "", false
}
// Bytes returns as a byte slice the string value of the first
// node matched by p on the given context.
//
// See the documentation of Node.String.
func (p *Path) Bytes(node *Node) (b []byte, ok bool) {
iter := p.Iter(node)
if iter.Next() {
return iter.Node().Bytes(), true
}
return nil, false
}
// Iter iterates over node sets.
type Iter struct {
state []pathStepState
seen []bool
}
// Node returns the current node.
// Must only be called after Iter.Next returns true.
func (iter *Iter) Node() *Node {
state := iter.state[len(iter.state)-1]
if state.pos == 0 {
panic("Iter.Node called before Iter.Next")
}
if state.node == nil {
panic("Iter.Node called after Iter.Next false")
}
return state.node
}
// Next iterates to the next node in the set, if any, and
// returns whether there is a node available.
func (iter *Iter) Next() bool {
tip := len(iter.state) - 1
outer:
for {
for !iter.state[tip].next() {
tip--
if tip == -1 {
return false
}
}
for tip < len(iter.state)-1 {
tip++
iter.state[tip].init(iter.state[tip-1].node)
if !iter.state[tip].next() {
tip--
continue outer
}
}
if iter.seen[iter.state[tip].node.pos] {
continue
}
iter.seen[iter.state[tip].node.pos] = true
return true
}
panic("unreachable")
}
type pathStepState struct {
step *pathStep
node *Node
pos int
idx int
aux int
}
func (s *pathStepState) init(node *Node) {
s.node = node
s.pos = 0
s.idx = 0
s.aux = 0
}
func (s *pathStepState) next() bool {
for s._next() {
s.pos++
if s.step.pred == nil {
return true
}
if s.step.pred.bval {
if s.step.pred.path.Exists(s.node) {
return true
}
} else if s.step.pred.path != nil {
iter := s.step.pred.path.Iter(s.node)
for iter.Next() {
if iter.Node().equals(s.step.pred.sval) {
return true
}
}
} else {
if s.step.pred.ival == s.pos {
return true
}
}
}
return false
}
func (s *pathStepState) _next() bool {
if s.node == nil {
return false
}
if s.step.root && s.idx == 0 {
for s.node.up != nil {
s.node = s.node.up
}
}
switch s.step.axis {
case "self":
if s.idx == 0 && s.step.match(s.node) {
s.idx++
return true
}
case "parent":
if s.idx == 0 && s.node.up != nil && s.step.match(s.node.up) {
s.idx++
s.node = s.node.up
return true
}
case "ancestor", "ancestor-or-self":
if s.idx == 0 && s.step.axis == "ancestor-or-self" {
s.idx++
if s.step.match(s.node) {
return true
}
}
for s.node.up != nil {
s.node = s.node.up
s.idx++
if s.step.match(s.node) {
return true
}
}
case "child":
var down []*Node
if s.idx == 0 {
down = s.node.down
} else {
down = s.node.up.down
}
for s.idx < len(down) {
node := down[s.idx]
s.idx++
if s.step.match(node) {
s.node = node
return true
}
}
case "descendant", "descendant-or-self":
if s.idx == 0 {
s.idx = s.node.pos
s.aux = s.node.end
if s.step.axis == "descendant" {
s.idx++
}
}
for s.idx < s.aux {
node := &s.node.nodes[s.idx]
s.idx++
if node.kind == attrNode {
continue
}
if s.step.match(node) {
s.node = node
return true
}
}
case "following":
if s.idx == 0 {
s.idx = s.node.end
}
for s.idx < len(s.node.nodes) {
node := &s.node.nodes[s.idx]
s.idx++
if node.kind == attrNode {
continue
}
if s.step.match(node) {
s.node = node
return true
}
}
case "following-sibling":
var down []*Node
if s.node.up != nil {
down = s.node.up.down
if s.idx == 0 {
for s.idx < len(down) {
node := down[s.idx]
s.idx++
if node == s.node {
break
}
}
}
}
for s.idx < len(down) {
node := down[s.idx]
s.idx++
if s.step.match(node) {
s.node = node
return true
}
}
case "preceding":
if s.idx == 0 {
s.aux = s.node.pos // Detect ancestors.
s.idx = s.node.pos - 1
}
for s.idx >= 0 {
node := &s.node.nodes[s.idx]
s.idx--
if node.kind == attrNode {
continue
}
if node == s.node.nodes[s.aux].up {
s.aux = s.node.nodes[s.aux].up.pos
continue
}
if s.step.match(node) {
s.node = node
return true
}
}
case "preceding-sibling":
var down []*Node
if s.node.up != nil {
down = s.node.up.down
if s.aux == 0 {
s.aux = 1
for s.idx < len(down) {
node := down[s.idx]
s.idx++
if node == s.node {
s.idx--
break
}
}
}
}
for s.idx >= 0 {
node := down[s.idx]
s.idx--
if s.step.match(node) {
s.node = node
return true
}
}
case "attribute":
if s.idx == 0 {
s.idx = s.node.pos + 1
s.aux = s.node.end
}
for s.idx < s.aux {
node := &s.node.nodes[s.idx]
s.idx++
if node.kind != attrNode {
break
}
if s.step.match(node) {
s.node = node
return true
}
}
}
s.node = nil
return false
}
type pathPredicate struct {
path *Path
sval string
ival int
bval bool
}
type pathStep struct {
root bool
axis string
name string
prefix string
uri string
kind nodeKind
pred *pathPredicate
}
func (step *pathStep) match(node *Node) bool {
return node.kind != endNode &&
(step.kind == anyNode || step.kind == node.kind) &&
(step.name == "*" || (node.name.Local == step.name && (node.name.Space != "" && node.name.Space == step.uri || node.name.Space == "")))
}
// MustCompile returns the compiled path, and panics if
// there are any errors.
func MustCompile(path string) *Path {
e, err := Compile(path)
if err != nil {
panic(err)
}
return e
}
// Compile returns the compiled path.
func Compile(path string) (*Path, error) {
c := pathCompiler{path, 0, []Namespace{} }
if path == "" {
return nil, c.errorf("empty path")
}
p, err := c.parsePath()
if err != nil {
return nil, err
}
return p, nil
}
// Compile the path with the knowledge of the given namespaces
func CompileWithNamespace(path string, ns []Namespace) (*Path, error) {
c := pathCompiler{path, 0, ns}
if path == "" {
return nil, c.errorf("empty path")
}
p, err := c.parsePath()
if err != nil {
return nil, err
}
return p, nil
}
type pathCompiler struct {
path string
i int
ns []Namespace
}
func (c *pathCompiler) errorf(format string, args ...interface{}) error {
return fmt.Errorf("compiling xml path %q:%d: %s", c.path, c.i, fmt.Sprintf(format, args...))
}
func (c *pathCompiler) parsePath() (path *Path, err error) {
var steps []pathStep
var start = c.i
for {
step := pathStep{axis: "child"}
if c.i == 0 && c.skipByte('/') {
step.root = true
if len(c.path) == 1 {
step.name = "*"
}
}
if c.peekByte('/') {
step.axis = "descendant-or-self"
step.name = "*"
} else if c.skipByte('@') {
mark := c.i
if !c.skipName() {
return nil, c.errorf("missing name after @")
}
step.axis = "attribute"
step.name = c.path[mark:c.i]
step.kind = attrNode
} else {
mark := c.i
if c.skipName() {
step.name = c.path[mark:c.i]
}
if step.name == "" {
return nil, c.errorf("missing name")
} else if step.name == "*" {
step.kind = startNode
} else if step.name == "." {
step.axis = "self"
step.name = "*"
} else if step.name == ".." {
step.axis = "parent"
step.name = "*"
} else {
if c.skipByte(':') {
if !c.skipByte(':') {
mark = c.i
if c.skipName() {
step.prefix = step.name
step.name = c.path[mark:c.i]
// check prefix
found := false
for _, ns := range c.ns {
if ns.Prefix == step.prefix {
step.uri = ns.Uri
found = true
break
}
}
if !found {
return nil, c.errorf("unknown namespace prefix: %s", step.prefix)
}
} else {
return nil, c.errorf("missing name after namespace prefix")
}
} else {
switch step.name {
case "attribute":
step.kind = attrNode
case "self", "child", "parent":
case "descendant", "descendant-or-self":
case "ancestor", "ancestor-or-self":
case "following", "following-sibling":
case "preceding", "preceding-sibling":
default:
return nil, c.errorf("unsupported axis: %q", step.name)
}
step.axis = step.name
mark = c.i
if !c.skipName() {
return nil, c.errorf("missing name")
}
step.name = c.path[mark:c.i]
}
}
if c.skipByte('(') {
conflict := step.kind != anyNode
switch step.name {
case "node":
// must be anyNode
case "text":
step.kind = textNode
case "comment":
step.kind = commentNode
case "processing-instruction":
step.kind = procInstNode
default:
return nil, c.errorf("unsupported expression: %s()", step.name)
}
if conflict {
return nil, c.errorf("%s() cannot succeed on axis %q", step.name, step.axis)
}
literal, err := c.parseLiteral()
if err == errNoLiteral {
step.name = "*"
} else if err != nil {
return nil, c.errorf("%v", err)
} else if step.kind == procInstNode {
step.name = literal
} else {
return nil, c.errorf("%s() has no arguments", step.name)
}
if !c.skipByte(')') {
return nil, c.errorf("missing )")
}
} else if step.name == "*" && step.kind == anyNode {
step.kind = startNode
}
}
}
if c.skipByte('[') {
step.pred = &pathPredicate{}
if ival, ok := c.parseInt(); ok {
if ival == 0 {
return nil, c.errorf("positions start at 1")
}
step.pred.ival = ival
} else {
path, err := c.parsePath()
if err != nil {
return nil, err
}
if path.path[0] == '-' {
if _, err = strconv.Atoi(path.path); err == nil {
return nil, c.errorf("positions must be positive")
}
}
step.pred.path = path
if c.skipByte('=') {
sval, err := c.parseLiteral()
if err != nil {
return nil, c.errorf("%v", err)
}
step.pred.sval = sval
} else {
step.pred.bval = true
}
}
if !c.skipByte(']') {
return nil, c.errorf("expected ']'")
}
}
steps = append(steps, step)
//fmt.Printf("step: %#v\n", step)
if !c.skipByte('/') {
if (start == 0 || start == c.i) && c.i < len(c.path) {
return nil, c.errorf("unexpected %q", c.path[c.i])
}
return &Path{steps: steps, path: c.path[start:c.i]}, nil
}
}
panic("unreachable")
}
var errNoLiteral = fmt.Errorf("expected a literal string")
func (c *pathCompiler) parseLiteral() (string, error) {
if c.skipByte('"') {
mark := c.i
if !c.skipByteFind('"') {
return "", fmt.Errorf(`missing '"'`)
}
return c.path[mark:c.i-1], nil
}
if c.skipByte('\'') {
mark := c.i
if !c.skipByteFind('\'') {
return "", fmt.Errorf(`missing "'"`)
}
return c.path[mark:c.i-1], nil
}
return "", errNoLiteral
}
func (c *pathCompiler) parseInt() (v int, ok bool) {
mark := c.i
for c.i < len(c.path) && c.path[c.i] >= '0' && c.path[c.i] <= '9' {
v *= 10
v += int(c.path[c.i]) - '0'
c.i++
}
if c.i == mark {
return 0, false
}
return v, true
}
func (c *pathCompiler) skipByte(b byte) bool {
if c.i < len(c.path) && c.path[c.i] == b {
c.i++
return true
}
return false
}
func (c *pathCompiler) skipByteFind(b byte) bool {
for i := c.i; i < len(c.path); i++ {
if c.path[i] == b {
c.i = i+1
return true
}
}
return false
}
func (c *pathCompiler) peekByte(b byte) bool {
return c.i < len(c.path) && c.path[c.i] == b
}
func (c *pathCompiler) skipName() bool {
if c.i >= len(c.path) {
return false
}
if c.path[c.i] == '*' {
c.i++
return true
}
start := c.i
for c.i < len(c.path) && (c.path[c.i] >= utf8.RuneSelf || isNameByte(c.path[c.i])) {
c.i++
}
return c.i > start
}
func isNameByte(c byte) bool {
return 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || '0' <= c && c <= '9' || c == '_' || c == '.' || c == '-'
}

View File

@ -11,14 +11,14 @@ import (
"github.com/masterzen/winrm"
)
func parseEndpoint(addr string, https bool, insecure bool, caCert []byte, timeout time.Duration) (*winrm.Endpoint, error) {
func parseEndpoint(addr string, https bool, insecure bool, tlsServerName string, caCert []byte, timeout time.Duration) (*winrm.Endpoint, error) {
var host string
var port int
if addr == "" {
return nil, errors.New("Couldn't convert \"\" to an address.")
}
if !strings.Contains(addr, ":") {
if !strings.Contains(addr, ":") || (strings.HasPrefix(addr, "[") && strings.HasSuffix(addr, "]")) {
host = addr
port = 5985
} else {
@ -26,7 +26,8 @@ func parseEndpoint(addr string, https bool, insecure bool, caCert []byte, timeou
if err != nil {
return nil, fmt.Errorf("Couldn't convert \"%s\" to an address.", addr)
}
host = shost
// Check for IPv6 addresses and reformat appropriately
host = IpFormat(shost)
port, err = strconv.Atoi(sport)
if err != nil {
return nil, errors.New("Couldn't convert \"%s\" to a port number.")
@ -34,11 +35,24 @@ func parseEndpoint(addr string, https bool, insecure bool, caCert []byte, timeou
}
return &winrm.Endpoint{
Host: host,
Port: port,
HTTPS: https,
Insecure: insecure,
CACert: caCert,
Timeout: timeout,
Host: host,
Port: port,
HTTPS: https,
Insecure: insecure,
TLSServerName: tlsServerName,
CACert: caCert,
Timeout: timeout,
}, nil
}
// IpFormat formats the IP correctly, so we don't provide IPv6 address in an IPv4 format during node communication.
// We return the ip parameter as is if it's an IPv4 address or a hostname.
func IpFormat(ip string) string {
ipObj := net.ParseIP(ip)
// Return the ip/host as is if it's either a hostname or an IPv4 address.
if ipObj == nil || ipObj.To4() != nil {
return ip
}
return fmt.Sprintf("[%s]", ip)
}

View File

@ -20,6 +20,7 @@ type Config struct {
Auth Auth
Https bool
Insecure bool
TLSServerName string
CACertBytes []byte
ConnectTimeout time.Duration
OperationTimeout time.Duration
@ -33,7 +34,7 @@ type Auth struct {
}
func New(addr string, config *Config) (*Winrmcp, error) {
endpoint, err := parseEndpoint(addr, config.Https, config.Insecure, config.CACertBytes, config.ConnectTimeout)
endpoint, err := parseEndpoint(addr, config.Https, config.Insecure, config.TLSServerName, config.CACertBytes, config.ConnectTimeout)
if err != nil {
return nil, err
}

View File

@ -1,4 +1,4 @@
Copyright (C) 2013-2015 by Maxim Bublis <b@codemonkey.ru>
Copyright (C) 2013-2016 by Maxim Bublis <b@codemonkey.ru>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the

View File

@ -1,6 +1,7 @@
# UUID package for Go language
[![Build Status](https://travis-ci.org/satori/go.uuid.png?branch=master)](https://travis-ci.org/satori/go.uuid)
[![Coverage Status](https://coveralls.io/repos/github/satori/go.uuid/badge.svg?branch=master)](https://coveralls.io/github/satori/go.uuid)
[![GoDoc](http://godoc.org/github.com/satori/go.uuid?status.png)](http://godoc.org/github.com/satori/go.uuid)
This package provides pure Go implementation of Universally Unique Identifier (UUID). Supported both creation and parsing of UUIDs.
@ -22,9 +23,7 @@ Use the `go` command:
## Requirements
UUID package requires any stable version of Go Programming Language.
It is tested against following versions of Go: 1.0-1.5
UUID package requires Go >= 1.2.
## Example
@ -60,7 +59,7 @@ func main() {
## Copyright
Copyright (C) 2013-2015 by Maxim Bublis <b@codemonkey.ru>.
Copyright (C) 2013-2016 by Maxim Bublis <b@codemonkey.ru>.
UUID package released under MIT License.
See [LICENSE](https://github.com/satori/go.uuid/blob/master/LICENSE) for details.

View File

@ -127,6 +127,13 @@ func unixTimeFunc() uint64 {
// described in RFC 4122.
type UUID [16]byte
// NullUUID can be used with the standard sql package to represent a
// UUID value that can be NULL in the database
type NullUUID struct {
UUID UUID
Valid bool
}
// The nil UUID is special form of UUID that is specified to have all
// 128 bits set to zero.
var Nil = UUID{}
@ -227,30 +234,48 @@ func (u UUID) MarshalText() (text []byte, err error) {
// "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8"
func (u *UUID) UnmarshalText(text []byte) (err error) {
if len(text) < 32 {
err = fmt.Errorf("uuid: invalid UUID string: %s", text)
err = fmt.Errorf("uuid: UUID string too short: %s", text)
return
}
if bytes.Equal(text[:9], urnPrefix) {
text = text[9:]
} else if text[0] == '{' {
text = text[1:]
t := text[:]
braced := false
if bytes.Equal(t[:9], urnPrefix) {
t = t[9:]
} else if t[0] == '{' {
braced = true
t = t[1:]
}
b := u[:]
for _, byteGroup := range byteGroups {
if text[0] == '-' {
text = text[1:]
for i, byteGroup := range byteGroups {
if i > 0 {
if t[0] != '-' {
err = fmt.Errorf("uuid: invalid string format")
return
}
t = t[1:]
}
_, err = hex.Decode(b[:byteGroup/2], text[:byteGroup])
if len(t) < byteGroup {
err = fmt.Errorf("uuid: UUID string too short: %s", text)
return
}
if i == 4 && len(t) > byteGroup &&
((braced && t[byteGroup] != '}') || len(t[byteGroup:]) > 1 || !braced) {
err = fmt.Errorf("uuid: UUID string too long: %s", text)
return
}
_, err = hex.Decode(b[:byteGroup/2], t[:byteGroup])
if err != nil {
return
}
text = text[byteGroup:]
t = t[byteGroup:]
b = b[byteGroup/2:]
}
@ -298,6 +323,27 @@ func (u *UUID) Scan(src interface{}) error {
return fmt.Errorf("uuid: cannot convert %T to UUID", src)
}
// Value implements the driver.Valuer interface.
func (u NullUUID) Value() (driver.Value, error) {
if !u.Valid {
return nil, nil
}
// Delegate to UUID Value function
return u.UUID.Value()
}
// Scan implements the sql.Scanner interface.
func (u *NullUUID) Scan(src interface{}) error {
if src == nil {
u.UUID, u.Valid = Nil, false
return nil
}
// Delegate to UUID Scan function
u.Valid = true
return u.UUID.Scan(src)
}
// FromBytes returns UUID converted from raw byte slice input.
// It will return error if the slice isn't 16 bytes long.
func FromBytes(input []byte) (u UUID, err error) {

View File

@ -1,648 +0,0 @@
// Copyright 2012 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
// This program generates table.go and table_test.go.
// Invoke as
//
// go run gen.go |gofmt >table.go
// go run gen.go -test |gofmt >table_test.go
import (
"flag"
"fmt"
"math/rand"
"os"
"sort"
"strings"
)
// identifier converts s to a Go exported identifier.
// It converts "div" to "Div" and "accept-charset" to "AcceptCharset".
func identifier(s string) string {
b := make([]byte, 0, len(s))
cap := true
for _, c := range s {
if c == '-' {
cap = true
continue
}
if cap && 'a' <= c && c <= 'z' {
c -= 'a' - 'A'
}
cap = false
b = append(b, byte(c))
}
return string(b)
}
var test = flag.Bool("test", false, "generate table_test.go")
func main() {
flag.Parse()
var all []string
all = append(all, elements...)
all = append(all, attributes...)
all = append(all, eventHandlers...)
all = append(all, extra...)
sort.Strings(all)
if *test {
fmt.Printf("// generated by go run gen.go -test; DO NOT EDIT\n\n")
fmt.Printf("package atom\n\n")
fmt.Printf("var testAtomList = []string{\n")
for _, s := range all {
fmt.Printf("\t%q,\n", s)
}
fmt.Printf("}\n")
return
}
// uniq - lists have dups
// compute max len too
maxLen := 0
w := 0
for _, s := range all {
if w == 0 || all[w-1] != s {
if maxLen < len(s) {
maxLen = len(s)
}
all[w] = s
w++
}
}
all = all[:w]
// Find hash that minimizes table size.
var best *table
for i := 0; i < 1000000; i++ {
if best != nil && 1<<(best.k-1) < len(all) {
break
}
h := rand.Uint32()
for k := uint(0); k <= 16; k++ {
if best != nil && k >= best.k {
break
}
var t table
if t.init(h, k, all) {
best = &t
break
}
}
}
if best == nil {
fmt.Fprintf(os.Stderr, "failed to construct string table\n")
os.Exit(1)
}
// Lay out strings, using overlaps when possible.
layout := append([]string{}, all...)
// Remove strings that are substrings of other strings
for changed := true; changed; {
changed = false
for i, s := range layout {
if s == "" {
continue
}
for j, t := range layout {
if i != j && t != "" && strings.Contains(s, t) {
changed = true
layout[j] = ""
}
}
}
}
// Join strings where one suffix matches another prefix.
for {
// Find best i, j, k such that layout[i][len-k:] == layout[j][:k],
// maximizing overlap length k.
besti := -1
bestj := -1
bestk := 0
for i, s := range layout {
if s == "" {
continue
}
for j, t := range layout {
if i == j {
continue
}
for k := bestk + 1; k <= len(s) && k <= len(t); k++ {
if s[len(s)-k:] == t[:k] {
besti = i
bestj = j
bestk = k
}
}
}
}
if bestk > 0 {
layout[besti] += layout[bestj][bestk:]
layout[bestj] = ""
continue
}
break
}
text := strings.Join(layout, "")
atom := map[string]uint32{}
for _, s := range all {
off := strings.Index(text, s)
if off < 0 {
panic("lost string " + s)
}
atom[s] = uint32(off<<8 | len(s))
}
// Generate the Go code.
fmt.Printf("// generated by go run gen.go; DO NOT EDIT\n\n")
fmt.Printf("package atom\n\nconst (\n")
for _, s := range all {
fmt.Printf("\t%s Atom = %#x\n", identifier(s), atom[s])
}
fmt.Printf(")\n\n")
fmt.Printf("const hash0 = %#x\n\n", best.h0)
fmt.Printf("const maxAtomLen = %d\n\n", maxLen)
fmt.Printf("var table = [1<<%d]Atom{\n", best.k)
for i, s := range best.tab {
if s == "" {
continue
}
fmt.Printf("\t%#x: %#x, // %s\n", i, atom[s], s)
}
fmt.Printf("}\n")
datasize := (1 << best.k) * 4
fmt.Printf("const atomText =\n")
textsize := len(text)
for len(text) > 60 {
fmt.Printf("\t%q +\n", text[:60])
text = text[60:]
}
fmt.Printf("\t%q\n\n", text)
fmt.Fprintf(os.Stderr, "%d atoms; %d string bytes + %d tables = %d total data\n", len(all), textsize, datasize, textsize+datasize)
}
type byLen []string
func (x byLen) Less(i, j int) bool { return len(x[i]) > len(x[j]) }
func (x byLen) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
func (x byLen) Len() int { return len(x) }
// fnv computes the FNV hash with an arbitrary starting value h.
func fnv(h uint32, s string) uint32 {
for i := 0; i < len(s); i++ {
h ^= uint32(s[i])
h *= 16777619
}
return h
}
// A table represents an attempt at constructing the lookup table.
// The lookup table uses cuckoo hashing, meaning that each string
// can be found in one of two positions.
type table struct {
h0 uint32
k uint
mask uint32
tab []string
}
// hash returns the two hashes for s.
func (t *table) hash(s string) (h1, h2 uint32) {
h := fnv(t.h0, s)
h1 = h & t.mask
h2 = (h >> 16) & t.mask
return
}
// init initializes the table with the given parameters.
// h0 is the initial hash value,
// k is the number of bits of hash value to use, and
// x is the list of strings to store in the table.
// init returns false if the table cannot be constructed.
func (t *table) init(h0 uint32, k uint, x []string) bool {
t.h0 = h0
t.k = k
t.tab = make([]string, 1<<k)
t.mask = 1<<k - 1
for _, s := range x {
if !t.insert(s) {
return false
}
}
return true
}
// insert inserts s in the table.
func (t *table) insert(s string) bool {
h1, h2 := t.hash(s)
if t.tab[h1] == "" {
t.tab[h1] = s
return true
}
if t.tab[h2] == "" {
t.tab[h2] = s
return true
}
if t.push(h1, 0) {
t.tab[h1] = s
return true
}
if t.push(h2, 0) {
t.tab[h2] = s
return true
}
return false
}
// push attempts to push aside the entry in slot i.
func (t *table) push(i uint32, depth int) bool {
if depth > len(t.tab) {
return false
}
s := t.tab[i]
h1, h2 := t.hash(s)
j := h1 + h2 - i
if t.tab[j] != "" && !t.push(j, depth+1) {
return false
}
t.tab[j] = s
return true
}
// The lists of element names and attribute keys were taken from
// https://html.spec.whatwg.org/multipage/indices.html#index
// as of the "HTML Living Standard - Last Updated 21 February 2015" version.
var elements = []string{
"a",
"abbr",
"address",
"area",
"article",
"aside",
"audio",
"b",
"base",
"bdi",
"bdo",
"blockquote",
"body",
"br",
"button",
"canvas",
"caption",
"cite",
"code",
"col",
"colgroup",
"command",
"data",
"datalist",
"dd",
"del",
"details",
"dfn",
"dialog",
"div",
"dl",
"dt",
"em",
"embed",
"fieldset",
"figcaption",
"figure",
"footer",
"form",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"head",
"header",
"hgroup",
"hr",
"html",
"i",
"iframe",
"img",
"input",
"ins",
"kbd",
"keygen",
"label",
"legend",
"li",
"link",
"map",
"mark",
"menu",
"menuitem",
"meta",
"meter",
"nav",
"noscript",
"object",
"ol",
"optgroup",
"option",
"output",
"p",
"param",
"pre",
"progress",
"q",
"rp",
"rt",
"ruby",
"s",
"samp",
"script",
"section",
"select",
"small",
"source",
"span",
"strong",
"style",
"sub",
"summary",
"sup",
"table",
"tbody",
"td",
"template",
"textarea",
"tfoot",
"th",
"thead",
"time",
"title",
"tr",
"track",
"u",
"ul",
"var",
"video",
"wbr",
}
// https://html.spec.whatwg.org/multipage/indices.html#attributes-3
var attributes = []string{
"abbr",
"accept",
"accept-charset",
"accesskey",
"action",
"alt",
"async",
"autocomplete",
"autofocus",
"autoplay",
"challenge",
"charset",
"checked",
"cite",
"class",
"cols",
"colspan",
"command",
"content",
"contenteditable",
"contextmenu",
"controls",
"coords",
"crossorigin",
"data",
"datetime",
"default",
"defer",
"dir",
"dirname",
"disabled",
"download",
"draggable",
"dropzone",
"enctype",
"for",
"form",
"formaction",
"formenctype",
"formmethod",
"formnovalidate",
"formtarget",
"headers",
"height",
"hidden",
"high",
"href",
"hreflang",
"http-equiv",
"icon",
"id",
"inputmode",
"ismap",
"itemid",
"itemprop",
"itemref",
"itemscope",
"itemtype",
"keytype",
"kind",
"label",
"lang",
"list",
"loop",
"low",
"manifest",
"max",
"maxlength",
"media",
"mediagroup",
"method",
"min",
"minlength",
"multiple",
"muted",
"name",
"novalidate",
"open",
"optimum",
"pattern",
"ping",
"placeholder",
"poster",
"preload",
"radiogroup",
"readonly",
"rel",
"required",
"reversed",
"rows",
"rowspan",
"sandbox",
"spellcheck",
"scope",
"scoped",
"seamless",
"selected",
"shape",
"size",
"sizes",
"sortable",
"sorted",
"span",
"src",
"srcdoc",
"srclang",
"start",
"step",
"style",
"tabindex",
"target",
"title",
"translate",
"type",
"typemustmatch",
"usemap",
"value",
"width",
"wrap",
}
var eventHandlers = []string{
"onabort",
"onautocomplete",
"onautocompleteerror",
"onafterprint",
"onbeforeprint",
"onbeforeunload",
"onblur",
"oncancel",
"oncanplay",
"oncanplaythrough",
"onchange",
"onclick",
"onclose",
"oncontextmenu",
"oncuechange",
"ondblclick",
"ondrag",
"ondragend",
"ondragenter",
"ondragleave",
"ondragover",
"ondragstart",
"ondrop",
"ondurationchange",
"onemptied",
"onended",
"onerror",
"onfocus",
"onhashchange",
"oninput",
"oninvalid",
"onkeydown",
"onkeypress",
"onkeyup",
"onlanguagechange",
"onload",
"onloadeddata",
"onloadedmetadata",
"onloadstart",
"onmessage",
"onmousedown",
"onmousemove",
"onmouseout",
"onmouseover",
"onmouseup",
"onmousewheel",
"onoffline",
"ononline",
"onpagehide",
"onpageshow",
"onpause",
"onplay",
"onplaying",
"onpopstate",
"onprogress",
"onratechange",
"onreset",
"onresize",
"onscroll",
"onseeked",
"onseeking",
"onselect",
"onshow",
"onsort",
"onstalled",
"onstorage",
"onsubmit",
"onsuspend",
"ontimeupdate",
"ontoggle",
"onunload",
"onvolumechange",
"onwaiting",
}
// extra are ad-hoc values not covered by any of the lists above.
var extra = []string{
"align",
"annotation",
"annotation-xml",
"applet",
"basefont",
"bgsound",
"big",
"blink",
"center",
"color",
"desc",
"face",
"font",
"foreignObject", // HTML is case-insensitive, but SVG-embedded-in-HTML is case-sensitive.
"foreignobject",
"frame",
"frameset",
"image",
"isindex",
"listing",
"malignmark",
"marquee",
"math",
"mglyph",
"mi",
"mn",
"mo",
"ms",
"mtext",
"nobr",
"noembed",
"noframes",
"plaintext",
"prompt",
"public",
"spacer",
"strike",
"svg",
"system",
"tt",
"xmp",
}

257
vendor/golang.org/x/net/html/charset/charset.go generated vendored Normal file
View File

@ -0,0 +1,257 @@
// Copyright 2013 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 charset provides common text encodings for HTML documents.
//
// The mapping from encoding labels to encodings is defined at
// https://encoding.spec.whatwg.org/.
package charset // import "golang.org/x/net/html/charset"
import (
"bytes"
"fmt"
"io"
"mime"
"strings"
"unicode/utf8"
"golang.org/x/net/html"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/charmap"
"golang.org/x/text/encoding/htmlindex"
"golang.org/x/text/transform"
)
// Lookup returns the encoding with the specified label, and its canonical
// name. It returns nil and the empty string if label is not one of the
// standard encodings for HTML. Matching is case-insensitive and ignores
// leading and trailing whitespace. Encoders will use HTML escape sequences for
// runes that are not supported by the character set.
func Lookup(label string) (e encoding.Encoding, name string) {
e, err := htmlindex.Get(label)
if err != nil {
return nil, ""
}
name, _ = htmlindex.Name(e)
return &htmlEncoding{e}, name
}
type htmlEncoding struct{ encoding.Encoding }
func (h *htmlEncoding) NewEncoder() *encoding.Encoder {
// HTML requires a non-terminating legacy encoder. We use HTML escapes to
// substitute unsupported code points.
return encoding.HTMLEscapeUnsupported(h.Encoding.NewEncoder())
}
// DetermineEncoding determines the encoding of an HTML document by examining
// up to the first 1024 bytes of content and the declared Content-Type.
//
// See http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#determining-the-character-encoding
func DetermineEncoding(content []byte, contentType string) (e encoding.Encoding, name string, certain bool) {
if len(content) > 1024 {
content = content[:1024]
}
for _, b := range boms {
if bytes.HasPrefix(content, b.bom) {
e, name = Lookup(b.enc)
return e, name, true
}
}
if _, params, err := mime.ParseMediaType(contentType); err == nil {
if cs, ok := params["charset"]; ok {
if e, name = Lookup(cs); e != nil {
return e, name, true
}
}
}
if len(content) > 0 {
e, name = prescan(content)
if e != nil {
return e, name, false
}
}
// Try to detect UTF-8.
// First eliminate any partial rune at the end.
for i := len(content) - 1; i >= 0 && i > len(content)-4; i-- {
b := content[i]
if b < 0x80 {
break
}
if utf8.RuneStart(b) {
content = content[:i]
break
}
}
hasHighBit := false
for _, c := range content {
if c >= 0x80 {
hasHighBit = true
break
}
}
if hasHighBit && utf8.Valid(content) {
return encoding.Nop, "utf-8", false
}
// TODO: change default depending on user's locale?
return charmap.Windows1252, "windows-1252", false
}
// NewReader returns an io.Reader that converts the content of r to UTF-8.
// It calls DetermineEncoding to find out what r's encoding is.
func NewReader(r io.Reader, contentType string) (io.Reader, error) {
preview := make([]byte, 1024)
n, err := io.ReadFull(r, preview)
switch {
case err == io.ErrUnexpectedEOF:
preview = preview[:n]
r = bytes.NewReader(preview)
case err != nil:
return nil, err
default:
r = io.MultiReader(bytes.NewReader(preview), r)
}
if e, _, _ := DetermineEncoding(preview, contentType); e != encoding.Nop {
r = transform.NewReader(r, e.NewDecoder())
}
return r, nil
}
// NewReaderLabel returns a reader that converts from the specified charset to
// UTF-8. It uses Lookup to find the encoding that corresponds to label, and
// returns an error if Lookup returns nil. It is suitable for use as
// encoding/xml.Decoder's CharsetReader function.
func NewReaderLabel(label string, input io.Reader) (io.Reader, error) {
e, _ := Lookup(label)
if e == nil {
return nil, fmt.Errorf("unsupported charset: %q", label)
}
return transform.NewReader(input, e.NewDecoder()), nil
}
func prescan(content []byte) (e encoding.Encoding, name string) {
z := html.NewTokenizer(bytes.NewReader(content))
for {
switch z.Next() {
case html.ErrorToken:
return nil, ""
case html.StartTagToken, html.SelfClosingTagToken:
tagName, hasAttr := z.TagName()
if !bytes.Equal(tagName, []byte("meta")) {
continue
}
attrList := make(map[string]bool)
gotPragma := false
const (
dontKnow = iota
doNeedPragma
doNotNeedPragma
)
needPragma := dontKnow
name = ""
e = nil
for hasAttr {
var key, val []byte
key, val, hasAttr = z.TagAttr()
ks := string(key)
if attrList[ks] {
continue
}
attrList[ks] = true
for i, c := range val {
if 'A' <= c && c <= 'Z' {
val[i] = c + 0x20
}
}
switch ks {
case "http-equiv":
if bytes.Equal(val, []byte("content-type")) {
gotPragma = true
}
case "content":
if e == nil {
name = fromMetaElement(string(val))
if name != "" {
e, name = Lookup(name)
if e != nil {
needPragma = doNeedPragma
}
}
}
case "charset":
e, name = Lookup(string(val))
needPragma = doNotNeedPragma
}
}
if needPragma == dontKnow || needPragma == doNeedPragma && !gotPragma {
continue
}
if strings.HasPrefix(name, "utf-16") {
name = "utf-8"
e = encoding.Nop
}
if e != nil {
return e, name
}
}
}
}
func fromMetaElement(s string) string {
for s != "" {
csLoc := strings.Index(s, "charset")
if csLoc == -1 {
return ""
}
s = s[csLoc+len("charset"):]
s = strings.TrimLeft(s, " \t\n\f\r")
if !strings.HasPrefix(s, "=") {
continue
}
s = s[1:]
s = strings.TrimLeft(s, " \t\n\f\r")
if s == "" {
return ""
}
if q := s[0]; q == '"' || q == '\'' {
s = s[1:]
closeQuote := strings.IndexRune(s, rune(q))
if closeQuote == -1 {
return ""
}
return s[:closeQuote]
}
end := strings.IndexAny(s, "; \t\n\f\r")
if end == -1 {
end = len(s)
}
return s[:end]
}
return ""
}
var boms = []struct {
bom []byte
enc string
}{
{[]byte{0xfe, 0xff}, "utf-16be"},
{[]byte{0xff, 0xfe}, "utf-16le"},
{[]byte{0xef, 0xbb, 0xbf}, "utf-8"},
}

249
vendor/golang.org/x/text/encoding/charmap/charmap.go generated vendored Normal file
View File

@ -0,0 +1,249 @@
// Copyright 2013 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.
//go:generate go run maketables.go
// Package charmap provides simple character encodings such as IBM Code Page 437
// and Windows 1252.
package charmap // import "golang.org/x/text/encoding/charmap"
import (
"unicode/utf8"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/internal"
"golang.org/x/text/encoding/internal/identifier"
"golang.org/x/text/transform"
)
// These encodings vary only in the way clients should interpret them. Their
// coded character set is identical and a single implementation can be shared.
var (
// ISO8859_6E is the ISO 8859-6E encoding.
ISO8859_6E encoding.Encoding = &iso8859_6E
// ISO8859_6I is the ISO 8859-6I encoding.
ISO8859_6I encoding.Encoding = &iso8859_6I
// ISO8859_8E is the ISO 8859-8E encoding.
ISO8859_8E encoding.Encoding = &iso8859_8E
// ISO8859_8I is the ISO 8859-8I encoding.
ISO8859_8I encoding.Encoding = &iso8859_8I
iso8859_6E = internal.Encoding{
Encoding: ISO8859_6,
Name: "ISO-8859-6E",
MIB: identifier.ISO88596E,
}
iso8859_6I = internal.Encoding{
Encoding: ISO8859_6,
Name: "ISO-8859-6I",
MIB: identifier.ISO88596I,
}
iso8859_8E = internal.Encoding{
Encoding: ISO8859_8,
Name: "ISO-8859-8E",
MIB: identifier.ISO88598E,
}
iso8859_8I = internal.Encoding{
Encoding: ISO8859_8,
Name: "ISO-8859-8I",
MIB: identifier.ISO88598I,
}
)
// All is a list of all defined encodings in this package.
var All []encoding.Encoding = listAll
// TODO: implement these encodings, in order of importance.
// ASCII, ISO8859_1: Rather common. Close to Windows 1252.
// ISO8859_9: Close to Windows 1254.
// utf8Enc holds a rune's UTF-8 encoding in data[:len].
type utf8Enc struct {
len uint8
data [3]byte
}
// Charmap is an 8-bit character set encoding.
type Charmap struct {
// name is the encoding's name.
name string
// mib is the encoding type of this encoder.
mib identifier.MIB
// asciiSuperset states whether the encoding is a superset of ASCII.
asciiSuperset bool
// low is the lower bound of the encoded byte for a non-ASCII rune. If
// Charmap.asciiSuperset is true then this will be 0x80, otherwise 0x00.
low uint8
// replacement is the encoded replacement character.
replacement byte
// decode is the map from encoded byte to UTF-8.
decode [256]utf8Enc
// encoding is the map from runes to encoded bytes. Each entry is a
// uint32: the high 8 bits are the encoded byte and the low 24 bits are
// the rune. The table entries are sorted by ascending rune.
encode [256]uint32
}
// NewDecoder implements the encoding.Encoding interface.
func (m *Charmap) NewDecoder() *encoding.Decoder {
return &encoding.Decoder{Transformer: charmapDecoder{charmap: m}}
}
// NewEncoder implements the encoding.Encoding interface.
func (m *Charmap) NewEncoder() *encoding.Encoder {
return &encoding.Encoder{Transformer: charmapEncoder{charmap: m}}
}
// String returns the Charmap's name.
func (m *Charmap) String() string {
return m.name
}
// ID implements an internal interface.
func (m *Charmap) ID() (mib identifier.MIB, other string) {
return m.mib, ""
}
// charmapDecoder implements transform.Transformer by decoding to UTF-8.
type charmapDecoder struct {
transform.NopResetter
charmap *Charmap
}
func (m charmapDecoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
for i, c := range src {
if m.charmap.asciiSuperset && c < utf8.RuneSelf {
if nDst >= len(dst) {
err = transform.ErrShortDst
break
}
dst[nDst] = c
nDst++
nSrc = i + 1
continue
}
decode := &m.charmap.decode[c]
n := int(decode.len)
if nDst+n > len(dst) {
err = transform.ErrShortDst
break
}
// It's 15% faster to avoid calling copy for these tiny slices.
for j := 0; j < n; j++ {
dst[nDst] = decode.data[j]
nDst++
}
nSrc = i + 1
}
return nDst, nSrc, err
}
// DecodeByte returns the Charmap's rune decoding of the byte b.
func (m *Charmap) DecodeByte(b byte) rune {
switch x := &m.decode[b]; x.len {
case 1:
return rune(x.data[0])
case 2:
return rune(x.data[0]&0x1f)<<6 | rune(x.data[1]&0x3f)
default:
return rune(x.data[0]&0x0f)<<12 | rune(x.data[1]&0x3f)<<6 | rune(x.data[2]&0x3f)
}
}
// charmapEncoder implements transform.Transformer by encoding from UTF-8.
type charmapEncoder struct {
transform.NopResetter
charmap *Charmap
}
func (m charmapEncoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
r, size := rune(0), 0
loop:
for nSrc < len(src) {
if nDst >= len(dst) {
err = transform.ErrShortDst
break
}
r = rune(src[nSrc])
// Decode a 1-byte rune.
if r < utf8.RuneSelf {
if m.charmap.asciiSuperset {
nSrc++
dst[nDst] = uint8(r)
nDst++
continue
}
size = 1
} else {
// Decode a multi-byte rune.
r, size = utf8.DecodeRune(src[nSrc:])
if size == 1 {
// All valid runes of size 1 (those below utf8.RuneSelf) were
// handled above. We have invalid UTF-8 or we haven't seen the
// full character yet.
if !atEOF && !utf8.FullRune(src[nSrc:]) {
err = transform.ErrShortSrc
} else {
err = internal.RepertoireError(m.charmap.replacement)
}
break
}
}
// Binary search in [low, high) for that rune in the m.charmap.encode table.
for low, high := int(m.charmap.low), 0x100; ; {
if low >= high {
err = internal.RepertoireError(m.charmap.replacement)
break loop
}
mid := (low + high) / 2
got := m.charmap.encode[mid]
gotRune := rune(got & (1<<24 - 1))
if gotRune < r {
low = mid + 1
} else if gotRune > r {
high = mid
} else {
dst[nDst] = byte(got >> 24)
nDst++
break
}
}
nSrc += size
}
return nDst, nSrc, err
}
// EncodeRune returns the Charmap's byte encoding of the rune r. ok is whether
// r is in the Charmap's repertoire. If not, b is set to the Charmap's
// replacement byte. This is often the ASCII substitute character '\x1a'.
func (m *Charmap) EncodeRune(r rune) (b byte, ok bool) {
if r < utf8.RuneSelf && m.asciiSuperset {
return byte(r), true
}
for low, high := int(m.low), 0x100; ; {
if low >= high {
return m.replacement, false
}
mid := (low + high) / 2
got := m.encode[mid]
gotRune := rune(got & (1<<24 - 1))
if gotRune < r {
low = mid + 1
} else if gotRune > r {
high = mid
} else {
return byte(got >> 24), true
}
}
}

7410
vendor/golang.org/x/text/encoding/charmap/tables.go generated vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,86 @@
// 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.
//go:generate go run gen.go
// Package htmlindex maps character set encoding names to Encodings as
// recommended by the W3C for use in HTML 5. See http://www.w3.org/TR/encoding.
package htmlindex
// TODO: perhaps have a "bare" version of the index (used by this package) that
// is not pre-loaded with all encodings. Global variables in encodings prevent
// the linker from being able to purge unneeded tables. This means that
// referencing all encodings, as this package does for the default index, links
// in all encodings unconditionally.
//
// This issue can be solved by either solving the linking issue (see
// https://github.com/golang/go/issues/6330) or refactoring the encoding tables
// (e.g. moving the tables to internal packages that do not use global
// variables).
// TODO: allow canonicalizing names
import (
"errors"
"strings"
"sync"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/internal/identifier"
"golang.org/x/text/language"
)
var (
errInvalidName = errors.New("htmlindex: invalid encoding name")
errUnknown = errors.New("htmlindex: unknown Encoding")
errUnsupported = errors.New("htmlindex: this encoding is not supported")
)
var (
matcherOnce sync.Once
matcher language.Matcher
)
// LanguageDefault returns the canonical name of the default encoding for a
// given language.
func LanguageDefault(tag language.Tag) string {
matcherOnce.Do(func() {
tags := []language.Tag{}
for _, t := range strings.Split(locales, " ") {
tags = append(tags, language.MustParse(t))
}
matcher = language.NewMatcher(tags)
})
_, i, _ := matcher.Match(tag)
return canonical[localeMap[i]] // Default is Windows-1252.
}
// Get returns an Encoding for one of the names listed in
// http://www.w3.org/TR/encoding using the Default Index. Matching is case-
// insensitive.
func Get(name string) (encoding.Encoding, error) {
x, ok := nameMap[strings.ToLower(strings.TrimSpace(name))]
if !ok {
return nil, errInvalidName
}
return encodings[x], nil
}
// Name reports the canonical name of the given Encoding. It will return
// an error if e is not associated with a supported encoding scheme.
func Name(e encoding.Encoding) (string, error) {
id, ok := e.(identifier.Interface)
if !ok {
return "", errUnknown
}
mib, _ := id.ID()
if mib == 0 {
return "", errUnknown
}
v, ok := mibMap[mib]
if !ok {
return "", errUnsupported
}
return canonical[v], nil
}

105
vendor/golang.org/x/text/encoding/htmlindex/map.go generated vendored Normal file
View File

@ -0,0 +1,105 @@
// 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 htmlindex
import (
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/charmap"
"golang.org/x/text/encoding/internal/identifier"
"golang.org/x/text/encoding/japanese"
"golang.org/x/text/encoding/korean"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/encoding/traditionalchinese"
"golang.org/x/text/encoding/unicode"
)
// mibMap maps a MIB identifier to an htmlEncoding index.
var mibMap = map[identifier.MIB]htmlEncoding{
identifier.UTF8: utf8,
identifier.UTF16BE: utf16be,
identifier.UTF16LE: utf16le,
identifier.IBM866: ibm866,
identifier.ISOLatin2: iso8859_2,
identifier.ISOLatin3: iso8859_3,
identifier.ISOLatin4: iso8859_4,
identifier.ISOLatinCyrillic: iso8859_5,
identifier.ISOLatinArabic: iso8859_6,
identifier.ISOLatinGreek: iso8859_7,
identifier.ISOLatinHebrew: iso8859_8,
identifier.ISO88598I: iso8859_8I,
identifier.ISOLatin6: iso8859_10,
identifier.ISO885913: iso8859_13,
identifier.ISO885914: iso8859_14,
identifier.ISO885915: iso8859_15,
identifier.ISO885916: iso8859_16,
identifier.KOI8R: koi8r,
identifier.KOI8U: koi8u,
identifier.Macintosh: macintosh,
identifier.MacintoshCyrillic: macintoshCyrillic,
identifier.Windows874: windows874,
identifier.Windows1250: windows1250,
identifier.Windows1251: windows1251,
identifier.Windows1252: windows1252,
identifier.Windows1253: windows1253,
identifier.Windows1254: windows1254,
identifier.Windows1255: windows1255,
identifier.Windows1256: windows1256,
identifier.Windows1257: windows1257,
identifier.Windows1258: windows1258,
identifier.XUserDefined: xUserDefined,
identifier.GBK: gbk,
identifier.GB18030: gb18030,
identifier.Big5: big5,
identifier.EUCPkdFmtJapanese: eucjp,
identifier.ISO2022JP: iso2022jp,
identifier.ShiftJIS: shiftJIS,
identifier.EUCKR: euckr,
identifier.Replacement: replacement,
}
// encodings maps the internal htmlEncoding to an Encoding.
// TODO: consider using a reusable index in encoding/internal.
var encodings = [numEncodings]encoding.Encoding{
utf8: unicode.UTF8,
ibm866: charmap.CodePage866,
iso8859_2: charmap.ISO8859_2,
iso8859_3: charmap.ISO8859_3,
iso8859_4: charmap.ISO8859_4,
iso8859_5: charmap.ISO8859_5,
iso8859_6: charmap.ISO8859_6,
iso8859_7: charmap.ISO8859_7,
iso8859_8: charmap.ISO8859_8,
iso8859_8I: charmap.ISO8859_8I,
iso8859_10: charmap.ISO8859_10,
iso8859_13: charmap.ISO8859_13,
iso8859_14: charmap.ISO8859_14,
iso8859_15: charmap.ISO8859_15,
iso8859_16: charmap.ISO8859_16,
koi8r: charmap.KOI8R,
koi8u: charmap.KOI8U,
macintosh: charmap.Macintosh,
windows874: charmap.Windows874,
windows1250: charmap.Windows1250,
windows1251: charmap.Windows1251,
windows1252: charmap.Windows1252,
windows1253: charmap.Windows1253,
windows1254: charmap.Windows1254,
windows1255: charmap.Windows1255,
windows1256: charmap.Windows1256,
windows1257: charmap.Windows1257,
windows1258: charmap.Windows1258,
macintoshCyrillic: charmap.MacintoshCyrillic,
gbk: simplifiedchinese.GBK,
gb18030: simplifiedchinese.GB18030,
big5: traditionalchinese.Big5,
eucjp: japanese.EUCJP,
iso2022jp: japanese.ISO2022JP,
shiftJIS: japanese.ShiftJIS,
euckr: korean.EUCKR,
replacement: encoding.Replacement,
utf16be: unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM),
utf16le: unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM),
xUserDefined: charmap.XUserDefined,
}

352
vendor/golang.org/x/text/encoding/htmlindex/tables.go generated vendored Normal file
View File

@ -0,0 +1,352 @@
// Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
package htmlindex
type htmlEncoding byte
const (
utf8 htmlEncoding = iota
ibm866
iso8859_2
iso8859_3
iso8859_4
iso8859_5
iso8859_6
iso8859_7
iso8859_8
iso8859_8I
iso8859_10
iso8859_13
iso8859_14
iso8859_15
iso8859_16
koi8r
koi8u
macintosh
windows874
windows1250
windows1251
windows1252
windows1253
windows1254
windows1255
windows1256
windows1257
windows1258
macintoshCyrillic
gbk
gb18030
big5
eucjp
iso2022jp
shiftJIS
euckr
replacement
utf16be
utf16le
xUserDefined
numEncodings
)
var canonical = [numEncodings]string{
"utf-8",
"ibm866",
"iso-8859-2",
"iso-8859-3",
"iso-8859-4",
"iso-8859-5",
"iso-8859-6",
"iso-8859-7",
"iso-8859-8",
"iso-8859-8-i",
"iso-8859-10",
"iso-8859-13",
"iso-8859-14",
"iso-8859-15",
"iso-8859-16",
"koi8-r",
"koi8-u",
"macintosh",
"windows-874",
"windows-1250",
"windows-1251",
"windows-1252",
"windows-1253",
"windows-1254",
"windows-1255",
"windows-1256",
"windows-1257",
"windows-1258",
"x-mac-cyrillic",
"gbk",
"gb18030",
"big5",
"euc-jp",
"iso-2022-jp",
"shift_jis",
"euc-kr",
"replacement",
"utf-16be",
"utf-16le",
"x-user-defined",
}
var nameMap = map[string]htmlEncoding{
"unicode-1-1-utf-8": utf8,
"utf-8": utf8,
"utf8": utf8,
"866": ibm866,
"cp866": ibm866,
"csibm866": ibm866,
"ibm866": ibm866,
"csisolatin2": iso8859_2,
"iso-8859-2": iso8859_2,
"iso-ir-101": iso8859_2,
"iso8859-2": iso8859_2,
"iso88592": iso8859_2,
"iso_8859-2": iso8859_2,
"iso_8859-2:1987": iso8859_2,
"l2": iso8859_2,
"latin2": iso8859_2,
"csisolatin3": iso8859_3,
"iso-8859-3": iso8859_3,
"iso-ir-109": iso8859_3,
"iso8859-3": iso8859_3,
"iso88593": iso8859_3,
"iso_8859-3": iso8859_3,
"iso_8859-3:1988": iso8859_3,
"l3": iso8859_3,
"latin3": iso8859_3,
"csisolatin4": iso8859_4,
"iso-8859-4": iso8859_4,
"iso-ir-110": iso8859_4,
"iso8859-4": iso8859_4,
"iso88594": iso8859_4,
"iso_8859-4": iso8859_4,
"iso_8859-4:1988": iso8859_4,
"l4": iso8859_4,
"latin4": iso8859_4,
"csisolatincyrillic": iso8859_5,
"cyrillic": iso8859_5,
"iso-8859-5": iso8859_5,
"iso-ir-144": iso8859_5,
"iso8859-5": iso8859_5,
"iso88595": iso8859_5,
"iso_8859-5": iso8859_5,
"iso_8859-5:1988": iso8859_5,
"arabic": iso8859_6,
"asmo-708": iso8859_6,
"csiso88596e": iso8859_6,
"csiso88596i": iso8859_6,
"csisolatinarabic": iso8859_6,
"ecma-114": iso8859_6,
"iso-8859-6": iso8859_6,
"iso-8859-6-e": iso8859_6,
"iso-8859-6-i": iso8859_6,
"iso-ir-127": iso8859_6,
"iso8859-6": iso8859_6,
"iso88596": iso8859_6,
"iso_8859-6": iso8859_6,
"iso_8859-6:1987": iso8859_6,
"csisolatingreek": iso8859_7,
"ecma-118": iso8859_7,
"elot_928": iso8859_7,
"greek": iso8859_7,
"greek8": iso8859_7,
"iso-8859-7": iso8859_7,
"iso-ir-126": iso8859_7,
"iso8859-7": iso8859_7,
"iso88597": iso8859_7,
"iso_8859-7": iso8859_7,
"iso_8859-7:1987": iso8859_7,
"sun_eu_greek": iso8859_7,
"csiso88598e": iso8859_8,
"csisolatinhebrew": iso8859_8,
"hebrew": iso8859_8,
"iso-8859-8": iso8859_8,
"iso-8859-8-e": iso8859_8,
"iso-ir-138": iso8859_8,
"iso8859-8": iso8859_8,
"iso88598": iso8859_8,
"iso_8859-8": iso8859_8,
"iso_8859-8:1988": iso8859_8,
"visual": iso8859_8,
"csiso88598i": iso8859_8I,
"iso-8859-8-i": iso8859_8I,
"logical": iso8859_8I,
"csisolatin6": iso8859_10,
"iso-8859-10": iso8859_10,
"iso-ir-157": iso8859_10,
"iso8859-10": iso8859_10,
"iso885910": iso8859_10,
"l6": iso8859_10,
"latin6": iso8859_10,
"iso-8859-13": iso8859_13,
"iso8859-13": iso8859_13,
"iso885913": iso8859_13,
"iso-8859-14": iso8859_14,
"iso8859-14": iso8859_14,
"iso885914": iso8859_14,
"csisolatin9": iso8859_15,
"iso-8859-15": iso8859_15,
"iso8859-15": iso8859_15,
"iso885915": iso8859_15,
"iso_8859-15": iso8859_15,
"l9": iso8859_15,
"iso-8859-16": iso8859_16,
"cskoi8r": koi8r,
"koi": koi8r,
"koi8": koi8r,
"koi8-r": koi8r,
"koi8_r": koi8r,
"koi8-ru": koi8u,
"koi8-u": koi8u,
"csmacintosh": macintosh,
"mac": macintosh,
"macintosh": macintosh,
"x-mac-roman": macintosh,
"dos-874": windows874,
"iso-8859-11": windows874,
"iso8859-11": windows874,
"iso885911": windows874,
"tis-620": windows874,
"windows-874": windows874,
"cp1250": windows1250,
"windows-1250": windows1250,
"x-cp1250": windows1250,
"cp1251": windows1251,
"windows-1251": windows1251,
"x-cp1251": windows1251,
"ansi_x3.4-1968": windows1252,
"ascii": windows1252,
"cp1252": windows1252,
"cp819": windows1252,
"csisolatin1": windows1252,
"ibm819": windows1252,
"iso-8859-1": windows1252,
"iso-ir-100": windows1252,
"iso8859-1": windows1252,
"iso88591": windows1252,
"iso_8859-1": windows1252,
"iso_8859-1:1987": windows1252,
"l1": windows1252,
"latin1": windows1252,
"us-ascii": windows1252,
"windows-1252": windows1252,
"x-cp1252": windows1252,
"cp1253": windows1253,
"windows-1253": windows1253,
"x-cp1253": windows1253,
"cp1254": windows1254,
"csisolatin5": windows1254,
"iso-8859-9": windows1254,
"iso-ir-148": windows1254,
"iso8859-9": windows1254,
"iso88599": windows1254,
"iso_8859-9": windows1254,
"iso_8859-9:1989": windows1254,
"l5": windows1254,
"latin5": windows1254,
"windows-1254": windows1254,
"x-cp1254": windows1254,
"cp1255": windows1255,
"windows-1255": windows1255,
"x-cp1255": windows1255,
"cp1256": windows1256,
"windows-1256": windows1256,
"x-cp1256": windows1256,
"cp1257": windows1257,
"windows-1257": windows1257,
"x-cp1257": windows1257,
"cp1258": windows1258,
"windows-1258": windows1258,
"x-cp1258": windows1258,
"x-mac-cyrillic": macintoshCyrillic,
"x-mac-ukrainian": macintoshCyrillic,
"chinese": gbk,
"csgb2312": gbk,
"csiso58gb231280": gbk,
"gb2312": gbk,
"gb_2312": gbk,
"gb_2312-80": gbk,
"gbk": gbk,
"iso-ir-58": gbk,
"x-gbk": gbk,
"gb18030": gb18030,
"big5": big5,
"big5-hkscs": big5,
"cn-big5": big5,
"csbig5": big5,
"x-x-big5": big5,
"cseucpkdfmtjapanese": eucjp,
"euc-jp": eucjp,
"x-euc-jp": eucjp,
"csiso2022jp": iso2022jp,
"iso-2022-jp": iso2022jp,
"csshiftjis": shiftJIS,
"ms932": shiftJIS,
"ms_kanji": shiftJIS,
"shift-jis": shiftJIS,
"shift_jis": shiftJIS,
"sjis": shiftJIS,
"windows-31j": shiftJIS,
"x-sjis": shiftJIS,
"cseuckr": euckr,
"csksc56011987": euckr,
"euc-kr": euckr,
"iso-ir-149": euckr,
"korean": euckr,
"ks_c_5601-1987": euckr,
"ks_c_5601-1989": euckr,
"ksc5601": euckr,
"ksc_5601": euckr,
"windows-949": euckr,
"csiso2022kr": replacement,
"hz-gb-2312": replacement,
"iso-2022-cn": replacement,
"iso-2022-cn-ext": replacement,
"iso-2022-kr": replacement,
"utf-16be": utf16be,
"utf-16": utf16le,
"utf-16le": utf16le,
"x-user-defined": xUserDefined,
}
var localeMap = []htmlEncoding{
windows1252, // und_Latn
windows1256, // ar
windows1251, // ba
windows1251, // be
windows1251, // bg
windows1250, // cs
iso8859_7, // el
windows1257, // et
windows1256, // fa
windows1255, // he
windows1250, // hr
iso8859_2, // hu
shiftJIS, // ja
windows1251, // kk
euckr, // ko
windows1254, // ku
windows1251, // ky
windows1257, // lt
windows1257, // lv
windows1251, // mk
iso8859_2, // pl
windows1251, // ru
windows1251, // sah
windows1250, // sk
iso8859_2, // sl
windows1251, // sr
windows1251, // tg
windows874, // th
windows1254, // tr
windows1251, // tt
windows1251, // uk
windows1258, // vi
gb18030, // zh-hans
big5, // zh-hant
}
const locales = "und_Latn ar ba be bg cs el et fa he hr hu ja kk ko ku ky lt lv mk pl ru sah sk sl sr tg th tr tt uk vi zh-hans zh-hant"

View File

@ -1,137 +0,0 @@
// 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
import (
"bytes"
"encoding/xml"
"fmt"
"io"
"log"
"strings"
"golang.org/x/text/internal/gen"
)
type registry struct {
XMLName xml.Name `xml:"registry"`
Updated string `xml:"updated"`
Registry []struct {
ID string `xml:"id,attr"`
Record []struct {
Name string `xml:"name"`
Xref []struct {
Type string `xml:"type,attr"`
Data string `xml:"data,attr"`
} `xml:"xref"`
Desc struct {
Data string `xml:",innerxml"`
// Any []struct {
// Data string `xml:",chardata"`
// } `xml:",any"`
// Data string `xml:",chardata"`
} `xml:"description,"`
MIB string `xml:"value"`
Alias []string `xml:"alias"`
MIME string `xml:"preferred_alias"`
} `xml:"record"`
} `xml:"registry"`
}
func main() {
r := gen.OpenIANAFile("assignments/character-sets/character-sets.xml")
reg := &registry{}
if err := xml.NewDecoder(r).Decode(&reg); err != nil && err != io.EOF {
log.Fatalf("Error decoding charset registry: %v", err)
}
if len(reg.Registry) == 0 || reg.Registry[0].ID != "character-sets-1" {
log.Fatalf("Unexpected ID %s", reg.Registry[0].ID)
}
w := &bytes.Buffer{}
fmt.Fprintf(w, "const (\n")
for _, rec := range reg.Registry[0].Record {
constName := ""
for _, a := range rec.Alias {
if strings.HasPrefix(a, "cs") && strings.IndexByte(a, '-') == -1 {
// Some of the constant definitions have comments in them. Strip those.
constName = strings.Title(strings.SplitN(a[2:], "\n", 2)[0])
}
}
if constName == "" {
switch rec.MIB {
case "2085":
constName = "HZGB2312" // Not listed as alias for some reason.
default:
log.Fatalf("No cs alias defined for %s.", rec.MIB)
}
}
if rec.MIME != "" {
rec.MIME = fmt.Sprintf(" (MIME: %s)", rec.MIME)
}
fmt.Fprintf(w, "// %s is the MIB identifier with IANA name %s%s.\n//\n", constName, rec.Name, rec.MIME)
if len(rec.Desc.Data) > 0 {
fmt.Fprint(w, "// ")
d := xml.NewDecoder(strings.NewReader(rec.Desc.Data))
inElem := true
attr := ""
for {
t, err := d.Token()
if err != nil {
if err != io.EOF {
log.Fatal(err)
}
break
}
switch x := t.(type) {
case xml.CharData:
attr = "" // Don't need attribute info.
a := bytes.Split([]byte(x), []byte("\n"))
for i, b := range a {
if b = bytes.TrimSpace(b); len(b) != 0 {
if !inElem && i > 0 {
fmt.Fprint(w, "\n// ")
}
inElem = false
fmt.Fprintf(w, "%s ", string(b))
}
}
case xml.StartElement:
if x.Name.Local == "xref" {
inElem = true
use := false
for _, a := range x.Attr {
if a.Name.Local == "type" {
use = use || a.Value != "person"
}
if a.Name.Local == "data" && use {
attr = a.Value + " "
}
}
}
case xml.EndElement:
inElem = false
fmt.Fprint(w, attr)
}
}
fmt.Fprint(w, "\n")
}
for _, x := range rec.Xref {
switch x.Type {
case "rfc":
fmt.Fprintf(w, "// Reference: %s\n", strings.ToUpper(x.Data))
case "uri":
fmt.Fprintf(w, "// Reference: %s\n", x.Data)
}
}
fmt.Fprintf(w, "%s MIB = %s\n", constName, rec.MIB)
fmt.Fprintln(w)
}
fmt.Fprintln(w, ")")
gen.WriteGoFile("mib.go", "identifier", w.Bytes())
}

View File

@ -36,8 +36,8 @@ package identifier
// - http://www.ietf.org/rfc/rfc2978.txt
// - http://www.unicode.org/reports/tr22/
// - http://www.w3.org/TR/encoding/
// - http://www.w3.org/TR/encoding/indexes/encodings.json
// - https://encoding.spec.whatwg.org/
// - https://encoding.spec.whatwg.org/encodings.json
// - https://tools.ietf.org/html/rfc6657#section-5
// Interface can be implemented by Encodings to define the CCS or CES for which

View File

@ -1,4 +1,4 @@
// This file was generated by go generate; DO NOT EDIT
// Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
package identifier

12
vendor/golang.org/x/text/encoding/japanese/all.go generated vendored Normal file
View File

@ -0,0 +1,12 @@
// 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 japanese
import (
"golang.org/x/text/encoding"
)
// All is a list of all defined encodings in this package.
var All = []encoding.Encoding{EUCJP, ISO2022JP, ShiftJIS}

225
vendor/golang.org/x/text/encoding/japanese/eucjp.go generated vendored Normal file
View File

@ -0,0 +1,225 @@
// Copyright 2013 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 japanese
import (
"unicode/utf8"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/internal"
"golang.org/x/text/encoding/internal/identifier"
"golang.org/x/text/transform"
)
// EUCJP is the EUC-JP encoding.
var EUCJP encoding.Encoding = &eucJP
var eucJP = internal.Encoding{
&internal.SimpleEncoding{eucJPDecoder{}, eucJPEncoder{}},
"EUC-JP",
identifier.EUCPkdFmtJapanese,
}
type eucJPDecoder struct{ transform.NopResetter }
// See https://encoding.spec.whatwg.org/#euc-jp-decoder.
func (eucJPDecoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
r, size := rune(0), 0
loop:
for ; nSrc < len(src); nSrc += size {
switch c0 := src[nSrc]; {
case c0 < utf8.RuneSelf:
r, size = rune(c0), 1
case c0 == 0x8e:
if nSrc+1 >= len(src) {
if !atEOF {
err = transform.ErrShortSrc
break loop
}
r, size = utf8.RuneError, 1
break
}
c1 := src[nSrc+1]
switch {
case c1 < 0xa1:
r, size = utf8.RuneError, 1
case c1 > 0xdf:
r, size = utf8.RuneError, 2
if c1 == 0xff {
size = 1
}
default:
r, size = rune(c1)+(0xff61-0xa1), 2
}
case c0 == 0x8f:
if nSrc+2 >= len(src) {
if !atEOF {
err = transform.ErrShortSrc
break loop
}
r, size = utf8.RuneError, 1
if p := nSrc + 1; p < len(src) && 0xa1 <= src[p] && src[p] < 0xfe {
size = 2
}
break
}
c1 := src[nSrc+1]
if c1 < 0xa1 || 0xfe < c1 {
r, size = utf8.RuneError, 1
break
}
c2 := src[nSrc+2]
if c2 < 0xa1 || 0xfe < c2 {
r, size = utf8.RuneError, 2
break
}
r, size = utf8.RuneError, 3
if i := int(c1-0xa1)*94 + int(c2-0xa1); i < len(jis0212Decode) {
r = rune(jis0212Decode[i])
if r == 0 {
r = utf8.RuneError
}
}
case 0xa1 <= c0 && c0 <= 0xfe:
if nSrc+1 >= len(src) {
if !atEOF {
err = transform.ErrShortSrc
break loop
}
r, size = utf8.RuneError, 1
break
}
c1 := src[nSrc+1]
if c1 < 0xa1 || 0xfe < c1 {
r, size = utf8.RuneError, 1
break
}
r, size = utf8.RuneError, 2
if i := int(c0-0xa1)*94 + int(c1-0xa1); i < len(jis0208Decode) {
r = rune(jis0208Decode[i])
if r == 0 {
r = utf8.RuneError
}
}
default:
r, size = utf8.RuneError, 1
}
if nDst+utf8.RuneLen(r) > len(dst) {
err = transform.ErrShortDst
break loop
}
nDst += utf8.EncodeRune(dst[nDst:], r)
}
return nDst, nSrc, err
}
type eucJPEncoder struct{ transform.NopResetter }
func (eucJPEncoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
r, size := rune(0), 0
for ; nSrc < len(src); nSrc += size {
r = rune(src[nSrc])
// Decode a 1-byte rune.
if r < utf8.RuneSelf {
size = 1
} else {
// Decode a multi-byte rune.
r, size = utf8.DecodeRune(src[nSrc:])
if size == 1 {
// All valid runes of size 1 (those below utf8.RuneSelf) were
// handled above. We have invalid UTF-8 or we haven't seen the
// full character yet.
if !atEOF && !utf8.FullRune(src[nSrc:]) {
err = transform.ErrShortSrc
break
}
}
// func init checks that the switch covers all tables.
switch {
case encode0Low <= r && r < encode0High:
if r = rune(encode0[r-encode0Low]); r != 0 {
goto write2or3
}
case encode1Low <= r && r < encode1High:
if r = rune(encode1[r-encode1Low]); r != 0 {
goto write2or3
}
case encode2Low <= r && r < encode2High:
if r = rune(encode2[r-encode2Low]); r != 0 {
goto write2or3
}
case encode3Low <= r && r < encode3High:
if r = rune(encode3[r-encode3Low]); r != 0 {
goto write2or3
}
case encode4Low <= r && r < encode4High:
if r = rune(encode4[r-encode4Low]); r != 0 {
goto write2or3
}
case encode5Low <= r && r < encode5High:
if 0xff61 <= r && r < 0xffa0 {
goto write2
}
if r = rune(encode5[r-encode5Low]); r != 0 {
goto write2or3
}
}
err = internal.ErrASCIIReplacement
break
}
if nDst >= len(dst) {
err = transform.ErrShortDst
break
}
dst[nDst] = uint8(r)
nDst++
continue
write2or3:
if r>>tableShift == jis0208 {
if nDst+2 > len(dst) {
err = transform.ErrShortDst
break
}
} else {
if nDst+3 > len(dst) {
err = transform.ErrShortDst
break
}
dst[nDst] = 0x8f
nDst++
}
dst[nDst+0] = 0xa1 + uint8(r>>codeShift)&codeMask
dst[nDst+1] = 0xa1 + uint8(r)&codeMask
nDst += 2
continue
write2:
if nDst+2 > len(dst) {
err = transform.ErrShortDst
break
}
dst[nDst+0] = 0x8e
dst[nDst+1] = uint8(r - (0xff61 - 0xa1))
nDst += 2
continue
}
return nDst, nSrc, err
}
func init() {
// Check that the hard-coded encode switch covers all tables.
if numEncodeTables != 6 {
panic("bad numEncodeTables")
}
}

299
vendor/golang.org/x/text/encoding/japanese/iso2022jp.go generated vendored Normal file
View File

@ -0,0 +1,299 @@
// Copyright 2013 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 japanese
import (
"unicode/utf8"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/internal"
"golang.org/x/text/encoding/internal/identifier"
"golang.org/x/text/transform"
)
// ISO2022JP is the ISO-2022-JP encoding.
var ISO2022JP encoding.Encoding = &iso2022JP
var iso2022JP = internal.Encoding{
internal.FuncEncoding{iso2022JPNewDecoder, iso2022JPNewEncoder},
"ISO-2022-JP",
identifier.ISO2022JP,
}
func iso2022JPNewDecoder() transform.Transformer {
return new(iso2022JPDecoder)
}
func iso2022JPNewEncoder() transform.Transformer {
return new(iso2022JPEncoder)
}
const (
asciiState = iota
katakanaState
jis0208State
jis0212State
)
const asciiEsc = 0x1b
type iso2022JPDecoder int
func (d *iso2022JPDecoder) Reset() {
*d = asciiState
}
func (d *iso2022JPDecoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
r, size := rune(0), 0
for ; nSrc < len(src); nSrc += size {
c0 := src[nSrc]
if c0 >= utf8.RuneSelf {
r, size = '\ufffd', 1
goto write
}
if c0 == asciiEsc {
if nSrc+2 >= len(src) {
if !atEOF {
return nDst, nSrc, transform.ErrShortSrc
}
// TODO: is it correct to only skip 1??
r, size = '\ufffd', 1
goto write
}
size = 3
c1 := src[nSrc+1]
c2 := src[nSrc+2]
switch {
case c1 == '$' && (c2 == '@' || c2 == 'B'): // 0x24 {0x40, 0x42}
*d = jis0208State
continue
case c1 == '$' && c2 == '(': // 0x24 0x28
if nSrc+3 >= len(src) {
if !atEOF {
return nDst, nSrc, transform.ErrShortSrc
}
r, size = '\ufffd', 1
goto write
}
size = 4
if src[nSrc+3] == 'D' {
*d = jis0212State
continue
}
case c1 == '(' && (c2 == 'B' || c2 == 'J'): // 0x28 {0x42, 0x4A}
*d = asciiState
continue
case c1 == '(' && c2 == 'I': // 0x28 0x49
*d = katakanaState
continue
}
r, size = '\ufffd', 1
goto write
}
switch *d {
case asciiState:
r, size = rune(c0), 1
case katakanaState:
if c0 < 0x21 || 0x60 <= c0 {
r, size = '\ufffd', 1
goto write
}
r, size = rune(c0)+(0xff61-0x21), 1
default:
if c0 == 0x0a {
*d = asciiState
r, size = rune(c0), 1
goto write
}
if nSrc+1 >= len(src) {
if !atEOF {
return nDst, nSrc, transform.ErrShortSrc
}
r, size = '\ufffd', 1
goto write
}
size = 2
c1 := src[nSrc+1]
i := int(c0-0x21)*94 + int(c1-0x21)
if *d == jis0208State && i < len(jis0208Decode) {
r = rune(jis0208Decode[i])
} else if *d == jis0212State && i < len(jis0212Decode) {
r = rune(jis0212Decode[i])
} else {
r = '\ufffd'
goto write
}
if r == 0 {
r = '\ufffd'
}
}
write:
if nDst+utf8.RuneLen(r) > len(dst) {
return nDst, nSrc, transform.ErrShortDst
}
nDst += utf8.EncodeRune(dst[nDst:], r)
}
return nDst, nSrc, err
}
type iso2022JPEncoder int
func (e *iso2022JPEncoder) Reset() {
*e = asciiState
}
func (e *iso2022JPEncoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
r, size := rune(0), 0
for ; nSrc < len(src); nSrc += size {
r = rune(src[nSrc])
// Decode a 1-byte rune.
if r < utf8.RuneSelf {
size = 1
} else {
// Decode a multi-byte rune.
r, size = utf8.DecodeRune(src[nSrc:])
if size == 1 {
// All valid runes of size 1 (those below utf8.RuneSelf) were
// handled above. We have invalid UTF-8 or we haven't seen the
// full character yet.
if !atEOF && !utf8.FullRune(src[nSrc:]) {
err = transform.ErrShortSrc
break
}
}
// func init checks that the switch covers all tables.
//
// http://encoding.spec.whatwg.org/#iso-2022-jp says that "the index jis0212
// is not used by the iso-2022-jp encoder due to lack of widespread support".
//
// TODO: do we have to special-case U+00A5 and U+203E, as per
// http://encoding.spec.whatwg.org/#iso-2022-jp
// Doing so would mean that "\u00a5" would not be preserved
// after an encode-decode round trip.
switch {
case encode0Low <= r && r < encode0High:
if r = rune(encode0[r-encode0Low]); r>>tableShift == jis0208 {
goto writeJIS
}
case encode1Low <= r && r < encode1High:
if r = rune(encode1[r-encode1Low]); r>>tableShift == jis0208 {
goto writeJIS
}
case encode2Low <= r && r < encode2High:
if r = rune(encode2[r-encode2Low]); r>>tableShift == jis0208 {
goto writeJIS
}
case encode3Low <= r && r < encode3High:
if r = rune(encode3[r-encode3Low]); r>>tableShift == jis0208 {
goto writeJIS
}
case encode4Low <= r && r < encode4High:
if r = rune(encode4[r-encode4Low]); r>>tableShift == jis0208 {
goto writeJIS
}
case encode5Low <= r && r < encode5High:
if 0xff61 <= r && r < 0xffa0 {
goto writeKatakana
}
if r = rune(encode5[r-encode5Low]); r>>tableShift == jis0208 {
goto writeJIS
}
}
// Switch back to ASCII state in case of error so that an ASCII
// replacement character can be written in the correct state.
if *e != asciiState {
if nDst+3 > len(dst) {
err = transform.ErrShortDst
break
}
*e = asciiState
dst[nDst+0] = asciiEsc
dst[nDst+1] = '('
dst[nDst+2] = 'B'
nDst += 3
}
err = internal.ErrASCIIReplacement
break
}
if *e != asciiState {
if nDst+4 > len(dst) {
err = transform.ErrShortDst
break
}
*e = asciiState
dst[nDst+0] = asciiEsc
dst[nDst+1] = '('
dst[nDst+2] = 'B'
nDst += 3
} else if nDst >= len(dst) {
err = transform.ErrShortDst
break
}
dst[nDst] = uint8(r)
nDst++
continue
writeJIS:
if *e != jis0208State {
if nDst+5 > len(dst) {
err = transform.ErrShortDst
break
}
*e = jis0208State
dst[nDst+0] = asciiEsc
dst[nDst+1] = '$'
dst[nDst+2] = 'B'
nDst += 3
} else if nDst+2 > len(dst) {
err = transform.ErrShortDst
break
}
dst[nDst+0] = 0x21 + uint8(r>>codeShift)&codeMask
dst[nDst+1] = 0x21 + uint8(r)&codeMask
nDst += 2
continue
writeKatakana:
if *e != katakanaState {
if nDst+4 > len(dst) {
err = transform.ErrShortDst
break
}
*e = katakanaState
dst[nDst+0] = asciiEsc
dst[nDst+1] = '('
dst[nDst+2] = 'I'
nDst += 3
} else if nDst >= len(dst) {
err = transform.ErrShortDst
break
}
dst[nDst] = uint8(r - (0xff61 - 0x21))
nDst++
continue
}
if atEOF && err == nil && *e != asciiState {
if nDst+3 > len(dst) {
err = transform.ErrShortDst
} else {
*e = asciiState
dst[nDst+0] = asciiEsc
dst[nDst+1] = '('
dst[nDst+2] = 'B'
nDst += 3
}
}
return nDst, nSrc, err
}

189
vendor/golang.org/x/text/encoding/japanese/shiftjis.go generated vendored Normal file
View File

@ -0,0 +1,189 @@
// Copyright 2013 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 japanese
import (
"unicode/utf8"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/internal"
"golang.org/x/text/encoding/internal/identifier"
"golang.org/x/text/transform"
)
// ShiftJIS is the Shift JIS encoding, also known as Code Page 932 and
// Windows-31J.
var ShiftJIS encoding.Encoding = &shiftJIS
var shiftJIS = internal.Encoding{
&internal.SimpleEncoding{shiftJISDecoder{}, shiftJISEncoder{}},
"Shift JIS",
identifier.ShiftJIS,
}
type shiftJISDecoder struct{ transform.NopResetter }
func (shiftJISDecoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
r, size := rune(0), 0
loop:
for ; nSrc < len(src); nSrc += size {
switch c0 := src[nSrc]; {
case c0 < utf8.RuneSelf:
r, size = rune(c0), 1
case 0xa1 <= c0 && c0 < 0xe0:
r, size = rune(c0)+(0xff61-0xa1), 1
case (0x81 <= c0 && c0 < 0xa0) || (0xe0 <= c0 && c0 < 0xfd):
if c0 <= 0x9f {
c0 -= 0x70
} else {
c0 -= 0xb0
}
c0 = 2*c0 - 0x21
if nSrc+1 >= len(src) {
if !atEOF {
err = transform.ErrShortSrc
break loop
}
r, size = '\ufffd', 1
goto write
}
c1 := src[nSrc+1]
switch {
case c1 < 0x40:
r, size = '\ufffd', 1 // c1 is ASCII so output on next round
goto write
case c1 < 0x7f:
c0--
c1 -= 0x40
case c1 == 0x7f:
r, size = '\ufffd', 1 // c1 is ASCII so output on next round
goto write
case c1 < 0x9f:
c0--
c1 -= 0x41
case c1 < 0xfd:
c1 -= 0x9f
default:
r, size = '\ufffd', 2
goto write
}
r, size = '\ufffd', 2
if i := int(c0)*94 + int(c1); i < len(jis0208Decode) {
r = rune(jis0208Decode[i])
if r == 0 {
r = '\ufffd'
}
}
case c0 == 0x80:
r, size = 0x80, 1
default:
r, size = '\ufffd', 1
}
write:
if nDst+utf8.RuneLen(r) > len(dst) {
err = transform.ErrShortDst
break loop
}
nDst += utf8.EncodeRune(dst[nDst:], r)
}
return nDst, nSrc, err
}
type shiftJISEncoder struct{ transform.NopResetter }
func (shiftJISEncoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
r, size := rune(0), 0
loop:
for ; nSrc < len(src); nSrc += size {
r = rune(src[nSrc])
// Decode a 1-byte rune.
if r < utf8.RuneSelf {
size = 1
} else {
// Decode a multi-byte rune.
r, size = utf8.DecodeRune(src[nSrc:])
if size == 1 {
// All valid runes of size 1 (those below utf8.RuneSelf) were
// handled above. We have invalid UTF-8 or we haven't seen the
// full character yet.
if !atEOF && !utf8.FullRune(src[nSrc:]) {
err = transform.ErrShortSrc
break loop
}
}
// func init checks that the switch covers all tables.
switch {
case encode0Low <= r && r < encode0High:
if r = rune(encode0[r-encode0Low]); r>>tableShift == jis0208 {
goto write2
}
case encode1Low <= r && r < encode1High:
if r = rune(encode1[r-encode1Low]); r>>tableShift == jis0208 {
goto write2
}
case encode2Low <= r && r < encode2High:
if r = rune(encode2[r-encode2Low]); r>>tableShift == jis0208 {
goto write2
}
case encode3Low <= r && r < encode3High:
if r = rune(encode3[r-encode3Low]); r>>tableShift == jis0208 {
goto write2
}
case encode4Low <= r && r < encode4High:
if r = rune(encode4[r-encode4Low]); r>>tableShift == jis0208 {
goto write2
}
case encode5Low <= r && r < encode5High:
if 0xff61 <= r && r < 0xffa0 {
r -= 0xff61 - 0xa1
goto write1
}
if r = rune(encode5[r-encode5Low]); r>>tableShift == jis0208 {
goto write2
}
}
err = internal.ErrASCIIReplacement
break
}
write1:
if nDst >= len(dst) {
err = transform.ErrShortDst
break
}
dst[nDst] = uint8(r)
nDst++
continue
write2:
j1 := uint8(r>>codeShift) & codeMask
j2 := uint8(r) & codeMask
if nDst+2 > len(dst) {
err = transform.ErrShortDst
break loop
}
if j1 <= 61 {
dst[nDst+0] = 129 + j1/2
} else {
dst[nDst+0] = 193 + j1/2
}
if j1&1 == 0 {
dst[nDst+1] = j2 + j2/63 + 64
} else {
dst[nDst+1] = j2 + 159
}
nDst += 2
continue
}
return nDst, nSrc, err
}

26971
vendor/golang.org/x/text/encoding/japanese/tables.go generated vendored Normal file

File diff suppressed because it is too large Load Diff

177
vendor/golang.org/x/text/encoding/korean/euckr.go generated vendored Normal file
View File

@ -0,0 +1,177 @@
// Copyright 2013 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 korean
import (
"unicode/utf8"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/internal"
"golang.org/x/text/encoding/internal/identifier"
"golang.org/x/text/transform"
)
// All is a list of all defined encodings in this package.
var All = []encoding.Encoding{EUCKR}
// EUCKR is the EUC-KR encoding, also known as Code Page 949.
var EUCKR encoding.Encoding = &eucKR
var eucKR = internal.Encoding{
&internal.SimpleEncoding{eucKRDecoder{}, eucKREncoder{}},
"EUC-KR",
identifier.EUCKR,
}
type eucKRDecoder struct{ transform.NopResetter }
func (eucKRDecoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
r, size := rune(0), 0
loop:
for ; nSrc < len(src); nSrc += size {
switch c0 := src[nSrc]; {
case c0 < utf8.RuneSelf:
r, size = rune(c0), 1
case 0x81 <= c0 && c0 < 0xff:
if nSrc+1 >= len(src) {
if !atEOF {
err = transform.ErrShortSrc
break loop
}
r, size = utf8.RuneError, 1
break
}
c1 := src[nSrc+1]
size = 2
if c0 < 0xc7 {
r = 178 * rune(c0-0x81)
switch {
case 0x41 <= c1 && c1 < 0x5b:
r += rune(c1) - (0x41 - 0*26)
case 0x61 <= c1 && c1 < 0x7b:
r += rune(c1) - (0x61 - 1*26)
case 0x81 <= c1 && c1 < 0xff:
r += rune(c1) - (0x81 - 2*26)
default:
goto decError
}
} else if 0xa1 <= c1 && c1 < 0xff {
r = 178*(0xc7-0x81) + rune(c0-0xc7)*94 + rune(c1-0xa1)
} else {
goto decError
}
if int(r) < len(decode) {
r = rune(decode[r])
if r != 0 {
break
}
}
decError:
r = utf8.RuneError
if c1 < utf8.RuneSelf {
size = 1
}
default:
r, size = utf8.RuneError, 1
break
}
if nDst+utf8.RuneLen(r) > len(dst) {
err = transform.ErrShortDst
break
}
nDst += utf8.EncodeRune(dst[nDst:], r)
}
return nDst, nSrc, err
}
type eucKREncoder struct{ transform.NopResetter }
func (eucKREncoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
r, size := rune(0), 0
for ; nSrc < len(src); nSrc += size {
r = rune(src[nSrc])
// Decode a 1-byte rune.
if r < utf8.RuneSelf {
size = 1
if nDst >= len(dst) {
err = transform.ErrShortDst
break
}
dst[nDst] = uint8(r)
nDst++
continue
} else {
// Decode a multi-byte rune.
r, size = utf8.DecodeRune(src[nSrc:])
if size == 1 {
// All valid runes of size 1 (those below utf8.RuneSelf) were
// handled above. We have invalid UTF-8 or we haven't seen the
// full character yet.
if !atEOF && !utf8.FullRune(src[nSrc:]) {
err = transform.ErrShortSrc
break
}
}
// func init checks that the switch covers all tables.
switch {
case encode0Low <= r && r < encode0High:
if r = rune(encode0[r-encode0Low]); r != 0 {
goto write2
}
case encode1Low <= r && r < encode1High:
if r = rune(encode1[r-encode1Low]); r != 0 {
goto write2
}
case encode2Low <= r && r < encode2High:
if r = rune(encode2[r-encode2Low]); r != 0 {
goto write2
}
case encode3Low <= r && r < encode3High:
if r = rune(encode3[r-encode3Low]); r != 0 {
goto write2
}
case encode4Low <= r && r < encode4High:
if r = rune(encode4[r-encode4Low]); r != 0 {
goto write2
}
case encode5Low <= r && r < encode5High:
if r = rune(encode5[r-encode5Low]); r != 0 {
goto write2
}
case encode6Low <= r && r < encode6High:
if r = rune(encode6[r-encode6Low]); r != 0 {
goto write2
}
}
err = internal.ErrASCIIReplacement
break
}
write2:
if nDst+2 > len(dst) {
err = transform.ErrShortDst
break
}
dst[nDst+0] = uint8(r >> 8)
dst[nDst+1] = uint8(r)
nDst += 2
continue
}
return nDst, nSrc, err
}
func init() {
// Check that the hard-coded encode switch covers all tables.
if numEncodeTables != 7 {
panic("bad numEncodeTables")
}
}

34152
vendor/golang.org/x/text/encoding/korean/tables.go generated vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,12 @@
// 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 simplifiedchinese
import (
"golang.org/x/text/encoding"
)
// All is a list of all defined encodings in this package.
var All = []encoding.Encoding{GB18030, GBK, HZGB2312}

View File

@ -0,0 +1,269 @@
// Copyright 2013 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 simplifiedchinese
import (
"unicode/utf8"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/internal"
"golang.org/x/text/encoding/internal/identifier"
"golang.org/x/text/transform"
)
var (
// GB18030 is the GB18030 encoding.
GB18030 encoding.Encoding = &gbk18030
// GBK is the GBK encoding. It encodes an extension of the GB2312 character set
// and is also known as Code Page 936.
GBK encoding.Encoding = &gbk
)
var gbk = internal.Encoding{
&internal.SimpleEncoding{
gbkDecoder{gb18030: false},
gbkEncoder{gb18030: false},
},
"GBK",
identifier.GBK,
}
var gbk18030 = internal.Encoding{
&internal.SimpleEncoding{
gbkDecoder{gb18030: true},
gbkEncoder{gb18030: true},
},
"GB18030",
identifier.GB18030,
}
type gbkDecoder struct {
transform.NopResetter
gb18030 bool
}
func (d gbkDecoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
r, size := rune(0), 0
loop:
for ; nSrc < len(src); nSrc += size {
switch c0 := src[nSrc]; {
case c0 < utf8.RuneSelf:
r, size = rune(c0), 1
// Microsoft's Code Page 936 extends GBK 1.0 to encode the euro sign U+20AC
// as 0x80. The HTML5 specification at http://encoding.spec.whatwg.org/#gbk
// says to treat "gbk" as Code Page 936.
case c0 == 0x80:
r, size = '€', 1
case c0 < 0xff:
if nSrc+1 >= len(src) {
if !atEOF {
err = transform.ErrShortSrc
break loop
}
r, size = utf8.RuneError, 1
goto write
}
c1 := src[nSrc+1]
switch {
case 0x40 <= c1 && c1 < 0x7f:
c1 -= 0x40
case 0x80 <= c1 && c1 < 0xff:
c1 -= 0x41
case d.gb18030 && 0x30 <= c1 && c1 < 0x40:
if nSrc+3 >= len(src) {
if !atEOF {
err = transform.ErrShortSrc
break loop
}
// The second byte here is always ASCII, so we can set size
// to 1 in all cases.
r, size = utf8.RuneError, 1
goto write
}
c2 := src[nSrc+2]
if c2 < 0x81 || 0xff <= c2 {
r, size = utf8.RuneError, 1
goto write
}
c3 := src[nSrc+3]
if c3 < 0x30 || 0x3a <= c3 {
r, size = utf8.RuneError, 1
goto write
}
size = 4
r = ((rune(c0-0x81)*10+rune(c1-0x30))*126+rune(c2-0x81))*10 + rune(c3-0x30)
if r < 39420 {
i, j := 0, len(gb18030)
for i < j {
h := i + (j-i)/2
if r >= rune(gb18030[h][0]) {
i = h + 1
} else {
j = h
}
}
dec := &gb18030[i-1]
r += rune(dec[1]) - rune(dec[0])
goto write
}
r -= 189000
if 0 <= r && r < 0x100000 {
r += 0x10000
} else {
r, size = utf8.RuneError, 1
}
goto write
default:
r, size = utf8.RuneError, 1
goto write
}
r, size = '\ufffd', 2
if i := int(c0-0x81)*190 + int(c1); i < len(decode) {
r = rune(decode[i])
if r == 0 {
r = '\ufffd'
}
}
default:
r, size = utf8.RuneError, 1
}
write:
if nDst+utf8.RuneLen(r) > len(dst) {
err = transform.ErrShortDst
break loop
}
nDst += utf8.EncodeRune(dst[nDst:], r)
}
return nDst, nSrc, err
}
type gbkEncoder struct {
transform.NopResetter
gb18030 bool
}
func (e gbkEncoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
r, r2, size := rune(0), rune(0), 0
for ; nSrc < len(src); nSrc += size {
r = rune(src[nSrc])
// Decode a 1-byte rune.
if r < utf8.RuneSelf {
size = 1
} else {
// Decode a multi-byte rune.
r, size = utf8.DecodeRune(src[nSrc:])
if size == 1 {
// All valid runes of size 1 (those below utf8.RuneSelf) were
// handled above. We have invalid UTF-8 or we haven't seen the
// full character yet.
if !atEOF && !utf8.FullRune(src[nSrc:]) {
err = transform.ErrShortSrc
break
}
}
// func init checks that the switch covers all tables.
switch {
case encode0Low <= r && r < encode0High:
if r2 = rune(encode0[r-encode0Low]); r2 != 0 {
goto write2
}
case encode1Low <= r && r < encode1High:
// Microsoft's Code Page 936 extends GBK 1.0 to encode the euro sign U+20AC
// as 0x80. The HTML5 specification at http://encoding.spec.whatwg.org/#gbk
// says to treat "gbk" as Code Page 936.
if r == '€' {
r = 0x80
goto write1
}
if r2 = rune(encode1[r-encode1Low]); r2 != 0 {
goto write2
}
case encode2Low <= r && r < encode2High:
if r2 = rune(encode2[r-encode2Low]); r2 != 0 {
goto write2
}
case encode3Low <= r && r < encode3High:
if r2 = rune(encode3[r-encode3Low]); r2 != 0 {
goto write2
}
case encode4Low <= r && r < encode4High:
if r2 = rune(encode4[r-encode4Low]); r2 != 0 {
goto write2
}
}
if e.gb18030 {
if r < 0x10000 {
i, j := 0, len(gb18030)
for i < j {
h := i + (j-i)/2
if r >= rune(gb18030[h][1]) {
i = h + 1
} else {
j = h
}
}
dec := &gb18030[i-1]
r += rune(dec[0]) - rune(dec[1])
goto write4
} else if r < 0x110000 {
r += 189000 - 0x10000
goto write4
}
}
err = internal.ErrASCIIReplacement
break
}
write1:
if nDst >= len(dst) {
err = transform.ErrShortDst
break
}
dst[nDst] = uint8(r)
nDst++
continue
write2:
if nDst+2 > len(dst) {
err = transform.ErrShortDst
break
}
dst[nDst+0] = uint8(r2 >> 8)
dst[nDst+1] = uint8(r2)
nDst += 2
continue
write4:
if nDst+4 > len(dst) {
err = transform.ErrShortDst
break
}
dst[nDst+3] = uint8(r%10 + 0x30)
r /= 10
dst[nDst+2] = uint8(r%126 + 0x81)
r /= 126
dst[nDst+1] = uint8(r%10 + 0x30)
r /= 10
dst[nDst+0] = uint8(r + 0x81)
nDst += 4
continue
}
return nDst, nSrc, err
}
func init() {
// Check that the hard-coded encode switch covers all tables.
if numEncodeTables != 5 {
panic("bad numEncodeTables")
}
}

View File

@ -0,0 +1,245 @@
// Copyright 2013 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 simplifiedchinese
import (
"unicode/utf8"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/internal"
"golang.org/x/text/encoding/internal/identifier"
"golang.org/x/text/transform"
)
// HZGB2312 is the HZ-GB2312 encoding.
var HZGB2312 encoding.Encoding = &hzGB2312
var hzGB2312 = internal.Encoding{
internal.FuncEncoding{hzGB2312NewDecoder, hzGB2312NewEncoder},
"HZ-GB2312",
identifier.HZGB2312,
}
func hzGB2312NewDecoder() transform.Transformer {
return new(hzGB2312Decoder)
}
func hzGB2312NewEncoder() transform.Transformer {
return new(hzGB2312Encoder)
}
const (
asciiState = iota
gbState
)
type hzGB2312Decoder int
func (d *hzGB2312Decoder) Reset() {
*d = asciiState
}
func (d *hzGB2312Decoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
r, size := rune(0), 0
loop:
for ; nSrc < len(src); nSrc += size {
c0 := src[nSrc]
if c0 >= utf8.RuneSelf {
r, size = utf8.RuneError, 1
goto write
}
if c0 == '~' {
if nSrc+1 >= len(src) {
if !atEOF {
err = transform.ErrShortSrc
break loop
}
r = utf8.RuneError
goto write
}
size = 2
switch src[nSrc+1] {
case '{':
*d = gbState
continue
case '}':
*d = asciiState
continue
case '~':
if nDst >= len(dst) {
err = transform.ErrShortDst
break loop
}
dst[nDst] = '~'
nDst++
continue
case '\n':
continue
default:
r = utf8.RuneError
goto write
}
}
if *d == asciiState {
r, size = rune(c0), 1
} else {
if nSrc+1 >= len(src) {
if !atEOF {
err = transform.ErrShortSrc
break loop
}
r, size = utf8.RuneError, 1
goto write
}
size = 2
c1 := src[nSrc+1]
if c0 < 0x21 || 0x7e <= c0 || c1 < 0x21 || 0x7f <= c1 {
// error
} else if i := int(c0-0x01)*190 + int(c1+0x3f); i < len(decode) {
r = rune(decode[i])
if r != 0 {
goto write
}
}
if c1 > utf8.RuneSelf {
// Be consistent and always treat non-ASCII as a single error.
size = 1
}
r = utf8.RuneError
}
write:
if nDst+utf8.RuneLen(r) > len(dst) {
err = transform.ErrShortDst
break loop
}
nDst += utf8.EncodeRune(dst[nDst:], r)
}
return nDst, nSrc, err
}
type hzGB2312Encoder int
func (d *hzGB2312Encoder) Reset() {
*d = asciiState
}
func (e *hzGB2312Encoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
r, size := rune(0), 0
for ; nSrc < len(src); nSrc += size {
r = rune(src[nSrc])
// Decode a 1-byte rune.
if r < utf8.RuneSelf {
size = 1
if r == '~' {
if nDst+2 > len(dst) {
err = transform.ErrShortDst
break
}
dst[nDst+0] = '~'
dst[nDst+1] = '~'
nDst += 2
continue
} else if *e != asciiState {
if nDst+3 > len(dst) {
err = transform.ErrShortDst
break
}
*e = asciiState
dst[nDst+0] = '~'
dst[nDst+1] = '}'
nDst += 2
} else if nDst >= len(dst) {
err = transform.ErrShortDst
break
}
dst[nDst] = uint8(r)
nDst += 1
continue
}
// Decode a multi-byte rune.
r, size = utf8.DecodeRune(src[nSrc:])
if size == 1 {
// All valid runes of size 1 (those below utf8.RuneSelf) were
// handled above. We have invalid UTF-8 or we haven't seen the
// full character yet.
if !atEOF && !utf8.FullRune(src[nSrc:]) {
err = transform.ErrShortSrc
break
}
}
// func init checks that the switch covers all tables.
switch {
case encode0Low <= r && r < encode0High:
if r = rune(encode0[r-encode0Low]); r != 0 {
goto writeGB
}
case encode1Low <= r && r < encode1High:
if r = rune(encode1[r-encode1Low]); r != 0 {
goto writeGB
}
case encode2Low <= r && r < encode2High:
if r = rune(encode2[r-encode2Low]); r != 0 {
goto writeGB
}
case encode3Low <= r && r < encode3High:
if r = rune(encode3[r-encode3Low]); r != 0 {
goto writeGB
}
case encode4Low <= r && r < encode4High:
if r = rune(encode4[r-encode4Low]); r != 0 {
goto writeGB
}
}
terminateInASCIIState:
// Switch back to ASCII state in case of error so that an ASCII
// replacement character can be written in the correct state.
if *e != asciiState {
if nDst+2 > len(dst) {
err = transform.ErrShortDst
break
}
dst[nDst+0] = '~'
dst[nDst+1] = '}'
nDst += 2
}
err = internal.ErrASCIIReplacement
break
writeGB:
c0 := uint8(r>>8) - 0x80
c1 := uint8(r) - 0x80
if c0 < 0x21 || 0x7e <= c0 || c1 < 0x21 || 0x7f <= c1 {
goto terminateInASCIIState
}
if *e == asciiState {
if nDst+4 > len(dst) {
err = transform.ErrShortDst
break
}
*e = gbState
dst[nDst+0] = '~'
dst[nDst+1] = '{'
nDst += 2
} else if nDst+2 > len(dst) {
err = transform.ErrShortDst
break
}
dst[nDst+0] = c0
dst[nDst+1] = c1
nDst += 2
continue
}
// TODO: should one always terminate in ASCII state to make it safe to
// concatenate two HZ-GB2312-encoded strings?
return nDst, nSrc, err
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,199 @@
// Copyright 2013 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 traditionalchinese
import (
"unicode/utf8"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/internal"
"golang.org/x/text/encoding/internal/identifier"
"golang.org/x/text/transform"
)
// All is a list of all defined encodings in this package.
var All = []encoding.Encoding{Big5}
// Big5 is the Big5 encoding, also known as Code Page 950.
var Big5 encoding.Encoding = &big5
var big5 = internal.Encoding{
&internal.SimpleEncoding{big5Decoder{}, big5Encoder{}},
"Big5",
identifier.Big5,
}
type big5Decoder struct{ transform.NopResetter }
func (big5Decoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
r, size, s := rune(0), 0, ""
loop:
for ; nSrc < len(src); nSrc += size {
switch c0 := src[nSrc]; {
case c0 < utf8.RuneSelf:
r, size = rune(c0), 1
case 0x81 <= c0 && c0 < 0xff:
if nSrc+1 >= len(src) {
if !atEOF {
err = transform.ErrShortSrc
break loop
}
r, size = utf8.RuneError, 1
goto write
}
c1 := src[nSrc+1]
switch {
case 0x40 <= c1 && c1 < 0x7f:
c1 -= 0x40
case 0xa1 <= c1 && c1 < 0xff:
c1 -= 0x62
case c1 < 0x40:
r, size = utf8.RuneError, 1
goto write
default:
r, size = utf8.RuneError, 2
goto write
}
r, size = '\ufffd', 2
if i := int(c0-0x81)*157 + int(c1); i < len(decode) {
if 1133 <= i && i < 1167 {
// The two-rune special cases for LATIN CAPITAL / SMALL E WITH CIRCUMFLEX
// AND MACRON / CARON are from http://encoding.spec.whatwg.org/#big5
switch i {
case 1133:
s = "\u00CA\u0304"
goto writeStr
case 1135:
s = "\u00CA\u030C"
goto writeStr
case 1164:
s = "\u00EA\u0304"
goto writeStr
case 1166:
s = "\u00EA\u030C"
goto writeStr
}
}
r = rune(decode[i])
if r == 0 {
r = '\ufffd'
}
}
default:
r, size = utf8.RuneError, 1
}
write:
if nDst+utf8.RuneLen(r) > len(dst) {
err = transform.ErrShortDst
break loop
}
nDst += utf8.EncodeRune(dst[nDst:], r)
continue loop
writeStr:
if nDst+len(s) > len(dst) {
err = transform.ErrShortDst
break loop
}
nDst += copy(dst[nDst:], s)
continue loop
}
return nDst, nSrc, err
}
type big5Encoder struct{ transform.NopResetter }
func (big5Encoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
r, size := rune(0), 0
for ; nSrc < len(src); nSrc += size {
r = rune(src[nSrc])
// Decode a 1-byte rune.
if r < utf8.RuneSelf {
size = 1
if nDst >= len(dst) {
err = transform.ErrShortDst
break
}
dst[nDst] = uint8(r)
nDst++
continue
} else {
// Decode a multi-byte rune.
r, size = utf8.DecodeRune(src[nSrc:])
if size == 1 {
// All valid runes of size 1 (those below utf8.RuneSelf) were
// handled above. We have invalid UTF-8 or we haven't seen the
// full character yet.
if !atEOF && !utf8.FullRune(src[nSrc:]) {
err = transform.ErrShortSrc
break
}
}
}
if r >= utf8.RuneSelf {
// func init checks that the switch covers all tables.
switch {
case encode0Low <= r && r < encode0High:
if r = rune(encode0[r-encode0Low]); r != 0 {
goto write2
}
case encode1Low <= r && r < encode1High:
if r = rune(encode1[r-encode1Low]); r != 0 {
goto write2
}
case encode2Low <= r && r < encode2High:
if r = rune(encode2[r-encode2Low]); r != 0 {
goto write2
}
case encode3Low <= r && r < encode3High:
if r = rune(encode3[r-encode3Low]); r != 0 {
goto write2
}
case encode4Low <= r && r < encode4High:
if r = rune(encode4[r-encode4Low]); r != 0 {
goto write2
}
case encode5Low <= r && r < encode5High:
if r = rune(encode5[r-encode5Low]); r != 0 {
goto write2
}
case encode6Low <= r && r < encode6High:
if r = rune(encode6[r-encode6Low]); r != 0 {
goto write2
}
case encode7Low <= r && r < encode7High:
if r = rune(encode7[r-encode7Low]); r != 0 {
goto write2
}
}
err = internal.ErrASCIIReplacement
break
}
write2:
if nDst+2 > len(dst) {
err = transform.ErrShortDst
break
}
dst[nDst+0] = uint8(r >> 8)
dst[nDst+1] = uint8(r)
nDst += 2
continue
}
return nDst, nSrc, err
}
func init() {
// Check that the hard-coded encode switch covers all tables.
if numEncodeTables != 8 {
panic("bad numEncodeTables")
}
}

File diff suppressed because it is too large Load Diff

100
vendor/golang.org/x/text/internal/tag/tag.go generated vendored Normal file
View File

@ -0,0 +1,100 @@
// 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 tag contains functionality handling tags and related data.
package tag // import "golang.org/x/text/internal/tag"
import "sort"
// An Index converts tags to a compact numeric value.
//
// All elements are of size 4. Tags may be up to 4 bytes long. Excess bytes can
// be used to store additional information about the tag.
type Index string
// Elem returns the element data at the given index.
func (s Index) Elem(x int) string {
return string(s[x*4 : x*4+4])
}
// Index reports the index of the given key or -1 if it could not be found.
// Only the first len(key) bytes from the start of the 4-byte entries will be
// considered for the search and the first match in Index will be returned.
func (s Index) Index(key []byte) int {
n := len(key)
// search the index of the first entry with an equal or higher value than
// key in s.
index := sort.Search(len(s)/4, func(i int) bool {
return cmp(s[i*4:i*4+n], key) != -1
})
i := index * 4
if cmp(s[i:i+len(key)], key) != 0 {
return -1
}
return index
}
// Next finds the next occurrence of key after index x, which must have been
// obtained from a call to Index using the same key. It returns x+1 or -1.
func (s Index) Next(key []byte, x int) int {
if x++; x*4 < len(s) && cmp(s[x*4:x*4+len(key)], key) == 0 {
return x
}
return -1
}
// cmp returns an integer comparing a and b lexicographically.
func cmp(a Index, b []byte) int {
n := len(a)
if len(b) < n {
n = len(b)
}
for i, c := range b[:n] {
switch {
case a[i] > c:
return 1
case a[i] < c:
return -1
}
}
switch {
case len(a) < len(b):
return -1
case len(a) > len(b):
return 1
}
return 0
}
// Compare returns an integer comparing a and b lexicographically.
func Compare(a string, b []byte) int {
return cmp(Index(a), b)
}
// FixCase reformats b to the same pattern of cases as form.
// If returns false if string b is malformed.
func FixCase(form string, b []byte) bool {
if len(form) != len(b) {
return false
}
for i, c := range b {
if form[i] <= 'Z' {
if c >= 'a' {
c -= 'z' - 'Z'
}
if c < 'A' || 'Z' < c {
return false
}
} else {
if c <= 'Z' {
c += 'z' - 'Z'
}
if c < 'a' || 'z' < c {
return false
}
}
b[i] = c
}
return true
}

16
vendor/golang.org/x/text/language/Makefile generated vendored Normal file
View File

@ -0,0 +1,16 @@
# Copyright 2013 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.
CLEANFILES+=maketables
maketables: maketables.go
go build $^
tables: maketables
./maketables > tables.go
gofmt -w -s tables.go
# Build (but do not run) maketables during testing,
# just to make sure it still compiles.
testshort: maketables

16
vendor/golang.org/x/text/language/common.go generated vendored Normal file
View File

@ -0,0 +1,16 @@
// Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
package language
// This file contains code common to the maketables.go and the package code.
// langAliasType is the type of an alias in langAliasMap.
type langAliasType int8
const (
langDeprecated langAliasType = iota
langMacro
langLegacy
langAliasTypeUnknown langAliasType = -1
)

197
vendor/golang.org/x/text/language/coverage.go generated vendored Normal file
View File

@ -0,0 +1,197 @@
// Copyright 2014 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 language
import (
"fmt"
"sort"
)
// The Coverage interface is used to define the level of coverage of an
// internationalization service. Note that not all types are supported by all
// services. As lists may be generated on the fly, it is recommended that users
// of a Coverage cache the results.
type Coverage interface {
// Tags returns the list of supported tags.
Tags() []Tag
// BaseLanguages returns the list of supported base languages.
BaseLanguages() []Base
// Scripts returns the list of supported scripts.
Scripts() []Script
// Regions returns the list of supported regions.
Regions() []Region
}
var (
// Supported defines a Coverage that lists all supported subtags. Tags
// always returns nil.
Supported Coverage = allSubtags{}
)
// TODO:
// - Support Variants, numbering systems.
// - CLDR coverage levels.
// - Set of common tags defined in this package.
type allSubtags struct{}
// Regions returns the list of supported regions. As all regions are in a
// consecutive range, it simply returns a slice of numbers in increasing order.
// The "undefined" region is not returned.
func (s allSubtags) Regions() []Region {
reg := make([]Region, numRegions)
for i := range reg {
reg[i] = Region{regionID(i + 1)}
}
return reg
}
// Scripts returns the list of supported scripts. As all scripts are in a
// consecutive range, it simply returns a slice of numbers in increasing order.
// The "undefined" script is not returned.
func (s allSubtags) Scripts() []Script {
scr := make([]Script, numScripts)
for i := range scr {
scr[i] = Script{scriptID(i + 1)}
}
return scr
}
// BaseLanguages returns the list of all supported base languages. It generates
// the list by traversing the internal structures.
func (s allSubtags) BaseLanguages() []Base {
base := make([]Base, 0, numLanguages)
for i := 0; i < langNoIndexOffset; i++ {
// We included "und" already for the value 0.
if i != nonCanonicalUnd {
base = append(base, Base{langID(i)})
}
}
i := langNoIndexOffset
for _, v := range langNoIndex {
for k := 0; k < 8; k++ {
if v&1 == 1 {
base = append(base, Base{langID(i)})
}
v >>= 1
i++
}
}
return base
}
// Tags always returns nil.
func (s allSubtags) Tags() []Tag {
return nil
}
// coverage is used used by NewCoverage which is used as a convenient way for
// creating Coverage implementations for partially defined data. Very often a
// package will only need to define a subset of slices. coverage provides a
// convenient way to do this. Moreover, packages using NewCoverage, instead of
// their own implementation, will not break if later new slice types are added.
type coverage struct {
tags func() []Tag
bases func() []Base
scripts func() []Script
regions func() []Region
}
func (s *coverage) Tags() []Tag {
if s.tags == nil {
return nil
}
return s.tags()
}
// bases implements sort.Interface and is used to sort base languages.
type bases []Base
func (b bases) Len() int {
return len(b)
}
func (b bases) Swap(i, j int) {
b[i], b[j] = b[j], b[i]
}
func (b bases) Less(i, j int) bool {
return b[i].langID < b[j].langID
}
// BaseLanguages returns the result from calling s.bases if it is specified or
// otherwise derives the set of supported base languages from tags.
func (s *coverage) BaseLanguages() []Base {
if s.bases == nil {
tags := s.Tags()
if len(tags) == 0 {
return nil
}
a := make([]Base, len(tags))
for i, t := range tags {
a[i] = Base{langID(t.lang)}
}
sort.Sort(bases(a))
k := 0
for i := 1; i < len(a); i++ {
if a[k] != a[i] {
k++
a[k] = a[i]
}
}
return a[:k+1]
}
return s.bases()
}
func (s *coverage) Scripts() []Script {
if s.scripts == nil {
return nil
}
return s.scripts()
}
func (s *coverage) Regions() []Region {
if s.regions == nil {
return nil
}
return s.regions()
}
// NewCoverage returns a Coverage for the given lists. It is typically used by
// packages providing internationalization services to define their level of
// coverage. A list may be of type []T or func() []T, where T is either Tag,
// Base, Script or Region. The returned Coverage derives the value for Bases
// from Tags if no func or slice for []Base is specified. For other unspecified
// types the returned Coverage will return nil for the respective methods.
func NewCoverage(list ...interface{}) Coverage {
s := &coverage{}
for _, x := range list {
switch v := x.(type) {
case func() []Base:
s.bases = v
case func() []Script:
s.scripts = v
case func() []Region:
s.regions = v
case func() []Tag:
s.tags = v
case []Base:
s.bases = func() []Base { return v }
case []Script:
s.scripts = func() []Script { return v }
case []Region:
s.regions = func() []Region { return v }
case []Tag:
s.tags = func() []Tag { return v }
default:
panic(fmt.Sprintf("language: unsupported set type %T", v))
}
}
return s
}

38
vendor/golang.org/x/text/language/go1_1.go generated vendored Normal file
View File

@ -0,0 +1,38 @@
// Copyright 2013 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.2
package language
import "sort"
func sortStable(s sort.Interface) {
ss := stableSort{
s: s,
pos: make([]int, s.Len()),
}
for i := range ss.pos {
ss.pos[i] = i
}
sort.Sort(&ss)
}
type stableSort struct {
s sort.Interface
pos []int
}
func (s *stableSort) Len() int {
return len(s.pos)
}
func (s *stableSort) Less(i, j int) bool {
return s.s.Less(i, j) || !s.s.Less(j, i) && s.pos[i] < s.pos[j]
}
func (s *stableSort) Swap(i, j int) {
s.s.Swap(i, j)
s.pos[i], s.pos[j] = s.pos[j], s.pos[i]
}

11
vendor/golang.org/x/text/language/go1_2.go generated vendored Normal file
View File

@ -0,0 +1,11 @@
// Copyright 2013 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.2
package language
import "sort"
var sortStable = sort.Stable

769
vendor/golang.org/x/text/language/index.go generated vendored Normal file
View File

@ -0,0 +1,769 @@
// Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
package language
// NumCompactTags is the number of common tags. The maximum tag is
// NumCompactTags-1.
const NumCompactTags = 754
var specialTags = []Tag{ // 2 elements
0: {lang: 0xd7, region: 0x6d, script: 0x0, pVariant: 0x5, pExt: 0xe, str: "ca-ES-valencia"},
1: {lang: 0x138, region: 0x134, script: 0x0, pVariant: 0x5, pExt: 0x5, str: "en-US-u-va-posix"},
} // Size: 72 bytes
var coreTags = map[uint32]uint16{
0x0: 0, // und
0x01600000: 3, // af
0x016000d1: 4, // af-NA
0x01600160: 5, // af-ZA
0x01c00000: 6, // agq
0x01c00051: 7, // agq-CM
0x02100000: 8, // ak
0x0210007f: 9, // ak-GH
0x02700000: 10, // am
0x0270006e: 11, // am-ET
0x03a00000: 12, // ar
0x03a00001: 13, // ar-001
0x03a00022: 14, // ar-AE
0x03a00038: 15, // ar-BH
0x03a00061: 16, // ar-DJ
0x03a00066: 17, // ar-DZ
0x03a0006a: 18, // ar-EG
0x03a0006b: 19, // ar-EH
0x03a0006c: 20, // ar-ER
0x03a00096: 21, // ar-IL
0x03a0009a: 22, // ar-IQ
0x03a000a0: 23, // ar-JO
0x03a000a7: 24, // ar-KM
0x03a000ab: 25, // ar-KW
0x03a000af: 26, // ar-LB
0x03a000b8: 27, // ar-LY
0x03a000b9: 28, // ar-MA
0x03a000c8: 29, // ar-MR
0x03a000e0: 30, // ar-OM
0x03a000ec: 31, // ar-PS
0x03a000f2: 32, // ar-QA
0x03a00107: 33, // ar-SA
0x03a0010a: 34, // ar-SD
0x03a00114: 35, // ar-SO
0x03a00116: 36, // ar-SS
0x03a0011b: 37, // ar-SY
0x03a0011f: 38, // ar-TD
0x03a00127: 39, // ar-TN
0x03a0015d: 40, // ar-YE
0x04000000: 41, // ars
0x04300000: 42, // as
0x04300098: 43, // as-IN
0x04400000: 44, // asa
0x0440012e: 45, // asa-TZ
0x04800000: 46, // ast
0x0480006d: 47, // ast-ES
0x05800000: 48, // az
0x0581e000: 49, // az-Cyrl
0x0581e031: 50, // az-Cyrl-AZ
0x05852000: 51, // az-Latn
0x05852031: 52, // az-Latn-AZ
0x05e00000: 53, // bas
0x05e00051: 54, // bas-CM
0x07100000: 55, // be
0x07100046: 56, // be-BY
0x07500000: 57, // bem
0x07500161: 58, // bem-ZM
0x07900000: 59, // bez
0x0790012e: 60, // bez-TZ
0x07e00000: 61, // bg
0x07e00037: 62, // bg-BG
0x08200000: 63, // bh
0x0a000000: 64, // bm
0x0a0000c2: 65, // bm-ML
0x0a500000: 66, // bn
0x0a500034: 67, // bn-BD
0x0a500098: 68, // bn-IN
0x0a900000: 69, // bo
0x0a900052: 70, // bo-CN
0x0a900098: 71, // bo-IN
0x0b200000: 72, // br
0x0b200077: 73, // br-FR
0x0b500000: 74, // brx
0x0b500098: 75, // brx-IN
0x0b700000: 76, // bs
0x0b71e000: 77, // bs-Cyrl
0x0b71e032: 78, // bs-Cyrl-BA
0x0b752000: 79, // bs-Latn
0x0b752032: 80, // bs-Latn-BA
0x0d700000: 81, // ca
0x0d700021: 82, // ca-AD
0x0d70006d: 83, // ca-ES
0x0d700077: 84, // ca-FR
0x0d70009d: 85, // ca-IT
0x0dc00000: 86, // ce
0x0dc00105: 87, // ce-RU
0x0df00000: 88, // cgg
0x0df00130: 89, // cgg-UG
0x0e500000: 90, // chr
0x0e500134: 91, // chr-US
0x0e900000: 92, // ckb
0x0e90009a: 93, // ckb-IQ
0x0e90009b: 94, // ckb-IR
0x0f900000: 95, // cs
0x0f90005d: 96, // cs-CZ
0x0fd00000: 97, // cu
0x0fd00105: 98, // cu-RU
0x0ff00000: 99, // cy
0x0ff0007a: 100, // cy-GB
0x10000000: 101, // da
0x10000062: 102, // da-DK
0x10000081: 103, // da-GL
0x10700000: 104, // dav
0x107000a3: 105, // dav-KE
0x10c00000: 106, // de
0x10c0002d: 107, // de-AT
0x10c00035: 108, // de-BE
0x10c0004d: 109, // de-CH
0x10c0005f: 110, // de-DE
0x10c0009d: 111, // de-IT
0x10c000b1: 112, // de-LI
0x10c000b6: 113, // de-LU
0x11600000: 114, // dje
0x116000d3: 115, // dje-NE
0x11e00000: 116, // dsb
0x11e0005f: 117, // dsb-DE
0x12300000: 118, // dua
0x12300051: 119, // dua-CM
0x12700000: 120, // dv
0x12a00000: 121, // dyo
0x12a00113: 122, // dyo-SN
0x12c00000: 123, // dz
0x12c00042: 124, // dz-BT
0x12e00000: 125, // ebu
0x12e000a3: 126, // ebu-KE
0x12f00000: 127, // ee
0x12f0007f: 128, // ee-GH
0x12f00121: 129, // ee-TG
0x13500000: 130, // el
0x1350005c: 131, // el-CY
0x13500086: 132, // el-GR
0x13800000: 133, // en
0x13800001: 134, // en-001
0x1380001a: 135, // en-150
0x13800024: 136, // en-AG
0x13800025: 137, // en-AI
0x1380002c: 138, // en-AS
0x1380002d: 139, // en-AT
0x1380002e: 140, // en-AU
0x13800033: 141, // en-BB
0x13800035: 142, // en-BE
0x13800039: 143, // en-BI
0x1380003c: 144, // en-BM
0x13800041: 145, // en-BS
0x13800045: 146, // en-BW
0x13800047: 147, // en-BZ
0x13800048: 148, // en-CA
0x13800049: 149, // en-CC
0x1380004d: 150, // en-CH
0x1380004f: 151, // en-CK
0x13800051: 152, // en-CM
0x1380005b: 153, // en-CX
0x1380005c: 154, // en-CY
0x1380005f: 155, // en-DE
0x13800060: 156, // en-DG
0x13800062: 157, // en-DK
0x13800063: 158, // en-DM
0x1380006c: 159, // en-ER
0x13800071: 160, // en-FI
0x13800072: 161, // en-FJ
0x13800073: 162, // en-FK
0x13800074: 163, // en-FM
0x1380007a: 164, // en-GB
0x1380007b: 165, // en-GD
0x1380007e: 166, // en-GG
0x1380007f: 167, // en-GH
0x13800080: 168, // en-GI
0x13800082: 169, // en-GM
0x13800089: 170, // en-GU
0x1380008b: 171, // en-GY
0x1380008c: 172, // en-HK
0x13800095: 173, // en-IE
0x13800096: 174, // en-IL
0x13800097: 175, // en-IM
0x13800098: 176, // en-IN
0x13800099: 177, // en-IO
0x1380009e: 178, // en-JE
0x1380009f: 179, // en-JM
0x138000a3: 180, // en-KE
0x138000a6: 181, // en-KI
0x138000a8: 182, // en-KN
0x138000ac: 183, // en-KY
0x138000b0: 184, // en-LC
0x138000b3: 185, // en-LR
0x138000b4: 186, // en-LS
0x138000be: 187, // en-MG
0x138000bf: 188, // en-MH
0x138000c5: 189, // en-MO
0x138000c6: 190, // en-MP
0x138000c9: 191, // en-MS
0x138000ca: 192, // en-MT
0x138000cb: 193, // en-MU
0x138000cd: 194, // en-MW
0x138000cf: 195, // en-MY
0x138000d1: 196, // en-NA
0x138000d4: 197, // en-NF
0x138000d5: 198, // en-NG
0x138000d8: 199, // en-NL
0x138000dc: 200, // en-NR
0x138000de: 201, // en-NU
0x138000df: 202, // en-NZ
0x138000e5: 203, // en-PG
0x138000e6: 204, // en-PH
0x138000e7: 205, // en-PK
0x138000ea: 206, // en-PN
0x138000eb: 207, // en-PR
0x138000ef: 208, // en-PW
0x13800106: 209, // en-RW
0x13800108: 210, // en-SB
0x13800109: 211, // en-SC
0x1380010a: 212, // en-SD
0x1380010b: 213, // en-SE
0x1380010c: 214, // en-SG
0x1380010d: 215, // en-SH
0x1380010e: 216, // en-SI
0x13800111: 217, // en-SL
0x13800116: 218, // en-SS
0x1380011a: 219, // en-SX
0x1380011c: 220, // en-SZ
0x1380011e: 221, // en-TC
0x13800124: 222, // en-TK
0x13800128: 223, // en-TO
0x1380012b: 224, // en-TT
0x1380012c: 225, // en-TV
0x1380012e: 226, // en-TZ
0x13800130: 227, // en-UG
0x13800132: 228, // en-UM
0x13800134: 229, // en-US
0x13800138: 230, // en-VC
0x1380013b: 231, // en-VG
0x1380013c: 232, // en-VI
0x1380013e: 233, // en-VU
0x13800141: 234, // en-WS
0x13800160: 235, // en-ZA
0x13800161: 236, // en-ZM
0x13800163: 237, // en-ZW
0x13b00000: 238, // eo
0x13b00001: 239, // eo-001
0x13d00000: 240, // es
0x13d0001e: 241, // es-419
0x13d0002b: 242, // es-AR
0x13d0003e: 243, // es-BO
0x13d00040: 244, // es-BR
0x13d00047: 245, // es-BZ
0x13d00050: 246, // es-CL
0x13d00053: 247, // es-CO
0x13d00055: 248, // es-CR
0x13d00058: 249, // es-CU
0x13d00064: 250, // es-DO
0x13d00067: 251, // es-EA
0x13d00068: 252, // es-EC
0x13d0006d: 253, // es-ES
0x13d00085: 254, // es-GQ
0x13d00088: 255, // es-GT
0x13d0008e: 256, // es-HN
0x13d00093: 257, // es-IC
0x13d000ce: 258, // es-MX
0x13d000d7: 259, // es-NI
0x13d000e1: 260, // es-PA
0x13d000e3: 261, // es-PE
0x13d000e6: 262, // es-PH
0x13d000eb: 263, // es-PR
0x13d000f0: 264, // es-PY
0x13d00119: 265, // es-SV
0x13d00134: 266, // es-US
0x13d00135: 267, // es-UY
0x13d0013a: 268, // es-VE
0x13f00000: 269, // et
0x13f00069: 270, // et-EE
0x14400000: 271, // eu
0x1440006d: 272, // eu-ES
0x14500000: 273, // ewo
0x14500051: 274, // ewo-CM
0x14700000: 275, // fa
0x14700023: 276, // fa-AF
0x1470009b: 277, // fa-IR
0x14d00000: 278, // ff
0x14d00051: 279, // ff-CM
0x14d00083: 280, // ff-GN
0x14d000c8: 281, // ff-MR
0x14d00113: 282, // ff-SN
0x15000000: 283, // fi
0x15000071: 284, // fi-FI
0x15200000: 285, // fil
0x152000e6: 286, // fil-PH
0x15700000: 287, // fo
0x15700062: 288, // fo-DK
0x15700075: 289, // fo-FO
0x15d00000: 290, // fr
0x15d00035: 291, // fr-BE
0x15d00036: 292, // fr-BF
0x15d00039: 293, // fr-BI
0x15d0003a: 294, // fr-BJ
0x15d0003b: 295, // fr-BL
0x15d00048: 296, // fr-CA
0x15d0004a: 297, // fr-CD
0x15d0004b: 298, // fr-CF
0x15d0004c: 299, // fr-CG
0x15d0004d: 300, // fr-CH
0x15d0004e: 301, // fr-CI
0x15d00051: 302, // fr-CM
0x15d00061: 303, // fr-DJ
0x15d00066: 304, // fr-DZ
0x15d00077: 305, // fr-FR
0x15d00079: 306, // fr-GA
0x15d0007d: 307, // fr-GF
0x15d00083: 308, // fr-GN
0x15d00084: 309, // fr-GP
0x15d00085: 310, // fr-GQ
0x15d00090: 311, // fr-HT
0x15d000a7: 312, // fr-KM
0x15d000b6: 313, // fr-LU
0x15d000b9: 314, // fr-MA
0x15d000ba: 315, // fr-MC
0x15d000bd: 316, // fr-MF
0x15d000be: 317, // fr-MG
0x15d000c2: 318, // fr-ML
0x15d000c7: 319, // fr-MQ
0x15d000c8: 320, // fr-MR
0x15d000cb: 321, // fr-MU
0x15d000d2: 322, // fr-NC
0x15d000d3: 323, // fr-NE
0x15d000e4: 324, // fr-PF
0x15d000e9: 325, // fr-PM
0x15d00101: 326, // fr-RE
0x15d00106: 327, // fr-RW
0x15d00109: 328, // fr-SC
0x15d00113: 329, // fr-SN
0x15d0011b: 330, // fr-SY
0x15d0011f: 331, // fr-TD
0x15d00121: 332, // fr-TG
0x15d00127: 333, // fr-TN
0x15d0013e: 334, // fr-VU
0x15d0013f: 335, // fr-WF
0x15d0015e: 336, // fr-YT
0x16800000: 337, // fur
0x1680009d: 338, // fur-IT
0x16c00000: 339, // fy
0x16c000d8: 340, // fy-NL
0x16d00000: 341, // ga
0x16d00095: 342, // ga-IE
0x17c00000: 343, // gd
0x17c0007a: 344, // gd-GB
0x18e00000: 345, // gl
0x18e0006d: 346, // gl-ES
0x1a100000: 347, // gsw
0x1a10004d: 348, // gsw-CH
0x1a100077: 349, // gsw-FR
0x1a1000b1: 350, // gsw-LI
0x1a200000: 351, // gu
0x1a200098: 352, // gu-IN
0x1a700000: 353, // guw
0x1a900000: 354, // guz
0x1a9000a3: 355, // guz-KE
0x1aa00000: 356, // gv
0x1aa00097: 357, // gv-IM
0x1b200000: 358, // ha
0x1b20007f: 359, // ha-GH
0x1b2000d3: 360, // ha-NE
0x1b2000d5: 361, // ha-NG
0x1b600000: 362, // haw
0x1b600134: 363, // haw-US
0x1ba00000: 364, // he
0x1ba00096: 365, // he-IL
0x1bc00000: 366, // hi
0x1bc00098: 367, // hi-IN
0x1cf00000: 368, // hr
0x1cf00032: 369, // hr-BA
0x1cf0008f: 370, // hr-HR
0x1d000000: 371, // hsb
0x1d00005f: 372, // hsb-DE
0x1d300000: 373, // hu
0x1d300091: 374, // hu-HU
0x1d500000: 375, // hy
0x1d500027: 376, // hy-AM
0x1df00000: 377, // id
0x1df00094: 378, // id-ID
0x1e500000: 379, // ig
0x1e5000d5: 380, // ig-NG
0x1e800000: 381, // ii
0x1e800052: 382, // ii-CN
0x1f600000: 383, // is
0x1f60009c: 384, // is-IS
0x1f700000: 385, // it
0x1f70004d: 386, // it-CH
0x1f70009d: 387, // it-IT
0x1f700112: 388, // it-SM
0x1f700137: 389, // it-VA
0x1f800000: 390, // iu
0x1fe00000: 391, // ja
0x1fe000a1: 392, // ja-JP
0x20100000: 393, // jbo
0x20500000: 394, // jgo
0x20500051: 395, // jgo-CM
0x20800000: 396, // jmc
0x2080012e: 397, // jmc-TZ
0x20c00000: 398, // jv
0x20e00000: 399, // ka
0x20e0007c: 400, // ka-GE
0x21000000: 401, // kab
0x21000066: 402, // kab-DZ
0x21400000: 403, // kaj
0x21500000: 404, // kam
0x215000a3: 405, // kam-KE
0x21d00000: 406, // kcg
0x22100000: 407, // kde
0x2210012e: 408, // kde-TZ
0x22500000: 409, // kea
0x22500059: 410, // kea-CV
0x23200000: 411, // khq
0x232000c2: 412, // khq-ML
0x23700000: 413, // ki
0x237000a3: 414, // ki-KE
0x24000000: 415, // kk
0x240000ad: 416, // kk-KZ
0x24200000: 417, // kkj
0x24200051: 418, // kkj-CM
0x24300000: 419, // kl
0x24300081: 420, // kl-GL
0x24400000: 421, // kln
0x244000a3: 422, // kln-KE
0x24800000: 423, // km
0x248000a5: 424, // km-KH
0x24f00000: 425, // kn
0x24f00098: 426, // kn-IN
0x25200000: 427, // ko
0x252000a9: 428, // ko-KP
0x252000aa: 429, // ko-KR
0x25400000: 430, // kok
0x25400098: 431, // kok-IN
0x26800000: 432, // ks
0x26800098: 433, // ks-IN
0x26900000: 434, // ksb
0x2690012e: 435, // ksb-TZ
0x26b00000: 436, // ksf
0x26b00051: 437, // ksf-CM
0x26c00000: 438, // ksh
0x26c0005f: 439, // ksh-DE
0x27200000: 440, // ku
0x27f00000: 441, // kw
0x27f0007a: 442, // kw-GB
0x28800000: 443, // ky
0x288000a4: 444, // ky-KG
0x28f00000: 445, // lag
0x28f0012e: 446, // lag-TZ
0x29300000: 447, // lb
0x293000b6: 448, // lb-LU
0x2a100000: 449, // lg
0x2a100130: 450, // lg-UG
0x2ad00000: 451, // lkt
0x2ad00134: 452, // lkt-US
0x2b300000: 453, // ln
0x2b300029: 454, // ln-AO
0x2b30004a: 455, // ln-CD
0x2b30004b: 456, // ln-CF
0x2b30004c: 457, // ln-CG
0x2b600000: 458, // lo
0x2b6000ae: 459, // lo-LA
0x2bd00000: 460, // lrc
0x2bd0009a: 461, // lrc-IQ
0x2bd0009b: 462, // lrc-IR
0x2be00000: 463, // lt
0x2be000b5: 464, // lt-LT
0x2c000000: 465, // lu
0x2c00004a: 466, // lu-CD
0x2c200000: 467, // luo
0x2c2000a3: 468, // luo-KE
0x2c300000: 469, // luy
0x2c3000a3: 470, // luy-KE
0x2c500000: 471, // lv
0x2c5000b7: 472, // lv-LV
0x2cf00000: 473, // mas
0x2cf000a3: 474, // mas-KE
0x2cf0012e: 475, // mas-TZ
0x2e700000: 476, // mer
0x2e7000a3: 477, // mer-KE
0x2eb00000: 478, // mfe
0x2eb000cb: 479, // mfe-MU
0x2ef00000: 480, // mg
0x2ef000be: 481, // mg-MG
0x2f000000: 482, // mgh
0x2f0000d0: 483, // mgh-MZ
0x2f200000: 484, // mgo
0x2f200051: 485, // mgo-CM
0x2fd00000: 486, // mk
0x2fd000c1: 487, // mk-MK
0x30200000: 488, // ml
0x30200098: 489, // ml-IN
0x30900000: 490, // mn
0x309000c4: 491, // mn-MN
0x31900000: 492, // mr
0x31900098: 493, // mr-IN
0x31d00000: 494, // ms
0x31d0003d: 495, // ms-BN
0x31d000cf: 496, // ms-MY
0x31d0010c: 497, // ms-SG
0x31e00000: 498, // mt
0x31e000ca: 499, // mt-MT
0x32300000: 500, // mua
0x32300051: 501, // mua-CM
0x32f00000: 502, // my
0x32f000c3: 503, // my-MM
0x33800000: 504, // mzn
0x3380009b: 505, // mzn-IR
0x33f00000: 506, // nah
0x34300000: 507, // naq
0x343000d1: 508, // naq-NA
0x34500000: 509, // nb
0x345000d9: 510, // nb-NO
0x3450010f: 511, // nb-SJ
0x34c00000: 512, // nd
0x34c00163: 513, // nd-ZW
0x34e00000: 514, // nds
0x34e0005f: 515, // nds-DE
0x34e000d8: 516, // nds-NL
0x34f00000: 517, // ne
0x34f00098: 518, // ne-IN
0x34f000da: 519, // ne-NP
0x36500000: 520, // nl
0x3650002f: 521, // nl-AW
0x36500035: 522, // nl-BE
0x3650003f: 523, // nl-BQ
0x3650005a: 524, // nl-CW
0x365000d8: 525, // nl-NL
0x36500115: 526, // nl-SR
0x3650011a: 527, // nl-SX
0x36600000: 528, // nmg
0x36600051: 529, // nmg-CM
0x36800000: 530, // nn
0x368000d9: 531, // nn-NO
0x36a00000: 532, // nnh
0x36a00051: 533, // nnh-CM
0x36d00000: 534, // no
0x37300000: 535, // nqo
0x37400000: 536, // nr
0x37800000: 537, // nso
0x37e00000: 538, // nus
0x37e00116: 539, // nus-SS
0x38500000: 540, // ny
0x38700000: 541, // nyn
0x38700130: 542, // nyn-UG
0x38e00000: 543, // om
0x38e0006e: 544, // om-ET
0x38e000a3: 545, // om-KE
0x39300000: 546, // or
0x39300098: 547, // or-IN
0x39600000: 548, // os
0x3960007c: 549, // os-GE
0x39600105: 550, // os-RU
0x39b00000: 551, // pa
0x39b05000: 552, // pa-Arab
0x39b050e7: 553, // pa-Arab-PK
0x39b2f000: 554, // pa-Guru
0x39b2f098: 555, // pa-Guru-IN
0x39f00000: 556, // pap
0x3b100000: 557, // pl
0x3b1000e8: 558, // pl-PL
0x3bb00000: 559, // prg
0x3bb00001: 560, // prg-001
0x3bc00000: 561, // ps
0x3bc00023: 562, // ps-AF
0x3be00000: 563, // pt
0x3be00029: 564, // pt-AO
0x3be00040: 565, // pt-BR
0x3be0004d: 566, // pt-CH
0x3be00059: 567, // pt-CV
0x3be00085: 568, // pt-GQ
0x3be0008a: 569, // pt-GW
0x3be000b6: 570, // pt-LU
0x3be000c5: 571, // pt-MO
0x3be000d0: 572, // pt-MZ
0x3be000ed: 573, // pt-PT
0x3be00117: 574, // pt-ST
0x3be00125: 575, // pt-TL
0x3c200000: 576, // qu
0x3c20003e: 577, // qu-BO
0x3c200068: 578, // qu-EC
0x3c2000e3: 579, // qu-PE
0x3d200000: 580, // rm
0x3d20004d: 581, // rm-CH
0x3d700000: 582, // rn
0x3d700039: 583, // rn-BI
0x3da00000: 584, // ro
0x3da000bb: 585, // ro-MD
0x3da00103: 586, // ro-RO
0x3dc00000: 587, // rof
0x3dc0012e: 588, // rof-TZ
0x3e000000: 589, // ru
0x3e000046: 590, // ru-BY
0x3e0000a4: 591, // ru-KG
0x3e0000ad: 592, // ru-KZ
0x3e0000bb: 593, // ru-MD
0x3e000105: 594, // ru-RU
0x3e00012f: 595, // ru-UA
0x3e300000: 596, // rw
0x3e300106: 597, // rw-RW
0x3e400000: 598, // rwk
0x3e40012e: 599, // rwk-TZ
0x3e900000: 600, // sah
0x3e900105: 601, // sah-RU
0x3ea00000: 602, // saq
0x3ea000a3: 603, // saq-KE
0x3f100000: 604, // sbp
0x3f10012e: 605, // sbp-TZ
0x3fa00000: 606, // sdh
0x3fb00000: 607, // se
0x3fb00071: 608, // se-FI
0x3fb000d9: 609, // se-NO
0x3fb0010b: 610, // se-SE
0x3fd00000: 611, // seh
0x3fd000d0: 612, // seh-MZ
0x3ff00000: 613, // ses
0x3ff000c2: 614, // ses-ML
0x40000000: 615, // sg
0x4000004b: 616, // sg-CF
0x40600000: 617, // shi
0x40652000: 618, // shi-Latn
0x406520b9: 619, // shi-Latn-MA
0x406d2000: 620, // shi-Tfng
0x406d20b9: 621, // shi-Tfng-MA
0x40a00000: 622, // si
0x40a000b2: 623, // si-LK
0x41000000: 624, // sk
0x41000110: 625, // sk-SK
0x41400000: 626, // sl
0x4140010e: 627, // sl-SI
0x41a00000: 628, // sma
0x41b00000: 629, // smi
0x41c00000: 630, // smj
0x41d00000: 631, // smn
0x41d00071: 632, // smn-FI
0x42000000: 633, // sms
0x42100000: 634, // sn
0x42100163: 635, // sn-ZW
0x42700000: 636, // so
0x42700061: 637, // so-DJ
0x4270006e: 638, // so-ET
0x427000a3: 639, // so-KE
0x42700114: 640, // so-SO
0x42f00000: 641, // sq
0x42f00026: 642, // sq-AL
0x42f000c1: 643, // sq-MK
0x42f0014c: 644, // sq-XK
0x43000000: 645, // sr
0x4301e000: 646, // sr-Cyrl
0x4301e032: 647, // sr-Cyrl-BA
0x4301e0bc: 648, // sr-Cyrl-ME
0x4301e104: 649, // sr-Cyrl-RS
0x4301e14c: 650, // sr-Cyrl-XK
0x43052000: 651, // sr-Latn
0x43052032: 652, // sr-Latn-BA
0x430520bc: 653, // sr-Latn-ME
0x43052104: 654, // sr-Latn-RS
0x4305214c: 655, // sr-Latn-XK
0x43500000: 656, // ss
0x43800000: 657, // ssy
0x43900000: 658, // st
0x44200000: 659, // sv
0x44200030: 660, // sv-AX
0x44200071: 661, // sv-FI
0x4420010b: 662, // sv-SE
0x44300000: 663, // sw
0x4430004a: 664, // sw-CD
0x443000a3: 665, // sw-KE
0x4430012e: 666, // sw-TZ
0x44300130: 667, // sw-UG
0x44c00000: 668, // syr
0x44e00000: 669, // ta
0x44e00098: 670, // ta-IN
0x44e000b2: 671, // ta-LK
0x44e000cf: 672, // ta-MY
0x44e0010c: 673, // ta-SG
0x45f00000: 674, // te
0x45f00098: 675, // te-IN
0x46200000: 676, // teo
0x462000a3: 677, // teo-KE
0x46200130: 678, // teo-UG
0x46900000: 679, // th
0x46900122: 680, // th-TH
0x46d00000: 681, // ti
0x46d0006c: 682, // ti-ER
0x46d0006e: 683, // ti-ET
0x46f00000: 684, // tig
0x47400000: 685, // tk
0x47400126: 686, // tk-TM
0x47e00000: 687, // tn
0x48000000: 688, // to
0x48000128: 689, // to-TO
0x48800000: 690, // tr
0x4880005c: 691, // tr-CY
0x4880012a: 692, // tr-TR
0x48c00000: 693, // ts
0x4a200000: 694, // twq
0x4a2000d3: 695, // twq-NE
0x4a700000: 696, // tzm
0x4a7000b9: 697, // tzm-MA
0x4aa00000: 698, // ug
0x4aa00052: 699, // ug-CN
0x4ac00000: 700, // uk
0x4ac0012f: 701, // uk-UA
0x4b200000: 702, // ur
0x4b200098: 703, // ur-IN
0x4b2000e7: 704, // ur-PK
0x4ba00000: 705, // uz
0x4ba05000: 706, // uz-Arab
0x4ba05023: 707, // uz-Arab-AF
0x4ba1e000: 708, // uz-Cyrl
0x4ba1e136: 709, // uz-Cyrl-UZ
0x4ba52000: 710, // uz-Latn
0x4ba52136: 711, // uz-Latn-UZ
0x4bc00000: 712, // vai
0x4bc52000: 713, // vai-Latn
0x4bc520b3: 714, // vai-Latn-LR
0x4bcd9000: 715, // vai-Vaii
0x4bcd90b3: 716, // vai-Vaii-LR
0x4be00000: 717, // ve
0x4c100000: 718, // vi
0x4c10013d: 719, // vi-VN
0x4c700000: 720, // vo
0x4c700001: 721, // vo-001
0x4ca00000: 722, // vun
0x4ca0012e: 723, // vun-TZ
0x4cc00000: 724, // wa
0x4cd00000: 725, // wae
0x4cd0004d: 726, // wae-CH
0x4e300000: 727, // wo
0x4f000000: 728, // xh
0x4f900000: 729, // xog
0x4f900130: 730, // xog-UG
0x50700000: 731, // yav
0x50700051: 732, // yav-CM
0x51000000: 733, // yi
0x51000001: 734, // yi-001
0x51600000: 735, // yo
0x5160003a: 736, // yo-BJ
0x516000d5: 737, // yo-NG
0x51d00000: 738, // yue
0x51d0008c: 739, // yue-HK
0x52600000: 740, // zgh
0x526000b9: 741, // zgh-MA
0x52700000: 742, // zh
0x52734000: 743, // zh-Hans
0x52734052: 744, // zh-Hans-CN
0x5273408c: 745, // zh-Hans-HK
0x527340c5: 746, // zh-Hans-MO
0x5273410c: 747, // zh-Hans-SG
0x52735000: 748, // zh-Hant
0x5273508c: 749, // zh-Hant-HK
0x527350c5: 750, // zh-Hant-MO
0x5273512d: 751, // zh-Hant-TW
0x52c00000: 752, // zu
0x52c00160: 753, // zu-ZA
}
// Total table size 4592 bytes (4KiB); checksum: C25F8AFF

982
vendor/golang.org/x/text/language/language.go generated vendored Normal file
View File

@ -0,0 +1,982 @@
// Copyright 2013 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.
//go:generate go run gen.go gen_common.go -output tables.go
//go:generate go run gen_index.go
// Package language implements BCP 47 language tags and related functionality.
//
// The Tag type, which is used to represent languages, is agnostic to the
// meaning of its subtags. Tags are not fully canonicalized to preserve
// information that may be valuable in certain contexts. As a consequence, two
// different tags may represent identical languages.
//
// Initializing language- or locale-specific components usually consists of
// two steps. The first step is to select a display language based on the
// preferred languages of the user and the languages supported by an application.
// The second step is to create the language-specific services based on
// this selection. Each is discussed in more details below.
//
// Matching preferred against supported languages
//
// An application may support various languages. This list is typically limited
// by the languages for which there exists translations of the user interface.
// Similarly, a user may provide a list of preferred languages which is limited
// by the languages understood by this user.
// An application should use a Matcher to find the best supported language based
// on the user's preferred list.
// Matchers are aware of the intricacies of equivalence between languages.
// The default Matcher implementation takes into account things such as
// deprecated subtags, legacy tags, and mutual intelligibility between scripts
// and languages.
//
// A Matcher for English, Australian English, Danish, and standard Mandarin can
// be defined as follows:
//
// var matcher = language.NewMatcher([]language.Tag{
// language.English, // The first language is used as fallback.
// language.MustParse("en-AU"),
// language.Danish,
// language.Chinese,
// })
//
// The following code selects the best match for someone speaking Spanish and
// Norwegian:
//
// preferred := []language.Tag{ language.Spanish, language.Norwegian }
// tag, _, _ := matcher.Match(preferred...)
//
// In this case, the best match is Danish, as Danish is sufficiently a match to
// Norwegian to not have to fall back to the default.
// See ParseAcceptLanguage on how to handle the Accept-Language HTTP header.
//
// Selecting language-specific services
//
// One should always use the Tag returned by the Matcher to create an instance
// of any of the language-specific services provided by the text repository.
// This prevents the mixing of languages, such as having a different language for
// messages and display names, as well as improper casing or sorting order for
// the selected language.
// Using the returned Tag also allows user-defined settings, such as collation
// order or numbering system to be transparently passed as options.
//
// If you have language-specific data in your application, however, it will in
// most cases suffice to use the index returned by the matcher to identify
// the user language.
// The following loop provides an alternative in case this is not sufficient:
//
// supported := map[language.Tag]data{
// language.English: enData,
// language.MustParse("en-AU"): enAUData,
// language.Danish: daData,
// language.Chinese: zhData,
// }
// tag, _, _ := matcher.Match(preferred...)
// for ; tag != language.Und; tag = tag.Parent() {
// if v, ok := supported[tag]; ok {
// return v
// }
// }
// return enData // should not reach here
//
// Repeatedly taking the Parent of the tag returned by Match will eventually
// match one of the tags used to initialize the Matcher.
//
// Canonicalization
//
// By default, only legacy and deprecated tags are converted into their
// canonical equivalent. All other information is preserved. This approach makes
// the confidence scores more accurate and allows matchers to distinguish
// between variants that are otherwise lost.
//
// As a consequence, two tags that should be treated as identical according to
// BCP 47 or CLDR, like "en-Latn" and "en", will be represented differently. The
// Matchers will handle such distinctions, though, and are aware of the
// equivalence relations. The CanonType type can be used to alter the
// canonicalization form.
//
// References
//
// BCP 47 - Tags for Identifying Languages
// http://tools.ietf.org/html/bcp47
package language // import "golang.org/x/text/language"
// TODO: Remove above NOTE after:
// - verifying that tables are dropped correctly (most notably matcher tables).
import (
"errors"
"fmt"
"strings"
)
const (
// maxCoreSize is the maximum size of a BCP 47 tag without variants and
// extensions. Equals max lang (3) + script (4) + max reg (3) + 2 dashes.
maxCoreSize = 12
// max99thPercentileSize is a somewhat arbitrary buffer size that presumably
// is large enough to hold at least 99% of the BCP 47 tags.
max99thPercentileSize = 32
// maxSimpleUExtensionSize is the maximum size of a -u extension with one
// key-type pair. Equals len("-u-") + key (2) + dash + max value (8).
maxSimpleUExtensionSize = 14
)
// Tag represents a BCP 47 language tag. It is used to specify an instance of a
// specific language or locale. All language tag values are guaranteed to be
// well-formed.
type Tag struct {
lang langID
region regionID
// TODO: we will soon run out of positions for script. Idea: instead of
// storing lang, region, and script codes, store only the compact index and
// have a lookup table from this code to its expansion. This greatly speeds
// up table lookup, speed up common variant cases.
// This will also immediately free up 3 extra bytes. Also, the pVariant
// field can now be moved to the lookup table, as the compact index uniquely
// determines the offset of a possible variant.
script scriptID
pVariant byte // offset in str, includes preceding '-'
pExt uint16 // offset of first extension, includes preceding '-'
// str is the string representation of the Tag. It will only be used if the
// tag has variants or extensions.
str string
}
// Make is a convenience wrapper for Parse that omits the error.
// In case of an error, a sensible default is returned.
func Make(s string) Tag {
return Default.Make(s)
}
// Make is a convenience wrapper for c.Parse that omits the error.
// In case of an error, a sensible default is returned.
func (c CanonType) Make(s string) Tag {
t, _ := c.Parse(s)
return t
}
// Raw returns the raw base language, script and region, without making an
// attempt to infer their values.
func (t Tag) Raw() (b Base, s Script, r Region) {
return Base{t.lang}, Script{t.script}, Region{t.region}
}
// equalTags compares language, script and region subtags only.
func (t Tag) equalTags(a Tag) bool {
return t.lang == a.lang && t.script == a.script && t.region == a.region
}
// IsRoot returns true if t is equal to language "und".
func (t Tag) IsRoot() bool {
if int(t.pVariant) < len(t.str) {
return false
}
return t.equalTags(und)
}
// private reports whether the Tag consists solely of a private use tag.
func (t Tag) private() bool {
return t.str != "" && t.pVariant == 0
}
// CanonType can be used to enable or disable various types of canonicalization.
type CanonType int
const (
// Replace deprecated base languages with their preferred replacements.
DeprecatedBase CanonType = 1 << iota
// Replace deprecated scripts with their preferred replacements.
DeprecatedScript
// Replace deprecated regions with their preferred replacements.
DeprecatedRegion
// Remove redundant scripts.
SuppressScript
// Normalize legacy encodings. This includes legacy languages defined in
// CLDR as well as bibliographic codes defined in ISO-639.
Legacy
// Map the dominant language of a macro language group to the macro language
// subtag. For example cmn -> zh.
Macro
// The CLDR flag should be used if full compatibility with CLDR is required.
// There are a few cases where language.Tag may differ from CLDR. To follow all
// of CLDR's suggestions, use All|CLDR.
CLDR
// Raw can be used to Compose or Parse without Canonicalization.
Raw CanonType = 0
// Replace all deprecated tags with their preferred replacements.
Deprecated = DeprecatedBase | DeprecatedScript | DeprecatedRegion
// All canonicalizations recommended by BCP 47.
BCP47 = Deprecated | SuppressScript
// All canonicalizations.
All = BCP47 | Legacy | Macro
// Default is the canonicalization used by Parse, Make and Compose. To
// preserve as much information as possible, canonicalizations that remove
// potentially valuable information are not included. The Matcher is
// designed to recognize similar tags that would be the same if
// they were canonicalized using All.
Default = Deprecated | Legacy
canonLang = DeprecatedBase | Legacy | Macro
// TODO: LikelyScript, LikelyRegion: suppress similar to ICU.
)
// canonicalize returns the canonicalized equivalent of the tag and
// whether there was any change.
func (t Tag) canonicalize(c CanonType) (Tag, bool) {
if c == Raw {
return t, false
}
changed := false
if c&SuppressScript != 0 {
if t.lang < langNoIndexOffset && uint8(t.script) == suppressScript[t.lang] {
t.script = 0
changed = true
}
}
if c&canonLang != 0 {
for {
if l, aliasType := normLang(t.lang); l != t.lang {
switch aliasType {
case langLegacy:
if c&Legacy != 0 {
if t.lang == _sh && t.script == 0 {
t.script = _Latn
}
t.lang = l
changed = true
}
case langMacro:
if c&Macro != 0 {
// We deviate here from CLDR. The mapping "nb" -> "no"
// qualifies as a typical Macro language mapping. However,
// for legacy reasons, CLDR maps "no", the macro language
// code for Norwegian, to the dominant variant "nb". This
// change is currently under consideration for CLDR as well.
// See http://unicode.org/cldr/trac/ticket/2698 and also
// http://unicode.org/cldr/trac/ticket/1790 for some of the
// practical implications. TODO: this check could be removed
// if CLDR adopts this change.
if c&CLDR == 0 || t.lang != _nb {
changed = true
t.lang = l
}
}
case langDeprecated:
if c&DeprecatedBase != 0 {
if t.lang == _mo && t.region == 0 {
t.region = _MD
}
t.lang = l
changed = true
// Other canonicalization types may still apply.
continue
}
}
} else if c&Legacy != 0 && t.lang == _no && c&CLDR != 0 {
t.lang = _nb
changed = true
}
break
}
}
if c&DeprecatedScript != 0 {
if t.script == _Qaai {
changed = true
t.script = _Zinh
}
}
if c&DeprecatedRegion != 0 {
if r := normRegion(t.region); r != 0 {
changed = true
t.region = r
}
}
return t, changed
}
// Canonicalize returns the canonicalized equivalent of the tag.
func (c CanonType) Canonicalize(t Tag) (Tag, error) {
t, changed := t.canonicalize(c)
if changed {
t.remakeString()
}
return t, nil
}
// Confidence indicates the level of certainty for a given return value.
// For example, Serbian may be written in Cyrillic or Latin script.
// The confidence level indicates whether a value was explicitly specified,
// whether it is typically the only possible value, or whether there is
// an ambiguity.
type Confidence int
const (
No Confidence = iota // full confidence that there was no match
Low // most likely value picked out of a set of alternatives
High // value is generally assumed to be the correct match
Exact // exact match or explicitly specified value
)
var confName = []string{"No", "Low", "High", "Exact"}
func (c Confidence) String() string {
return confName[c]
}
// remakeString is used to update t.str in case lang, script or region changed.
// It is assumed that pExt and pVariant still point to the start of the
// respective parts.
func (t *Tag) remakeString() {
if t.str == "" {
return
}
extra := t.str[t.pVariant:]
if t.pVariant > 0 {
extra = extra[1:]
}
if t.equalTags(und) && strings.HasPrefix(extra, "x-") {
t.str = extra
t.pVariant = 0
t.pExt = 0
return
}
var buf [max99thPercentileSize]byte // avoid extra memory allocation in most cases.
b := buf[:t.genCoreBytes(buf[:])]
if extra != "" {
diff := len(b) - int(t.pVariant)
b = append(b, '-')
b = append(b, extra...)
t.pVariant = uint8(int(t.pVariant) + diff)
t.pExt = uint16(int(t.pExt) + diff)
} else {
t.pVariant = uint8(len(b))
t.pExt = uint16(len(b))
}
t.str = string(b)
}
// genCoreBytes writes a string for the base languages, script and region tags
// to the given buffer and returns the number of bytes written. It will never
// write more than maxCoreSize bytes.
func (t *Tag) genCoreBytes(buf []byte) int {
n := t.lang.stringToBuf(buf[:])
if t.script != 0 {
n += copy(buf[n:], "-")
n += copy(buf[n:], t.script.String())
}
if t.region != 0 {
n += copy(buf[n:], "-")
n += copy(buf[n:], t.region.String())
}
return n
}
// String returns the canonical string representation of the language tag.
func (t Tag) String() string {
if t.str != "" {
return t.str
}
if t.script == 0 && t.region == 0 {
return t.lang.String()
}
buf := [maxCoreSize]byte{}
return string(buf[:t.genCoreBytes(buf[:])])
}
// Base returns the base language of the language tag. If the base language is
// unspecified, an attempt will be made to infer it from the context.
// It uses a variant of CLDR's Add Likely Subtags algorithm. This is subject to change.
func (t Tag) Base() (Base, Confidence) {
if t.lang != 0 {
return Base{t.lang}, Exact
}
c := High
if t.script == 0 && !(Region{t.region}).IsCountry() {
c = Low
}
if tag, err := addTags(t); err == nil && tag.lang != 0 {
return Base{tag.lang}, c
}
return Base{0}, No
}
// Script infers the script for the language tag. If it was not explicitly given, it will infer
// a most likely candidate.
// If more than one script is commonly used for a language, the most likely one
// is returned with a low confidence indication. For example, it returns (Cyrl, Low)
// for Serbian.
// If a script cannot be inferred (Zzzz, No) is returned. We do not use Zyyy (undetermined)
// as one would suspect from the IANA registry for BCP 47. In a Unicode context Zyyy marks
// common characters (like 1, 2, 3, '.', etc.) and is therefore more like multiple scripts.
// See http://www.unicode.org/reports/tr24/#Values for more details. Zzzz is also used for
// unknown value in CLDR. (Zzzz, Exact) is returned if Zzzz was explicitly specified.
// Note that an inferred script is never guaranteed to be the correct one. Latin is
// almost exclusively used for Afrikaans, but Arabic has been used for some texts
// in the past. Also, the script that is commonly used may change over time.
// It uses a variant of CLDR's Add Likely Subtags algorithm. This is subject to change.
func (t Tag) Script() (Script, Confidence) {
if t.script != 0 {
return Script{t.script}, Exact
}
sc, c := scriptID(_Zzzz), No
if t.lang < langNoIndexOffset {
if scr := scriptID(suppressScript[t.lang]); scr != 0 {
// Note: it is not always the case that a language with a suppress
// script value is only written in one script (e.g. kk, ms, pa).
if t.region == 0 {
return Script{scriptID(scr)}, High
}
sc, c = scr, High
}
}
if tag, err := addTags(t); err == nil {
if tag.script != sc {
sc, c = tag.script, Low
}
} else {
t, _ = (Deprecated | Macro).Canonicalize(t)
if tag, err := addTags(t); err == nil && tag.script != sc {
sc, c = tag.script, Low
}
}
return Script{sc}, c
}
// Region returns the region for the language tag. If it was not explicitly given, it will
// infer a most likely candidate from the context.
// It uses a variant of CLDR's Add Likely Subtags algorithm. This is subject to change.
func (t Tag) Region() (Region, Confidence) {
if t.region != 0 {
return Region{t.region}, Exact
}
if t, err := addTags(t); err == nil {
return Region{t.region}, Low // TODO: differentiate between high and low.
}
t, _ = (Deprecated | Macro).Canonicalize(t)
if tag, err := addTags(t); err == nil {
return Region{tag.region}, Low
}
return Region{_ZZ}, No // TODO: return world instead of undetermined?
}
// Variant returns the variants specified explicitly for this language tag.
// or nil if no variant was specified.
func (t Tag) Variants() []Variant {
v := []Variant{}
if int(t.pVariant) < int(t.pExt) {
for x, str := "", t.str[t.pVariant:t.pExt]; str != ""; {
x, str = nextToken(str)
v = append(v, Variant{x})
}
}
return v
}
// Parent returns the CLDR parent of t. In CLDR, missing fields in data for a
// specific language are substituted with fields from the parent language.
// The parent for a language may change for newer versions of CLDR.
func (t Tag) Parent() Tag {
if t.str != "" {
// Strip the variants and extensions.
t, _ = Raw.Compose(t.Raw())
if t.region == 0 && t.script != 0 && t.lang != 0 {
base, _ := addTags(Tag{lang: t.lang})
if base.script == t.script {
return Tag{lang: t.lang}
}
}
return t
}
if t.lang != 0 {
if t.region != 0 {
maxScript := t.script
if maxScript == 0 {
max, _ := addTags(t)
maxScript = max.script
}
for i := range parents {
if langID(parents[i].lang) == t.lang && scriptID(parents[i].maxScript) == maxScript {
for _, r := range parents[i].fromRegion {
if regionID(r) == t.region {
return Tag{
lang: t.lang,
script: scriptID(parents[i].script),
region: regionID(parents[i].toRegion),
}
}
}
}
}
// Strip the script if it is the default one.
base, _ := addTags(Tag{lang: t.lang})
if base.script != maxScript {
return Tag{lang: t.lang, script: maxScript}
}
return Tag{lang: t.lang}
} else if t.script != 0 {
// The parent for an base-script pair with a non-default script is
// "und" instead of the base language.
base, _ := addTags(Tag{lang: t.lang})
if base.script != t.script {
return und
}
return Tag{lang: t.lang}
}
}
return und
}
// returns token t and the rest of the string.
func nextToken(s string) (t, tail string) {
p := strings.Index(s[1:], "-")
if p == -1 {
return s[1:], ""
}
p++
return s[1:p], s[p:]
}
// Extension is a single BCP 47 extension.
type Extension struct {
s string
}
// String returns the string representation of the extension, including the
// type tag.
func (e Extension) String() string {
return e.s
}
// ParseExtension parses s as an extension and returns it on success.
func ParseExtension(s string) (e Extension, err error) {
scan := makeScannerString(s)
var end int
if n := len(scan.token); n != 1 {
return Extension{}, errSyntax
}
scan.toLower(0, len(scan.b))
end = parseExtension(&scan)
if end != len(s) {
return Extension{}, errSyntax
}
return Extension{string(scan.b)}, nil
}
// Type returns the one-byte extension type of e. It returns 0 for the zero
// exception.
func (e Extension) Type() byte {
if e.s == "" {
return 0
}
return e.s[0]
}
// Tokens returns the list of tokens of e.
func (e Extension) Tokens() []string {
return strings.Split(e.s, "-")
}
// Extension returns the extension of type x for tag t. It will return
// false for ok if t does not have the requested extension. The returned
// extension will be invalid in this case.
func (t Tag) Extension(x byte) (ext Extension, ok bool) {
for i := int(t.pExt); i < len(t.str)-1; {
var ext string
i, ext = getExtension(t.str, i)
if ext[0] == x {
return Extension{ext}, true
}
}
return Extension{}, false
}
// Extensions returns all extensions of t.
func (t Tag) Extensions() []Extension {
e := []Extension{}
for i := int(t.pExt); i < len(t.str)-1; {
var ext string
i, ext = getExtension(t.str, i)
e = append(e, Extension{ext})
}
return e
}
// TypeForKey returns the type associated with the given key, where key and type
// are of the allowed values defined for the Unicode locale extension ('u') in
// http://www.unicode.org/reports/tr35/#Unicode_Language_and_Locale_Identifiers.
// TypeForKey will traverse the inheritance chain to get the correct value.
func (t Tag) TypeForKey(key string) string {
if start, end, _ := t.findTypeForKey(key); end != start {
return t.str[start:end]
}
return ""
}
var (
errPrivateUse = errors.New("cannot set a key on a private use tag")
errInvalidArguments = errors.New("invalid key or type")
)
// SetTypeForKey returns a new Tag with the key set to type, where key and type
// are of the allowed values defined for the Unicode locale extension ('u') in
// http://www.unicode.org/reports/tr35/#Unicode_Language_and_Locale_Identifiers.
// An empty value removes an existing pair with the same key.
func (t Tag) SetTypeForKey(key, value string) (Tag, error) {
if t.private() {
return t, errPrivateUse
}
if len(key) != 2 {
return t, errInvalidArguments
}
// Remove the setting if value is "".
if value == "" {
start, end, _ := t.findTypeForKey(key)
if start != end {
// Remove key tag and leading '-'.
start -= 4
// Remove a possible empty extension.
if (end == len(t.str) || t.str[end+2] == '-') && t.str[start-2] == '-' {
start -= 2
}
if start == int(t.pVariant) && end == len(t.str) {
t.str = ""
t.pVariant, t.pExt = 0, 0
} else {
t.str = fmt.Sprintf("%s%s", t.str[:start], t.str[end:])
}
}
return t, nil
}
if len(value) < 3 || len(value) > 8 {
return t, errInvalidArguments
}
var (
buf [maxCoreSize + maxSimpleUExtensionSize]byte
uStart int // start of the -u extension.
)
// Generate the tag string if needed.
if t.str == "" {
uStart = t.genCoreBytes(buf[:])
buf[uStart] = '-'
uStart++
}
// Create new key-type pair and parse it to verify.
b := buf[uStart:]
copy(b, "u-")
copy(b[2:], key)
b[4] = '-'
b = b[:5+copy(b[5:], value)]
scan := makeScanner(b)
if parseExtensions(&scan); scan.err != nil {
return t, scan.err
}
// Assemble the replacement string.
if t.str == "" {
t.pVariant, t.pExt = byte(uStart-1), uint16(uStart-1)
t.str = string(buf[:uStart+len(b)])
} else {
s := t.str
start, end, hasExt := t.findTypeForKey(key)
if start == end {
if hasExt {
b = b[2:]
}
t.str = fmt.Sprintf("%s-%s%s", s[:start], b, s[end:])
} else {
t.str = fmt.Sprintf("%s%s%s", s[:start], value, s[end:])
}
}
return t, nil
}
// findKeyAndType returns the start and end position for the type corresponding
// to key or the point at which to insert the key-value pair if the type
// wasn't found. The hasExt return value reports whether an -u extension was present.
// Note: the extensions are typically very small and are likely to contain
// only one key-type pair.
func (t Tag) findTypeForKey(key string) (start, end int, hasExt bool) {
p := int(t.pExt)
if len(key) != 2 || p == len(t.str) || p == 0 {
return p, p, false
}
s := t.str
// Find the correct extension.
for p++; s[p] != 'u'; p++ {
if s[p] > 'u' {
p--
return p, p, false
}
if p = nextExtension(s, p); p == len(s) {
return len(s), len(s), false
}
}
// Proceed to the hyphen following the extension name.
p++
// curKey is the key currently being processed.
curKey := ""
// Iterate over keys until we get the end of a section.
for {
// p points to the hyphen preceding the current token.
if p3 := p + 3; s[p3] == '-' {
// Found a key.
// Check whether we just processed the key that was requested.
if curKey == key {
return start, p, true
}
// Set to the next key and continue scanning type tokens.
curKey = s[p+1 : p3]
if curKey > key {
return p, p, true
}
// Start of the type token sequence.
start = p + 4
// A type is at least 3 characters long.
p += 7 // 4 + 3
} else {
// Attribute or type, which is at least 3 characters long.
p += 4
}
// p points past the third character of a type or attribute.
max := p + 5 // maximum length of token plus hyphen.
if len(s) < max {
max = len(s)
}
for ; p < max && s[p] != '-'; p++ {
}
// Bail if we have exhausted all tokens or if the next token starts
// a new extension.
if p == len(s) || s[p+2] == '-' {
if curKey == key {
return start, p, true
}
return p, p, true
}
}
}
// CompactIndex returns an index, where 0 <= index < NumCompactTags, for tags
// for which data exists in the text repository. The index will change over time
// and should not be stored in persistent storage. Extensions, except for the
// 'va' type of the 'u' extension, are ignored. It will return 0, false if no
// compact tag exists, where 0 is the index for the root language (Und).
func CompactIndex(t Tag) (index int, ok bool) {
// TODO: perhaps give more frequent tags a lower index.
// TODO: we could make the indexes stable. This will excluded some
// possibilities for optimization, so don't do this quite yet.
b, s, r := t.Raw()
if len(t.str) > 0 {
if strings.HasPrefix(t.str, "x-") {
// We have no entries for user-defined tags.
return 0, false
}
if uint16(t.pVariant) != t.pExt {
// There are no tags with variants and an u-va type.
if t.TypeForKey("va") != "" {
return 0, false
}
t, _ = Raw.Compose(b, s, r, t.Variants())
} else if _, ok := t.Extension('u'); ok {
// Strip all but the 'va' entry.
variant := t.TypeForKey("va")
t, _ = Raw.Compose(b, s, r)
t, _ = t.SetTypeForKey("va", variant)
}
if len(t.str) > 0 {
// We have some variants.
for i, s := range specialTags {
if s == t {
return i + 1, true
}
}
return 0, false
}
}
// No variants specified: just compare core components.
// The key has the form lllssrrr, where l, s, and r are nibbles for
// respectively the langID, scriptID, and regionID.
key := uint32(b.langID) << (8 + 12)
key |= uint32(s.scriptID) << 12
key |= uint32(r.regionID)
x, ok := coreTags[key]
return int(x), ok
}
// Base is an ISO 639 language code, used for encoding the base language
// of a language tag.
type Base struct {
langID
}
// ParseBase parses a 2- or 3-letter ISO 639 code.
// It returns a ValueError if s is a well-formed but unknown language identifier
// or another error if another error occurred.
func ParseBase(s string) (Base, error) {
if n := len(s); n < 2 || 3 < n {
return Base{}, errSyntax
}
var buf [3]byte
l, err := getLangID(buf[:copy(buf[:], s)])
return Base{l}, err
}
// Script is a 4-letter ISO 15924 code for representing scripts.
// It is idiomatically represented in title case.
type Script struct {
scriptID
}
// ParseScript parses a 4-letter ISO 15924 code.
// It returns a ValueError if s is a well-formed but unknown script identifier
// or another error if another error occurred.
func ParseScript(s string) (Script, error) {
if len(s) != 4 {
return Script{}, errSyntax
}
var buf [4]byte
sc, err := getScriptID(script, buf[:copy(buf[:], s)])
return Script{sc}, err
}
// Region is an ISO 3166-1 or UN M.49 code for representing countries and regions.
type Region struct {
regionID
}
// EncodeM49 returns the Region for the given UN M.49 code.
// It returns an error if r is not a valid code.
func EncodeM49(r int) (Region, error) {
rid, err := getRegionM49(r)
return Region{rid}, err
}
// ParseRegion parses a 2- or 3-letter ISO 3166-1 or a UN M.49 code.
// It returns a ValueError if s is a well-formed but unknown region identifier
// or another error if another error occurred.
func ParseRegion(s string) (Region, error) {
if n := len(s); n < 2 || 3 < n {
return Region{}, errSyntax
}
var buf [3]byte
r, err := getRegionID(buf[:copy(buf[:], s)])
return Region{r}, err
}
// IsCountry returns whether this region is a country or autonomous area. This
// includes non-standard definitions from CLDR.
func (r Region) IsCountry() bool {
if r.regionID == 0 || r.IsGroup() || r.IsPrivateUse() && r.regionID != _XK {
return false
}
return true
}
// IsGroup returns whether this region defines a collection of regions. This
// includes non-standard definitions from CLDR.
func (r Region) IsGroup() bool {
if r.regionID == 0 {
return false
}
return int(regionInclusion[r.regionID]) < len(regionContainment)
}
// Contains returns whether Region c is contained by Region r. It returns true
// if c == r.
func (r Region) Contains(c Region) bool {
return r.regionID.contains(c.regionID)
}
func (r regionID) contains(c regionID) bool {
if r == c {
return true
}
g := regionInclusion[r]
if g >= nRegionGroups {
return false
}
m := regionContainment[g]
d := regionInclusion[c]
b := regionInclusionBits[d]
// A contained country may belong to multiple disjoint groups. Matching any
// of these indicates containment. If the contained region is a group, it
// must strictly be a subset.
if d >= nRegionGroups {
return b&m != 0
}
return b&^m == 0
}
var errNoTLD = errors.New("language: region is not a valid ccTLD")
// TLD returns the country code top-level domain (ccTLD). UK is returned for GB.
// In all other cases it returns either the region itself or an error.
//
// This method may return an error for a region for which there exists a
// canonical form with a ccTLD. To get that ccTLD canonicalize r first. The
// region will already be canonicalized it was obtained from a Tag that was
// obtained using any of the default methods.
func (r Region) TLD() (Region, error) {
// See http://en.wikipedia.org/wiki/Country_code_top-level_domain for the
// difference between ISO 3166-1 and IANA ccTLD.
if r.regionID == _GB {
r = Region{_UK}
}
if (r.typ() & ccTLD) == 0 {
return Region{}, errNoTLD
}
return r, nil
}
// Canonicalize returns the region or a possible replacement if the region is
// deprecated. It will not return a replacement for deprecated regions that
// are split into multiple regions.
func (r Region) Canonicalize() Region {
if cr := normRegion(r.regionID); cr != 0 {
return Region{cr}
}
return r
}
// Variant represents a registered variant of a language as defined by BCP 47.
type Variant struct {
variant string
}
// ParseVariant parses and returns a Variant. An error is returned if s is not
// a valid variant.
func ParseVariant(s string) (Variant, error) {
s = strings.ToLower(s)
if _, ok := variantIndex[s]; ok {
return Variant{s}, nil
}
return Variant{}, mkErrInvalid([]byte(s))
}
// String returns the string representation of the variant.
func (v Variant) String() string {
return v.variant
}

396
vendor/golang.org/x/text/language/lookup.go generated vendored Normal file
View File

@ -0,0 +1,396 @@
// Copyright 2013 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 language
import (
"bytes"
"fmt"
"sort"
"strconv"
"golang.org/x/text/internal/tag"
)
// findIndex tries to find the given tag in idx and returns a standardized error
// if it could not be found.
func findIndex(idx tag.Index, key []byte, form string) (index int, err error) {
if !tag.FixCase(form, key) {
return 0, errSyntax
}
i := idx.Index(key)
if i == -1 {
return 0, mkErrInvalid(key)
}
return i, nil
}
func searchUint(imap []uint16, key uint16) int {
return sort.Search(len(imap), func(i int) bool {
return imap[i] >= key
})
}
type langID uint16
// getLangID returns the langID of s if s is a canonical subtag
// or langUnknown if s is not a canonical subtag.
func getLangID(s []byte) (langID, error) {
if len(s) == 2 {
return getLangISO2(s)
}
return getLangISO3(s)
}
// mapLang returns the mapped langID of id according to mapping m.
func normLang(id langID) (langID, langAliasType) {
k := sort.Search(len(langAliasMap), func(i int) bool {
return langAliasMap[i].from >= uint16(id)
})
if k < len(langAliasMap) && langAliasMap[k].from == uint16(id) {
return langID(langAliasMap[k].to), langAliasTypes[k]
}
return id, langAliasTypeUnknown
}
// getLangISO2 returns the langID for the given 2-letter ISO language code
// or unknownLang if this does not exist.
func getLangISO2(s []byte) (langID, error) {
if !tag.FixCase("zz", s) {
return 0, errSyntax
}
if i := lang.Index(s); i != -1 && lang.Elem(i)[3] != 0 {
return langID(i), nil
}
return 0, mkErrInvalid(s)
}
const base = 'z' - 'a' + 1
func strToInt(s []byte) uint {
v := uint(0)
for i := 0; i < len(s); i++ {
v *= base
v += uint(s[i] - 'a')
}
return v
}
// converts the given integer to the original ASCII string passed to strToInt.
// len(s) must match the number of characters obtained.
func intToStr(v uint, s []byte) {
for i := len(s) - 1; i >= 0; i-- {
s[i] = byte(v%base) + 'a'
v /= base
}
}
// getLangISO3 returns the langID for the given 3-letter ISO language code
// or unknownLang if this does not exist.
func getLangISO3(s []byte) (langID, error) {
if tag.FixCase("und", s) {
// first try to match canonical 3-letter entries
for i := lang.Index(s[:2]); i != -1; i = lang.Next(s[:2], i) {
if e := lang.Elem(i); e[3] == 0 && e[2] == s[2] {
// We treat "und" as special and always translate it to "unspecified".
// Note that ZZ and Zzzz are private use and are not treated as
// unspecified by default.
id := langID(i)
if id == nonCanonicalUnd {
return 0, nil
}
return id, nil
}
}
if i := altLangISO3.Index(s); i != -1 {
return langID(altLangIndex[altLangISO3.Elem(i)[3]]), nil
}
n := strToInt(s)
if langNoIndex[n/8]&(1<<(n%8)) != 0 {
return langID(n) + langNoIndexOffset, nil
}
// Check for non-canonical uses of ISO3.
for i := lang.Index(s[:1]); i != -1; i = lang.Next(s[:1], i) {
if e := lang.Elem(i); e[2] == s[1] && e[3] == s[2] {
return langID(i), nil
}
}
return 0, mkErrInvalid(s)
}
return 0, errSyntax
}
// stringToBuf writes the string to b and returns the number of bytes
// written. cap(b) must be >= 3.
func (id langID) stringToBuf(b []byte) int {
if id >= langNoIndexOffset {
intToStr(uint(id)-langNoIndexOffset, b[:3])
return 3
} else if id == 0 {
return copy(b, "und")
}
l := lang[id<<2:]
if l[3] == 0 {
return copy(b, l[:3])
}
return copy(b, l[:2])
}
// String returns the BCP 47 representation of the langID.
// Use b as variable name, instead of id, to ensure the variable
// used is consistent with that of Base in which this type is embedded.
func (b langID) String() string {
if b == 0 {
return "und"
} else if b >= langNoIndexOffset {
b -= langNoIndexOffset
buf := [3]byte{}
intToStr(uint(b), buf[:])
return string(buf[:])
}
l := lang.Elem(int(b))
if l[3] == 0 {
return l[:3]
}
return l[:2]
}
// ISO3 returns the ISO 639-3 language code.
func (b langID) ISO3() string {
if b == 0 || b >= langNoIndexOffset {
return b.String()
}
l := lang.Elem(int(b))
if l[3] == 0 {
return l[:3]
} else if l[2] == 0 {
return altLangISO3.Elem(int(l[3]))[:3]
}
// This allocation will only happen for 3-letter ISO codes
// that are non-canonical BCP 47 language identifiers.
return l[0:1] + l[2:4]
}
// IsPrivateUse reports whether this language code is reserved for private use.
func (b langID) IsPrivateUse() bool {
return langPrivateStart <= b && b <= langPrivateEnd
}
type regionID uint16
// getRegionID returns the region id for s if s is a valid 2-letter region code
// or unknownRegion.
func getRegionID(s []byte) (regionID, error) {
if len(s) == 3 {
if isAlpha(s[0]) {
return getRegionISO3(s)
}
if i, err := strconv.ParseUint(string(s), 10, 10); err == nil {
return getRegionM49(int(i))
}
}
return getRegionISO2(s)
}
// getRegionISO2 returns the regionID for the given 2-letter ISO country code
// or unknownRegion if this does not exist.
func getRegionISO2(s []byte) (regionID, error) {
i, err := findIndex(regionISO, s, "ZZ")
if err != nil {
return 0, err
}
return regionID(i) + isoRegionOffset, nil
}
// getRegionISO3 returns the regionID for the given 3-letter ISO country code
// or unknownRegion if this does not exist.
func getRegionISO3(s []byte) (regionID, error) {
if tag.FixCase("ZZZ", s) {
for i := regionISO.Index(s[:1]); i != -1; i = regionISO.Next(s[:1], i) {
if e := regionISO.Elem(i); e[2] == s[1] && e[3] == s[2] {
return regionID(i) + isoRegionOffset, nil
}
}
for i := 0; i < len(altRegionISO3); i += 3 {
if tag.Compare(altRegionISO3[i:i+3], s) == 0 {
return regionID(altRegionIDs[i/3]), nil
}
}
return 0, mkErrInvalid(s)
}
return 0, errSyntax
}
func getRegionM49(n int) (regionID, error) {
if 0 < n && n <= 999 {
const (
searchBits = 7
regionBits = 9
regionMask = 1<<regionBits - 1
)
idx := n >> searchBits
buf := fromM49[m49Index[idx]:m49Index[idx+1]]
val := uint16(n) << regionBits // we rely on bits shifting out
i := sort.Search(len(buf), func(i int) bool {
return buf[i] >= val
})
if r := fromM49[int(m49Index[idx])+i]; r&^regionMask == val {
return regionID(r & regionMask), nil
}
}
var e ValueError
fmt.Fprint(bytes.NewBuffer([]byte(e.v[:])), n)
return 0, e
}
// normRegion returns a region if r is deprecated or 0 otherwise.
// TODO: consider supporting BYS (-> BLR), CSK (-> 200 or CZ), PHI (-> PHL) and AFI (-> DJ).
// TODO: consider mapping split up regions to new most populous one (like CLDR).
func normRegion(r regionID) regionID {
m := regionOldMap
k := sort.Search(len(m), func(i int) bool {
return m[i].from >= uint16(r)
})
if k < len(m) && m[k].from == uint16(r) {
return regionID(m[k].to)
}
return 0
}
const (
iso3166UserAssigned = 1 << iota
ccTLD
bcp47Region
)
func (r regionID) typ() byte {
return regionTypes[r]
}
// String returns the BCP 47 representation for the region.
// It returns "ZZ" for an unspecified region.
func (r regionID) String() string {
if r < isoRegionOffset {
if r == 0 {
return "ZZ"
}
return fmt.Sprintf("%03d", r.M49())
}
r -= isoRegionOffset
return regionISO.Elem(int(r))[:2]
}
// ISO3 returns the 3-letter ISO code of r.
// Note that not all regions have a 3-letter ISO code.
// In such cases this method returns "ZZZ".
func (r regionID) ISO3() string {
if r < isoRegionOffset {
return "ZZZ"
}
r -= isoRegionOffset
reg := regionISO.Elem(int(r))
switch reg[2] {
case 0:
return altRegionISO3[reg[3]:][:3]
case ' ':
return "ZZZ"
}
return reg[0:1] + reg[2:4]
}
// M49 returns the UN M.49 encoding of r, or 0 if this encoding
// is not defined for r.
func (r regionID) M49() int {
return int(m49[r])
}
// IsPrivateUse reports whether r has the ISO 3166 User-assigned status. This
// may include private-use tags that are assigned by CLDR and used in this
// implementation. So IsPrivateUse and IsCountry can be simultaneously true.
func (r regionID) IsPrivateUse() bool {
return r.typ()&iso3166UserAssigned != 0
}
type scriptID uint8
// getScriptID returns the script id for string s. It assumes that s
// is of the format [A-Z][a-z]{3}.
func getScriptID(idx tag.Index, s []byte) (scriptID, error) {
i, err := findIndex(idx, s, "Zzzz")
return scriptID(i), err
}
// String returns the script code in title case.
// It returns "Zzzz" for an unspecified script.
func (s scriptID) String() string {
if s == 0 {
return "Zzzz"
}
return script.Elem(int(s))
}
// IsPrivateUse reports whether this script code is reserved for private use.
func (s scriptID) IsPrivateUse() bool {
return _Qaaa <= s && s <= _Qabx
}
const (
maxAltTaglen = len("en-US-POSIX")
maxLen = maxAltTaglen
)
var (
// grandfatheredMap holds a mapping from legacy and grandfathered tags to
// their base language or index to more elaborate tag.
grandfatheredMap = map[[maxLen]byte]int16{
[maxLen]byte{'a', 'r', 't', '-', 'l', 'o', 'j', 'b', 'a', 'n'}: _jbo, // art-lojban
[maxLen]byte{'i', '-', 'a', 'm', 'i'}: _ami, // i-ami
[maxLen]byte{'i', '-', 'b', 'n', 'n'}: _bnn, // i-bnn
[maxLen]byte{'i', '-', 'h', 'a', 'k'}: _hak, // i-hak
[maxLen]byte{'i', '-', 'k', 'l', 'i', 'n', 'g', 'o', 'n'}: _tlh, // i-klingon
[maxLen]byte{'i', '-', 'l', 'u', 'x'}: _lb, // i-lux
[maxLen]byte{'i', '-', 'n', 'a', 'v', 'a', 'j', 'o'}: _nv, // i-navajo
[maxLen]byte{'i', '-', 'p', 'w', 'n'}: _pwn, // i-pwn
[maxLen]byte{'i', '-', 't', 'a', 'o'}: _tao, // i-tao
[maxLen]byte{'i', '-', 't', 'a', 'y'}: _tay, // i-tay
[maxLen]byte{'i', '-', 't', 's', 'u'}: _tsu, // i-tsu
[maxLen]byte{'n', 'o', '-', 'b', 'o', 'k'}: _nb, // no-bok
[maxLen]byte{'n', 'o', '-', 'n', 'y', 'n'}: _nn, // no-nyn
[maxLen]byte{'s', 'g', 'n', '-', 'b', 'e', '-', 'f', 'r'}: _sfb, // sgn-BE-FR
[maxLen]byte{'s', 'g', 'n', '-', 'b', 'e', '-', 'n', 'l'}: _vgt, // sgn-BE-NL
[maxLen]byte{'s', 'g', 'n', '-', 'c', 'h', '-', 'd', 'e'}: _sgg, // sgn-CH-DE
[maxLen]byte{'z', 'h', '-', 'g', 'u', 'o', 'y', 'u'}: _cmn, // zh-guoyu
[maxLen]byte{'z', 'h', '-', 'h', 'a', 'k', 'k', 'a'}: _hak, // zh-hakka
[maxLen]byte{'z', 'h', '-', 'm', 'i', 'n', '-', 'n', 'a', 'n'}: _nan, // zh-min-nan
[maxLen]byte{'z', 'h', '-', 'x', 'i', 'a', 'n', 'g'}: _hsn, // zh-xiang
// Grandfathered tags with no modern replacement will be converted as
// follows:
[maxLen]byte{'c', 'e', 'l', '-', 'g', 'a', 'u', 'l', 'i', 's', 'h'}: -1, // cel-gaulish
[maxLen]byte{'e', 'n', '-', 'g', 'b', '-', 'o', 'e', 'd'}: -2, // en-GB-oed
[maxLen]byte{'i', '-', 'd', 'e', 'f', 'a', 'u', 'l', 't'}: -3, // i-default
[maxLen]byte{'i', '-', 'e', 'n', 'o', 'c', 'h', 'i', 'a', 'n'}: -4, // i-enochian
[maxLen]byte{'i', '-', 'm', 'i', 'n', 'g', 'o'}: -5, // i-mingo
[maxLen]byte{'z', 'h', '-', 'm', 'i', 'n'}: -6, // zh-min
// CLDR-specific tag.
[maxLen]byte{'r', 'o', 'o', 't'}: 0, // root
[maxLen]byte{'e', 'n', '-', 'u', 's', '-', 'p', 'o', 's', 'i', 'x'}: -7, // en_US_POSIX"
}
altTagIndex = [...]uint8{0, 17, 31, 45, 61, 74, 86, 102}
altTags = "xtg-x-cel-gaulishen-GB-oxendicten-x-i-defaultund-x-i-enochiansee-x-i-mingonan-x-zh-minen-US-u-va-posix"
)
func grandfathered(s [maxAltTaglen]byte) (t Tag, ok bool) {
if v, ok := grandfatheredMap[s]; ok {
if v < 0 {
return Make(altTags[altTagIndex[-v-1]:altTagIndex[-v]]), true
}
t.lang = langID(v)
return t, true
}
return t, false
}

933
vendor/golang.org/x/text/language/match.go generated vendored Normal file
View File

@ -0,0 +1,933 @@
// Copyright 2013 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 language
import "errors"
// A MatchOption configures a Matcher.
type MatchOption func(*matcher)
// PreferSameScript will, in the absence of a match, result in the first
// preferred tag with the same script as a supported tag to match this supported
// tag. The default is currently true, but this may change in the future.
func PreferSameScript(preferSame bool) MatchOption {
return func(m *matcher) { m.preferSameScript = preferSame }
}
// Matcher is the interface that wraps the Match method.
//
// Match returns the best match for any of the given tags, along with
// a unique index associated with the returned tag and a confidence
// score.
type Matcher interface {
Match(t ...Tag) (tag Tag, index int, c Confidence)
}
// Comprehends reports the confidence score for a speaker of a given language
// to being able to comprehend the written form of an alternative language.
func Comprehends(speaker, alternative Tag) Confidence {
_, _, c := NewMatcher([]Tag{alternative}).Match(speaker)
return c
}
// NewMatcher returns a Matcher that matches an ordered list of preferred tags
// against a list of supported tags based on written intelligibility, closeness
// of dialect, equivalence of subtags and various other rules. It is initialized
// with the list of supported tags. The first element is used as the default
// value in case no match is found.
//
// Its Match method matches the first of the given Tags to reach a certain
// confidence threshold. The tags passed to Match should therefore be specified
// in order of preference. Extensions are ignored for matching.
//
// The index returned by the Match method corresponds to the index of the
// matched tag in t, but is augmented with the Unicode extension ('u')of the
// corresponding preferred tag. This allows user locale options to be passed
// transparently.
func NewMatcher(t []Tag, options ...MatchOption) Matcher {
return newMatcher(t, options)
}
func (m *matcher) Match(want ...Tag) (t Tag, index int, c Confidence) {
match, w, c := m.getBest(want...)
if match != nil {
t, index = match.tag, match.index
} else {
// TODO: this should be an option
t = m.default_.tag
if m.preferSameScript {
outer:
for _, w := range want {
script, _ := w.Script()
if script.scriptID == 0 {
// Don't do anything if there is no script, such as with
// private subtags.
continue
}
for i, h := range m.supported {
if script.scriptID == h.maxScript {
t, index = h.tag, i
break outer
}
}
}
}
// TODO: select first language tag based on script.
}
if w.region != 0 && t.region != 0 && t.region.contains(w.region) {
t, _ = Raw.Compose(t, Region{w.region})
}
// Copy options from the user-provided tag into the result tag. This is hard
// to do after the fact, so we do it here.
// TODO: add in alternative variants to -u-va-.
// TODO: add preferred region to -u-rg-.
// TODO: add other extensions. Merge with existing extensions.
if u, ok := w.Extension('u'); ok {
t, _ = Raw.Compose(t, u)
}
return t, index, c
}
type scriptRegionFlags uint8
const (
isList = 1 << iota
scriptInFrom
regionInFrom
)
func (t *Tag) setUndefinedLang(id langID) {
if t.lang == 0 {
t.lang = id
}
}
func (t *Tag) setUndefinedScript(id scriptID) {
if t.script == 0 {
t.script = id
}
}
func (t *Tag) setUndefinedRegion(id regionID) {
if t.region == 0 || t.region.contains(id) {
t.region = id
}
}
// ErrMissingLikelyTagsData indicates no information was available
// to compute likely values of missing tags.
var ErrMissingLikelyTagsData = errors.New("missing likely tags data")
// addLikelySubtags sets subtags to their most likely value, given the locale.
// In most cases this means setting fields for unknown values, but in some
// cases it may alter a value. It returns a ErrMissingLikelyTagsData error
// if the given locale cannot be expanded.
func (t Tag) addLikelySubtags() (Tag, error) {
id, err := addTags(t)
if err != nil {
return t, err
} else if id.equalTags(t) {
return t, nil
}
id.remakeString()
return id, nil
}
// specializeRegion attempts to specialize a group region.
func specializeRegion(t *Tag) bool {
if i := regionInclusion[t.region]; i < nRegionGroups {
x := likelyRegionGroup[i]
if langID(x.lang) == t.lang && scriptID(x.script) == t.script {
t.region = regionID(x.region)
}
return true
}
return false
}
func addTags(t Tag) (Tag, error) {
// We leave private use identifiers alone.
if t.private() {
return t, nil
}
if t.script != 0 && t.region != 0 {
if t.lang != 0 {
// already fully specified
specializeRegion(&t)
return t, nil
}
// Search matches for und-script-region. Note that for these cases
// region will never be a group so there is no need to check for this.
list := likelyRegion[t.region : t.region+1]
if x := list[0]; x.flags&isList != 0 {
list = likelyRegionList[x.lang : x.lang+uint16(x.script)]
}
for _, x := range list {
// Deviating from the spec. See match_test.go for details.
if scriptID(x.script) == t.script {
t.setUndefinedLang(langID(x.lang))
return t, nil
}
}
}
if t.lang != 0 {
// Search matches for lang-script and lang-region, where lang != und.
if t.lang < langNoIndexOffset {
x := likelyLang[t.lang]
if x.flags&isList != 0 {
list := likelyLangList[x.region : x.region+uint16(x.script)]
if t.script != 0 {
for _, x := range list {
if scriptID(x.script) == t.script && x.flags&scriptInFrom != 0 {
t.setUndefinedRegion(regionID(x.region))
return t, nil
}
}
} else if t.region != 0 {
count := 0
goodScript := true
tt := t
for _, x := range list {
// We visit all entries for which the script was not
// defined, including the ones where the region was not
// defined. This allows for proper disambiguation within
// regions.
if x.flags&scriptInFrom == 0 && t.region.contains(regionID(x.region)) {
tt.region = regionID(x.region)
tt.setUndefinedScript(scriptID(x.script))
goodScript = goodScript && tt.script == scriptID(x.script)
count++
}
}
if count == 1 {
return tt, nil
}
// Even if we fail to find a unique Region, we might have
// an unambiguous script.
if goodScript {
t.script = tt.script
}
}
}
}
} else {
// Search matches for und-script.
if t.script != 0 {
x := likelyScript[t.script]
if x.region != 0 {
t.setUndefinedRegion(regionID(x.region))
t.setUndefinedLang(langID(x.lang))
return t, nil
}
}
// Search matches for und-region. If und-script-region exists, it would
// have been found earlier.
if t.region != 0 {
if i := regionInclusion[t.region]; i < nRegionGroups {
x := likelyRegionGroup[i]
if x.region != 0 {
t.setUndefinedLang(langID(x.lang))
t.setUndefinedScript(scriptID(x.script))
t.region = regionID(x.region)
}
} else {
x := likelyRegion[t.region]
if x.flags&isList != 0 {
x = likelyRegionList[x.lang]
}
if x.script != 0 && x.flags != scriptInFrom {
t.setUndefinedLang(langID(x.lang))
t.setUndefinedScript(scriptID(x.script))
return t, nil
}
}
}
}
// Search matches for lang.
if t.lang < langNoIndexOffset {
x := likelyLang[t.lang]
if x.flags&isList != 0 {
x = likelyLangList[x.region]
}
if x.region != 0 {
t.setUndefinedScript(scriptID(x.script))
t.setUndefinedRegion(regionID(x.region))
}
specializeRegion(&t)
if t.lang == 0 {
t.lang = _en // default language
}
return t, nil
}
return t, ErrMissingLikelyTagsData
}
func (t *Tag) setTagsFrom(id Tag) {
t.lang = id.lang
t.script = id.script
t.region = id.region
}
// minimize removes the region or script subtags from t such that
// t.addLikelySubtags() == t.minimize().addLikelySubtags().
func (t Tag) minimize() (Tag, error) {
t, err := minimizeTags(t)
if err != nil {
return t, err
}
t.remakeString()
return t, nil
}
// minimizeTags mimics the behavior of the ICU 51 C implementation.
func minimizeTags(t Tag) (Tag, error) {
if t.equalTags(und) {
return t, nil
}
max, err := addTags(t)
if err != nil {
return t, err
}
for _, id := range [...]Tag{
{lang: t.lang},
{lang: t.lang, region: t.region},
{lang: t.lang, script: t.script},
} {
if x, err := addTags(id); err == nil && max.equalTags(x) {
t.setTagsFrom(id)
break
}
}
return t, nil
}
// Tag Matching
// CLDR defines an algorithm for finding the best match between two sets of language
// tags. The basic algorithm defines how to score a possible match and then find
// the match with the best score
// (see http://www.unicode.org/reports/tr35/#LanguageMatching).
// Using scoring has several disadvantages. The scoring obfuscates the importance of
// the various factors considered, making the algorithm harder to understand. Using
// scoring also requires the full score to be computed for each pair of tags.
//
// We will use a different algorithm which aims to have the following properties:
// - clarity on the precedence of the various selection factors, and
// - improved performance by allowing early termination of a comparison.
//
// Matching algorithm (overview)
// Input:
// - supported: a set of supported tags
// - default: the default tag to return in case there is no match
// - desired: list of desired tags, ordered by preference, starting with
// the most-preferred.
//
// Algorithm:
// 1) Set the best match to the lowest confidence level
// 2) For each tag in "desired":
// a) For each tag in "supported":
// 1) compute the match between the two tags.
// 2) if the match is better than the previous best match, replace it
// with the new match. (see next section)
// b) if the current best match is above a certain threshold, return this
// match without proceeding to the next tag in "desired". [See Note 1]
// 3) If the best match so far is below a certain threshold, return "default".
//
// Ranking:
// We use two phases to determine whether one pair of tags are a better match
// than another pair of tags. First, we determine a rough confidence level. If the
// levels are different, the one with the highest confidence wins.
// Second, if the rough confidence levels are identical, we use a set of tie-breaker
// rules.
//
// The confidence level of matching a pair of tags is determined by finding the
// lowest confidence level of any matches of the corresponding subtags (the
// result is deemed as good as its weakest link).
// We define the following levels:
// Exact - An exact match of a subtag, before adding likely subtags.
// MaxExact - An exact match of a subtag, after adding likely subtags.
// [See Note 2].
// High - High level of mutual intelligibility between different subtag
// variants.
// Low - Low level of mutual intelligibility between different subtag
// variants.
// No - No mutual intelligibility.
//
// The following levels can occur for each type of subtag:
// Base: Exact, MaxExact, High, Low, No
// Script: Exact, MaxExact [see Note 3], Low, No
// Region: Exact, MaxExact, High
// Variant: Exact, High
// Private: Exact, No
//
// Any result with a confidence level of Low or higher is deemed a possible match.
// Once a desired tag matches any of the supported tags with a level of MaxExact
// or higher, the next desired tag is not considered (see Step 2.b).
// Note that CLDR provides languageMatching data that defines close equivalence
// classes for base languages, scripts and regions.
//
// Tie-breaking
// If we get the same confidence level for two matches, we apply a sequence of
// tie-breaking rules. The first that succeeds defines the result. The rules are
// applied in the following order.
// 1) Original language was defined and was identical.
// 2) Original region was defined and was identical.
// 3) Distance between two maximized regions was the smallest.
// 4) Original script was defined and was identical.
// 5) Distance from want tag to have tag using the parent relation [see Note 5.]
// If there is still no winner after these rules are applied, the first match
// found wins.
//
// Notes:
// [1] Note that even if we may not have a perfect match, if a match is above a
// certain threshold, it is considered a better match than any other match
// to a tag later in the list of preferred language tags.
// [2] In practice, as matching of Exact is done in a separate phase from
// matching the other levels, we reuse the Exact level to mean MaxExact in
// the second phase. As a consequence, we only need the levels defined by
// the Confidence type. The MaxExact confidence level is mapped to High in
// the public API.
// [3] We do not differentiate between maximized script values that were derived
// from suppressScript versus most likely tag data. We determined that in
// ranking the two, one ranks just after the other. Moreover, the two cannot
// occur concurrently. As a consequence, they are identical for practical
// purposes.
// [4] In case of deprecated, macro-equivalents and legacy mappings, we assign
// the MaxExact level to allow iw vs he to still be a closer match than
// en-AU vs en-US, for example.
// [5] In CLDR a locale inherits fields that are unspecified for this locale
// from its parent. Therefore, if a locale is a parent of another locale,
// it is a strong measure for closeness, especially when no other tie
// breaker rule applies. One could also argue it is inconsistent, for
// example, when pt-AO matches pt (which CLDR equates with pt-BR), even
// though its parent is pt-PT according to the inheritance rules.
//
// Implementation Details:
// There are several performance considerations worth pointing out. Most notably,
// we preprocess as much as possible (within reason) at the time of creation of a
// matcher. This includes:
// - creating a per-language map, which includes data for the raw base language
// and its canonicalized variant (if applicable),
// - expanding entries for the equivalence classes defined in CLDR's
// languageMatch data.
// The per-language map ensures that typically only a very small number of tags
// need to be considered. The pre-expansion of canonicalized subtags and
// equivalence classes reduces the amount of map lookups that need to be done at
// runtime.
// matcher keeps a set of supported language tags, indexed by language.
type matcher struct {
default_ *haveTag
supported []*haveTag
index map[langID]*matchHeader
passSettings bool
preferSameScript bool
}
// matchHeader has the lists of tags for exact matches and matches based on
// maximized and canonicalized tags for a given language.
type matchHeader struct {
exact []*haveTag
max []*haveTag
}
// haveTag holds a supported Tag and its maximized script and region. The maximized
// or canonicalized language is not stored as it is not needed during matching.
type haveTag struct {
tag Tag
// index of this tag in the original list of supported tags.
index int
// conf is the maximum confidence that can result from matching this haveTag.
// When conf < Exact this means it was inserted after applying a CLDR equivalence rule.
conf Confidence
// Maximized region and script.
maxRegion regionID
maxScript scriptID
// altScript may be checked as an alternative match to maxScript. If altScript
// matches, the confidence level for this match is Low. Theoretically there
// could be multiple alternative scripts. This does not occur in practice.
altScript scriptID
// nextMax is the index of the next haveTag with the same maximized tags.
nextMax uint16
}
func makeHaveTag(tag Tag, index int) (haveTag, langID) {
max := tag
if tag.lang != 0 {
max, _ = max.canonicalize(All)
max, _ = addTags(max)
max.remakeString()
}
return haveTag{tag, index, Exact, max.region, max.script, altScript(max.lang, max.script), 0}, max.lang
}
// altScript returns an alternative script that may match the given script with
// a low confidence. At the moment, the langMatch data allows for at most one
// script to map to another and we rely on this to keep the code simple.
func altScript(l langID, s scriptID) scriptID {
for _, alt := range matchScript {
// TODO: also match cases where language is not the same.
if (langID(alt.wantLang) == l || langID(alt.haveLang) == l) &&
scriptID(alt.haveScript) == s {
return scriptID(alt.wantScript)
}
}
return 0
}
// addIfNew adds a haveTag to the list of tags only if it is a unique tag.
// Tags that have the same maximized values are linked by index.
func (h *matchHeader) addIfNew(n haveTag, exact bool) {
// Don't add new exact matches.
for _, v := range h.exact {
if v.tag.equalsRest(n.tag) {
return
}
}
if exact {
h.exact = append(h.exact, &n)
}
// Allow duplicate maximized tags, but create a linked list to allow quickly
// comparing the equivalents and bail out.
for i, v := range h.max {
if v.maxScript == n.maxScript &&
v.maxRegion == n.maxRegion &&
v.tag.variantOrPrivateTagStr() == n.tag.variantOrPrivateTagStr() {
for h.max[i].nextMax != 0 {
i = int(h.max[i].nextMax)
}
h.max[i].nextMax = uint16(len(h.max))
break
}
}
h.max = append(h.max, &n)
}
// header returns the matchHeader for the given language. It creates one if
// it doesn't already exist.
func (m *matcher) header(l langID) *matchHeader {
if h := m.index[l]; h != nil {
return h
}
h := &matchHeader{}
m.index[l] = h
return h
}
func toConf(d uint8) Confidence {
if d <= 10 {
return High
}
if d < 30 {
return Low
}
return No
}
// newMatcher builds an index for the given supported tags and returns it as
// a matcher. It also expands the index by considering various equivalence classes
// for a given tag.
func newMatcher(supported []Tag, options []MatchOption) *matcher {
m := &matcher{
index: make(map[langID]*matchHeader),
preferSameScript: true,
}
for _, o := range options {
o(m)
}
if len(supported) == 0 {
m.default_ = &haveTag{}
return m
}
// Add supported languages to the index. Add exact matches first to give
// them precedence.
for i, tag := range supported {
pair, _ := makeHaveTag(tag, i)
m.header(tag.lang).addIfNew(pair, true)
m.supported = append(m.supported, &pair)
}
m.default_ = m.header(supported[0].lang).exact[0]
for i, tag := range supported {
pair, max := makeHaveTag(tag, i)
if max != tag.lang {
m.header(max).addIfNew(pair, false)
}
}
// TODO: include alt script.
// - don't replace regions, but allow regions to be made more specific.
// update is used to add indexes in the map for equivalent languages.
// If force is true, the update will also apply to derived entries. To
// avoid applying a "transitive closure", use false.
update := func(want, have uint16, conf Confidence, force bool) {
if hh := m.index[langID(have)]; hh != nil {
if !force && len(hh.exact) == 0 {
return
}
hw := m.header(langID(want))
for _, ht := range hh.max {
v := *ht
if conf < v.conf {
v.conf = conf
}
v.nextMax = 0 // this value needs to be recomputed
if v.altScript != 0 {
v.altScript = altScript(langID(want), v.maxScript)
}
hw.addIfNew(v, conf == Exact && len(hh.exact) > 0)
}
}
}
// Add entries for languages with mutual intelligibility as defined by CLDR's
// languageMatch data.
for _, ml := range matchLang {
update(ml.want, ml.have, toConf(ml.distance), false)
if !ml.oneway {
update(ml.have, ml.want, toConf(ml.distance), false)
}
}
// Add entries for possible canonicalizations. This is an optimization to
// ensure that only one map lookup needs to be done at runtime per desired tag.
// First we match deprecated equivalents. If they are perfect equivalents
// (their canonicalization simply substitutes a different language code, but
// nothing else), the match confidence is Exact, otherwise it is High.
for i, lm := range langAliasMap {
if lm.from == _sh {
continue
}
// If deprecated codes match and there is no fiddling with the script or
// or region, we consider it an exact match.
conf := Exact
if langAliasTypes[i] != langMacro {
if !isExactEquivalent(langID(lm.from)) {
conf = High
}
update(lm.to, lm.from, conf, true)
}
update(lm.from, lm.to, conf, true)
}
return m
}
// getBest gets the best matching tag in m for any of the given tags, taking into
// account the order of preference of the given tags.
func (m *matcher) getBest(want ...Tag) (got *haveTag, orig Tag, c Confidence) {
best := bestMatch{}
for _, w := range want {
var max Tag
// Check for exact match first.
h := m.index[w.lang]
if w.lang != 0 {
// Base language is defined.
if h == nil {
continue
}
for i := range h.exact {
have := h.exact[i]
if have.tag.equalsRest(w) {
return have, w, Exact
}
}
max, _ = w.canonicalize(Legacy | Deprecated)
max, _ = addTags(max)
} else {
// Base language is not defined.
if h != nil {
for i := range h.exact {
have := h.exact[i]
if have.tag.equalsRest(w) {
return have, w, Exact
}
}
}
if w.script == 0 && w.region == 0 {
// We skip all tags matching und for approximate matching, including
// private tags.
continue
}
max, _ = addTags(w)
if h = m.index[max.lang]; h == nil {
continue
}
}
// Check for match based on maximized tag.
for i := range h.max {
have := h.max[i]
best.update(have, w, max.script, max.region)
if best.conf == Exact {
for have.nextMax != 0 {
have = h.max[have.nextMax]
best.update(have, w, max.script, max.region)
}
return best.have, best.want, High
}
}
}
if best.conf <= No {
if len(want) != 0 {
return nil, want[0], No
}
return nil, Tag{}, No
}
return best.have, best.want, best.conf
}
// bestMatch accumulates the best match so far.
type bestMatch struct {
have *haveTag
want Tag
conf Confidence
// Cached results from applying tie-breaking rules.
origLang bool
origReg bool
regGroupDist uint8
regDist uint8
origScript bool
parentDist uint8 // 255 if have is not an ancestor of want tag.
}
// update updates the existing best match if the new pair is considered to be a
// better match.
// To determine if the given pair is a better match, it first computes the rough
// confidence level. If this surpasses the current match, it will replace it and
// update the tie-breaker rule cache. If there is a tie, it proceeds with applying
// a series of tie-breaker rules. If there is no conclusive winner after applying
// the tie-breaker rules, it leaves the current match as the preferred match.
func (m *bestMatch) update(have *haveTag, tag Tag, maxScript scriptID, maxRegion regionID) {
// Bail if the maximum attainable confidence is below that of the current best match.
c := have.conf
if c < m.conf {
return
}
if have.maxScript != maxScript {
// There is usually very little comprehension between different scripts.
// In a few cases there may still be Low comprehension. This possibility is
// pre-computed and stored in have.altScript.
if Low < m.conf || have.altScript != maxScript {
return
}
c = Low
} else if have.maxRegion != maxRegion {
// There is usually a small difference between languages across regions.
// We use the region distance (below) to disambiguate between equal matches.
if High < c {
c = High
}
}
// We store the results of the computations of the tie-breaker rules along
// with the best match. There is no need to do the checks once we determine
// we have a winner, but we do still need to do the tie-breaker computations.
// We use "beaten" to keep track if we still need to do the checks.
beaten := false // true if the new pair defeats the current one.
if c != m.conf {
if c < m.conf {
return
}
beaten = true
}
// Tie-breaker rules:
// We prefer if the pre-maximized language was specified and identical.
origLang := have.tag.lang == tag.lang && tag.lang != 0
if !beaten && m.origLang != origLang {
if m.origLang {
return
}
beaten = true
}
regGroupDist := regionGroupDist(have.maxRegion, maxRegion, maxScript, tag.lang)
if !beaten && m.regGroupDist != regGroupDist {
if regGroupDist > m.regGroupDist {
return
}
beaten = true
}
// We prefer if the pre-maximized region was specified and identical.
origReg := have.tag.region == tag.region && tag.region != 0
if !beaten && m.origReg != origReg {
if m.origReg {
return
}
beaten = true
}
// TODO: remove the region distance rule. Region distance has been replaced
// by the region grouping rule. For now we leave it as it still seems to
// have a net positive effect when applied after the grouping rule.
// Possible solutions:
// - apply the primary locale rule first to effectively disable region
// region distance if groups are defined.
// - express the following errors in terms of grouping (if possible)
// - find another method of handling the following cases.
// maximization of legacy: find mo in
// "sr-Cyrl, sr-Latn, ro, ro-MD": have ro; want ro-MD (High)
// region distance French: find fr-US in
// "en, fr, fr-CA, fr-CH": have fr; want fr-CA (High)
// Next we prefer smaller distances between regions, as defined by
// regionDist.
regDist := uint8(regionDistance(have.maxRegion, maxRegion))
if !beaten && m.regDist != regDist {
if regDist > m.regDist {
return
}
beaten = true
}
// Next we prefer if the pre-maximized script was specified and identical.
origScript := have.tag.script == tag.script && tag.script != 0
if !beaten && m.origScript != origScript {
if m.origScript {
return
}
beaten = true
}
// Finally we prefer tags which have a closer parent relationship.
// TODO: the parent relationship no longer seems necessary. It doesn't hurt
// to leave it in as the final tie-breaker, though, especially until the
// grouping data has further matured.
parentDist := parentDistance(have.tag.region, tag)
if !beaten && m.parentDist != parentDist {
if parentDist > m.parentDist {
return
}
beaten = true
}
// Update m to the newly found best match.
if beaten {
m.have = have
m.want = tag
m.conf = c
m.origLang = origLang
m.origReg = origReg
m.origScript = origScript
m.regGroupDist = regGroupDist
m.regDist = regDist
m.parentDist = parentDist
}
}
// parentDistance returns the number of times Parent must be called before the
// regions match. It is assumed that it has already been checked that lang and
// script are identical. If haveRegion does not occur in the ancestor chain of
// tag, it returns 255.
func parentDistance(haveRegion regionID, tag Tag) uint8 {
p := tag.Parent()
d := uint8(1)
for haveRegion != p.region {
if p.region == 0 {
return 255
}
p = p.Parent()
d++
}
return d
}
// regionGroupDist computes the distance between two regions based on their
// CLDR grouping.
func regionGroupDist(a, b regionID, script scriptID, lang langID) uint8 {
aGroup := uint(regionToGroups[a]) << 1
bGroup := uint(regionToGroups[b]) << 1
for _, ri := range matchRegion {
if langID(ri.lang) == lang && (ri.script == 0 || scriptID(ri.script) == script) {
group := uint(1 << (ri.group &^ 0x80))
if 0x80&ri.group == 0 {
if aGroup&bGroup&group != 0 { // Both regions are in the group.
return ri.distance
}
} else {
if (aGroup|bGroup)&group == 0 { // Both regions are not in the group.
return ri.distance
}
}
}
}
const defaultDistance = 4
return defaultDistance
}
// regionDistance computes the distance between two regions based on the
// distance in the graph of region containments as defined in CLDR. It iterates
// over increasingly inclusive sets of groups, represented as bit vectors, until
// the source bit vector has bits in common with the destination vector.
func regionDistance(a, b regionID) int {
if a == b {
return 0
}
p, q := regionInclusion[a], regionInclusion[b]
if p < nRegionGroups {
p, q = q, p
}
set := regionInclusionBits
if q < nRegionGroups && set[p]&(1<<q) != 0 {
return 1
}
d := 2
for goal := set[q]; set[p]&goal == 0; p = regionInclusionNext[p] {
d++
}
return d
}
func (t Tag) variants() string {
if t.pVariant == 0 {
return ""
}
return t.str[t.pVariant:t.pExt]
}
// variantOrPrivateTagStr returns variants or private use tags.
func (t Tag) variantOrPrivateTagStr() string {
if t.pExt > 0 {
return t.str[t.pVariant:t.pExt]
}
return t.str[t.pVariant:]
}
// equalsRest compares everything except the language.
func (a Tag) equalsRest(b Tag) bool {
// TODO: don't include extensions in this comparison. To do this efficiently,
// though, we should handle private tags separately.
return a.script == b.script && a.region == b.region && a.variantOrPrivateTagStr() == b.variantOrPrivateTagStr()
}
// isExactEquivalent returns true if canonicalizing the language will not alter
// the script or region of a tag.
func isExactEquivalent(l langID) bool {
for _, o := range notEquivalent {
if o == l {
return false
}
}
return true
}
var notEquivalent []langID
func init() {
// Create a list of all languages for which canonicalization may alter the
// script or region.
for _, lm := range langAliasMap {
tag := Tag{lang: langID(lm.from)}
if tag, _ = tag.canonicalize(All); tag.script != 0 || tag.region != 0 {
notEquivalent = append(notEquivalent, langID(lm.from))
}
}
}

859
vendor/golang.org/x/text/language/parse.go generated vendored Normal file
View File

@ -0,0 +1,859 @@
// Copyright 2013 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 language
import (
"bytes"
"errors"
"fmt"
"sort"
"strconv"
"strings"
"golang.org/x/text/internal/tag"
)
// isAlpha returns true if the byte is not a digit.
// b must be an ASCII letter or digit.
func isAlpha(b byte) bool {
return b > '9'
}
// isAlphaNum returns true if the string contains only ASCII letters or digits.
func isAlphaNum(s []byte) bool {
for _, c := range s {
if !('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || '0' <= c && c <= '9') {
return false
}
}
return true
}
// errSyntax is returned by any of the parsing functions when the
// input is not well-formed, according to BCP 47.
// TODO: return the position at which the syntax error occurred?
var errSyntax = errors.New("language: tag is not well-formed")
// ValueError is returned by any of the parsing functions when the
// input is well-formed but the respective subtag is not recognized
// as a valid value.
type ValueError struct {
v [8]byte
}
func mkErrInvalid(s []byte) error {
var e ValueError
copy(e.v[:], s)
return e
}
func (e ValueError) tag() []byte {
n := bytes.IndexByte(e.v[:], 0)
if n == -1 {
n = 8
}
return e.v[:n]
}
// Error implements the error interface.
func (e ValueError) Error() string {
return fmt.Sprintf("language: subtag %q is well-formed but unknown", e.tag())
}
// Subtag returns the subtag for which the error occurred.
func (e ValueError) Subtag() string {
return string(e.tag())
}
// scanner is used to scan BCP 47 tokens, which are separated by _ or -.
type scanner struct {
b []byte
bytes [max99thPercentileSize]byte
token []byte
start int // start position of the current token
end int // end position of the current token
next int // next point for scan
err error
done bool
}
func makeScannerString(s string) scanner {
scan := scanner{}
if len(s) <= len(scan.bytes) {
scan.b = scan.bytes[:copy(scan.bytes[:], s)]
} else {
scan.b = []byte(s)
}
scan.init()
return scan
}
// makeScanner returns a scanner using b as the input buffer.
// b is not copied and may be modified by the scanner routines.
func makeScanner(b []byte) scanner {
scan := scanner{b: b}
scan.init()
return scan
}
func (s *scanner) init() {
for i, c := range s.b {
if c == '_' {
s.b[i] = '-'
}
}
s.scan()
}
// restToLower converts the string between start and end to lower case.
func (s *scanner) toLower(start, end int) {
for i := start; i < end; i++ {
c := s.b[i]
if 'A' <= c && c <= 'Z' {
s.b[i] += 'a' - 'A'
}
}
}
func (s *scanner) setError(e error) {
if s.err == nil || (e == errSyntax && s.err != errSyntax) {
s.err = e
}
}
// resizeRange shrinks or grows the array at position oldStart such that
// a new string of size newSize can fit between oldStart and oldEnd.
// Sets the scan point to after the resized range.
func (s *scanner) resizeRange(oldStart, oldEnd, newSize int) {
s.start = oldStart
if end := oldStart + newSize; end != oldEnd {
diff := end - oldEnd
if end < cap(s.b) {
b := make([]byte, len(s.b)+diff)
copy(b, s.b[:oldStart])
copy(b[end:], s.b[oldEnd:])
s.b = b
} else {
s.b = append(s.b[end:], s.b[oldEnd:]...)
}
s.next = end + (s.next - s.end)
s.end = end
}
}
// replace replaces the current token with repl.
func (s *scanner) replace(repl string) {
s.resizeRange(s.start, s.end, len(repl))
copy(s.b[s.start:], repl)
}
// gobble removes the current token from the input.
// Caller must call scan after calling gobble.
func (s *scanner) gobble(e error) {
s.setError(e)
if s.start == 0 {
s.b = s.b[:+copy(s.b, s.b[s.next:])]
s.end = 0
} else {
s.b = s.b[:s.start-1+copy(s.b[s.start-1:], s.b[s.end:])]
s.end = s.start - 1
}
s.next = s.start
}
// deleteRange removes the given range from s.b before the current token.
func (s *scanner) deleteRange(start, end int) {
s.setError(errSyntax)
s.b = s.b[:start+copy(s.b[start:], s.b[end:])]
diff := end - start
s.next -= diff
s.start -= diff
s.end -= diff
}
// scan parses the next token of a BCP 47 string. Tokens that are larger
// than 8 characters or include non-alphanumeric characters result in an error
// and are gobbled and removed from the output.
// It returns the end position of the last token consumed.
func (s *scanner) scan() (end int) {
end = s.end
s.token = nil
for s.start = s.next; s.next < len(s.b); {
i := bytes.IndexByte(s.b[s.next:], '-')
if i == -1 {
s.end = len(s.b)
s.next = len(s.b)
i = s.end - s.start
} else {
s.end = s.next + i
s.next = s.end + 1
}
token := s.b[s.start:s.end]
if i < 1 || i > 8 || !isAlphaNum(token) {
s.gobble(errSyntax)
continue
}
s.token = token
return end
}
if n := len(s.b); n > 0 && s.b[n-1] == '-' {
s.setError(errSyntax)
s.b = s.b[:len(s.b)-1]
}
s.done = true
return end
}
// acceptMinSize parses multiple tokens of the given size or greater.
// It returns the end position of the last token consumed.
func (s *scanner) acceptMinSize(min int) (end int) {
end = s.end
s.scan()
for ; len(s.token) >= min; s.scan() {
end = s.end
}
return end
}
// Parse parses the given BCP 47 string and returns a valid Tag. If parsing
// failed it returns an error and any part of the tag that could be parsed.
// If parsing succeeded but an unknown value was found, it returns
// ValueError. The Tag returned in this case is just stripped of the unknown
// value. All other values are preserved. It accepts tags in the BCP 47 format
// and extensions to this standard defined in
// http://www.unicode.org/reports/tr35/#Unicode_Language_and_Locale_Identifiers.
// The resulting tag is canonicalized using the default canonicalization type.
func Parse(s string) (t Tag, err error) {
return Default.Parse(s)
}
// Parse parses the given BCP 47 string and returns a valid Tag. If parsing
// failed it returns an error and any part of the tag that could be parsed.
// If parsing succeeded but an unknown value was found, it returns
// ValueError. The Tag returned in this case is just stripped of the unknown
// value. All other values are preserved. It accepts tags in the BCP 47 format
// and extensions to this standard defined in
// http://www.unicode.org/reports/tr35/#Unicode_Language_and_Locale_Identifiers.
// The resulting tag is canonicalized using the the canonicalization type c.
func (c CanonType) Parse(s string) (t Tag, err error) {
// TODO: consider supporting old-style locale key-value pairs.
if s == "" {
return und, errSyntax
}
if len(s) <= maxAltTaglen {
b := [maxAltTaglen]byte{}
for i, c := range s {
// Generating invalid UTF-8 is okay as it won't match.
if 'A' <= c && c <= 'Z' {
c += 'a' - 'A'
} else if c == '_' {
c = '-'
}
b[i] = byte(c)
}
if t, ok := grandfathered(b); ok {
return t, nil
}
}
scan := makeScannerString(s)
t, err = parse(&scan, s)
t, changed := t.canonicalize(c)
if changed {
t.remakeString()
}
return t, err
}
func parse(scan *scanner, s string) (t Tag, err error) {
t = und
var end int
if n := len(scan.token); n <= 1 {
scan.toLower(0, len(scan.b))
if n == 0 || scan.token[0] != 'x' {
return t, errSyntax
}
end = parseExtensions(scan)
} else if n >= 4 {
return und, errSyntax
} else { // the usual case
t, end = parseTag(scan)
if n := len(scan.token); n == 1 {
t.pExt = uint16(end)
end = parseExtensions(scan)
} else if end < len(scan.b) {
scan.setError(errSyntax)
scan.b = scan.b[:end]
}
}
if int(t.pVariant) < len(scan.b) {
if end < len(s) {
s = s[:end]
}
if len(s) > 0 && tag.Compare(s, scan.b) == 0 {
t.str = s
} else {
t.str = string(scan.b)
}
} else {
t.pVariant, t.pExt = 0, 0
}
return t, scan.err
}
// parseTag parses language, script, region and variants.
// It returns a Tag and the end position in the input that was parsed.
func parseTag(scan *scanner) (t Tag, end int) {
var e error
// TODO: set an error if an unknown lang, script or region is encountered.
t.lang, e = getLangID(scan.token)
scan.setError(e)
scan.replace(t.lang.String())
langStart := scan.start
end = scan.scan()
for len(scan.token) == 3 && isAlpha(scan.token[0]) {
// From http://tools.ietf.org/html/bcp47, <lang>-<extlang> tags are equivalent
// to a tag of the form <extlang>.
lang, e := getLangID(scan.token)
if lang != 0 {
t.lang = lang
copy(scan.b[langStart:], lang.String())
scan.b[langStart+3] = '-'
scan.start = langStart + 4
}
scan.gobble(e)
end = scan.scan()
}
if len(scan.token) == 4 && isAlpha(scan.token[0]) {
t.script, e = getScriptID(script, scan.token)
if t.script == 0 {
scan.gobble(e)
}
end = scan.scan()
}
if n := len(scan.token); n >= 2 && n <= 3 {
t.region, e = getRegionID(scan.token)
if t.region == 0 {
scan.gobble(e)
} else {
scan.replace(t.region.String())
}
end = scan.scan()
}
scan.toLower(scan.start, len(scan.b))
t.pVariant = byte(end)
end = parseVariants(scan, end, t)
t.pExt = uint16(end)
return t, end
}
var separator = []byte{'-'}
// parseVariants scans tokens as long as each token is a valid variant string.
// Duplicate variants are removed.
func parseVariants(scan *scanner, end int, t Tag) int {
start := scan.start
varIDBuf := [4]uint8{}
variantBuf := [4][]byte{}
varID := varIDBuf[:0]
variant := variantBuf[:0]
last := -1
needSort := false
for ; len(scan.token) >= 4; scan.scan() {
// TODO: measure the impact of needing this conversion and redesign
// the data structure if there is an issue.
v, ok := variantIndex[string(scan.token)]
if !ok {
// unknown variant
// TODO: allow user-defined variants?
scan.gobble(mkErrInvalid(scan.token))
continue
}
varID = append(varID, v)
variant = append(variant, scan.token)
if !needSort {
if last < int(v) {
last = int(v)
} else {
needSort = true
// There is no legal combinations of more than 7 variants
// (and this is by no means a useful sequence).
const maxVariants = 8
if len(varID) > maxVariants {
break
}
}
}
end = scan.end
}
if needSort {
sort.Sort(variantsSort{varID, variant})
k, l := 0, -1
for i, v := range varID {
w := int(v)
if l == w {
// Remove duplicates.
continue
}
varID[k] = varID[i]
variant[k] = variant[i]
k++
l = w
}
if str := bytes.Join(variant[:k], separator); len(str) == 0 {
end = start - 1
} else {
scan.resizeRange(start, end, len(str))
copy(scan.b[scan.start:], str)
end = scan.end
}
}
return end
}
type variantsSort struct {
i []uint8
v [][]byte
}
func (s variantsSort) Len() int {
return len(s.i)
}
func (s variantsSort) Swap(i, j int) {
s.i[i], s.i[j] = s.i[j], s.i[i]
s.v[i], s.v[j] = s.v[j], s.v[i]
}
func (s variantsSort) Less(i, j int) bool {
return s.i[i] < s.i[j]
}
type bytesSort [][]byte
func (b bytesSort) Len() int {
return len(b)
}
func (b bytesSort) Swap(i, j int) {
b[i], b[j] = b[j], b[i]
}
func (b bytesSort) Less(i, j int) bool {
return bytes.Compare(b[i], b[j]) == -1
}
// parseExtensions parses and normalizes the extensions in the buffer.
// It returns the last position of scan.b that is part of any extension.
// It also trims scan.b to remove excess parts accordingly.
func parseExtensions(scan *scanner) int {
start := scan.start
exts := [][]byte{}
private := []byte{}
end := scan.end
for len(scan.token) == 1 {
extStart := scan.start
ext := scan.token[0]
end = parseExtension(scan)
extension := scan.b[extStart:end]
if len(extension) < 3 || (ext != 'x' && len(extension) < 4) {
scan.setError(errSyntax)
end = extStart
continue
} else if start == extStart && (ext == 'x' || scan.start == len(scan.b)) {
scan.b = scan.b[:end]
return end
} else if ext == 'x' {
private = extension
break
}
exts = append(exts, extension)
}
sort.Sort(bytesSort(exts))
if len(private) > 0 {
exts = append(exts, private)
}
scan.b = scan.b[:start]
if len(exts) > 0 {
scan.b = append(scan.b, bytes.Join(exts, separator)...)
} else if start > 0 {
// Strip trailing '-'.
scan.b = scan.b[:start-1]
}
return end
}
// parseExtension parses a single extension and returns the position of
// the extension end.
func parseExtension(scan *scanner) int {
start, end := scan.start, scan.end
switch scan.token[0] {
case 'u':
attrStart := end
scan.scan()
for last := []byte{}; len(scan.token) > 2; scan.scan() {
if bytes.Compare(scan.token, last) != -1 {
// Attributes are unsorted. Start over from scratch.
p := attrStart + 1
scan.next = p
attrs := [][]byte{}
for scan.scan(); len(scan.token) > 2; scan.scan() {
attrs = append(attrs, scan.token)
end = scan.end
}
sort.Sort(bytesSort(attrs))
copy(scan.b[p:], bytes.Join(attrs, separator))
break
}
last = scan.token
end = scan.end
}
var last, key []byte
for attrEnd := end; len(scan.token) == 2; last = key {
key = scan.token
keyEnd := scan.end
end = scan.acceptMinSize(3)
// TODO: check key value validity
if keyEnd == end || bytes.Compare(key, last) != 1 {
// We have an invalid key or the keys are not sorted.
// Start scanning keys from scratch and reorder.
p := attrEnd + 1
scan.next = p
keys := [][]byte{}
for scan.scan(); len(scan.token) == 2; {
keyStart, keyEnd := scan.start, scan.end
end = scan.acceptMinSize(3)
if keyEnd != end {
keys = append(keys, scan.b[keyStart:end])
} else {
scan.setError(errSyntax)
end = keyStart
}
}
sort.Sort(bytesSort(keys))
reordered := bytes.Join(keys, separator)
if e := p + len(reordered); e < end {
scan.deleteRange(e, end)
end = e
}
copy(scan.b[p:], bytes.Join(keys, separator))
break
}
}
case 't':
scan.scan()
if n := len(scan.token); n >= 2 && n <= 3 && isAlpha(scan.token[1]) {
_, end = parseTag(scan)
scan.toLower(start, end)
}
for len(scan.token) == 2 && !isAlpha(scan.token[1]) {
end = scan.acceptMinSize(3)
}
case 'x':
end = scan.acceptMinSize(1)
default:
end = scan.acceptMinSize(2)
}
return end
}
// Compose creates a Tag from individual parts, which may be of type Tag, Base,
// Script, Region, Variant, []Variant, Extension, []Extension or error. If a
// Base, Script or Region or slice of type Variant or Extension is passed more
// than once, the latter will overwrite the former. Variants and Extensions are
// accumulated, but if two extensions of the same type are passed, the latter
// will replace the former. A Tag overwrites all former values and typically
// only makes sense as the first argument. The resulting tag is returned after
// canonicalizing using the Default CanonType. If one or more errors are
// encountered, one of the errors is returned.
func Compose(part ...interface{}) (t Tag, err error) {
return Default.Compose(part...)
}
// Compose creates a Tag from individual parts, which may be of type Tag, Base,
// Script, Region, Variant, []Variant, Extension, []Extension or error. If a
// Base, Script or Region or slice of type Variant or Extension is passed more
// than once, the latter will overwrite the former. Variants and Extensions are
// accumulated, but if two extensions of the same type are passed, the latter
// will replace the former. A Tag overwrites all former values and typically
// only makes sense as the first argument. The resulting tag is returned after
// canonicalizing using CanonType c. If one or more errors are encountered,
// one of the errors is returned.
func (c CanonType) Compose(part ...interface{}) (t Tag, err error) {
var b builder
if err = b.update(part...); err != nil {
return und, err
}
t, _ = b.tag.canonicalize(c)
if len(b.ext) > 0 || len(b.variant) > 0 {
sort.Sort(sortVariant(b.variant))
sort.Strings(b.ext)
if b.private != "" {
b.ext = append(b.ext, b.private)
}
n := maxCoreSize + tokenLen(b.variant...) + tokenLen(b.ext...)
buf := make([]byte, n)
p := t.genCoreBytes(buf)
t.pVariant = byte(p)
p += appendTokens(buf[p:], b.variant...)
t.pExt = uint16(p)
p += appendTokens(buf[p:], b.ext...)
t.str = string(buf[:p])
} else if b.private != "" {
t.str = b.private
t.remakeString()
}
return
}
type builder struct {
tag Tag
private string // the x extension
ext []string
variant []string
err error
}
func (b *builder) addExt(e string) {
if e == "" {
} else if e[0] == 'x' {
b.private = e
} else {
b.ext = append(b.ext, e)
}
}
var errInvalidArgument = errors.New("invalid Extension or Variant")
func (b *builder) update(part ...interface{}) (err error) {
replace := func(l *[]string, s string, eq func(a, b string) bool) bool {
if s == "" {
b.err = errInvalidArgument
return true
}
for i, v := range *l {
if eq(v, s) {
(*l)[i] = s
return true
}
}
return false
}
for _, x := range part {
switch v := x.(type) {
case Tag:
b.tag.lang = v.lang
b.tag.region = v.region
b.tag.script = v.script
if v.str != "" {
b.variant = nil
for x, s := "", v.str[v.pVariant:v.pExt]; s != ""; {
x, s = nextToken(s)
b.variant = append(b.variant, x)
}
b.ext, b.private = nil, ""
for i, e := int(v.pExt), ""; i < len(v.str); {
i, e = getExtension(v.str, i)
b.addExt(e)
}
}
case Base:
b.tag.lang = v.langID
case Script:
b.tag.script = v.scriptID
case Region:
b.tag.region = v.regionID
case Variant:
if !replace(&b.variant, v.variant, func(a, b string) bool { return a == b }) {
b.variant = append(b.variant, v.variant)
}
case Extension:
if !replace(&b.ext, v.s, func(a, b string) bool { return a[0] == b[0] }) {
b.addExt(v.s)
}
case []Variant:
b.variant = nil
for _, x := range v {
b.update(x)
}
case []Extension:
b.ext, b.private = nil, ""
for _, e := range v {
b.update(e)
}
// TODO: support parsing of raw strings based on morphology or just extensions?
case error:
err = v
}
}
return
}
func tokenLen(token ...string) (n int) {
for _, t := range token {
n += len(t) + 1
}
return
}
func appendTokens(b []byte, token ...string) int {
p := 0
for _, t := range token {
b[p] = '-'
copy(b[p+1:], t)
p += 1 + len(t)
}
return p
}
type sortVariant []string
func (s sortVariant) Len() int {
return len(s)
}
func (s sortVariant) Swap(i, j int) {
s[j], s[i] = s[i], s[j]
}
func (s sortVariant) Less(i, j int) bool {
return variantIndex[s[i]] < variantIndex[s[j]]
}
func findExt(list []string, x byte) int {
for i, e := range list {
if e[0] == x {
return i
}
}
return -1
}
// getExtension returns the name, body and end position of the extension.
func getExtension(s string, p int) (end int, ext string) {
if s[p] == '-' {
p++
}
if s[p] == 'x' {
return len(s), s[p:]
}
end = nextExtension(s, p)
return end, s[p:end]
}
// nextExtension finds the next extension within the string, searching
// for the -<char>- pattern from position p.
// In the fast majority of cases, language tags will have at most
// one extension and extensions tend to be small.
func nextExtension(s string, p int) int {
for n := len(s) - 3; p < n; {
if s[p] == '-' {
if s[p+2] == '-' {
return p
}
p += 3
} else {
p++
}
}
return len(s)
}
var errInvalidWeight = errors.New("ParseAcceptLanguage: invalid weight")
// ParseAcceptLanguage parses the contents of a Accept-Language header as
// defined in http://www.ietf.org/rfc/rfc2616.txt and returns a list of Tags and
// a list of corresponding quality weights. It is more permissive than RFC 2616
// and may return non-nil slices even if the input is not valid.
// The Tags will be sorted by highest weight first and then by first occurrence.
// Tags with a weight of zero will be dropped. An error will be returned if the
// input could not be parsed.
func ParseAcceptLanguage(s string) (tag []Tag, q []float32, err error) {
var entry string
for s != "" {
if entry, s = split(s, ','); entry == "" {
continue
}
entry, weight := split(entry, ';')
// Scan the language.
t, err := Parse(entry)
if err != nil {
id, ok := acceptFallback[entry]
if !ok {
return nil, nil, err
}
t = Tag{lang: id}
}
// Scan the optional weight.
w := 1.0
if weight != "" {
weight = consume(weight, 'q')
weight = consume(weight, '=')
// consume returns the empty string when a token could not be
// consumed, resulting in an error for ParseFloat.
if w, err = strconv.ParseFloat(weight, 32); err != nil {
return nil, nil, errInvalidWeight
}
// Drop tags with a quality weight of 0.
if w <= 0 {
continue
}
}
tag = append(tag, t)
q = append(q, float32(w))
}
sortStable(&tagSort{tag, q})
return tag, q, nil
}
// consume removes a leading token c from s and returns the result or the empty
// string if there is no such token.
func consume(s string, c byte) string {
if s == "" || s[0] != c {
return ""
}
return strings.TrimSpace(s[1:])
}
func split(s string, c byte) (head, tail string) {
if i := strings.IndexByte(s, c); i >= 0 {
return strings.TrimSpace(s[:i]), strings.TrimSpace(s[i+1:])
}
return strings.TrimSpace(s), ""
}
// Add hack mapping to deal with a small number of cases that that occur
// in Accept-Language (with reasonable frequency).
var acceptFallback = map[string]langID{
"english": _en,
"deutsch": _de,
"italian": _it,
"french": _fr,
"*": _mul, // defined in the spec to match all languages.
}
type tagSort struct {
tag []Tag
q []float32
}
func (s *tagSort) Len() int {
return len(s.q)
}
func (s *tagSort) Less(i, j int) bool {
return s.q[i] > s.q[j]
}
func (s *tagSort) Swap(i, j int) {
s.tag[i], s.tag[j] = s.tag[j], s.tag[i]
s.q[i], s.q[j] = s.q[j], s.q[i]
}

3654
vendor/golang.org/x/text/language/tables.go generated vendored Normal file

File diff suppressed because it is too large Load Diff

143
vendor/golang.org/x/text/language/tags.go generated vendored Normal file
View File

@ -0,0 +1,143 @@
// Copyright 2013 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 language
// TODO: Various sets of commonly use tags and regions.
// MustParse is like Parse, but panics if the given BCP 47 tag cannot be parsed.
// It simplifies safe initialization of Tag values.
func MustParse(s string) Tag {
t, err := Parse(s)
if err != nil {
panic(err)
}
return t
}
// MustParse is like Parse, but panics if the given BCP 47 tag cannot be parsed.
// It simplifies safe initialization of Tag values.
func (c CanonType) MustParse(s string) Tag {
t, err := c.Parse(s)
if err != nil {
panic(err)
}
return t
}
// MustParseBase is like ParseBase, but panics if the given base cannot be parsed.
// It simplifies safe initialization of Base values.
func MustParseBase(s string) Base {
b, err := ParseBase(s)
if err != nil {
panic(err)
}
return b
}
// MustParseScript is like ParseScript, but panics if the given script cannot be
// parsed. It simplifies safe initialization of Script values.
func MustParseScript(s string) Script {
scr, err := ParseScript(s)
if err != nil {
panic(err)
}
return scr
}
// MustParseRegion is like ParseRegion, but panics if the given region cannot be
// parsed. It simplifies safe initialization of Region values.
func MustParseRegion(s string) Region {
r, err := ParseRegion(s)
if err != nil {
panic(err)
}
return r
}
var (
und = Tag{}
Und Tag = Tag{}
Afrikaans Tag = Tag{lang: _af} // af
Amharic Tag = Tag{lang: _am} // am
Arabic Tag = Tag{lang: _ar} // ar
ModernStandardArabic Tag = Tag{lang: _ar, region: _001} // ar-001
Azerbaijani Tag = Tag{lang: _az} // az
Bulgarian Tag = Tag{lang: _bg} // bg
Bengali Tag = Tag{lang: _bn} // bn
Catalan Tag = Tag{lang: _ca} // ca
Czech Tag = Tag{lang: _cs} // cs
Danish Tag = Tag{lang: _da} // da
German Tag = Tag{lang: _de} // de
Greek Tag = Tag{lang: _el} // el
English Tag = Tag{lang: _en} // en
AmericanEnglish Tag = Tag{lang: _en, region: _US} // en-US
BritishEnglish Tag = Tag{lang: _en, region: _GB} // en-GB
Spanish Tag = Tag{lang: _es} // es
EuropeanSpanish Tag = Tag{lang: _es, region: _ES} // es-ES
LatinAmericanSpanish Tag = Tag{lang: _es, region: _419} // es-419
Estonian Tag = Tag{lang: _et} // et
Persian Tag = Tag{lang: _fa} // fa
Finnish Tag = Tag{lang: _fi} // fi
Filipino Tag = Tag{lang: _fil} // fil
French Tag = Tag{lang: _fr} // fr
CanadianFrench Tag = Tag{lang: _fr, region: _CA} // fr-CA
Gujarati Tag = Tag{lang: _gu} // gu
Hebrew Tag = Tag{lang: _he} // he
Hindi Tag = Tag{lang: _hi} // hi
Croatian Tag = Tag{lang: _hr} // hr
Hungarian Tag = Tag{lang: _hu} // hu
Armenian Tag = Tag{lang: _hy} // hy
Indonesian Tag = Tag{lang: _id} // id
Icelandic Tag = Tag{lang: _is} // is
Italian Tag = Tag{lang: _it} // it
Japanese Tag = Tag{lang: _ja} // ja
Georgian Tag = Tag{lang: _ka} // ka
Kazakh Tag = Tag{lang: _kk} // kk
Khmer Tag = Tag{lang: _km} // km
Kannada Tag = Tag{lang: _kn} // kn
Korean Tag = Tag{lang: _ko} // ko
Kirghiz Tag = Tag{lang: _ky} // ky
Lao Tag = Tag{lang: _lo} // lo
Lithuanian Tag = Tag{lang: _lt} // lt
Latvian Tag = Tag{lang: _lv} // lv
Macedonian Tag = Tag{lang: _mk} // mk
Malayalam Tag = Tag{lang: _ml} // ml
Mongolian Tag = Tag{lang: _mn} // mn
Marathi Tag = Tag{lang: _mr} // mr
Malay Tag = Tag{lang: _ms} // ms
Burmese Tag = Tag{lang: _my} // my
Nepali Tag = Tag{lang: _ne} // ne
Dutch Tag = Tag{lang: _nl} // nl
Norwegian Tag = Tag{lang: _no} // no
Punjabi Tag = Tag{lang: _pa} // pa
Polish Tag = Tag{lang: _pl} // pl
Portuguese Tag = Tag{lang: _pt} // pt
BrazilianPortuguese Tag = Tag{lang: _pt, region: _BR} // pt-BR
EuropeanPortuguese Tag = Tag{lang: _pt, region: _PT} // pt-PT
Romanian Tag = Tag{lang: _ro} // ro
Russian Tag = Tag{lang: _ru} // ru
Sinhala Tag = Tag{lang: _si} // si
Slovak Tag = Tag{lang: _sk} // sk
Slovenian Tag = Tag{lang: _sl} // sl
Albanian Tag = Tag{lang: _sq} // sq
Serbian Tag = Tag{lang: _sr} // sr
SerbianLatin Tag = Tag{lang: _sr, script: _Latn} // sr-Latn
Swedish Tag = Tag{lang: _sv} // sv
Swahili Tag = Tag{lang: _sw} // sw
Tamil Tag = Tag{lang: _ta} // ta
Telugu Tag = Tag{lang: _te} // te
Thai Tag = Tag{lang: _th} // th
Turkish Tag = Tag{lang: _tr} // tr
Ukrainian Tag = Tag{lang: _uk} // uk
Urdu Tag = Tag{lang: _ur} // ur
Uzbek Tag = Tag{lang: _uz} // uz
Vietnamese Tag = Tag{lang: _vi} // vi
Chinese Tag = Tag{lang: _zh} // zh
SimplifiedChinese Tag = Tag{lang: _zh, script: _Hans} // zh-Hans
TraditionalChinese Tag = Tag{lang: _zh, script: _Hant} // zh-Hant
Zulu Tag = Tag{lang: _zu} // zu
)

187
vendor/gopkg.in/xmlpath.v2/LICENSE generated vendored
View File

@ -1,187 +0,0 @@
Copyright (c) 2013-2014 Canonical Inc.
This software is licensed under the LGPLv3, included below.
As a special exception to the GNU Lesser General Public License version 3
("LGPL3"), the copyright holders of this Library give you permission to
convey to a third party a Combined Work that links statically or dynamically
to this Library without providing any Minimal Corresponding Source or
Minimal Application Code as set out in 4d or providing the installation
information set out in section 4e, provided that you comply with the other
provisions of LGPL3 and provided that you meet, for the Application the
terms and conditions of the license(s) which apply to the Application.
Except as stated in this special exception, the provisions of LGPL3 will
continue to comply in full to this Library. If you modify this Library, you
may apply this exception to your version of this Library, but you are not
obliged to do so. If you do not wish to do so, delete this exception
statement from your version. This exception does not (and cannot) modify any
license terms which apply to the Application, with which you must still
comply.
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.

View File

@ -1,4 +0,0 @@
Installation and usage
----------------------
See [gopkg.in/xmlpath.v2](https://gopkg.in/xmlpath.v2) for documentation and usage details.

View File

@ -1,88 +0,0 @@
package main
import (
"flag"
"fmt"
"gopkg.in/xmlpath.v2"
"io"
"net/http"
"os"
"regexp"
"strings"
)
var all = flag.Bool("all", false, "print all occurrences rather than the first one")
var trim = flag.Bool("trim", false, "trim spaces around results")
var line = flag.Bool("line", false, "reformat each match as a single line")
var quiet = flag.Bool("q", false, "run quietly with no stdout output")
func main() {
flag.Parse()
if len(flag.Args()) != 2 {
fmt.Fprintf(os.Stderr, "usage: webpath <xpath> <url>\n")
os.Exit(1)
}
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
var whitespace = regexp.MustCompile("[ \t\n]+")
func run() error {
args := flag.Args()
path, err := xmlpath.Compile(args[0])
if err != nil {
return err
}
loc := args[1]
var body io.Reader
if strings.HasPrefix(loc, "https:") || strings.HasPrefix(loc, "http:") {
resp, err := http.Get(args[1])
if err != nil {
return err
}
defer resp.Body.Close()
body = resp.Body
} else {
file, err := os.Open(loc)
if err != nil {
return err
}
defer file.Close()
body = file
}
n, err := xmlpath.ParseHTML(body)
if err != nil {
return err
}
iter := path.Iter(n)
ok := false
for iter.Next() {
ok = true
if *quiet {
break
}
s := iter.Node().String()
if *line {
s = strings.TrimSpace(whitespace.ReplaceAllString(s, " "))
} else if *trim {
s = strings.TrimSpace(s)
}
fmt.Println(s)
if !*all {
break
}
}
if !ok {
os.Exit(1)
}
return nil
}

75
vendor/gopkg.in/xmlpath.v2/doc.go generated vendored
View File

@ -1,75 +0,0 @@
// Package xmlpath implements a strict subset of the XPath specification for the Go language.
//
// The XPath specification is available at:
//
// http://www.w3.org/TR/xpath
//
// Path expressions supported by this package are in the following format,
// with all components being optional:
//
// /axis-name::node-test[predicate]/axis-name::node-test[predicate]
//
// At the moment, xmlpath is compatible with the XPath specification
// to the following extent:
//
// - All axes are supported ("child", "following-sibling", etc)
// - All abbreviated forms are supported (".", "//", etc)
// - All node types except for namespace are supported
// - Predicates may be [N], [path], [not(path)], [path=literal] or [contains(path, literal)]
// - Predicates may be joined with "or", "and", and parenthesis
// - Richer expressions and namespaces are not supported
//
// For example, assuming the following document:
//
// <library>
// <!-- Great book. -->
// <book id="b0836217462" available="true">
// <isbn>0836217462</isbn>
// <title lang="en">Being a Dog Is a Full-Time Job</title>
// <quote>I'd dog paddle the deepest ocean.</quote>
// <author id="CMS">
// <?echo "go rocks"?>
// <name>Charles M Schulz</name>
// <born>1922-11-26</born>
// <dead>2000-02-12</dead>
// </author>
// <character id="PP">
// <name>Peppermint Patty</name>
// <born>1966-08-22</born>
// <qualification>bold, brash and tomboyish</qualification>
// </character>
// <character id="Snoopy">
// <name>Snoopy</name>
// <born>1950-10-04</born>
// <qualification>extroverted beagle</qualification>
// </character>
// </book>
// </library>
//
// The following examples are valid path expressions, and the first
// match has the indicated value:
//
// /library/book/isbn => "0836217462"
// library/*/isbn => "0836217462"
// /library/book/../book/./isbn => "0836217462"
// /library/book/character[2]/name => "Snoopy"
// /library/book/character[born='1950-10-04']/name => "Snoopy"
// /library/book//node()[@id='PP']/name => "Peppermint Patty"
// //book[author/@id='CMS']/title => "Being a Dog Is a Full-Time Job",
// /library/book/preceding::comment() => " Great book. "
// //*[contains(born,'1922')]/name => "Charles M Schulz"
// //*[@id='PP' or @id='Snoopy']/born => {"1966-08-22", "1950-10-04"}
//
// To run an expression, compile it, and then apply the compiled path to any
// number of context nodes, from one or more parsed xml documents:
//
// path := xmlpath.MustCompile("/library/book/isbn")
// root, err := xmlpath.Parse(file)
// if err != nil {
// log.Fatal(err)
// }
// if value, ok := path.String(root); ok {
// fmt.Println("Found:", value)
// }
//
package xmlpath

Some files were not shown because too many files have changed in this diff Show More