aboutsummaryrefslogtreecommitdiff
path: root/libgo/go/text
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@golang.org>2017-09-14 17:11:35 +0000
committerIan Lance Taylor <ian@gcc.gnu.org>2017-09-14 17:11:35 +0000
commitbc998d034f45d1828a8663b2eed928faf22a7d01 (patch)
tree8d262a22ca7318f4bcd64269fe8fe9e45bcf8d0f /libgo/go/text
parenta41a6142df74219f596e612d3a7775f68ca6e96f (diff)
downloadgcc-bc998d034f45d1828a8663b2eed928faf22a7d01.zip
gcc-bc998d034f45d1828a8663b2eed928faf22a7d01.tar.gz
gcc-bc998d034f45d1828a8663b2eed928faf22a7d01.tar.bz2
libgo: update to go1.9
Reviewed-on: https://go-review.googlesource.com/63753 From-SVN: r252767
Diffstat (limited to 'libgo/go/text')
-rw-r--r--libgo/go/text/scanner/example_test.go35
-rw-r--r--libgo/go/text/scanner/scanner.go5
-rw-r--r--libgo/go/text/template/doc.go18
-rw-r--r--libgo/go/text/template/exec.go10
-rw-r--r--libgo/go/text/template/exec_test.go3
-rw-r--r--libgo/go/text/template/funcs.go5
-rw-r--r--libgo/go/text/template/parse/lex_test.go4
-rw-r--r--libgo/go/text/template/parse/parse.go1
-rw-r--r--libgo/go/text/template/template.go1
9 files changed, 47 insertions, 35 deletions
diff --git a/libgo/go/text/scanner/example_test.go b/libgo/go/text/scanner/example_test.go
index f48c31d..9e2d5b7 100644
--- a/libgo/go/text/scanner/example_test.go
+++ b/libgo/go/text/scanner/example_test.go
@@ -14,28 +14,25 @@ import (
func Example() {
const src = `
- // This is scanned code.
- if a > 10 {
- someParsable = text
- }`
+// This is scanned code.
+if a > 10 {
+ someParsable = text
+}`
var s scanner.Scanner
- s.Filename = "example"
s.Init(strings.NewReader(src))
- var tok rune
- for tok != scanner.EOF {
- tok = s.Scan()
- fmt.Println("At position", s.Pos(), ":", s.TokenText())
+ s.Filename = "example"
+ for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
+ fmt.Printf("%s: %s\n", s.Position, s.TokenText())
}
// Output:
- // At position example:3:4 : if
- // At position example:3:6 : a
- // At position example:3:8 : >
- // At position example:3:11 : 10
- // At position example:3:13 : {
- // At position example:4:15 : someParsable
- // At position example:4:17 : =
- // At position example:4:22 : text
- // At position example:5:3 : }
- // At position example:5:3 :
+ // example:3:1: if
+ // example:3:4: a
+ // example:3:6: >
+ // example:3:8: 10
+ // example:3:11: {
+ // example:4:2: someParsable
+ // example:4:15: =
+ // example:4:17: text
+ // example:5:1: }
}
diff --git a/libgo/go/text/scanner/scanner.go b/libgo/go/text/scanner/scanner.go
index e085f8a..6fb0422 100644
--- a/libgo/go/text/scanner/scanner.go
+++ b/libgo/go/text/scanner/scanner.go
@@ -166,7 +166,8 @@ type Scanner struct {
// The Filename field is always left untouched by the Scanner.
// If an error is reported (via Error) and Position is invalid,
// the scanner is not inside a token. Call Pos to obtain an error
- // position in that case.
+ // position in that case, or to obtain the position immediately
+ // after the most recently scanned token.
Position
}
@@ -637,6 +638,8 @@ redo:
// Pos returns the position of the character immediately after
// the character or token returned by the last call to Next or Scan.
+// Use the Scanner's Position field for the start position of the most
+// recently scanned token.
func (s *Scanner) Pos() (pos Position) {
pos.Filename = s.Filename
pos.Offset = s.srcBufOffset + s.srcPos - s.lastCharLen
diff --git a/libgo/go/text/template/doc.go b/libgo/go/text/template/doc.go
index fe59e3f..d174ebd 100644
--- a/libgo/go/text/template/doc.go
+++ b/libgo/go/text/template/doc.go
@@ -20,7 +20,8 @@ The input text for a template is UTF-8-encoded text in any format.
"{{" and "}}"; all text outside actions is copied to the output unchanged.
Except for raw strings, actions may not span newlines, although comments can.
-Once parsed, a template may be executed safely in parallel.
+Once parsed, a template may be executed safely in parallel, although if parallel
+executions share a Writer the output may be interleaved.
Here is a trivial example that prints "17 items are made of wool".
@@ -80,14 +81,14 @@ data, defined in detail in the corresponding sections that follow.
{{if pipeline}} T1 {{end}}
If the value of the pipeline is empty, no output is generated;
- otherwise, T1 is executed. The empty values are false, 0, any
+ otherwise, T1 is executed. The empty values are false, 0, any
nil pointer or interface value, and any array, slice, map, or
string of length zero.
Dot is unaffected.
{{if pipeline}} T1 {{else}} T0 {{end}}
If the value of the pipeline is empty, T0 is executed;
- otherwise, T1 is executed. Dot is unaffected.
+ otherwise, T1 is executed. Dot is unaffected.
{{if pipeline}} T1 {{else if pipeline}} T0 {{end}}
To simplify the appearance of if-else chains, the else action
@@ -241,19 +242,19 @@ where $variable is the name of the variable. An action that declares a
variable produces no output.
If a "range" action initializes a variable, the variable is set to the
-successive elements of the iteration. Also, a "range" may declare two
+successive elements of the iteration. Also, a "range" may declare two
variables, separated by a comma:
range $index, $element := pipeline
in which case $index and $element are set to the successive values of the
-array/slice index or map key and element, respectively. Note that if there is
+array/slice index or map key and element, respectively. Note that if there is
only one variable, it is assigned the element; this is opposite to the
convention in Go range clauses.
A variable's scope extends to the "end" action of the control structure ("if",
"with", or "range") in which it is declared, or to the end of the template if
-there is no such control structure. A template invocation does not inherit
+there is no such control structure. A template invocation does not inherit
variables from the point of its invocation.
When execution begins, $ is set to the data argument passed to Execute, that is,
@@ -314,7 +315,8 @@ Predefined global functions are named as follows.
or the returned error value is non-nil, execution stops.
html
Returns the escaped HTML equivalent of the textual
- representation of its arguments.
+ representation of its arguments. This function is unavailable
+ in html/template, with a few exceptions.
index
Returns the result of indexing its first argument by the
following arguments. Thus "index x 1 2 3" is, in Go syntax,
@@ -340,6 +342,8 @@ Predefined global functions are named as follows.
urlquery
Returns the escaped value of the textual representation of
its arguments in a form suitable for embedding in a URL query.
+ This function is unavailable in html/template, with a few
+ exceptions.
The boolean functions take any zero value to be false and a non-zero
value to be true.
diff --git a/libgo/go/text/template/exec.go b/libgo/go/text/template/exec.go
index 89d3e37..29eb68f 100644
--- a/libgo/go/text/template/exec.go
+++ b/libgo/go/text/template/exec.go
@@ -155,7 +155,8 @@ func errRecover(errp *error) {
// If an error occurs executing the template or writing its output,
// execution stops, but partial results may already have been written to
// the output writer.
-// A template may be executed safely in parallel.
+// A template may be executed safely in parallel, although if parallel
+// executions share a Writer the output may be interleaved.
func (t *Template) ExecuteTemplate(wr io.Writer, name string, data interface{}) error {
var tmpl *Template
if t.common != nil {
@@ -172,7 +173,8 @@ func (t *Template) ExecuteTemplate(wr io.Writer, name string, data interface{})
// If an error occurs executing the template or writing its output,
// execution stops, but partial results may already have been written to
// the output writer.
-// A template may be executed safely in parallel.
+// A template may be executed safely in parallel, although if parallel
+// executions share a Writer the output may be interleaved.
//
// If data is a reflect.Value, the template applies to the concrete
// value that the reflect.Value holds, as in fmt.Print.
@@ -553,7 +555,7 @@ func (s *state) evalField(dot reflect.Value, fieldName string, node parse.Node,
// Unless it's an interface, need to get to a value of type *T to guarantee
// we see all methods of T and *T.
ptr := receiver
- if ptr.Kind() != reflect.Interface && ptr.CanAddr() {
+ if ptr.Kind() != reflect.Interface && ptr.Kind() != reflect.Ptr && ptr.CanAddr() {
ptr = ptr.Addr()
}
if method := ptr.MethodByName(fieldName); method.IsValid() {
@@ -630,7 +632,7 @@ func (s *state) evalCall(dot, fun reflect.Value, node parse.Node, name string, a
if numIn < numFixed {
s.errorf("wrong number of args for %s: want at least %d got %d", name, typ.NumIn()-1, len(args))
}
- } else if numIn < typ.NumIn()-1 || !typ.IsVariadic() && numIn != typ.NumIn() {
+ } else if numIn != typ.NumIn() {
s.errorf("wrong number of args for %s: want %d got %d", name, typ.NumIn(), len(args))
}
if !goodFunc(typ) {
diff --git a/libgo/go/text/template/exec_test.go b/libgo/go/text/template/exec_test.go
index 5892b27..9f7e637 100644
--- a/libgo/go/text/template/exec_test.go
+++ b/libgo/go/text/template/exec_test.go
@@ -147,6 +147,8 @@ var tVal = &T{
Tmpl: Must(New("x").Parse("test template")), // "x" is the value of .X
}
+var tSliceOfNil = []*T{nil}
+
// A non-empty interface.
type I interface {
Method0() string
@@ -337,6 +339,7 @@ var execTests = []execTest{
"true", tVal, true},
{".NilOKFunc not nil", "{{call .NilOKFunc .PI}}", "false", tVal, true},
{".NilOKFunc nil", "{{call .NilOKFunc nil}}", "true", tVal, true},
+ {"method on nil value from slice", "-{{range .}}{{.Method1 1234}}{{end}}-", "-1234-", tSliceOfNil, true},
// Function call builtin.
{".BinaryFunc", "{{call .BinaryFunc `1` `2`}}", "[1=2]", tVal, true},
diff --git a/libgo/go/text/template/funcs.go b/libgo/go/text/template/funcs.go
index 3047b27..9107431 100644
--- a/libgo/go/text/template/funcs.go
+++ b/libgo/go/text/template/funcs.go
@@ -489,6 +489,7 @@ var (
htmlAmp = []byte("&amp;")
htmlLt = []byte("&lt;")
htmlGt = []byte("&gt;")
+ htmlNull = []byte("\uFFFD")
)
// HTMLEscape writes to w the escaped HTML equivalent of the plain text data b.
@@ -497,6 +498,8 @@ func HTMLEscape(w io.Writer, b []byte) {
for i, c := range b {
var html []byte
switch c {
+ case '\000':
+ html = htmlNull
case '"':
html = htmlQuot
case '\'':
@@ -520,7 +523,7 @@ func HTMLEscape(w io.Writer, b []byte) {
// HTMLEscapeString returns the escaped HTML equivalent of the plain text data s.
func HTMLEscapeString(s string) string {
// Avoid allocation if we can.
- if !strings.ContainsAny(s, `'"&<>`) {
+ if !strings.ContainsAny(s, "'\"&<>\000") {
return s
}
var b bytes.Buffer
diff --git a/libgo/go/text/template/parse/lex_test.go b/libgo/go/text/template/parse/lex_test.go
index d655d78..2c73bb6 100644
--- a/libgo/go/text/template/parse/lex_test.go
+++ b/libgo/go/text/template/parse/lex_test.go
@@ -498,7 +498,7 @@ func TestShutdown(t *testing.T) {
// We need to duplicate template.Parse here to hold on to the lexer.
const text = "erroneous{{define}}{{else}}1234"
lexer := lex("foo", text, "{{", "}}")
- _, err := New("root").parseLexer(lexer, text)
+ _, err := New("root").parseLexer(lexer)
if err == nil {
t.Fatalf("expected error")
}
@@ -511,7 +511,7 @@ func TestShutdown(t *testing.T) {
// parseLexer is a local version of parse that lets us pass in the lexer instead of building it.
// We expect an error, so the tree set and funcs list are explicitly nil.
-func (t *Tree) parseLexer(lex *lexer, text string) (tree *Tree, err error) {
+func (t *Tree) parseLexer(lex *lexer) (tree *Tree, err error) {
defer t.recover(&err)
t.ParseName = t.Name
t.startParse(nil, lex, map[string]*Tree{})
diff --git a/libgo/go/text/template/parse/parse.go b/libgo/go/text/template/parse/parse.go
index 6060c6d..a91a544 100644
--- a/libgo/go/text/template/parse/parse.go
+++ b/libgo/go/text/template/parse/parse.go
@@ -202,7 +202,6 @@ func (t *Tree) recover(errp *error) {
}
*errp = e.(error)
}
- return
}
// startParse initializes the parser, using the lexer.
diff --git a/libgo/go/text/template/template.go b/libgo/go/text/template/template.go
index 3b4f34b..2246f67 100644
--- a/libgo/go/text/template/template.go
+++ b/libgo/go/text/template/template.go
@@ -159,6 +159,7 @@ func (t *Template) Delims(left, right string) *Template {
}
// Funcs adds the elements of the argument map to the template's function map.
+// It must be called before the template is parsed.
// It panics if a value in the map is not a function with appropriate return
// type or if the name cannot be used syntactically as a function in a template.
// It is legal to overwrite elements of the map. The return value is the template,